package thyyxxk.webserver.dao.his.medicalinsurance;

import org.apache.ibatis.annotations.*;
import thyyxxk.webserver.entity.dictionary.CodeName;
import thyyxxk.webserver.entity.markmtfees.MzVisit;
import thyyxxk.webserver.entity.medicalinsurance.inpatient.ZyPatientInfo;
import thyyxxk.webserver.entity.medicalinsurance.query.*;
import thyyxxk.webserver.entity.medicalinsurance.setlinfo.TimesList;
import thyyxxk.webserver.entity.medicalinsurance.setllist.BrfChrgitm;
import thyyxxk.webserver.entity.medicalinsurance.setllist.CuminfoInYear;
import thyyxxk.webserver.entity.medicalinsurance.setllist.InptntSetlmtLst;
import thyyxxk.webserver.entity.medicalinsurance.setllist.OtptntSetlmtLst;
import thyyxxk.webserver.entity.inpatient.ZyActpatient;

import java.util.Date;
import java.util.List;

/**
 * @description: 查询服务Mapper
 * @author: DingJie
 * @create: 2021/7/2111:17
 */
@Mapper
public interface SiQueryDao {

    @Select("select isnull(max(ledger_sn), isnull(min(ledger_sn), 1)) from zy_ledger_file where " +
            "inpatient_no=#{inpatientNo} and admiss_times=#{admissTimes}")
    Integer selectMaxLedgerSn(String inpatientNo, int admissTimes);

    /**
     * 查询门诊患者的身份证和姓名
     *
     * @param patNo 门诊号
     * @return 门诊患者的身份证和姓名
     */
    @Select("select rtrim(social_no) as socialNo,rtrim(name) as name,times, " +
            "rtrim(certificate_type) as psnCertType from mz_patient_mi where patient_id=#{patNo}")
    QryPsnBsInfo selectMzSocialAndName(String patNo);

    /**
     * 是否已保存过此患者的医保基本信息
     *
     * @param inpatientNo 住院号
     * @param admissTimes 住院次数
     * @param ledgerSn    账页号
     * @return 0-未保存 1-已保存
     */
    @Select("select count(1) from t_si_pat_info where pat_no=#{inpatientNo} " +
            "and times=#{admissTimes} and ledger_sn=#{ledgerSn}")
    int selectSiZyInfoCount(String inpatientNo, int admissTimes, int ledgerSn);

    /**
     * 新增患者的医保基本信息（个人编码）
     *
     * @param psnBaseinfo 个人基本信息
     */
    @Insert("insert into t_si_pat_info (pat_no, times, ledger_sn, psn_no, psn_cert_type, balc, " +
            "certno, psn_name, gend, naty, brdy, age, med_type, psn_idet_type, create_datetime) " +
            "values (#{patNo},#{times},#{ledgerSn},#{psnNo},#{psnCertType},#{balc},#{certno}," +
            "#{psnName},#{gend},#{naty},#{brdy},#{age},#{medType},#{psnIdetType},getdate())")
    void insertSiZyInfoPsnNo(PsnBaseInfo psnBaseinfo);

    /**
     * 更新患者的医保基本信息（个人编码）
     *
     * @param psnBaseinfo 个人基本信息
     */
    @Update("update t_si_pat_info set psn_no=#{psnNo}, age=#{age}, med_type=#{medType} " +
            "where pat_no=#{patNo} and times=#{times} and ledger_sn=#{ledgerSn}")
    void updateSiZyInfoPsnNo(PsnBaseInfo psnBaseinfo);

    @Update("update zy_actpatient set med_type=#{medType} where inpatient_no=#{patNo}")
    void updateZyActpatient(PsnBaseInfo psnBaseInfo);

    @Update("update t_si_pat_info set psn_idet_type=#{psnIdetType} where psn_no=#{psnNo} ")
    void updatePsnIdetType(String psnIdetType, String psnNo);

    @Select("select * from t_si_pat_info where pat_no=#{patNo} and times=#{times} and ledger_sn=#{ledgerSn}")
    SiPatInfo selectSiPatInfo(String patNo, int times, int ledgerSn);

    @Select("select insuplc_admdvs_name from t_si_pat_info where " +
            "pat_no=#{inpatientNo} and times=#{admissTimes} and ledger_sn=#{ledgerSn}")
    String getInsuplcAdmdvsName(ZyPatientInfo p);

    @Select("select psn_no,insu_code as insuplc_admdvs from powersi_mip_setlinfo " +
            "where med_org_ord=#{hisOrdNum} and ord_state='SETTLED'")
    SiPatInfo selectSiPatInfo2(String hisOrdNum);

    /**
     * 获取患者的医保个人编号
     *
     * @param patNo 住院号/门诊ID
     * @return 患者的医保个人编号
     */
    @Select("select top 1 psn_no from t_si_pat_info where pat_no=#{patNo} order by times desc")
    String selectPsnNo(String patNo);

    @Select("select * from t_si_pat_info where mdtrt_id=#{mdtrtId}")
    SiPatInfo selectSiPatInfoByMdtrtId(String mdtrtId);

    /**
     * 获取患者的医保个人编号
     *
     * @param patNo   住院号
     * @param medType 医疗类别
     * @return 患者的医保个人编号
     */
    @Select("select top 1 insuplc_admdvs as code, psn_no as name from t_si_pat_info where pat_no=#{patNo} and med_type=#{medType} " +
            "and insuplc_admdvs is not null ")
    CodeName selectPsnNoByPatNoAndMedType(String patNo, String medType);

    /**
     * 取消入院登记后清空就诊id
     *
     * @param inpatientNo 住院号
     * @param admissTimes 住院次数
     * @param ledgerSn    账页号
     */
    @Update("update t_si_pat_info set mdtrt_id=#{nullCtnt}, adm_reg_msgid=#{nullCtnt}, setl_id=#{nullCtnt} " +
            "where pat_no=#{inpatientNo} and times=#{admissTimes} and ledger_sn=#{ledgerSn}")
    void clearMdtrtIdForZy(String inpatientNo, int admissTimes, int ledgerSn, String nullCtnt);

    @Select("select psn_cert_type from a_patient_mi where inpatient_no=#{patNo}")
    String selectPsnCertType(String patNo);

    @Select("select top 1 psn_cert_type from a_patient_mi where social_no=#{socialNo} and psn_cert_type is not null")
    String selectPsnCertTypeBoSocialNo(String socialNo);

    @Select("select setl_id from t_si_setlinfo where pat_no=#{patNo} and times=#{times} and ledger_sn=#{ledgerSn} and revoked=0")
    String selectSetlId(String patNo, int times, int ledgerSn);

    @Select("select top 1 insuplc_admdvs from t_si_pat_info where pat_no=#{patNo} and times=#{times} " +
            "and insuplc_admdvs is not null order by ledger_sn desc")
    String selectAdmdvs(String patNo, int times);

    @Select("select top 1 insuplc_admdvs from t_si_pat_info where pat_no=#{patNo} and insuplc_admdvs is not null order by times desc")
    String selectAdmdvsByPatNo(String patNo);

    @Select("select top 1 insuplc_admdvs from t_si_pat_info where psn_no=#{psnNo} and insuplc_admdvs is not null order by times desc")
    String selectAdmdvsByPsnNo(String psnNo);

    @Select("select top 1 insuplc_admdvs from t_si_pat_info where pat_no=#{patNo} and psn_no=#{psnNo} " +
            "and insuplc_admdvs is not null order by times desc")
    String selectAdmdvsByPatNoAndPsnNo(String patNo, String psnNo);

    @Select("select a.mdtrt_id,a.psn_name,a.gend,a.age,a.psn_no,b.emp_name,a.psn_type,a.cvlserv_flag,a.pat_no," +
            "a.setl_id,b.insuplc_admdvs,a.med_type,a.setl_time,a.hifes_pay,a.insutype, " +
            "tel=(select rtrim(home_tel) from a_patient_mi where a_patient_mi.inpatient_no=a.pat_no), " +
            "admDeptName=(select rtrim(name) from zd_unit_code where code=isnull(c.zk_ward,c.small_dept)), " +
            "c.bed_no as admBed,a.begntime,a.endtime,a.certno,a.mdtrt_cert_type,b.psn_idet_type, " +
            "dscgMaindiagName=(select rtrim(dis_diag_comment) from zy_dis_diag_yb where zy_dis_diag_yb.inpatient_no=a.pat_no " +
            "and zy_dis_diag_yb.admiss_times=a.times and zy_dis_diag_yb.dis_diag_no=1), " +
            "chfpdrName=(select rtrim(name) from a_employee_mi where code=c.refer_physician), " +
            "hosp_part_amt=(select hosp_part_amt from zy_ledger_file where inpatient_no=a.pat_no), " +
            "staffName=(select rtrim(name) from a_employee_mi where a_employee_mi.code=a.staff_id), " +
            "a.balc,a.medfee_sumamt,a.hifp_pay,a.acct_pay,a.cvlserv_pay,a.psn_cash_pay, " +
            "a.hifob_pay,a.oth_pay,a.hifmi_pay,a.maf_pay " +
            "from t_si_setlinfo a, t_si_pat_info b, ${table} c where a.setl_id=#{setlId} " +
            "and a.pat_no=b.pat_no and a.times=b.times and a.ledger_sn=b.ledger_sn and a.pat_no=c.inpatient_no " +
            "and a.times=c.admiss_times")
    InptntSetlmtLst selectZySetlinfo(String setlId, String table);

    @Select("select a.mdtrt_id,a.psn_name,a.gend,a.age,a.psn_no,b.emp_name,a.psn_type,a.cvlserv_flag,a.pat_no, " +
            "a.setl_id,b.insuplc_admdvs,a.med_type,a.setl_time,a.hifes_pay,a.insutype, " +
            "tel=(select rtrim(home_tel) from a_patient_mi where a_patient_mi.inpatient_no=a.pat_no), " +
            "admDeptName=(select rtrim(name) from zd_unit_code where code=c.visit_dept_code), " +
            "'JZ-1' as admBed,a.begntime,a.endtime,a.certno,a.mdtrt_cert_type,b.psn_idet_type, " +
            "dscgMaindiagName=(select rtrim(dis_diag_comment) from zy_dis_diag_yb where zy_dis_diag_yb.inpatient_no=a.pat_no " +
            "and zy_dis_diag_yb.admiss_times=a.times and zy_dis_diag_yb.dis_diag_no=1), " +
            "chfpdrName=(select rtrim(name) from a_employee_mi where code=c.doctor_code), " +
            "staffName=(select rtrim(name) from a_employee_mi where a_employee_mi.code=a.staff_id), " +
            "a.balc,a.medfee_sumamt,a.hifp_pay,a.acct_pay,a.cvlserv_pay,a.psn_cash_pay, " +
            "a.hifob_pay,a.oth_pay,a.hifmi_pay,a.maf_pay " +
            "from t_si_setlinfo a, t_si_pat_info b, mz_visit_table c where a.setl_id=#{setlId} " +
            "and a.pat_no=b.pat_no and a.times=b.times and a.ledger_sn=b.ledger_sn and a.pat_no=c.patient_id " +
            "and a.times=c.times")
    InptntSetlmtLst selectJzSetlinfo(String setlId);
    
    @Select("select a.mdtrt_id,a.setl_id,a.psn_name,a.gend,a.age,a.cvlserv_flag,a.psn_type,a.pat_no,a.setl_time, " +
            "b.dise_name,b.emp_name,a.certno,a.med_type,b.dise_code,a.medfee_sumamt,a.psn_no,a.begntime, " +
            "a.hifp_pay,a.acct_pay,a.cvlserv_pay,a.psn_cash_pay,b.insuplc_admdvs,a.insutype,a.balc, " +
            "a.hifmi_pay,a.maf_pay,a.hifob_pay,a.oth_pay,a.staff_id,a.hifes_pay,a.psn_cert_type, " +
            "staffName=(select rtrim(name) from a_employee_mi where code=staff_id),b.psn_idet_type, " +
            "chfpdrName=(select rtrim(doctor_code) from mz_visit_table where patient_id=#{patNo} and times=#{times}), " +
            "admDeptName=(select rtrim(name) from zd_unit_code where code=" +
            "(select t.visit_dept_code from mz_visit_table t where t.patient_id=#{patNo} and t.times=#{times})), " +
            "tel=(select rtrim(phone_no) from mz_patient_mi where patient_id=#{patNo}) " +
            "from t_si_setlinfo a, t_si_pat_info b where a.pat_no=#{patNo} " +
            "and a.times=#{times} and isnull(a.revoked,0)!=1 " +
            "and a.pat_no=b.pat_no and a.times=b.times")
    OtptntSetlmtLst selectMzSetlifo(String patNo, int times);

    @Select("select hi_ext_data from powersi_mip_setlinfo where med_org_ord=#{hisOrdNum} and ord_state='SETTLED'")
    String getHiExtData(String hisOrdNum);

    @Select("select isnull(icd_text_new,'') as icdText,visit_date,rtrim(doctor_code) as doctorCode, " +
            "visitDeptName=(select rtrim(d.name) from zd_unit_code d where d.code=visit_dept_code) " +
            "from mz_visit_table where patient_id=#{patNo} and times=#{times}")
    MzVisit getMzVisit(SetlIndex index);

    @Select("select charge_fee,med_chrgitm_type,chrgitm_lv from t_mt_receipt where patient_id=#{patNo} " +
            "and times=#{times} and yb_trans_flag=1")
    List<BrfChrgitm> selectMzBrfChrgitms(String patNo, int times);

    @Select("select insuplc_admdvs from t_si_pat_info where mdtrt_id=#{mdtrtId}")
    String selectInsuplcAdmdvsByMdtrtId(String mdtrtId);

    @Select("select setl_id,times,ledger_sn,endtime from t_si_setlinfo where pat_no=#{patNo} and revoked=0 " +
            "and setl_type=#{type} order by endtime desc")
    List<TimesList> fetchSiTimesList(String patNo, int type);

    @Select("select hi_doc_sn as setlId,med_org_ord as hisOrdNum,trace_time as endtime from powersi_mip_setlinfo " +
            "where med_org_ord like #{patNo} and ord_state='SETTLED' order by trace_time desc")
    List<TimesList> getMipayTimesList(String patNo);
    
    @Select("select * from t_si_setl_fee_detl where mdtrt_id=#{mdtrtId}")
    List<SiSetlFeeDetl> selectAllSetlFeeDtle(String mdtrtId);

    @Select("select a.feedetl_sn,a.fee_ocur_time,a.cnt,cast(a.pric as decimal(16,2)) as pric," +
            "cast(a.det_item_fee_sumamt as decimal(16,2)) as det_item_fee_sumamt, " +
            "a.selfpay_prop,cast(a.fulamt_ownpay_amt as decimal(16,2)) as fulamt_ownpay_amt," +
            "cast(a.overlmt_amt as decimal(16,2)) as overlmt_amt,cast(a.preselfpay_amt as decimal(16,2)) as preselfpay_amt, " +
            "cast(a.inscp_scp_amt as decimal(16,2)) as inscp_scp_amt,a.chrgitm_lv,a.medins_list_name,a.med_list_codg, " +
            "a.med_chrgitm_type,a.bilg_dept_codg,a.bilg_dept_name,a.bilg_dr_codg, " +
            "a.bilg_dr_name,a.selfpay_prop_percent,a.chrgitm_lv_name, " +
            "rtrim(b.charge_code_mx) as medins_list_codg from t_si_setl_fee_detl a, " +
            "zy_detail_charge b where mdtrt_id=#{mdtrtId} and b.inpatient_no=#{patNo}  " +
            "and b.admiss_times=#{times} and b.ledger_sn=#{sn} and b.detail_sn=a.feedetl_sn")
    List<SiSetlFeeDetl> getZyFeeDetlList(String mdtrtId, String patNo, int times, int sn);

    @Select("select a.feedetl_sn,a.fee_ocur_time,a.cnt,cast(a.pric as decimal(16,2)) as pric," +
            "cast(a.det_item_fee_sumamt as decimal(16,2)) as det_item_fee_sumamt, " +
            "a.selfpay_prop,cast(a.fulamt_ownpay_amt as decimal(16,2)) as fulamt_ownpay_amt," +
            "cast(a.overlmt_amt as decimal(16,2)) as overlmt_amt,cast(a.preselfpay_amt as decimal(16,2)) as preselfpay_amt, " +
            "cast(a.inscp_scp_amt as decimal(16,2)) as inscp_scp_amt,a.chrgitm_lv,a.medins_list_name,a.med_list_codg, " +
            "a.med_chrgitm_type,a.bilg_dept_codg,a.bilg_dept_name,a.bilg_dr_codg, " +
            "a.bilg_dr_name,a.selfpay_prop_percent,a.chrgitm_lv_name, " +
            "rtrim(b.his_item_code) as medins_list_codg from t_si_setl_fee_detl a, " +
            "t_mt_receipt b where a.mdtrt_id=#{mdtrtId} and b.patient_id=#{patNo}  " +
            "and b.times=#{times} and b.item_no=a.feedetl_sn")
    List<SiSetlFeeDetl> getMzFeeDetlList(String mdtrtId, String patNo, int times);

    @Select("select a.feedetl_sn,b.charge_date as fee_ocur_time,a.cnt," +
            "cast(a.pric as decimal(16,2)) as pric, cast(a.det_item_fee_sumamt as decimal(16,2)) as det_item_fee_sumamt, " +
            "a.selfpay_prop,cast(a.fulamt_ownpay_amt as decimal(16,2)) as fulamt_ownpay_amt, " +
            "cast(a.overlmt_amt as decimal(16,2)) as overlmt_amt,cast(a.preselfpay_amt as decimal(16,2)) as preselfpay_amt, " +
            "cast(a.inscp_scp_amt as decimal(16,2)) as inscp_scp_amt,a.chrgitm_lv, " +
            "med_list_codg=case when b.serial in ('','00') then (select max(national_code) from zd_charge_item where code=b.charge_code_mx) " +
            "else (select max(national_code) from yp_zd_dict where code=b.charge_code_mx) end, " +
            "medListName=case when b.serial in ('','00') then (select max(national_name) from zd_charge_item where code=b.charge_code_mx) " +
            "else (select max(national_name) from yp_zd_dict where code=b.charge_code_mx) end, " +
            "medins_list_name=case when b.serial in ('','00') then (select max(name) from zd_charge_item where code=b.charge_code_mx) " +
            "else (select max(name) from yp_zd_dict where code=b.charge_code_mx) end, " +
            "a.med_chrgitm_type,rtrim(b.ward_code) as bilg_dept_codg, " +
            "bilg_dept_name=(select rtrim(name) from zd_unit_code where code=ward_code), " +
            "bilg_dr_codg=isnull(isnull(b.doctor_code,b.refer_physician), b.op_id_code), " +
            "bilg_dr_name=isnull(isnull((select rtrim(name) from a_employee_mi where code=doctor_code), " +
            "(select rtrim(name) from a_employee_mi where code=refer_physician)), " +
            "(select rtrim(name) from a_employee_mi where code=op_id_code)), " +
            "rtrim(b.charge_code_mx) as medins_list_codg from t_si_charge_temp a, " +
            "zy_detail_charge b where a.pat_no=#{patNo} and a.times=#{times} and a.ledger_sn=#{sn} " +
            "and a.pat_no=b.inpatient_no and a.times=b.admiss_times and a.ledger_sn=b.ledger_sn " +
            "and a.feedetl_sn=b.detail_sn order by a.feedetl_sn")
    List<SiSetlFeeDetl> selectTempCharge(String patNo, int times, int sn);

    @Insert("<script>" +
            "insert into t_si_setl_fee_detl (mdtrt_id, setl_id, feedetl_sn, fee_ocur_time, cnt, pric, " +
            "det_item_fee_sumamt, selfpay_prop, fulamt_ownpay_amt, overlmt_amt, preselfpay_amt, inscp_scp_amt, " +
            "chrgitm_lv, hilist_code, hilist_name, list_type, med_list_codg, medins_list_codg, medins_list_name, " +
            "med_chrgitm_type, bilg_dept_codg, bilg_dept_name, bilg_dr_codg, bilg_dr_name, lmt_used_flag, " +
            "hosp_prep_flag, hosp_appr_flag, tcmdrug_used_way, prodplac_type, bas_medn_flag, hi_nego_drug_flag, " +
            "chld_medc_flag, etip_flag, etip_hosp_code, dscg_tkdrug_flag, list_sp_item_flag, matn_fee_flag, " +
            "drt_reim_flag, memo, opter_id, opter_name, opt_time, selfpay_prop_percent, chrgitm_lv_name) " +
            "values " +
            "<foreach collection='list' item='fee' open='' separator=',' close=''>" +
            "(#{fee.mdtrtId}, #{fee.setlId}, #{fee.feedetlSn}, #{fee.feeOcurTime}, #{fee.cnt}, #{fee.pric}, " +
            "#{fee.detItemFeeSumamt}, #{fee.selfpayProp}, #{fee.fulamtOwnpayAmt}, #{fee.overlmtAmt}, " +
            "#{fee.preselfpayAmt}, #{fee.inscpScpAmt}, #{fee.chrgitmLv}, #{fee.hilistCode}, #{fee.hilistName}, " +
            "#{fee.listType}, #{fee.medListCodg}, #{fee.medinsListCodg}, #{fee.medinsListName}, " +
            "#{fee.medChrgitmType}, #{fee.bilgDeptCodg}, #{fee.bilgDeptName}, #{fee.bilgDrCodg}, #{fee.bilgDrName}, " +
            "#{fee.lmtUsedFlag}, #{fee.hospPrepFlag}, #{fee.hospApprFlag}, #{fee.tcmdrugUsedWay}, #{fee.prodplacType}, " +
            "#{fee.basMednFlag}, #{fee.hiNegoDrugFlag}, #{fee.chldMedcFlag}, #{fee.etipFlag}, #{fee.etipHospCode}, " +
            "#{fee.dscgTkdrugFlag}, #{fee.listSpItemFlag}, #{fee.matnFeeFlag}, #{fee.drtReimFlag}, #{fee.memo}, " +
            "#{fee.opterId}, #{fee.opterName}, #{fee.optTime}, #{fee.selfpayPropPercent}, #{fee.chrgitmLvName})" +
            "</foreach>" +
            "</script>")
    void insertSetlFeeDetlBatch(List<SiSetlFeeDetl> list);

    @Select("select * from t_si_mz_matn_dise")
    List<SiMzMatnDise> selectAllMzMatnDises();

    @Update("update t_si_pat_info set mdtrt_id=#{mdtrtId},insutype=#{insutype} where pat_no=#{patNo} " +
            "and times=#{times} and ledger_sn=#{sn}")
    void updateInhospInfo(String patNo, int times, int sn, String mdtrtId, String insutype);

    @Select("SELECT pat_no, " +
            "   times, " +
            "   ledger_sn, " +
            "   insuplc_admdvs, " +
            "   medfee_sumamt, " +
            "   insutype, " +
            "   hifmi_pay, " +
            "   hifp_pay, " +
            "   cvlserv_pay, " +
            "   setl_list_id, " +
            "   psn_name, " +
            "   psn_cash_pay, " +
            "   setl_time, " +
            "   a.med_type, " +
            "   fundPaySumamt=(a.fund_pay_sumamt-a.hosp_part_amt), a.insutype, " +
            "  maf_pay, admiss_date = begntime, dis_date = endtime, " +
            "idCard = a.certno, dis_diag_comment = dis.dis_diag_comment, dis_diag = dis.dis_diag," +
            "    totalMedicalExpenses = medfee_sumamt," +
            "    fulamt_ownpay_amt," +
            "    overlmt_selfpay," +
            "    preselfpay_amt," +
            "    inscp_scp_amt," +
            "    act_pay_dedc," +
            "    hifp_pay," +
            "    hifes_pay," +
            "    hifob_pay,oth_pay,fund_pay_sumamt,psn_part_amt,acct_pay,balc,acct_mulaid_pay,hosp_part_amt " +
            "FROM t_si_setlinfo a " +
            "LEFT JOIN zy_dis_diag_yb dis " +
            " ON (a.pat_no = dis.inpatient_no " +
            "  AND a.times = dis.admiss_times and dis_diag_no = 1) " +
            "LEFT JOIN zy_inactpatient zy " +
            " ON (zy.inpatient_no = a.pat_no " +
            "  AND zy.admiss_times = a.times) " +
            "WHERE revoked = 0 " +
            "  AND setl_type = #{setlType} " +
            "  AND insutype LIKE #{insutype} " +
            "  AND setl_time>=#{begntime} " +
            "  AND setl_time<=#{endtime} " +
            "  AND insuplc_admdvs=#{pro} ")
    List<BaseSetlStatistics> selectBaseSetlStatisticsInProvinceLevel(String begntime, String endtime, String insutype, String setlType, String pro);

    @Select("select pat_no,times,ledger_sn,insuplc_admdvs,medfee_sumamt,insutype,hifmi_pay,hifp_pay,cvlserv_pay,setl_list_id,psn_name, " +
            "psn_cash_pay,setl_time,a.med_type,maf_pay,endtime as dis_date,begntime as admiss_date, " +
            "fundPaySumamt=(fund_pay_sumamt-hosp_part_amt),a.insutype, " +
            "       idCard = a.certno, " +
            "       dis_diag_comment = dis.dis_diag_comment, " +
            "       dis_diag = dis.dis_diag, " +
            "    totalMedicalExpenses = medfee_sumamt," +
            "    fulamt_ownpay_amt," +
            "    overlmt_selfpay," +
            "    preselfpay_amt," +
            "    inscp_scp_amt," +
            "    act_pay_dedc," +
            "    hifp_pay," +
            "    hifes_pay," +
            "    hifob_pay,oth_pay,fund_pay_sumamt,psn_part_amt,acct_pay,balc,acct_mulaid_pay,hosp_part_amt " +
            "from t_si_setlinfo a " +
            "         left join zy_dis_diag_yb dis on (a.pat_no = dis.inpatient_no and a.times = dis.admiss_times and dis_diag_no = 1) " +
            "         left join zy_inactpatient zy on (zy.inpatient_no = a.pat_no " +
            "    and zy.admiss_times = a.times) " +
            "where revoked=0 and setl_type=#{setlType} and insutype like #{insutype} and setl_time>=#{begntime} " +
            "and setl_time<=#{endtime} and insuplc_admdvs not in ('439900','430121','430181') and insuplc_admdvs like '4301%' ")
    List<BaseSetlStatistics> selectBaseSetlStatisticsInChangshaCity(String begntime, String endtime, String insutype, String setlType);

    @Select("select pat_no,times,ledger_sn,insuplc_admdvs,medfee_sumamt,insutype,hifmi_pay,hifp_pay,cvlserv_pay,setl_list_id,psn_name, " +
            "psn_cash_pay,setl_time,a.med_type, " +
            "fundPaySumamt=(fund_pay_sumamt-hosp_part_amt),a.insutype, " +
            "       maf_pay, " +
            "       admiss_date = begntime, " +
            "       dis_date = endtime, " +
            "       idCard = (select top 1 social_no from a_patient_mi where pat_no = inpatient_no), " +
            "       dis_diag_comment = dis.dis_diag_comment, " +
            "       dis_diag = dis.dis_diag, " +
            "    totalMedicalExpenses = medfee_sumamt," +
            "    fulamt_ownpay_amt," +
            "    overlmt_selfpay," +
            "    preselfpay_amt," +
            "    inscp_scp_amt," +
            "    act_pay_dedc," +
            "    hifp_pay," +
            "    hifes_pay," +
            "    hifob_pay,oth_pay,fund_pay_sumamt,psn_part_amt,acct_pay,balc,acct_mulaid_pay,hosp_part_amt " +
            "from t_si_setlinfo a " +
            "         left join zy_dis_diag_yb dis on (a.pat_no = dis.inpatient_no and a.times = dis.admiss_times and dis_diag_no = 1) " +
            "         left join zy_inactpatient zy on (zy.inpatient_no = a.pat_no " +
            "    and zy.admiss_times = a.times) " +
            "where revoked=0 and setl_type=#{setlType} and insutype like #{insutype} and setl_time>=#{begntime} " +
            "and setl_time<=#{endtime} and insuplc_admdvs='430121' ")
    List<BaseSetlStatistics> selectBaseSetlStatisticsInChangshaCounty(String begntime, String endtime, String insutype, String setlType);

    @Select("select pat_no,times,ledger_sn,insuplc_admdvs,medfee_sumamt,insutype,hifmi_pay,hifp_pay,cvlserv_pay,setl_list_id,psn_name, " +
            "psn_cash_pay,setl_time,a.med_type, " +
            "fundPaySumamt=(fund_pay_sumamt-hosp_part_amt),a.insutype, " +
            "       maf_pay, " +
            "       admiss_date = begntime, " +
            "       dis_date = endtime, " +
            "       idCard = (select top 1 social_no from a_patient_mi where pat_no = inpatient_no), " +
            "       dis_diag_comment = dis.dis_diag_comment, " +
            "       dis_diag = dis.dis_diag, " +
            "    totalMedicalExpenses = medfee_sumamt," +
            "    fulamt_ownpay_amt," +
            "    overlmt_selfpay," +
            "    preselfpay_amt," +
            "    inscp_scp_amt," +
            "    act_pay_dedc," +
            "    hifp_pay," +
            "    hifes_pay," +
            "    hifob_pay,oth_pay,fund_pay_sumamt,psn_part_amt,acct_pay,balc,acct_mulaid_pay,hosp_part_amt " +
            "from t_si_setlinfo a " +
            "         left join zy_dis_diag_yb dis on (a.pat_no = dis.inpatient_no and a.times = dis.admiss_times and dis_diag_no = 1) " +
            "         left join zy_inactpatient zy on (zy.inpatient_no = a.pat_no " +
            "    and zy.admiss_times = a.times) " +
            "where revoked=0 and setl_type=#{setlType} and insutype like #{insutype} and setl_time>=#{begntime} " +
            "and setl_time<=#{endtime} and insuplc_admdvs='430181' ")
    List<BaseSetlStatistics> selectBaseSetlStatisticsInLiuYangCity(String begntime, String endtime, String insutype, String setlType);

    @Select("select pat_no,times,ledger_sn,insuplc_admdvs,medfee_sumamt,insutype,hifmi_pay,hifp_pay,cvlserv_pay,setl_list_id,psn_name, " +
            "psn_cash_pay,setl_time,a.med_type, " +
            "fundPaySumamt=(fund_pay_sumamt-hosp_part_amt),a.insutype, " +
            "       maf_pay, " +
            "       admiss_date = begntime, " +
            "       dis_date = endtime, " +
            "       idCard = (select top 1 social_no from a_patient_mi where pat_no = inpatient_no), " +
            "       dis_diag_comment = dis.dis_diag_comment, " +
            "       dis_diag = dis.dis_diag, " +
            "    totalMedicalExpenses = medfee_sumamt," +
            "    fulamt_ownpay_amt," +
            "    overlmt_selfpay," +
            "    preselfpay_amt," +
            "    inscp_scp_amt," +
            "    act_pay_dedc," +
            "    hifp_pay," +
            "    hifes_pay," +
            "    hifob_pay,oth_pay,fund_pay_sumamt,psn_part_amt,acct_pay,balc,acct_mulaid_pay,hosp_part_amt " +
            "from t_si_setlinfo a " +
            "         left join zy_dis_diag_yb dis on (a.pat_no = dis.inpatient_no and a.times = dis.admiss_times and dis_diag_no = 1) " +
            "         left join zy_inactpatient zy on (zy.inpatient_no = a.pat_no " +
            "    and zy.admiss_times = a.times) " +
            "where revoked=0 and setl_type=#{setlType} and insutype like #{insutype} and setl_time>=#{begntime} " +
            "and setl_time<=#{endtime} and insuplc_admdvs!='439900' and insuplc_admdvs like '43%' and insuplc_admdvs not like '4301%' ")
    List<BaseSetlStatistics> selectBaseSetlStatisticsInProvinceOtherCities(String begntime, String endtime, String insutype, String setlType);

    @Select("select pat_no,times,ledger_sn,insuplc_admdvs,medfee_sumamt,insutype,hifmi_pay,hifp_pay,cvlserv_pay,setl_list_id,psn_name, " +
            "psn_cash_pay,setl_time,a.med_type, " +
            "fundPaySumamt=(fund_pay_sumamt-hosp_part_amt),a.insutype, " +
            "       maf_pay, " +
            "       admiss_date = begntime, " +
            "       dis_date = endtime, " +
            "       idCard = (select top 1 social_no from a_patient_mi where pat_no = inpatient_no), " +
            "       dis_diag_comment = dis.dis_diag_comment, " +
            "       dis_diag = dis.dis_diag, " +
            "    totalMedicalExpenses = medfee_sumamt," +
            "    fulamt_ownpay_amt," +
            "    overlmt_selfpay," +
            "    preselfpay_amt," +
            "    inscp_scp_amt," +
            "    act_pay_dedc," +
            "    hifp_pay," +
            "    hifes_pay," +
            "    hifob_pay,oth_pay,fund_pay_sumamt,psn_part_amt,acct_pay,balc,acct_mulaid_pay,hosp_part_amt " +
            "from t_si_setlinfo a " +
            "         left join zy_dis_diag_yb dis on (a.pat_no = dis.inpatient_no and a.times = dis.admiss_times and dis_diag_no = 1) " +
            "         left join zy_inactpatient zy on (zy.inpatient_no = a.pat_no " +
            "    and zy.admiss_times = a.times) " +
            "where revoked=0 and setl_type=#{setlType} and insutype like #{insutype} and setl_time>=#{begntime} " +
            "and setl_time<=#{endtime} and insuplc_admdvs!='439900' and insuplc_admdvs not like '43%' ")
    List<BaseSetlStatistics> selectBaseSetlStatisticsOutProvince(String begntime, String endtime, String insutype, String setlType);

    @Select("select pat_no,times,ledger_sn,insuplc_admdvs,medfee_sumamt,insutype,hifmi_pay,hifp_pay,cvlserv_pay,setl_list_id,psn_name, " +
            "psn_cash_pay,setl_time,a.med_type, " +
            "fundPaySumamt=(fund_pay_sumamt-hosp_part_amt),a.insutype, " +
            "       maf_pay, " +
            "       admiss_date = begntime, " +
            "       dis_date = endtime, " +
            "       idCard = (select top 1 social_no from a_patient_mi where pat_no = inpatient_no), " +
            "       dis_diag_comment = dis.dis_diag_comment, " +
            "       dis_diag = dis.dis_diag, " +
            "    totalMedicalExpenses = medfee_sumamt," +
            "    fulamt_ownpay_amt," +
            "    overlmt_selfpay," +
            "    preselfpay_amt," +
            "    inscp_scp_amt," +
            "    act_pay_dedc," +
            "    hifp_pay," +
            "    hifes_pay," +
            "    hifob_pay,oth_pay,fund_pay_sumamt,psn_part_amt,acct_pay,balc,acct_mulaid_pay,hosp_part_amt " +
            "from t_si_setlinfo a " +
            "         left join zy_dis_diag_yb dis on (a.pat_no = dis.inpatient_no and a.times = dis.admiss_times and dis_diag_no = 1) " +
            "         left join zy_inactpatient zy on (zy.inpatient_no = a.pat_no " +
            "    and zy.admiss_times = a.times) " +
            "where revoked=0 and setl_type=#{setlType} and insutype like #{insutype} and setl_time>=#{begntime} " +
            "and setl_time<=#{endtime} ")
    List<BaseSetlStatistics> selectBaseSetlStatisticsInAllPlaces(String begntime, String endtime, String insutype, String setlType);

    @Select("select code=(select b.name from t_region b where b.code=a.parent_code),a.name from t_region a where code=#{code}")
    CodeName selectAdmdvsNameAndParentName(String code);

    @Select("select name,code='' from t_si_admdvs where code=#{code}")
    CodeName selectAdmdvsName(String code);

    @Select("select count(1) from zy_ledger_file where inpatient_no=#{patNo} and admiss_times=#{times} and ledger_sn>0 ")
    Integer selectLedgerCount(String patNo, int times);

    @Select("select max(account_date) from zy_ledger_file where inpatient_no=#{patNo} and admiss_times=#{times} and ledger_sn>0")
    Date selectMaxLedgerAccountDate(String patNo, int times);

    @Select("select yb_register_date from zy_actpatient where inpatient_no=#{patNo}")
    Date selectRegisterDate(String patNo);

    @Select("<script>" +
            "select                     " +
            "                rtrim(a.inpatient_no)     inpatient_no," +
            "                a.admiss_times," +
            "                rtrim(name)               name," +
            " refer_physician_name = (select rtrim(name) from a_employee_mi where code = refer_physician)," +
            " small_dept_name      = (select rtrim(name) from zd_unit_code where code = small_dept)," +
            "                           zk_ward, " +
            "    zk_ward_name         = (select rtrim(name) " +
            "                            from zd_unit_code " +
            "                            where code = zk_ward)," +
            " responce_type_name   = (select rtrim(name) from zy_zd_responce_type where code = responce_type)," +
            "                total_charge," +
            "                sex," +
            "                rtrim(c.icd_code)         dis_diag," +
            "                rtrim(c.icd_text) dis_diag_comment," +
            "                certno," +
            "                insuplc_admdvs, " +
            " insuplc_admdvs_name  = isnull((select name from t_region where code = insuplc_admdvs), " +
            "                       (select name from t_si_admdvs where code = insuplc_admdvs))" +
            "from zy_actpatient a " +
            "left join t_si_pat_info b on (a.inpatient_no = b.pat_no and a.admiss_times = b.times) " +
            "left join zy_in_diag_yb c on (a.inpatient_no = c.inpatient_no and a.admiss_times = " +
            "c.admiss_times and c.diag_no = 1) " +
            "where  " +
            "setl_id is null and b.mdtrt_id is not null " +
            "<if test=\"startTime !=null and startTime != '' \">" +
            "and admiss_date &gt;= #{startTime} and admiss_date &lt;= #{endTime}  " +
            "</if>" +
            "<if test=\"patNo != null and patNo != '' \">" +
            "and a.inpatient_no = #{patNo} /* 住院号 */ " +
            "</if>" +
            "<if test=\"name != null and name != ''\">" +
            "and name like #{name} /*姓名*/ " +
            "</if>" +
            "<if test=\"certno !=null and certno != '' \">" +
            "and certno = '' /*身份证号码*/" +
            "</if>" +
            "</script>")
    List<ZyActpatient> zaiYuanHuanZheXinXi(String patNo, String startTime, String endTime, String name, String certno);

    @Select("select count(1) from t_si_cum_info where setl_id=#{setlId}")
    int selectCumInfoCount(String setlId);

    @Select("select * from t_si_cum_info where setl_id=#{setlId}")
    CuminfoInYear selectCumInfo(String setlId);

    @Insert("insert into t_si_cum_info (setl_id, year_big_dss_legal_fee, year_admtimes_zy, year_admtimes_mz, " +
            "year_period_fee_sum_zy, year_period_fee_sum_mz, year_medfee_sum_zy, year_medfee_sum_mz, year_payed_begnline_zy, " +
            "year_payed_begnline_mz, year_base_fund_pay_amt_zy, year_base_fund_pay_amt_mz, year_policy_self_pay_zy, year_pre_self_pay_zy, " +
            "year_policy_self_pay_mz, year_pre_self_pay_mz, year_big_amtpays_zy, year_big_amtpays_mz, " +
            "year_bigdss_fund_pay_amt_zy, year_bigdss_fund_pay_amt_mz, year_maf_pay_amt_zy, year_maf_pay_amt_mz, " +
            "year_acct_pay_zy, year_acct_pay_mz) " +
            "values (#{setlId},#{yearBigDssLegalFee},#{yearAdmtimesZy},#{yearAdmtimesMz},#{yearPeriodFeeSumZy}," +
            "#{yearPeriodFeeSumMz},#{yearMedfeeSumZy},#{yearMedfeeSumMz},#{yearPayedBegnlineZy},#{yearPayedBegnlineMz}," +
            "#{yearBaseFundPayAmtZy},#{yearBaseFundPayAmtMz},#{yearPolicySelfPayZy},#{yearPreSelfPayZy},#{yearPolicySelfPayMz}," +
            "#{yearPreSelfPayMz},#{yearBigAmtpaysZy},#{yearBigAmtpaysMz},#{yearBigdssFundPayAmtZy},#{yearBigdssFundPayAmtMz}," +
            "#{yearMafPayAmtZy},#{yearMafPayAmtMz},#{yearAcctPayZy},#{yearAcctPayMz})")
    void insertCumInfo(CuminfoInYear cuminfo);

    @Select("select count(1) from zy_detail_charge where inpatient_no=#{patNo} and admiss_times=#{times} " +
            "and ledger_sn=#{ledgerSn} and trans_flag_yb='1' ")
    int getUploadedCount(String patNo, int times, int ledgerSn);
    @Select("select detail_sn from zy_detail_charge where inpatient_no=#{zyh} and admiss_times=#{times} " +
            "and ledger_sn=#{ledger} and trans_flag_yb=1 and solid is null")
    List<Integer> getUnsolidSn(String zyh, int times, int ledger);

    @Select("select count(1) from zy_detail_charge where inpatient_no=#{zyh} and admiss_times=#{times} " +
            "and ledger_sn=#{ledger} and solid=1")
    Integer getDbSolidSize(String zyh, int times, int ledger);

    @Select("select count(1) from zy_detail_charge where inpatient_no=#{zyh} and admiss_times=#{times} " +
            "and ledger_sn=#{ledger} and trans_flag_yb=1")
    Integer getDbUploadedSize(String zyh, int times, int ledger);
}
