package thyyxxk.webserver.dao.his.api.forpowersi.drgdip;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import thyyxxk.webserver.entity.api.forpowersi.drgdip.*;
import thyyxxk.webserver.entity.api.forpowersi.drgdip.statement.*;

import java.util.List;

@Mapper
public interface DrgDipDao {

    @Select("select * from " +
            "(SELECT ROW_NUMBER() OVER(ORDER BY code) AS idx,del_flag as flag, " +
            "rtrim(code) as deptCode,rtrim(name) as deptName,rtrim(code) as sortNo, " +
            "isClinicDept=case when inpatient_flag=1 then 'Y' else 'N' end " +
            "from zd_unit_code with(nolock)) t where t.idx>#{page}*#{size} " +
            "and t.idx<=(#{page}+1)*#{size}")
    List<HisBaseDeptInfo> selectHisBaseDeptInfo(int page, int size);

    @Select("select * from " +
            "(SELECT ROW_NUMBER() OVER(ORDER BY code) AS idx, " +
            "rtrim(code) as empId, " +
            "rtrim(code_rs) as empNo, " +
            "rtrim(name) as empName, " +
            "rtrim(yb_code) as standardCode, " +
            "rtrim(dept_code) as empDeptId, " +
            "empDept=(select rtrim(z.name) from zd_unit_code z where z.code=dept_code) " +
            "from a_employee_mi with(nolock) where del_flag!=1) t where t.idx>#{page}*#{size} " +
            "and t.idx<=(#{page}+1)*#{size}")
    List<HisBaseEmpInfo> selectHisBaseEmpInfo(int page, int size);

    @Select("select * from " +
            "(SELECT ROW_NUMBER() OVER(ORDER BY code) AS idx, " +
            "rtrim(code) as drugListId, " +
            "rtrim(national_code) as hospListId " +
            "from yp_zd_dict with(nolock) where national_status=1) t where t.idx>#{page}*#{size} " +
            "and t.idx<=(#{page}+1)*#{size}")
    List<MiInsurHospDrugsRel> selectMiInsurHospDrugsRel(int page, int size);

    @Select("select * from " +
            "(SELECT ROW_NUMBER() OVER(ORDER BY code) AS idx, " +
            "rtrim(code) as itemsListId, " +
            "rtrim(national_code) as hospListId " +
            "from zd_charge_item with(nolock) where national_status=1) t where t.idx>#{page}*#{size} " +
            "and t.idx<=(#{page}+1)*#{size}")
    List<MiInsurHospItemsRel> selectMiInsurHospItemsRel(int page, int size);

    @Select("select * from " +
            "(SELECT ROW_NUMBER() OVER(ORDER BY a.setl_id) AS idx, " +
            "a.setl_id as settlId, " +
            "visitId=a.pat_no+'_'+cast(a.times as varchar)+'_'+cast(a.ledger_sn as varchar), " +
            "settlTime=convert(varchar(19), a.setl_time, 21), " +
            "visitTime=convert(varchar(19), a.begntime, 21), " +
            "a.medfee_sumamt as discountMoney, " +
            "flag=case when a.revoked=0 then 0 else 2 end, " +
            "diseaseName=b.dise_name, " +
            "icdCode=b.dise_code, " +
            "isSpecial=case when a.med_type='14' then 'Y' else 'N' end " +
            "from t_si_setlinfo a with(nolock), t_si_pat_info b with(nolock) " +
            "where a.setl_type=11 and a.revoked=0 and a.mdtrt_id=b.mdtrt_id) t where " +
            "t.idx>#{page}*#{size} and t.idx<=(#{page}+1)*#{size}")
    List<HisOutpSettlData> selectHisOutpSettlData(int page, int size);

    @Select("select " +
            "visitId=a.pat_no+'_'+cast(a.times as varchar)+'_'+cast(a.ledger_sn as varchar), " +
            "a.setl_id as settlId, " +
            "settlTime=convert(varchar(19), a.setl_time, 21), " +
            "visitTime=convert(varchar(19), a.begntime, 21), " +
            "a.medfee_sumamt as discountMoney, " +
            "flag=case when a.revoked=0 then 0 else 2 end, " +
            "diseaseName=b.dise_name, " +
            "icdCode=b.dise_code, " +
            "isSpecial=case when a.med_type='14' then 'Y' else 'N' end " +
            "from t_si_setlinfo a with(nolock), t_si_pat_info b with(nolock) " +
            "where a.setl_id=#{setlId} and a.mdtrt_id=b.mdtrt_id")
    List<HisOutpSettlData> selectHisOutpSettlDataByVisitId(String setlId);

    @Select("select " +
            "adviceDetailId=rtrim(b.patient_id)+'_'+cast(b.times as varchar)+'_'+" +
            "cast(b.receipt_no as varchar)+'_'+cast(b.item_no as varchar), " +
            "adviceId=rtrim(b.patient_id)+'_'+cast(b.times as varchar)+'_'+cast(b.receipt_no as varchar), " +
            "visitId=a.pat_no+'_'+cast(a.times as varchar)+'_'+cast(a.ledger_sn as varchar), " +
            "isnull(b.group_order,0) as adviceNo, " +
            "rtrim(b.charge_item_code) as itemsId, " +
            "itemsName=case when b.serial in ('01','99') then (select top 1 rtrim(d.name) from yp_zd_dict d " +
            "where d.code=b.charge_item_code) " +
            "else (select top 1 rtrim(d.name) from zd_charge_item d where d.code=b.charge_item_code) end, " +
            "dosForms=(select d.name from yp_zd_dosage d where d.code=(select e.dosage from yp_zd_dict e " +
            "where e.code=b.charge_item_code)) " +
            "from t_si_setlinfo a with(nolock), mz_charge_detail b with(nolock) " +
            "where a.setl_id=#{setlId} and a.pat_no=b.patient_id " +
            "and a.times=b.times and b.bill_item_code!='TC'")
    List<HisOutpAdviceDetail> selectHisOutpAdviceDetail(String setlId);

    @Select("select " +
            "chargeDetailId=a.pat_no+'_'+cast(a.times as varchar)+'_'+cast(b.receipt_no as varchar)+'_'+" +
            "cast(b.item_no as varchar)+'_'+rtrim(b.charge_item_code), " +
            "visitId=a.pat_no+'_'+cast(a.times as varchar)+'_'+cast(a.ledger_sn as varchar), " +
            "a.setl_id as settlId,rtrim(b.bill_item_code) as categoryId, " +
            "categoryName=(select d.name from mz_bill_item d where d.code=b.bill_item_code), " +
            "itemsCategory=case when b.bill_item_code in ('010','011','012') then 1 when " +
            "b.bill_item_code in ('013','018') then 5 else 2 end, " +
            "rtrim(b.charge_item_code) as itemsId, " +
            "itemsName=case when b.serial in ('01','99') then (select top 1 rtrim(d.name) from yp_zd_dict d " +
            "where d.code=b.charge_item_code) " +
            "else (select top 1 rtrim(d.name) from zd_charge_item d where d.code=b.charge_item_code) end, " +
            "stand=case when b.serial in ('01','99') then (select top 1 rtrim(d.specification) from yp_zd_dict d " +
            "where d.code=b.charge_item_code) " +
            "else (select top 1 rtrim(d.specification) from zd_charge_item d where d.code=b.charge_item_code) end, " +
            "unit=case when b.serial in ('01','99') then (select top 1 rtrim(d.unit) from yp_zd_dict d where " +
            "d.code=b.charge_item_code) " +
            "else (select top 1 rtrim(d.charge_unit) from zd_charge_item d where d.code=b.charge_item_code) end, " +
            "b.unit_price as unvln,b.quantity as num, " +
            "discountMoney=isnull(b.quantity,0)*isnull(b.unit_price,0)*isnull(b.drug_win,1), " +
            "occurTime=convert(varchar(19), b.charge_date, 21),rtrim(b.doctor_code) as prescrDoctorId, " +
            "prescrDoctor=(select rtrim(d.name) from a_employee_mi d where d.code=b.doctor_code), " +
            "rtrim(b.warn_dept) as prescrDeptId, " +
            "prescrDept=(select rtrim(d.name) from zd_unit_code d where d.code=b.warn_dept), " +
            "rtrim(b.exec_dept) as execDeptId, " +
            "execDept=(select rtrim(d.name) from zd_unit_code d where d.code=b.exec_dept), " +
            "visitDeptId=(select rtrim(d.visit_dept_code) from mz_visit_table d where d.patient_id=a.pat_no and d.times=a.times), " +
            "adviceDetailId=a.pat_no+'_'+cast(b.times as varchar)+'_'+cast(b.receipt_no as varchar)+'_'+cast(b.item_no as varchar), " +
            "flag=case when a.revoked=0 then 0 else 2 end " +
            "from t_si_setlinfo a with(nolock), mz_charge_detail b with(nolock) " +
            "where a.setl_id=#{setlId} and a.pat_no=b.patient_id " +
            "and a.times=b.times and b.bill_item_code!='TC' and b.charge_item_code!='BILL99'")
    List<HisOutpCharge> selectHisOutpCharge(String setlId);

    @Select("select visitId=rtrim(inpatient_no)+'_'+cast(admiss_times as varchar)+'_'+cast(times_billed as varchar) " +
            "from zy_actpatient a where a.med_type!='42'")
    List<VisitId> selectAllHisHsptzdPatientBase();

    @Select("select visitId=rtrim(inpatient_no)+'_'+cast(admiss_times as varchar)+'_'+cast(times_billed as varchar) " +
            "from zy_inactpatient where med_type!='42' and dis_date>=#{start} and dis_date<=#{end} ")
    List<VisitId> selectHisHsptzdPatientBaseByDateRange(String start, String end);

    @Select("select " +
            "visitId=rtrim(a.inpatient_no)+'_'+cast(a.admiss_times as varchar)+'_'+cast(a.times_billed as varchar), " +
            "rtrim(a.inpatient_no) as admNo,rtrim(b.name) as name,rtrim(b.sex) as sexId, " +
            "birthday=convert(varchar(10), b.birth_date, 21),rtrim(b.social_no) as idCard, " +
            "status='02',rtrim(a.small_dept) as visitDeptId,a.admiss_times as hsptzdCount, " +
            "hsptzdTime=convert(varchar(19), a.admiss_date, 21), " +
            "rtrim(isnull(a.refer_physician, a.admiss_physician)) as tbDoctorId," +
            "insurAreaId=(select d.insuplc_admdvs from t_si_pat_info d where d.pat_no=a.inpatient_no and d.times=a.admiss_times and d.ledger_sn=#{ledgerSn}), " +
            "insurType=(select d.insutype from t_si_pat_info d where d.pat_no=a.inpatient_no and d.times=a.admiss_times and d.ledger_sn=#{ledgerSn}), " +
            "ttmntTypeCode=(select d.med_type from t_si_pat_info d where d.pat_no=a.inpatient_no and d.times=a.admiss_times and d.ledger_sn=#{ledgerSn}), " +
            "disCode=(select d.icd_code from zy_in_diag_yb d where d.inpatient_no=a.inpatient_no and d.admiss_times=a.admiss_times and d.diag_no=1), " +
            "disName=(select d.icd_text from zy_in_diag_yb d where d.inpatient_no=a.inpatient_no and d.admiss_times=a.admiss_times and d.diag_no=1) " +
            "from zy_actpatient a, a_patient_mi b where a.inpatient_no=#{patNo} and a.admiss_times=#{times} and a.inpatient_no=b.inpatient_no ")
    List<HisHsptzdPatientBase> selectHisHsptzdPatientBaseInHosp(String patNo, int times, int ledgerSn);

    @Select("select " +
            "visitId=rtrim(a.inpatient_no)+'_'+cast(a.admiss_times as varchar)+'_'+cast(a.times_billed as varchar), " +
            "rtrim(a.inpatient_no) as admNo,rtrim(b.name) as name,rtrim(b.sex) as sexId, " +
            "birthday=convert(varchar(10), b.birth_date, 21),rtrim(b.social_no) as idCard, " +
            "status='11',rtrim(a.small_dept) as visitDeptId, " +
            "hsptzdTime=convert(varchar(19), a.admiss_date, 21), " +
            "rtrim(isnull(a.refer_physician,a.admiss_physician)) as tbDoctorId, " +
            "rtrim(a.zk_ward) as hospDischDeptId, " +
            "hospDischTime=convert(varchar(19),a.dis_date,21), " +
            "insurAreaId=(select d.insuplc_admdvs from t_si_pat_info d where d.pat_no=#{patNo} and d.times=#{times} and d.ledger_sn=#{sn}), " +
            "insurType=(select d.insutype from t_si_pat_info d where d.pat_no=#{patNo} and d.times=#{times} and d.ledger_sn=#{sn}), " +
            "ttmntTypeCode=(select d.med_type from t_si_pat_info d where d.pat_no=#{patNo} and d.times=#{times} and d.ledger_sn=#{sn})," +
            "a.admiss_times as hsptzdCount, " +
            "disCode=(select d.icd_code from zy_in_diag_yb d where d.inpatient_no=a.inpatient_no and d.admiss_times=a.admiss_times and d.diag_no=1), " +
            "disName=(select d.icd_text from zy_in_diag_yb d where d.inpatient_no=a.inpatient_no and d.admiss_times=a.admiss_times and d.diag_no=1) " +
            "from zy_inactpatient a, a_patient_mi b where a.inpatient_no=#{patNo} and a.admiss_times=#{times} and a.inpatient_no=b.inpatient_no ")
    List<HisHsptzdPatientBase> selectHisHsptzdPatientBaseOutHosp(String patNo, int times, int sn);

    @Select("select * from " +
            "(SELECT ROW_NUMBER() OVER(ORDER BY a.detail_sn) AS idx, " +
            "chargeDetailId=rtrim(a.inpatient_no)+'_'+cast(a.admiss_times as varchar)+'_'+cast(a.ledger_sn as varchar)+'_'+ " +
            "cast(a.detail_sn as varchar), " +
            "visitId=rtrim(a.inpatient_no)+'_'+cast(a.admiss_times as varchar)+'_'+cast(a.ledger_sn as varchar), " +
            "categoryId=case when a.serial in ('01','99') then " +
            "(select d.code from zy_bill_item d where d.charge_code=a.charge_code) " +
            "else (select d.bill_item_zy from zd_charge_item d where d.code=a.charge_code_mx) end, " +
            "categoryName=case when a.serial in ('01','99') then " +
            "(select d.name from zy_bill_item d where d.charge_code=a.charge_code) " +
            "else (select d.name from zy_bill_item d where d.code= " +
            "(select d.bill_item_zy from zd_charge_item d where d.code=a.charge_code_mx)) end, " +
            "itemsCategory=case when a.charge_code in ('BILL01','BILL02','BILL28') then 1 else " +
            "(case when (select d.bill_item_zy from zd_charge_item d where d.code=a.charge_code_mx)='027' " +
            "then 5 else 2 end) end, " +
            "rtrim(a.charge_code_mx) as itemsId, " +
            "itemsName=case when a.serial in ('01','99') then " +
            "(select top 1 rtrim(d.name) from yp_zd_dict d where d.code=a.charge_code_mx) " +
            "else (select d.name from zd_charge_item d where d.code=a.charge_code_mx) end, " +
            "stand=case when a.serial in ('01','99') then " +
            "(select top 1 rtrim(d.specification) from yp_zd_dict d where d.code=a.charge_code_mx) " +
            "else (select d.specification from zd_charge_item d where d.code=a.charge_code_mx) end, " +
            "unit=case when a.serial in ('01','99') then " +
            "(select top 1 rtrim(d.unit) from yp_zd_dict d where d.code=a.charge_code_mx) " +
            "else (select d.charge_unit from zd_charge_item d where d.code=a.charge_code_mx) end, " +
            "a.retprice as unvln,a.charge_amount as num,a.charge_fee as discountMoney, " +
            "occurTime=convert(varchar(19), a.charge_date, 21), " +
            "produceTime=convert(varchar(19), a.gen_time, 21), " +
            "prescrDoctorId=isnull(nullif(a.doctor_code,''),a.refer_physician), " +
            "prescrDoctor=(select rtrim(d.name) from a_employee_mi d where d.code= " +
            "isnull(nullif(a.doctor_code,''),a.refer_physician)), " +
            "rtrim(a.dept_code) as prescrDeptId, flag=0, " +
            "prescrDept=(select rtrim(d.name) from zd_unit_code d where d.code=a.dept_code), " +
            "rtrim(a.exec_unit) as execDeptId, " +
            "execDept=(select rtrim(d.name) from zd_unit_code d where d.code=a.exec_unit), " +
            "visitDeptId=rtrim(a.ward_code), " +
            "cast(cast(a.order_no as decimal) as varchar) as mdclAdviceId, " +
            "hospSend=case when trans_flag_yb=1 then 'Y' else 'N' end, " +
            "insurSettlId=(select d.setl_id from t_si_setlinfo d where d.pat_no=a.inpatient_no and d.times=a.admiss_times " +
            "and d.ledger_sn=a.ledger_sn and d.revoked=0) " +
            "from zy_detail_charge a with(nolock) where a.inpatient_no=#{patNo} and a.admiss_times=#{times} " +
            "and a.ledger_sn=#{ledgerSn} and a.trans_flag_yb!=2 and a.charge_status!='1') t " +
            "where t.idx>#{page}*#{size} and t.idx<=(#{page}+1)*#{size}")
    List<HisHsptzdCharge> selectHisHsptzdCharge(String patNo, int times, int ledgerSn, int page, int size);

    @Select("select visitId=b.pat_no+'_'+cast(b.times as varchar)+'_'+cast(b.ledger_sn as varchar), " +
            "rtrim(a.inpatient_no) as babyId,rtrim(a.name) as babyName " +
            "from a_patient_mi a, t_si_pat_info b where b.pat_no=#{patNo} and b.times=#{times} " +
            "and b.ledger_sn=#{ledgerSn} and a.inpatient_no like b.pat_no+'$%'")
    List<HisBabyInfo> selectHisBabyInfo(String patNo, int times, int ledgerSn);

    @Select("select count(1) from yz_act_order where inpatient_no=#{patNo} and admiss_times=#{times}")
    int selectInHospCount(String patNo, int times);

    @Select("select *," +
            "precrAdviceDept=(select rtrim(d.name) from zd_unit_code d where d.code=t.precrAdviceDeptId) " +
            "from (select visitId=#{visitId},rtrim(physician) as precrAdviceDoctorId, " +
            "cast(cast(act_order_no as decimal) as varchar) as mdclAdviceId, " +
            "mdclAdviceType=case when frequ_code='ALWAYS' then '00' else '10' end, " +
            "rtrim(order_code) as mdclAdviceCode,rtrim(modifier) as ceaseAdviceDoctorId, " +
            "mdclAdviceSource=case when order_code='06054' then '3' else " +
            "(case when serial in ('01','99') then 1 else 2 end) end, " +
            "cast(cast(parent_no as decimal) as varchar) as mdclAdviceNo, " +
            "rtrim(order_name) as mdclAdviceContent,dosForms='',rtrim(drug_specification) as stand, " +
            "rtrim(frequ_code) as hz,drug_weight as dosage,rtrim(signer) as checkDoctorId, " +
            "dosageUnit=(select d.name from yp_zd_unit d where d.code=drug_weight_unit), " +
            "entiyTime=convert(varchar(19), enter_time, 21),start_time as mdclAdviceStartTime, " +
            "precrAdviceDeptId=(select rtrim(d.dept_code) from a_employee_mi d where d.code=physician), " +
            "precrAdviceDoctor=(select rtrim(d.name) from a_employee_mi d where d.code=physician), " +
            "checkFlag=case when status_flag=1 then 0 else 1 end, " +
            "checkDoctor=(select rtrim(d.name) from a_employee_mi d where d.code=signer), " +
            "confirm_time as precrAdviceCheckTime,end_time as ceaseAdviceTime, " +
            "execStatus=case when status_flag in ('1','2') then 0 else 1 end, " +
            "ceaseAdviceDoctor=(select rtrim(d.name) from a_employee_mi d where d.code=modifier), " +
            "mdclAdviceStatus=case when end_time is null then 1 else 2 end " +
            "from ${table} where inpatient_no=#{patNo} and admiss_times=#{times} " +
            ") t")
    List<HisHsptzdMdclAdvice> selectHisHsptzdMdclAdvice(String patNo, int times, String table, String visitId);

    @Select("select visitId=pat_no+'_'+cast(times as varchar)+'_'+cast(ledger_sn as varchar), " +
            "mdtrt_id,psn_no,setl_id,insuplc_admdvs from t_si_setlinfo where revoked=0 and setl_type=21 " +
            "and setl_time>=#{start} and setl_time<=#{end} ")
    List<YibaoSetlinfo2304Brief> selectSetlBrief(String start, String end);

    @Select("select " +
            "mdtrt_id,psn_no,psn_type,cvlserv_flag,medfee_sumamt, " +
            "setlTime=convert(varchar(19), setl_time, 21),insutype, " +
            "fulamt_ownpay_amt,overlmt_selfpay,preselfpay_amt,inscp_scp_amt, " +
            "act_pay_dedc,hifp_pay,pool_prop_selfpay,cvlserv_pay,hifes_pay, " +
            "hifmi_pay,hifob_pay,maf_pay,oth_pay,fund_pay_sumamt,psn_part_amt, " +
            "acct_pay,psn_cash_pay,hosp_part_amt,balc,acct_mulaid_pay, " +
            "status=case when setl_list_id is null then 0 else 1 end, " +
            "visitId=pat_no+'_'+cast(times as varchar)+'_'+cast(ledger_sn as varchar), " +
            "setl_id,insuplc_admdvs,clr_way, " +
            "fixmedinsCode='H32132200561',fixmedinsName='沭阳铭和医院', " +
            "begndate=convert(varchar(19), begntime, 21), " +
            "enddate=convert(varchar(19), endtime, 21),clr_type " +
            "from t_si_setlinfo where revoked=0 and setl_id=#{setlId} ")
    List<YibaoSetlinfo2304> selectYibaoSetlinfo2304(String setlId);

    @Select("select " +
            "a.fund_pay_type,a.inscp_scp_amt,a.crt_payb_lmt_amt,a.fund_payamt,a.fund_pay_type_name, " +
            "a.setl_proc_info,b.setl_id, " +
            "visitId=b.pat_no+'_'+cast(b.times as varchar)+'_'+cast(b.ledger_sn as varchar) " +
            "from t_si_setldetail a, t_si_setlinfo b where b.setl_id=#{setlId} and " +
            "a.pat_no=b.pat_no and a.times=b.times and a.ledger_sn=b.ledger_sn")
    List<YibaoSetldetail2304> selectYibaoSetldetail2304(String setlId);

    @Select("select " +
            "a.mdtrt_id,a.setl_id,a.feedetl_sn,b.med_type,a.fee_ocur_time, " +
            "a.cnt,a.pric,a.det_item_fee_sumamt,a.chrgitm_lv,a.hilist_code, " +
            "a.hilist_name,a.list_type,a.med_list_codg,a.medins_list_codg, " +
            "a.medins_list_name,a.med_chrgitm_type,a.bilg_dept_codg, " +
            "a.bilg_dept_name,a.bilg_dr_codg,a.bilg_dr_name,a.lmt_used_flag, " +
            "a.hosp_prep_flag,a.hosp_appr_flag,a.tcmdrug_used_way,a.prodplac_type, " +
            "a.bas_medn_flag,a.hi_nego_drug_flag,a.chld_medc_flag,a.etip_flag, " +
            "a.etip_hosp_code,a.dscg_tkdrug_flag,a.list_sp_item_flag,a.matn_fee_flag, " +
            "a.drt_reim_flag,a.memo,a.opter_id,a.opter_name,a.opt_time, " +
            "visitId=b.pat_no+'_'+cast(b.times as varchar)+'_'+cast(b.ledger_sn as varchar) " +
            "from t_si_setl_fee_detl a, t_si_setlinfo b " +
            "where a.setl_id=#{setlId} and a.setl_id=b.setl_id ")
    List<Yibao5204> selectYibao5204(String setlId);

    @Select("select " +
            "dagnsCategory=1, " +
            "dagnsTypeId=case when diag_no=1 then 1 else 2 end, " +
            "dagnsWayId=1,icd_text as diseaseName, " +
            "icd_code from zy_in_diag_yb " +
            "where inpatient_no=#{patNo} and admiss_times=#{times} " +
            "order by diag_no")
    List<HisPatientDagns> selectHisPatientAdmDagns(String patNo, int times);

    @Select("select " +
            "dagnsCategory=case when si_diag_type in ('1','2') then 1 else 2 end, " +
            "dagnsTypeId=case when dis_diag_no=1 then 1 else 2 end, " +
            "dagnsWayId=2, " +
            "rtrim(dis_diag_comment) as diseaseName, " +
            "rtrim(dis_diag) as icdCode " +
            "from zy_dis_diag_yb " +
            "where inpatient_no=#{patNo} and admiss_times=#{times} " +
            "order by dis_diag_no")
    List<HisPatientDagns> selectHisPatientDisDangs(String patNo, int times);

    @Select("select no,code as oprtCode,name as oprtName, " +
            "oprtDate=convert(varchar(19), date, 21) " +
            "from t_case_frontsheet_surgery where bah=#{patNo} and times=#{times}")
    List<HisPatientOprtInfo> selectHisPatientOprtInfo(String patNo, int times);

    @Select("select visitId=bah+'_'+cast(admiss_times as varchar)+'_1' from t_case_frontsheet_main " +
            "where sign_date>=#{start} and sign_date<=#{end} ")
    List<VisitId> selectSignedVisitId(String start, String end);

    @Select("select " +
            "username='沭阳铭和医院', " +
            "ylfkfs=case when pay_method is null then '99' when pay_method in ('9','','-') then '99' " +
            "else '0' + pay_method end, " +
            "health_card_no as jkkh, " +
            "admiss_times as zycs, " +
            "bah,name as xm,sex as xb, " +
            "csrq=convert(varchar(10),birth_date,112), " +
            "age as nl, " +
            "gj=(select d.si_code from zd_country_code d where d.code=country), " +
            "age_days as bzyzsnl, " +
            "new_born_weight as xsecstz, " +
            "new_born_admiss_weight as xserytz, " +
            "birth_place_name as csd, " +
            "native_place_name as gg, " +
            "mz=(select d.code2 from zd_nation_code d where d.code=nation), " +
            "upper(social_no) as sfzh, " +
            "occupation as zy, " +
            "hy=(select d.si_code from zd_marital_status d where d.code=marriage), " +
            "live_place_combo as xzz, " +
            "phone as dh,addr_zip_code as yb1, " +
            "hk_place_name as hkdz,hk_zip_code as yb2, " +
            "unit_name+'/'+unit_place as gzdwjdz, " +
            "unit_phone as dwdh,unit_zip_code as yb3, " +
            "contact_name as lxrxm,contact_relation as lxrgx, " +
            "contact_addr_name as dz, " +
            "contact_phone as dh2, " +
            "zy_admiss_way as rytj, " +
            "rysj=convert(varchar(10),admiss_date,112), " +
            "rykb=(select d.si_caty from zd_unit_code d where d.code=admiss_dept_code), " +
            "admiss_ward as rybf, " +
            "zkkb=(select d.si_caty from zd_unit_code d where d.code=trans_dept_code), " +
            "cysj=convert(varchar(10),dismiss_date,112), " +
            "cykb=(select d.si_caty from zd_unit_code d where d.code=dismiss_dept_code), " +
            "dismiss_ward as cybf, " +
            "admiss_days as sjzyts, " +
            "clinic_diag_str as mzzd, " +
            "clinic_diag_code as jbbm, " +
            "hurt_reason_name as wbyy, " +
            "hurt_reason_code as h23, " +
            "pathologic_diag_str as blzd, " +
            "pathologic_diag_code as jbmm, " +
            "blh,allergy as ywgm, " +
            "autopsy as swhzsj,blood_type as xx, " +
            "rh=(select d.wjw_code from zd_hbsag d where d.code=rh), " +
            "dept_leader_name as kzr, " +
            "leader_doctor_name as zrys, " +
            "main_doctor_name as zzys, " +
            "admiss_doctor_name as zyys, " +
            "duty_nurse_name as zrhs, " +
            "internship_doctor_name as sxys, " +
            "coder_name as bmy, " +
            "bazl=(case when quality_control_level is null then 3 " +
            "when quality_control_level>=3 then 3 else quality_control_level end), " +
            "quality_control_doctor_name as zkys, " +
            "quality_control_nurse_name as zkhs, " +
            "zkrq=convert(varchar(10),quality_control_date,112), " +
            "lyfs=(select d.wjw_code from zd_zy_dismiss_way d where d.code=zy_dismiss_way), " +
            "dismiss_destination as yzzyYljg, " +
            "dismiss_destination as wsyYljg, " +
            "admiss_again_in_one_month as sfzzyjh, " +
            "admiss_again_purpose as md, " +
            "coma_days_before_admiss as ryqT, " +
            "coma_hours_before_admiss as ryqXs, " +
            "coma_minutes_before_admiss as ryqF, " +
            "coma_days_after_admiss as ryhT, " +
            "coma_hours_after_admiss as ryhXs, " +
            "coma_minutes_after_admiss as ryhF, " +
            "total_cost as zfy,self_pay as zfje, " +
            "general_medical_service_fee as ylfuf, " +
            "general_treatment_operating_fee as zlczf, " +
            "care_fee as hlf,else_fee as qtfy, " +
            "pathologic_diag_fee as blzdf, " +
            "experiment_diag_fee as syszdf, " +
            "ct_diag_fee as yxxzdf, " +
            "clinical_diag_fee as lczdxmf, " +
            "not_surgical_fee as fsszlxmf, " +
            "clinical_physical_fee as wlzlf, " +
            "surgical_fee as sszlf,anesthetize_fee as maf, " +
            "surgery_fee as ssf,recover_fee as kff, " +
            "tcm_treatment_fee as zyzlf, " +
            "west_medicine_fee as xyf, " +
            "chinese_patent_medicine_fee as zcyf, " +
            "chinese_herbal_medicine_fee as zcyf1, " +
            "blood_fee as xf,albumin_products_fee as bdblzpf, " +
            "globulin_products_fee as qdblzpf, " +
            "coagulation_factor_products_fee as nxyzlzpf, " +
            "cytokine_products_fee as xbyzlzpf, " +
            "cost_of_disposable_medical_materials_for_examination as hcyyclf, " +
            "cost_of_disposable_medical_materials_for_treatment as yyclf, " +
            "cost_of_disposable_medical_materials_for_operation as ycxyyclf, " +
            "other_fees as qtf,operDate=convert(varchar(19), sign_date, 21),operType=2 " +
            "from t_case_frontsheet_main where bah=#{patNo} and admiss_times=#{times}")
    List<YibaoN041> selectYibaoN041(String patNo, int times);

    @Select("select name as qtzd,code as jbdm,admiss_status as rybq from " +
            "t_case_frontsheet_disdiag where bah=#{patNo} and times=#{times} order by no")
    List<YibaoN041DiagItem> selectYibaoN041Diags(String patNo, int times);

    @Select("select " +
            "code as ssjczbm, " +
            "ssjczrq=convert(varchar(10),date,112), " +
            "ssjb=level, " +
            "name as ssjczmc,operator_name as sz, " +
            "assistant_one_name as yz, " +
            "assistant_two_name as ez, " +
            "qkdj=case when cut is null then 1 else cut+1 end, " +
            "qkyhlb=case when isnull(heal,0)=0 then 9 else heal end, " +
            "mzfs=(select d.yb_code from zd_anaesthesia d where d.code=anaesthesia), " +
            "anaesthesiaor_name as mzys " +
            "from t_case_frontsheet_surgery " +
            "where bah=#{patNo} and times=#{times} order by no")
    List<YibaoN041SurgeryItem> selectYibaoN041Surgeries(String patNo, int times);


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

    @Select("select visit_id = (select pat_no + '_' + cast(times as varchar) + '_' + cast(ledger_sn as varchar) " +
            "from t_si_setlinfo b where b.setl_type=21 and b.revoked=0 and a.setl_id=b.setl_id) from t_settlement_setlinfo a " +
            "where dcla_time >= #{startTime} and dcla_time <= #{endTime} ")
    List<VisitId> getSettlementDocumentVisitID(String startTime, String endTime);

    @Select("select '${visitId}' as visit_id,a.psn_no,a.setl_id, " +
            "open_date=(select convert(varchar(19), sign_date, 21) from t_case_frontsheet_main c " +
            "where a.medcasno = c.bah + '_' + cast(c.admiss_times as varchar)), " +
            "fixmedinsName=N'沭阳铭和医院',fixmedinsCode='H32132200561', " +
            "hi_setl_lv,hi_no,medcasno,dclaTime=convert(varchar(19),dcla_time,21), " +
            "a.psn_name,a.gend,brdy=convert(varchar(19),a.brdy,21),a.age,ntly,nwb_age,a.naty, " +
            "patn_cert_type,a.certno,prfs,curr_addr,emp_name,emp_addr,emp_tel,poscode,coner_name,patn_rlts, " +
            "coner_addr,coner_tel,hi_type,insuplc,sp_psn_type,nwb_adm_type,nwb_bir_wt,nwb_adm_wt, " +
            "(select top 1 name from zd_unit_code where si_caty=opsp_diag_caty) as opsp_diag_caty_name, " +
            "opsp_diag_caty as opsp_diag_caty_name, " +
            "trt_type,opspMdtrtDate=convert(varchar(19),opsp_mdtrt_date,21), " +
            "a.ipt_med_type,adm_way,admTime=convert(varchar(19),adm_time,21), " +
            "adm_caty,refldept_dept,resp_nurs_code, " +
            "dscgTime=convert(varchar(19),dscg_time,21), " +
            "dscg_caty,act_ipt_days,otp_wm_dise,wm_dise_code,otp_tcm_dise,tcm_dise_code, " +
            "vent_used_dura,pwcry_afadm_coma_dura,pwcry_bfadm_coma_dura,isnull(spga_nurscare_days,0) as spga_nurscare_days, " +
            "isnull(lv1_nurscare_days,0) as lv1_nurscare_days,isnull(scd_nurscare_days,0) as scd_nurscare_days," +
            "isnull(lv3_nurscare_days,0) as lv3_nurscare_days,dscg_way, " +
            "acp_medins_name,acp_optins_code,bill_code,bill_no,biz_sn, " +
            "days_rinp_flag31 as days_rinp_flag_31,days_rinp_pup31 as days_rinp_pup_31, " +
            "chfpdr_name,chfpdr_code, " +
            "(SELECT top 1 name FROM a_employee_mi WHERE yb_code=resp_nurs_code) as resp_nurs, " +
            "setlBegnDate=convert(varchar(19),setl_begn_date,21), " +
            "setlEndDate=convert(varchar(19),setl_end_date, 21), " +
            "(select sum(charge_fee) from zy_detail_charge where inpatient_no=b.pat_no " +
            "and admiss_times=b.times and b.ledger_sn=ledger_sn) as total_fee, " +
            "a.hi_paymtd,medins_fill_dept,medins_fill_psn, " +
            "oper_date=convert(varchar(19),dcla_time,21), " +
            "adm_caty as adm_dept_code, " +
            "adm_dept_name=(select top 1 name from zd_unit_code where si_caty=adm_caty), " +
            "traf_dept_flat='0',dscg_caty as dscg_dept_code, " +
            "dscg_dept_name=(select top 1 name from zd_unit_code where si_caty=dscg_caty), " +
            "b.insutype,psn_selfpay,psn_ownpay,a.acct_pay,psn_cashpay from t_settlement_setlinfo a, " +
            "t_si_setlinfo b WHERE a.setl_id = #{setlId} and a.setl_id = b.setl_id")
    List<YibaoSetlinfo4101a> basicPatientInfoOfStatement(String setlId, String visitId);

    @Select("select diag_name,diag_code,oprn_oprt_name,oprn_oprt_code, " +
            "case subscript when 1 then 1 else 0 end as maindiag_flag, " +
            "'${visitId}' as visit_id from t_settlement_opspdiseinfo " +
            "where setl_id = #{setlId}  and diag_code is not null")
    List<YibaoOpspdiseinfo4101a> getOpspdiseinfoBySetlId(String setlId, String visitId);


    @Select("select diag_type,diag_code,diag_name,adm_cond_type,maindiag_flag, " +
            "subscript as sort_no,'${visitId}' as visit_id " +
            "from t_settlement_diseinfo where setl_id = #{setlId} ")
    List<YibaoDiseinfo4101a> getDiseinfoBySetlId(String setlId, String visitId);

    @Select("select oprn_oprt_type,oprn_oprt_name,oprn_oprt_code,anst_way, " +
            "oper_dr_code,oper_dr_name,anst_dr_code,anst_dr_name," +
            "oprnOprtBegntime=convert(varchar(19), isnull(oprn_oprt_begntime,oprn_oprt_date), 21), " +
            "oprnOprtEndtime=convert(varchar(19),isnull(oprn_oprt_endtime,oprn_oprt_date) , 21), " +
            "anstBegntime=convert(varchar(19),isnull(anst_begntime,oprn_oprt_date) , 21), " +
            "anstEndtime=convert(varchar(19), isnull(anst_endtime,oprn_oprt_date) , 21), " +
            "'${visitId}' as visit_id, " +
            "subscript as sort_no," +
            "oprt_date=convert(varchar(19), oprn_oprt_begntime, 21) " +
            "from t_settlement_oprninfo where setl_id = #{setlId} ")
    List<YibaoOprninfo4101a> getOprninfoBySetlId(String setlId, String visitId);

    @Select("select scs_cutd_ward_type,scs_cutd_inpool_time,scs_cutd_exit_time, " +
            "'${visitId}' as visit_id,scs_cutd_sum_dura as total_hour, " +
            "subscript as sort_no from t_settlement_icuinfo where setl_id = #{setlId}")
    List<YibaoIcuinfo4101a> getIcoInfoBySetlId(String setlId, String visitId);

    @Select("select bld_cat,bld_amt,bld_unt,'${visitId}' as visit_id, " +
            "subscript as sort_no from t_settlement_bldinfo where setl_id = #{setlId} ")
    List<YibaoBldinfo4101a> getBldinfoBySetlId(String setlId, String visitId);

}
