package thyyxxk.webserver.dao.his.mzmedical;

import com.baomidou.mybatisplus.core.metadata.IPage;
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.mzmedical.MzMedicalField;
import thyyxxk.webserver.entity.mzmedical.MzMedicalMain;
import thyyxxk.webserver.entity.mzmedical.MzMedicalMainParam;

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

/**
 * @Description:
 * @Author:lihong
 * @Date: 2025/4/14
 */
@Mapper
public interface MzMedicalMainDao  {
    @Select(" select * from  mz_medical_field ")
    List<MzMedicalField> selectAllMzMedicalField();
    @Insert(" <script> " +
            " insert into mz_medical_main (hospital_name,social_credit_code,patient_id, times, name, sex, birth_day, marry_code, country, nation, certificate_type, social_no, adress, phone_no, gms, allergen_name, qtgms, qtgmy, gh_date, bd_date, visit_date, dept_code, doctor_name, doctor_zc, visit_type, first_or_not, sy_or_not, mz_mtb, mz_hz_fj, jz_hz_qx, zyz_date, emr_chief_complaint, icd_name, icd_code, icd_name1, icd_code1, icd_name2, icd_code2, icd_name3, icd_code3, icd_name4, icd_code4, icd_name5, icd_code5, icd_name6, icd_code6, icd_name7, icd_code7, icd_name8, icd_code8, icd_name9, icd_code9, icd_name10, icd_code10, opr_date1, opr_name1, opr_code1, opr_doctor1, mzfs1, mz_doctor1, opr_scale1, opr_date2, opr_name2, opr_code2, opr_doctor2, mzfs2, mz_doctor2, opr_scale2, opr_date3, opr_name3, opr_code3, opr_doctor3, mzfs3, mz_doctor3, opr_scale3, opr_date4, opr_name4, opr_code4, opr_doctor4, mzfs4, mz_doctor4, opr_scale4, opr_date5, opr_name5, opr_code5, opr_doctor5, mzfs5, mz_doctor5, opr_scale5, total_fee, zf_fee, fee1, fee2, fee3, fee4, fee5, fee6, fee7, fee8, fee901, fee902, fee1001, fee1002, fee1003, fee11, fee12, fee13, fee14, fee15, fee16, fee17, fee18, fee19, fee20, fee21, fee22, fee23, fee24, fee25) values \n" +
            " <foreach collection='list' item='item'  separator=','> " +
            " (#{item.hospitalName},#{item.socialCreditCode},#{item.patientId},#{item.times},#{item.name},#{item.sex},#{item.birthDay,jdbcType=TIMESTAMP},#{item.marryCode},#{item.country},#{item.nation},#{item.certificateType},#{item.socialNo},#{item.adress},#{item.phoneNo},#{item.gms},#{item.allergenName},#{item.qtgms},#{item.qtgmy},#{item.ghDate,jdbcType=TIMESTAMP},#{item.bdDate,jdbcType=TIMESTAMP},#{item.visitDate,jdbcType=TIMESTAMP},#{item.deptCode},#{item.doctorName},#{item.doctorZc},#{item.visitType},#{item.firstOrNot},#{item.syOrNot},#{item.mzMtb},#{item.mzHzFj},#{item.jzHzQx},#{item.zyzDate,jdbcType=TIMESTAMP},#{item.emrChiefComplaint},#{item.icdName},#{item.icdCode},#{item.icdName1},#{item.icdCode1},#{item.icdName2},#{item.icdCode2},#{item.icdName3},#{item.icdCode3},#{item.icdName4},#{item.icdCode4},#{item.icdName5},#{item.icdCode5},#{item.icdName6},#{item.icdCode6},#{item.icdName7},#{item.icdCode7},#{item.icdName8},#{item.icdCode8},#{item.icdName9},#{item.icdCode9},#{item.icdName10},#{item.icdCode10},#{item.oprDate1,jdbcType=TIMESTAMP},#{item.oprName1},#{item.oprCode1},#{item.oprDoctor1},#{item.mzfs1},#{item.mzDoctor1},#{item.oprScale1},#{item.oprDate2,jdbcType=TIMESTAMP},#{item.oprName2},#{item.oprCode2},#{item.oprDoctor2},#{item.mzfs2},#{item.mzDoctor2},#{item.oprScale2},#{item.oprDate3,jdbcType=TIMESTAMP},#{item.oprName3},#{item.oprCode3},#{item.oprDoctor3},#{item.mzfs3},#{item.mzDoctor3},#{item.oprScale3},#{item.oprDate4,jdbcType=TIMESTAMP},#{item.oprName4},#{item.oprCode4},#{item.oprDoctor4},#{item.mzfs4},#{item.mzDoctor4},#{item.oprScale4},#{item.oprDate5,jdbcType=TIMESTAMP},#{item.oprName5},#{item.oprCode5},#{item.oprDoctor5},#{item.mzfs5},#{item.mzDoctor5},#{item.oprScale5},#{item.totalFee,jdbcType=DECIMAL},#{item.zfFee,jdbcType=DECIMAL},#{item.fee1,jdbcType=DECIMAL},#{item.fee2,jdbcType=DECIMAL},#{item.fee3,jdbcType=DECIMAL},#{item.fee4,jdbcType=DECIMAL},#{item.fee5,jdbcType=DECIMAL},#{item.fee6,jdbcType=DECIMAL},#{item.fee7,jdbcType=DECIMAL},#{item.fee8,jdbcType=DECIMAL},#{item.fee901,jdbcType=DECIMAL},#{item.fee902,jdbcType=DECIMAL},#{item.fee1001,jdbcType=DECIMAL},#{item.fee1002,jdbcType=DECIMAL},#{item.fee1003,jdbcType=DECIMAL},#{item.fee11,jdbcType=DECIMAL},#{item.fee12,jdbcType=DECIMAL},#{item.fee13,jdbcType=DECIMAL},#{item.fee14,jdbcType=DECIMAL},#{item.fee15,jdbcType=DECIMAL},#{item.fee16,jdbcType=DECIMAL},#{item.fee17,jdbcType=DECIMAL},#{item.fee18,jdbcType=DECIMAL},#{item.fee19,jdbcType=DECIMAL},#{item.fee20,jdbcType=DECIMAL},#{item.fee21,jdbcType=DECIMAL},#{item.fee22,jdbcType=DECIMAL},#{item.fee23,jdbcType=DECIMAL},#{item.fee24,jdbcType=DECIMAL},#{item.fee25,jdbcType=DECIMAL})" +
            " </foreach> " +
            " </script>")
    int batchInsertData(List<MzMedicalMain> list);
    @Select(" <script> " +
            " select hospital_name,social_credit_code,patient_id, times, name, sex, birth_day, marry_code, country, nation, certificate_type, social_no, adress, phone_no, gms, allergen_name, qtgms, qtgmy, gh_date, bd_date, visit_date, dept_code, doctor_name, doctor_zc, visit_type, first_or_not, sy_or_not, mz_mtb, mz_hz_fj, jz_hz_qx, zyz_date, emr_chief_complaint, icd_name, icd_code, icd_name1, icd_code1, icd_name2, icd_code2, icd_name3, icd_code3, icd_name4, icd_code4, icd_name5, icd_code5, icd_name6, icd_code6, icd_name7, icd_code7, icd_name8, icd_code8, icd_name9, icd_code9, icd_name10, icd_code10, opr_date1, opr_name1, opr_code1, opr_doctor1, mzfs1, mz_doctor1, opr_scale1, opr_date2, opr_name2, opr_code2, opr_doctor2, mzfs2, mz_doctor2, opr_scale2, opr_date3, opr_name3, opr_code3, opr_doctor3, mzfs3, mz_doctor3, opr_scale3, opr_date4, opr_name4, opr_code4, opr_doctor4, mzfs4, mz_doctor4, opr_scale4, opr_date5, opr_name5, opr_code5, opr_doctor5, mzfs5, mz_doctor5, opr_scale5, total_fee, zf_fee, fee1, fee2, fee3, fee4, fee5, fee6, fee7, fee8, fee901, fee902, fee1001, fee1002, fee1003, fee11, fee12, fee13, fee14, fee15, fee16, fee17, fee18, fee19, fee20, fee21, fee22, fee23, fee24, fee25,del_flag from mz_medical_main   where del_flag = #{param.delFlag} and  visit_date &gt;= #{param.startTime} and visit_date &lt;=#{param.endTime}" +
            " <if test=\"  param.keyWard!='' and param.keyWard!=null \">" +
            " <bind name=\"keyWardPatten\" value=\"+'%'+param.keyWard+'%'\"/>"+
            " and (patient_id like #{keyWardPatten} or name like #{keyWardPatten}) "+
            "</if>"+
            "</script>")
    List<MzMedicalMain> selectMzMedicalMainPage(@Param("page")IPage<MzMedicalMain> page, @Param("param") MzMedicalMainParam param);
    @Update(" update mz_medical_main set del_flag = #{delFlag} where patient_id =#{patientId} and times=#{times}")
    int updateDelFlag(MzMedicalMain mzMedicalMain);

    @Select(" select  " +
            "    a.patient_id,  " +
            "    a.times,  " +
            "   0  del_flag,  " +
            "    mpm.name,  " +
            "    mpm.sex,  " +
            "    mpm.birth_day,  " +
            "    mpm.certificate_type ,  " +
            "    mpm.social_no,  " +
            "    mpm.adress,  " +
            "    mpm.phone_no,  " +
            "    '' marry_code ,  " +
            "    '中国' country,  " +
            "    '1' nation,  " +
            "    a.emr_chief_complaint,  " +
            "    b.icd_code_new,  " +
            "    b.icd_text_new,  " +
            "    case when isnull(b.jz_flag,'')='1' then '1' else '2' end visit_type,  " +
            "    a.visit_date,  " +
            "    mzr.visit_date  zyz_date,  " +
            "   (select name from zd_unit_code where a.dept_code=code ) dept_code,  " +
            "    (select name from a_employee_mi where a.doctor_code=code) doctor_name,  " +
            "    (select zet.medical_code from a_employee_mi aem ,zd_emp_title zet where a.doctor_code=aem.code and emp_tit_code=zet.code ) doctor_zc ,  " +
            "    a.first_or_not,  " +
            "    fee1,  " +
            "    fee2,  " +
            "    fee3,  " +
            "    fee4,  " +
            "    fee5,  " +
            "    fee6,  " +
            "    fee7,  " +
            "    fee8,  " +
            "    fee901,  " +
            "    fee902,  " +
            "    fee1001,  " +
            "    fee1002,  " +
            "    fee1003,  " +
            "    fee11,  " +
            "    fee12,  " +
            "    fee13,  " +
            "    fee14,  " +
            "    fee15,  " +
            "    fee16,  " +
            "    fee17,  " +
            "    fee18,  " +
            "    fee19,  " +
            "    fee20,  " +
            "    fee21,  " +
            "    fee22,  " +
            "    fee23,  " +
            "    fee24  " +
            " from mz_bl_record a  " +
            "    join mz_visit_table b on a.patient_id = b.patient_id and a.times = b.times  " +
            "    join mz_patient_mi mpm on a.patient_id = mpm.patient_id  " +
            "    join (  " +
            "    select patient_id,  " +
            "           times,  " +
            "           sum(case when zd_charge_ba_class = '01' then convert(decimal(18, 2),unit_price*quantity*drug_win) else 0 end) fee1,  " +
            "           sum(case when zd_charge_ba_class = '02' then convert(decimal(18, 2),unit_price*quantity*drug_win) else 0 end) fee2,  " +
            "           sum(case when zd_charge_ba_class = '03' then convert(decimal(18, 2),unit_price*quantity*drug_win) else 0 end) fee3,  " +
            "           sum(case when zd_charge_ba_class = '04' then convert(decimal(18, 2),unit_price*quantity*drug_win) else 0 end) fee4,  " +
            "           sum(case when zd_charge_ba_class = '05' then convert(decimal(18, 2),unit_price*quantity*drug_win) else 0 end) fee5,  " +
            "           sum(case when zd_charge_ba_class = '06' then convert(decimal(18, 2),unit_price*quantity*drug_win) else 0 end) fee6,  " +
            "           sum(case when zd_charge_ba_class = '07' then convert(decimal(18, 2),unit_price*quantity*drug_win) else 0 end) fee7,  " +
            "           sum(case when zd_charge_ba_class = '08' then convert(decimal(18, 2),unit_price*quantity*drug_win) else 0 end) fee8,  " +
            "           sum(case when zd_charge_ba_class = '0901' then convert(decimal(18, 2),unit_price*quantity*drug_win) else 0 end) fee901,  " +
            "           sum(case when zd_charge_ba_class = '0902' then convert(decimal(18, 2),unit_price*quantity*drug_win) else 0 end) fee902,  " +
            "           sum(case when zd_charge_ba_class = '1001' then convert(decimal(18, 2),unit_price*quantity*drug_win) else 0 end) fee1001,  " +
            "           sum(case when zd_charge_ba_class = '1002' then convert(decimal(18, 2),unit_price*quantity*drug_win) else 0 end) fee1002,  " +
            "           sum(case when zd_charge_ba_class = '1003' then convert(decimal(18, 2),unit_price*quantity*drug_win) else 0 end) fee1003,  " +
            "           sum(case when zd_charge_ba_class = '11' then convert(decimal(18, 2),unit_price*quantity*drug_win) else 0 end) fee11,  " +
            "           sum(case when zd_charge_ba_class = '12' then convert(decimal(18, 2),unit_price*quantity*drug_win) else 0 end) fee12,  " +
            "           sum(case when a.bill_item_code='010' then convert(decimal(18, 2),unit_price*quantity*drug_win) else 0 end) fee13,  " +
            "           sum(case when a.bill_item_code='011' then convert(decimal(18, 2),unit_price*quantity*drug_win) else 0 end) fee14,  " +
            "           sum(case when a.bill_item_code='012' then convert(decimal(18, 2),unit_price*quantity*drug_win) else 0 end) fee15,  " +
            "           sum(case when zd_charge_ba_class = '16' then convert(decimal(18, 2),unit_price*quantity*drug_win) else 0 end) fee16,  " +
            "           sum(case when zd_charge_ba_class = '17' then convert(decimal(18, 2),unit_price*quantity*drug_win) else 0 end) fee17,  " +
            "           sum(case when zd_charge_ba_class = '18' then convert(decimal(18, 2),unit_price*quantity*drug_win) else 0 end) fee18,  " +
            "           sum(case when zd_charge_ba_class = '19' then convert(decimal(18, 2),unit_price*quantity*drug_win) else 0 end) fee19,  " +
            "           sum(case when zd_charge_ba_class = '20' then convert(decimal(18, 2),unit_price*quantity*drug_win) else 0 end) fee20,  " +
            "           sum(case when zd_charge_ba_class = '21' then convert(decimal(18, 2),unit_price*quantity*drug_win) else 0 end) fee21,  " +
            "           sum(case when zd_charge_ba_class = '22' then convert(decimal(18, 2),unit_price*quantity*drug_win) else 0 end) fee22,  " +
            "           sum(case when zd_charge_ba_class = '23' then convert(decimal(18, 2),unit_price*quantity*drug_win) else 0 end) fee23,  " +
            "           sum(case when zd_charge_ba_class = '24' or (a.bill_item_code not in ('010','011','012') and isnull(zd_charge_ba_class,'')='') then convert(decimal(18, 2),unit_price*quantity*drug_win) else 0 end) fee24  " +
            "    from mz_charge_detail a  " +
            "            left join zd_charge_item b on a.charge_item_code = b.code  " +
            "    where  " +
            "       pay_mark = '0'  " +
            "      and a.bill_item_code !='TC' " +
            "      and tf_flag != 1  " +
            "      and receipt_no > 0  " +
            "    group by patient_id, times  " +
            "    )mzc on a.patient_id=mzc.patient_id and a.times=mzc.times " +
            "  left join mz_zy_req mzr on a.patient_id=mzr.patient_id and  a.visit_date=mzr.visit_date " +
            " where a.visit_date >= #{startTime}  " +
            "  and a.visit_date <= #{endTime}  " +
            "  and isnull(b.icd_code_new,'') != ''")
   List<MzMedicalMain> selectMzMedicalSourceData(@Param("startTime")String startTime ,@Param("endTime")String endTime);
    @Select(" select  " +
            "    a.patient_id,  " +
            "    a.times,  " +
            "    mpm.name,  " +
            "    mpm.sex,  " +
            "    mpm.birth_day,  " +
            "    mpm.certificate_type,  " +
            "    mpm.social_no,  " +
            "    mpm.adress,  " +
            "    mpm.phone_no,  " +
            "    '' marry_code ,  " +
            "    '中国' country,  " +
            "    '1' nation_code,  " +
            "    a.emr_chief_complaint,  " +
            "    b.icd_code_new,  " +
            "    b.icd_text_new,  " +
            "    case when isnull(b.jz_flag,'')='1' then '1' else '2' end visit_type,  " +
            "    a.visit_date,  " +
            "    a.dept_code,  " +
            "    (select name from a_employee_mi where a.doctor_code=code) doctor_name,  " +
            "    (select zet.medical_code from a_employee_mi aem ,zd_emp_title zet where a.doctor_code=aem.code and emp_tit_code=zet.code ) doctor_zc ,  " +
            "    a.first_or_not,  " +
            "    b.visit_date zyz_date  " +
            " from mz_bl_record a  " +
            "         join mz_visit_table b on a.patient_id = b.patient_id and a.times = b.times  " +
            "         join mz_patient_mi mpm on a.patient_id = mpm.patient_id  " +
            "         join mz_zy_req mzr on mzr.patient_id = b.patient_id and mzr.visit_date = b.visit_date  " +
            "    where  " +
            "          b.visit_date >=#{startTime}  " +
            "         and b.visit_date <=>=#{endTime} ")
    List<MzMedicalMain> selectZyzMzMedicalSourceData(@Param("startTime") String startTime, @Param("endTime") String endTime);

    @Select(" select pat_no patient_id," +
            "       case " +
            "           when type = 0 then (select name from zd_allergen where code = allergen_code) " +
            "           else (select top 1 name from yp_zd_dict where code = allergen_code) end allergen_name " +
            "from patient_allergen_info ")
    List<MzMedicalMain> selectAllergenInfo();

    @Select(" select  inpatient_no patient_id,  " +
            "              admiss_times times,  " +
            "              op_code opr_code1,  " +
            "              op_name opr_name1,  " +
            "              op_scale op_scale1,  " +
            "              a3.wjw_code mzfs1,  " +
            "               case when isnull(a3.wjw_code,'') !='' then isnull((select name from a_employee_mi where code= a2.doctor_mz_ys),(select name from a_employee_mi where code= a2.doctor_zd)) else '' end mz_doctor1,  " +
            "              (select name from a_employee_mi where code =doctor_zd ) opr_doctor1,  " +
            "              op_datetime opr_date1  " +
            " from op_record a1  " +
            "         join op_record_join a2 on a1.record_id = a2.record_id  " +
            "         left join  zd_anaesthesia a3 on  a1.hocus_code = a3.wjw_code  " +
            " where patient_type = '1' and op_datetime >=#{startTime} and op_datetime <=#{endTime}")
    List<MzMedicalMain> selectOprtSource(@Param("startTime") String startTime, @Param("endTime") String endTime);
    @Delete(" delete from  mz_medical_main where visit_date >=#{startTime} and visit_date <=#{endTime}")
    int delByVistDate(@Param("startTime") String startTime, @Param("endTime") String endTime);
    @Select(" select *  from mz_medical_zd  ")
    List<Map<String, Object>> selectMzMedicalZd();
    @Select(" select top 1 1  from mz_medical_main where visit_date >=#{startTime} and visit_date <=#{endTime}  ")
    Integer selectCount(@Param("startTime") String startTime, @Param("endTime") String endTime);
}
