package thyyxxk.webserver.dao.his.chronicDisease;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import thyyxxk.webserver.entity.chronicDisease.CrmPatientMi;
import thyyxxk.webserver.entity.chronicDisease.CrmPatientVo;

import java.util.List;

public interface CrmPatientMiDao extends BaseMapper<CrmPatientMi> {

    @Select(" select top 1 mi.*, tz.height, tz.weight, tz.temperature, tz.respiratory_rate, tz.heart_rate, " +
            " tz.blood_sugar, tz.blood_pressure_high, tz.blood_pressure_low, tz.blood_oxygen, rtrim(f.name) as userName, " +
            " rtrim(p.name) as referPhysicianName, rtrim(f.name) as creatIdName, j.name as deptName " +
            " from crm_patient_mi mi" +
            " left join crm_patient_tzxx tz on (mi.p_id = tz.p_id collate Chinese_PRC_BIN) and mi.visit_times = tz.visit_times " +
            " left join a_employee_mi p on mi.refer_physician = p.code collate Chinese_PRC_BIN " +
            " left join a_employee_mi f on mi.creat_id = f.code collate Chinese_PRC_BIN " +
            " left join zd_unit_code j on mi.creat_dept = j.code collate Chinese_PRC_BIN " +
            " where mi.social_no = #{keyCode} or mi.his_mz_no = #{keyCode} or mi.his_zy_no = #{keyCode} or mi.his_tj_no = #{keyCode}" +
            " or mi.p_name = #{keyCode} or mi.p_id = #{keyCode} ")
    CrmPatientMi selectCrmPatientMiByCode(@Param("keyCode") String keyCode);

    @Select(" select * from crm_patient_mi where social_no = #{socialNo} ")
    CrmPatientMi selectCrmPatientMiBySocialNo(@Param("socialNo") String socialNo);

    @Select(" select * from crm_patient_mi where p_id = #{pId} ")
    CrmPatientMi selectCrmPatientMiByPId(@Param("pId") String pId);

    @Delete(" delete from crm_patient_mi where social_no = #{socialNo} ")
    void deleteCrmPatientMiBySocialNo(@Param("socialNo") String socialNo);

    @Delete(" delete from crm_patient_mi where p_id = #{pId} ")
    void deleteCrmPatientMiByPId(@Param("pId") String pId);

    @Select(" select top 1 rtrim(mi.name) as p_name, mi.sex, rtrim(mi.patient_id) as his_mz_no, mi.social_no, mi.age, mi.birth_day as birth_date, " +
            " rtrim(mi.phone_no) as rel_tel, mi.province_code, mi.city_code, rtrim(mi.detail) as detail_adress, " +
            " mi.district_code as area_code " +
            " from mz_patient_mi mi " +
            " where mi.patient_id = #{code} or mi.social_no = #{code} or mi.tj_no = #{code} or mi.ic_card_no = #{code} ")
    CrmPatientMi selectCrmPatientMiMzByCode(@Param("code") String code);

    @Select(" select top 1 rtrim(mi.name) as p_name, mi.sex, rtrim(mi.inpatient_no) as his_zy_no, mi.social_no, mi.birth_date as birth_date, " +
            " rtrim(mi.relation_tel) as rel_tel, mi.province_code, mi.city_code, mi.district_code as area_code, rtrim(mi.relation_district) as detail_adress, " +
            " rtrim(mi.mz_no) as his_mz_no, rtrim(mi.relation_name) as rel_name " +
            " from a_patient_mi mi " +
            " where mi.inpatient_no = #{code} or mi.social_no = #{code} or mi.mz_no = #{code} ")
    CrmPatientMi selectCrmPatientMiZyByCode(@Param("code") String code);

    @Select("<script> " +
            " select mi.*, tz.height, tz.weight, tz.temperature, tz.respiratory_rate, tz.heart_rate, " +
            " tz.blood_sugar, tz.blood_pressure_high, tz.blood_pressure_low, tz.blood_oxygen, rtrim(f.name) as userName, " +
            " rtrim(p.name) as referPhysicianName, j.name as deptName " +
            " from crm_patient_mi mi" +
            " left join crm_patient_tzxx tz on (mi.p_id = tz.p_id collate Chinese_PRC_BIN) and mi.visit_times = tz.visit_times " +
            " left join a_employee_mi p on mi.refer_physician = CAST(p.code AS VARCHAR(20))  collate Chinese_PRC_BIN " +
            " left join a_employee_mi f on mi.creat_id = CAST(f.code AS VARCHAR(20)) collate Chinese_PRC_BIN " +
            " left join zd_unit_code j on mi.creat_dept = j.code collate Chinese_PRC_BIN " +
            " where (mi.social_no like '%${keyCode}%' or mi.his_mz_no like '%${keyCode}%' " +
            " or mi.his_zy_no like '%${keyCode}%' or mi.p_name like '%${keyCode}%')" +
            "<if test=\"startTime != null and startTime != '' \">" +
            " and mi.create_date &gt;= #{startTime}  " +
            "</if>" +
            "<if test=\"endTime != null and endTime != '' \">" +
            " and mi.create_date &lt;= #{endTime}  " +
            "</if>" +
            "<if test=\"diseaseType != null and diseaseType != '' \">" +
            " and chronic_disease_type like '%${diseaseType}%'  " +
            "</if>" +
            "<if test=\"delFlag == '0'.toString() \">" +
            " and isnull(mi.del_flag, '0') = '0'  " +
            "</if>" +
            "<if test=\"delFlag == '1'.toString() \">" +
            " and mi.del_flag = '1'  " +
            "</if>" +
            "<if test=\"deptCode != null and deptCode != '' \">" +
            " and mi.creat_dept like '%${deptCode}%'  " +
            "</if>" +
            " order by mi.create_date " +
            "</script> ")
    List<CrmPatientMi> selectChronicDisease(CrmPatientVo vo);

    @Select(" select name as userId from sys_parameters where code = 'mbDel' ")
    String selectMbDelPermissions();

    @Update(" update crm_patient_mi set del_flag = '1' where p_id = #{pId} ")
    int delChronicDiseaseById(@Param("pId") String pId);

    @Update(" update crm_patient_mi set del_flag = '0' where p_id = #{pId} ")
    int hfChronicDiseaseById(@Param("pId") String pId);

}
