package com.dashitech.businessdata.dao;

import com.dashitech.businessdata.entity.DepartmentEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by chi on 2019/9/12.
 */
@Repository
public interface IDepartmentDAO extends JpaRepository<DepartmentEntity, String> {

    @SuppressWarnings("unchecked")
    DepartmentEntity save(DepartmentEntity departmentEntity);

    //@Query(value = "select id from DepartmentEntity where deleteFlag = 0 and pcode =?1 limit 1 ",nativeQuery = true)
    @Query(value = "select id from itsm_uc_dept where delete_flag = 0 and code =?1 limit 1 ",nativeQuery = true)
    Long selectIdByCode(String code);

    @Query(value = "SELECT id FROM itsm_uc_hospital WHERE delete_flag = 0 and _hos_no=?1 limit 1", nativeQuery = true)
    Long getHospitalIdByHosNo(String hosNo);

/*    @Query(value = "SELECT dept FROM itsm_uc_dept g WHERE g._phone =?1 AND g.delete_flag = 0 limit 1", nativeQuery = true)
    String getDeptNameByPhone(String phone);*/
    @Query(value = "SELECT dept FROM itsm_uc_dept g WHERE g.many_phone like CONCAT('%',:phone,'%') AND g.delete_flag = 0 limit 1", nativeQuery = true)
    String getDeptNameByPhone(@Param("phone")String phone);

    @Query(value = "SELECT d.dept FROM itsm_uc_dept d WHERE id = (SELECT create_department_id FROM itsm_transport_workorder g WHERE g.work_order_phone =?1 LIMIT 1);",nativeQuery = true)
    String getDeptNameByWorkOrderPhone(String phone);

    @Query(value = "select code from itsm_uc_dept where delete_flag = 0",nativeQuery = true)
    List<String> getDeptCodeList();


    //用来补数据的查询
    @Query(value = "select * from itsm_uc_dept where delete_flag = 0 and (input_code is null or pinyin is null)",nativeQuery = true)
    List<DepartmentEntity> getNoPyDeptList();
//    @Modifying
//    @Query("update itsm_uc_dept set input_code = ?1 ,pinyin = ?2 where input_code is null or pinyin is null")
//    void upPinyin(String inputCode,String pinyin);

    @Query(value = "SELECT id FROM itsm_uc_hospital WHERE _hos_no =?1 AND delete_flag = 0 ", nativeQuery = true)
    Long getHosIdByCode(String gender);
}