package thyyxxk.webserver.dao.his.zygl;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import thyyxxk.webserver.entity.medicaladvice.medicamanage.MzZyReq;
import thyyxxk.webserver.entity.zygl.APatientMiVo;
import thyyxxk.webserver.entity.zygl.MzPatientMi;
import thyyxxk.webserver.entity.zygl.PatientMiSfz;
import thyyxxk.webserver.entity.zygl.ZyXnhRecord;

import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author:lihong
 * @Date: 2025/5/6
 */
@Mapper
public interface AdmissionRegistrationDao {

    /**
     * 查询所有的婚姻状态类型
     * @return
     */
    @Select("select rtrim(code) code,rtrim(name) name,rtrim(py_code) py_code from  zd_marital_status  WITH(NOLOCK)  ")
    List<Map<String,Object>> selectMarital();
    /**
     * @description: 查询证件类型
     * @author: lihong
     * @date: 2025/5/6 16:13
     * @return: java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     **/
    @Select("select code,name from t_zd_psn_cert_type")
    List<Map<String,Object>> selectPsnCertType();

    /**
     * 查询所有的病区
     * @return
     */
    @Select("select code,name from zd_unit_code  WITH(NOLOCK)  where code like '8%' and isnull(del_flag, '0') = '0' ")
    List<Map<String,Object>> selectAllInpatientWard();
    /**
     * @description: 查询医生下拉
     * @author: lihong
     * @date: 2025/5/6 16:31
     * @return: java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     **/
    @Select(" select rtrim(code) code,rtrim(name) name from a_employee_mi where del_flag is null or del_flag =0 ")
    List<Map<String, Object>> selectEmployeeMi();

    /**
     * 查询国家字典
     * @return
     */
    @Select("select rtrim(code) code, rtrim(name) name from zd_country_code WITH(NOLOCK) order by code")
    List<Map<String, Object>> selectCountry();

    /**
     * 民族字典
     * @return
     */
    @Select("select rtrim(code) code, rtrim(name) name from zd_nation_code WITH(NOLOCK) order by code")
    List<Map<String, Object>> selectNation();


    /**
     * 查询关系列表
     * @param
     * @return
     */
    @Select("select  rtrim(code) code, rtrim(name) name, rtrim(py_code) py_code from zy_zd_relative_code WITH(NOLOCK) ")
    List<Map<String, Object>> selectRelation();

    /**
     * 查询所有的职业
     * @return
     */
    @Select("select rtrim(code) code, rtrim(name) name, rtrim(py_code) py_code from zy_occupation_code WITH(NOLOCK) ")
    List<Map<String, Object>> selectOccupation();

    /**
     * 查询所有的入院状态
     * @return
     */
    @Select("select rtrim(code) code, rtrim(name) name, rtrim(py_code) py_code from zd_admiss_status WITH(NOLOCK) ")
    List<Map<String, Object>> selectAdmissStatus();
    /**
     * @description: 查询病人身份
     * @author: lihong
     * @date: 2025/5/6 16:37
     * @return: java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     **/
    @Select("select code,rtrim(name) name from zy_zd_responce_type  WITH(NOLOCK)   where del_flag = 0")
    List<Map<String, Object>> selectAllZyResponceType();

    /**
     * 查询所有的省份
     * @return
     */
    @Select("select cast(code as varchar) code ,name,parent_code from t_region where level=1 and del_flag = 0")
    List<Map<String, Object>> selectProvince();

    /**
     * @description: 查询下级市区
     * @author: lihong
     * @date: 2025/5/6 16:46
     * @return: java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     **/
    @Select("select cast(code as varchar) code ,name,parent_code from t_region where  parent_code=#{code} and del_flag = 0")
    List<Map<String, Object>> selectRegionByParentCode(String code);

    @Select(" select code, name " +
            " from zd_unit_code " +
            " where code in (select rtrim(small_dept) small_dept " +
            "               from zd_dept_all " +
            "               where dept = #{code}) ")
    List<Map<String, Object>> querySmallDeptByCode(String code);
    /**
     * @description: 查询病区床数
     * @author: lihong
     * @date: 2025/5/7 14:24
     * @param: code
     * @return: java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     **/
    @Select(" select  rtrim(bed_no)  code,rtrim(bed_no)  name  from zy_bed_mi    where ward_code =#{code}  and bed_status = 1 ")
    List<Map<String, Object>> selectBedNumByCode(String code);
    /**
     * @description:查询最近一次的住院证
     * @author: lihong
     * @date: 2025/5/7 15:07
     * @param: patientId
     * @return: thyyxxk.webserver.entity.medicaladvice.medicamanage.MzZyReq
     **/
    @Select(" select top 1 patient_id, " +
            "       name, " +
            "       visit_date, " +
            "       doctor_code, " +
            "       dept_code, " +
            "       req_dept, " +
            "       req_ward, " +
            "       bed_no, " +
            "       icd_code, " +
            "       icd_text, " +
            "       confirm_id, " +
            "       confirm_date, " +
            "       zy_serial_no, " +
            "       responce_type, " +
            "       admiss_status, " +
            "       req_status, " +
            "       small_dept, " +
            "       icd_code_new, " +
            "       icd_text_new, " +
            "       condition_code, " +
            "       route_of_admission, " +
            "       pre_bed_confirm_id, " +
            "       pre_bed_confirm_date, " +
            "       pre_bed_confirm_remark, " +
            "       contacter_name, " +
            "       contacter_phone, " +
            "       contacter_address, " +
            "       contacter_relationship  from mz_zy_req where patient_id =#{patientId} and req_status !='4'  order by visit_date desc ")
    MzZyReq selectLastMzZyReq(String patientId);

    @Select("<script>" +
            " select top 1 * from a_patient_mi  " +
            "<where>"+
            " <if test =\" inpatientNo !=null and inpatientNo!='' \" >" +
            "  inpatient_no = #{inpatientNo} "+
            " </if>"+
            " <if test =\" patientId !=null and patientId!='' \" >" +
            " and mz_no = #{patientId} "+
            " </if>"+
            "</where>"+
            "</script>")
    APatientMiVo selectApatientMi(@Param("inpatientNo") String inpatientNo, @Param("patientId")String patientId);

    @Select("<script>" +
            " <bind name=\"pattenName\" value=\"keyWard+'%'\" />"+
            " select * from a_patient_mi where name like #{pattenName}  or home_tel like #{pattenName} or mz_no like #{pattenName} or social_no like #{pattenName} " +
            "</script>")
    List<APatientMiVo> selectPatientInfoByKeyWard(String keyWard);
    @Select("<script>" +
            " <bind name=\"pattenName\" value=\"keyWard+'%'\" />"+
            "select * from mz_patient_mi where name like #{pattenName}  or phone_no like #{pattenName} or patient_id like #{pattenName} or social_no like #{pattenName} " +
            "</script>")
    List<MzPatientMi> selectMzPatientInfoByKeyWard(String keyWard);
    @Select(" select count(*) from zy_actpatient where inpatient_no = #{inpatientNo} ")
    Integer selectCountZy(String inpatientNo);
    /**
     * @description: 查询身份证信息
     * @author: lihong
     * @date: 2025/5/9 10:08
     * @param: patientId
     * @return: java.util.List<thyyxxk.webserver.entity.zygl.PatientMiSfz>
     **/
    @Select("select social_no, name, sex, nation, birth_day, address, agency, expirestart, expireend, tx_image, sfz_image, inpatient_no, patient_id from patient_mi_sfz where patient_id=#{patientId} and sfz_image is not null ")
    List<PatientMiSfz> selectPatientMiSfzsByPatientId(@Param("patientId") String patientId);

    @Select("select top 1 social_no, name, sex, nation, birth_day, address, agency, expirestart, expireend, tx_image, sfz_image, inpatient_no, patient_id from patient_mi_sfz where inpatient_no=#{patNO} and sfz_image is not null ")
    PatientMiSfz selectByPatNo(String patNO);

    @Update(" update patient_mi_sfz set patient_id=#{patientId} where inpatient_no=#{patNO} and sfz_image is not null ")
    void updatePatientId(@Param("patNO") String patNO,@Param("patientId") String patientId);

    @Select(" select inpatient_no,admiss_times,ledger_sn,file_xnh,item_no  from zy_xnh_record where inpatient_no =#{inpatientNo} and file_xnh is not null ")
    List<ZyXnhRecord> selectZyXnhRecordByPatNo(String inpatientNo);


    @Update(" update a_patient_mi set mz_no = #{mzNo} where inpatient_no =#{inpatientNo} ")
    int updateMzNoByInpatientNo(@Param("inpatientNo") String inpatientNo, @Param("mzNo") String mzNo);

    @Insert(" insert into zy_xnh_record (inpatient_no, admiss_times, ledger_sn, file_xnh, item_no, xnh_no, result) values " +
            "( #{inpatientNo}, #{admissTimes}, #{ledgerSn}, #{fileXnh}, #{itemNo}, #{xnhNo}, #{result} )")
    Integer insertZyXnhRecord(ZyXnhRecord zyXnhRecord);
    @Select(" select max(item_no)  from zy_xnh_record where inpatient_no =#{inpatientNo} and admiss_times=#{admissTimes} ")
    String selectZyXnhRecord(ZyXnhRecord zyXnhRecord);

    /**
     * 根据门诊编号删除身份证记录
     * @param patientId
     * @return
     */
    @Delete("delete from patient_mi_sfz where patient_id=#{patientId}")
    int deletePatientMiSfzByPatientId(@Param("patientId") String patientId);

    /**
     * 新增身份证记录
     * @param patientMiSfz
     * @return
     */
    @Insert("INSERT INTO patient_mi_sfz(social_no, name, sex, nation, birth_day, address, agency, expirestart, expireend, tx_image, sfz_image, inpatient_no, patient_id) VALUES" +
            " (#{socialNo}, #{name}, #{sex}, #{nation}, #{birthDay}, #{address}, #{agency}, #{expirestart}, #{expireend}, #{txImage}, #{sfzImage}, #{inpatientNo},#{patientId}) ")
    int insertPatientMiSfz(PatientMiSfz patientMiSfz);
    @Select(" select code,name from mz_zd_dept_no WITH(NOLOCK) ")
    List<Map<String, Object>> selectMzZdDeptNos();
}
