package thyyxxk.webserver.dao.his.inpatient;

import org.apache.ibatis.annotations.*;
import thyyxxk.webserver.entity.dictionary.CodeName;
import thyyxxk.webserver.entity.inpatient.dismiss.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Mapper
public interface DismissDao {
    @Select("select top 1 ltrim(rtrim(name)) from a_employee_mi where code=#{code}")
    String getOperateName(@Param("code") String code);

    @Select("select isnull(max ( ledger_sn ), isnull(min ( ledger_sn ), 1)) from zy_ledger_file where " +
            "inpatient_no=#{patNo} and admiss_times=#{times}")
    Integer getLedgerSn(@Param("patNo") String patNo, @Param("times") Integer times);

    @Update("exec zy_receive_drug @in_no=#{patNo},@times=#{times},@v_no=#{patNo}, @is_infant=0")
    void acceptFeeStepOne(@Param("patNo") String patNo, @Param("times") Integer times);

    @Update("exec zy_receive_one @in_no=#{patNo},@times=#{times},@v_no=#{patNo}, @is_infant=0")
    int acceptFeeStepTwo(@Param("patNo") String patNo, @Param("times") Integer times);

    @Update("exec dbo.zy_cngl_fyjs_list_yz_1 @in_no=#{patNo}, @in_times=#{times}")
    int getOrderList(@Param("patNo") String patNo, @Param("times") Integer times);

    @Select("select ltrim(rtrim(str(act_order_no))) as actOrderNo,rtrim(order_name) as orderName," +
            "charge_date as chargeDate, rtrim(charge_code) as chargeCode,charge_fee=rtrim(charge_fee), " +
            "chargeName=rtrim(charge_name), frequCode=rtrim(frequ_code)," +
            "cxFlag=cx_flag,drugSpecification=rtrim(drug_specification)," +
            "start_time,end_time,chargeAmount=charge_amount,page_no,charge_status," +
            "supplyCode=(select rtrim(name) from view_zd_supply_type where code=supply_code)," +
            "physician=(select rtrim(name) from a_employee_mi where code=physician)," +
            "enterOper=(select rtrim(name) from a_employee_mi where code=enter_oper)," +
            "execUnit=(select rtrim(name) from zd_unit_code where code=exec_unit)," +
            "wardName=(select rtrim(name) from zd_unit_code where code=ward_code)," +
            "order_code, group_no from zy_list where charge_status in ('5','6','8')")
    List<ActOrderDetail> getActOrderDetail();

    @Update("exec zy_calc_detail_again_new @in_no=#{patNo}," +
            "@times=#{times},@sn=#{ledgerSn},@orig_type='1',@new_type='1'")
    int hasUnsettledStepOne(@Param("patNo") String patNo,
                            @Param("times") Integer times,
                            @Param("ledgerSn") int ledgerSn);

    @Select("select isnull(sum(depo_amount),0) from zy_deposit_file where depo_type='Y' " +
            "and inpatient_no=#{patNo} and admiss_times=#{times}")
    int hasUnsettledStepTwo(@Param("patNo") String patNo, @Param("times") Integer times);

    @Select("SELECT count(1) FROM zy_detail_charge WHERE inpatient_no=#{patNo} AND " +
            "admiss_times=#{times} and ledger_sn>=1 AND charge_fee <> 0 and charge_status ='1'")
    int hasNotAccounted(@Param("patNo") String patNo, @Param("times") Integer times);

    @Select("SELECT message = N'药单号:' + CAST(page_no as VARCHAR) + " +
            "                 N',药品名称:' + + (select top 1 name from yp_zd_dict where charge_code = code) + " +
            "                 N',医嘱号:' + CAST(CAST(act_order_no AS DECIMAL) AS VARCHAR(20)) " +
            " FROM yp_zy_patient WHERE inpatient_no=#{patNo} AND " +
            "admiss_times=#{times} AND amount <> 0 and acct_sign <> '2' ")
    List<String> hasUnreceivedDrugList(@Param("patNo") String patNo, @Param("times") Integer times);

    @Select("SELECT count(1) FROM yz_zy_patient_fee WHERE inpatient_no=#{patNo} " +
            "AND admiss_times=#{times} and charge_date<=#{actOrderDisDate} AND charge_fee <> 0 " +
            "and charge_status <> '0' and charge_status <> '2' ")
    int hasUnreceivedFees(@Param("patNo") String patNo,
                          @Param("times") Integer times,
                          @Param("actOrderDisDate") Date actOrderDisDate);

    @Select("select count(1) from yz_yp_zy_order with(nolock) where inpatient_no=#{patNo} " +
            "and admiss_times=#{times} and (amount>0 or (amount<0 and drug_class!='d')) " +
            "and status_flag='1' and page_no=0")
    int hasUnSubmitDrugList(@Param("patNo") String patNo, @Param("times") Integer times);

    @Select("select count(1) from yz_act_order_cy where inpatient_no=#{patNo} " +
            "and admiss_times=#{times} and status_flag='3'")
    int hasUnDeliverHerbal(@Param("patNo") String patNo, @Param("times") Integer times);

    @Select("select b.group_name,pageNo=cast(a.page_no as decimal),a.page_no_ty " +
            "from yz_yp_zy_order a with(nolock),yp_zd_group_name b with(nolock) where a.inpatient_no=#{patNo} " +
            "and a.admiss_times=#{times} and (a.amount>0 or (a.amount<0 and a.drug_class!='d')) and " +
            "(case when a.amount>0 then a.page_no else a.page_no_ty end>0) and a.group_no=b.group_no and a.status_flag='1'")
    List<IllegalFee> hasUntreatedDrugOrder(@Param("patNo") String patNo, @Param("times") Integer times);

    @Select("select page_no_ty,charge_date as yp_charge_date from yz_yp_zy_order where inpatient_no=#{patNo} " +
            "and admiss_times=#{times} and page_class='HT' and status_flag='1'")
    List<IllegalFee> selectPageNoTys(@Param("patNo") String patNo, @Param("times") Integer times);

    @Select("SELECT count(1) FROM yz_zy_patient_fee WHERE inpatient_no=#{patNo} " +
            "AND admiss_times=#{times} and charge_date<=#{actOrderDisDate} AND " +
            "charge_fee <> 0 and charge_status='3'")
    int hasUnconfirmedMedicalTech(@Param("patNo") String patNo,
                                  @Param("times") Integer times,
                                  @Param("actOrderDisDate") Date actOrderDisDate);

    @Select("select * from (select sum(a.charge_fee) as code,a.charge_code_mx as chargeCode,b.name from zy_detail_charge a, " +
            "zd_charge_item b where a.inpatient_no=#{patNo} and a.admiss_times=#{times} and " +
            "a.charge_code_mx=b.code group by a.charge_code_mx,b.name union select " +
            "sum(a.charge_fee) as code,a.charge_code_mx as chargeCode,b.name from zy_detail_charge a,yp_zd_dict b " +
            "where a.inpatient_no=#{patNo} and a.admiss_times=#{times} and a.charge_code_mx=b.code " +
            "group by a.charge_code_mx,b.name ) as x where x.code<0")
    List<IllegalFee> feeOrderNegative(@Param("patNo") String patNo, @Param("times") Integer times);

    @Delete("delete zy_tmp_settle_select_detail where inpatient_no=#{patNo} and admiss_times=#{times}")
    void deleteTemporaryTable(@Param("patNo") String patNo, @Param("times") Integer times);

    @Select("select dateadd(second,1,account_date) from zy_ledger_file where inpatient_no=#{patNo} and " +
            "admiss_times=#{times} and ledger_sn=(#{ledgerSn}-1)")
    Date selectLastLedgerAccountDate(@Param("patNo") String patNo, @Param("times") int times, @Param("ledgerSn") int ledgerSn);

    @Select("select admiss_date from ${table} where inpatient_no=#{patNo} and admiss_times=#{times}")
    Date selectAdmissDate(@Param("patNo") String patNo, @Param("times") Integer times, @Param("table") String table);

    @Select("SELECT settle_type FROM zy_ledger_file WHERE inpatient_no=#{patNo} " +
            "AND admiss_times=#{times} AND ledger_sn=#{ledgerSn}")
    int hasSettled(@Param("patNo") String patNo,
                   @Param("times") Integer times, @Param("ledgerSn") int ledgerSn);

    @Select("select count(1) from zy_tmp_settle_select_detail where inpatient_no=#{patNo} " +
            "and admiss_times=#{times} and ledger_sn=#{ledgerSn}")
    int hasUncheckedFee(@Param("patNo") String patNo,
                        @Param("times") Integer times, @Param("ledgerSn") int ledgerSn);

    @Select("select count(1) from yz_act_order where inpatient_no=#{patNo} and admiss_times=#{times} " +
            "and status_flag > '2' and isnull(group_no,'00' )='00' and order_code in " +
            "(select order_code from yz_zd_order_item_confirm where item_name in (N'出院',N'死亡'))")
    Integer countDisActOrders(@Param("patNo") String patNo, @Param("times") Integer times);

    @Select("select count(1) from yz_act_order where inpatient_no=#{patNo} and admiss_times=#{times} " +
            "and status_flag > '2' and isnull(group_no,'00' )='00' and order_code in " +
            "(select order_code from yz_zd_order_item_confirm where item_name=N'死亡') ")
    Integer selectDeathOrderCount(@Param("patNo") String patNo, @Param("times") Integer times);

    @Select("select count(1) from yz_inact_order where inpatient_no=#{patNo} and admiss_times=#{times} " +
            "and status_flag > '2' and isnull(group_no,'00' )='00' and order_code in " +
            "(select order_code from yz_zd_order_item_confirm where item_name in (N'出院',N'死亡'))")
    Integer countDisActOrders2(@Param("patNo") String patNo, @Param("times") Integer times);

    @Select("select (select isnull(sum(charge_fee),0) from zy_detail_charge where inpatient_no=#{patNo} " +
            "and admiss_times=#{times} and ledger_sn=#{ledgerSn} and isnull(charge_status, '1') <> '1') - " +
            "(select isnull(total_charge,0) from zy_ledger_file " +
            "where inpatient_no=#{patNo} and admiss_times=#{times} and ledger_sn=#{ledgerSn})")
    BigDecimal getFeeOffset(@Param("patNo") String patNo,
                            @Param("times") Integer times,
                            @Param("ledgerSn") int ledgerSn);

    @Select("select isnull(sum(charge_fee),0) from zy_detail_charge where inpatient_no=#{patNo} and " +
            "admiss_times=#{times} and ledger_sn=#{ledgerSn} and isnull(charge_status, '1')!='1' and " +
            "isnull(trans_flag_yb,0)!=2 and charge_date ${compare} convert(varchar(19),#{timeLimit},21)")
    BigDecimal selectOverTimeLimitFee(@Param("patNo") String patNo,
                                      @Param("times") Integer times,
                                      @Param("ledgerSn") int ledgerSn,
                                      @Param("compare") String compare,
                                      @Param("timeLimit") Date timeLimit);

    @Delete("delete zy_ledger_file_yb where inpatient_no=#{patNo} and admiss_times=#{times} and ledger_sn=#{ledgerSn}")
    void deleteZyLedgerFileYb(@Param("patNo") String patNo,
                              @Param("times") Integer times,
                              @Param("ledgerSn") int ledgerSn);

    @Insert("insert into zy_ledger_file_yb (inpatient_no,admiss_times,ledger_sn,total_cost,tczf,grzhzf,dbzf,xjzf," +
            "full_self_pay,part_self_pay,hospital_pay) values (#{inpatientNo},#{admissTimes},#{ledgerSn},#{totalCost}," +
            "#{tczf},#{grzhzf},#{dbzf},#{xjzf},#{fullSelfPay},#{partSelfPay},#{hospitalPay})")
    int insertZyLedgerFileYb(MedinsSettleFee settleFee);

    @Update("update zy_ledger_file set charge_yb=1 where inpatient_no=#{patNo} and admiss_times=#{times} and ledger_sn=#{ledgerSn}")
    int beforeWriteReceiptTable(@Param("patNo") String patNo,
                                @Param("times") Integer times,
                                @Param("ledgerSn") int ledgerSn);

    @Select("select count(1) from zy_receipt where inpatient_no=#{patNo} and admiss_times=#{times} and ledger_sn=#{ledgerSn}")
    int selectReceiptCount(@Param("patNo") String patNo,
                           @Param("times") int times,
                           @Param("ledgerSn") int ledgerSn);

    @Insert("insert into zy_receipt (inpatient_no,admiss_times,ledger_sn,receipt_sn,receipt_no," +
            "account_date,date_1,date_2,ward,dept,print_date,responce_unit,op_id_code,total_charge," +
            "charge1,charge2,charge3,charge4,charge5,charge6,charge7,charge8,charge9,charge10,charge11," +
            "charge12,charge13,charge14,charge15,charge16,charge17,charge18,charge19,charge20,charge21," +
            "charge22,charge23,charge24,charge25,charge26,charge27,charge28,charge29,charge30) " +
            "values (#{patNo},#{times},#{ledgerSn},#{receiptSn},'00000000',#{dismissDate}," +
            "#{admissDate},#{dismissDate},#{wardCode},#{deptCode},#{printDate},#{responceUnit},#{codeRs}," +
            "#{map.total},#{map.32080209},#{map.32080210}, #{map.32080201},#{map.32080203}, " +
            "#{map.32080206},#{map.32080203},#{map.32080205}, #{map.32080203},#{map.32080203}, " +
            "#{map.32080204},#{map.32080203},#{map.32080206}, #{map.32080204},#{map.32080205}, " +
            "#{map.32080205},#{map.32080205},#{map.32080205}, #{map.32080203},#{map.32080207}, " +
            "#{map.32080299},#{map.32080205},#{map.32080205}, #{map.32080208},#{map.32080208}, " +
            "#{map.32080205},#{map.32080299},#{map.32080208}, #{map.32080212}, " +
            "'0.00','0.00')")
    int writeReceiptTable(@Param("patNo") String patNo, @Param("times") Integer times,
                          @Param("ledgerSn") int ledgerSn, @Param("receiptSn") int receiptSn,
                          @Param("admissDate") Date admissDate, @Param("dismissDate") Date dismissDate,
                          @Param("wardCode") String wardCode, @Param("deptCode") String deptCode,
                          @Param("printDate") Date printDate, @Param("responceUnit") String responceUnit,
                          @Param("codeRs") String codeRs, @Param("map") Map<String, String> map);

    @Select("select max(infant_flag) FROM zy_detail_charge WHERE inpatient_no=#{patNo} and admiss_times=#{times}")
    int hasInfant(@Param("patNo") String patNo, @Param("times") Integer times);

    @Update("update zy_ledger_file set account_date=#{zjdzDatetime},settle_type='1',balance=0,dept_code=#{deptCode}," +
            "last_balance=total_charge,settle=deposit-total_charge,ward_code=#{wardCode},op_id_code=#{staffId}," +
            "charge_yb=0,fund_pay_sumamt=0,acct_pay=0 from zy_ledger_file where " +
            "inpatient_no=#{inpatientNo} and admiss_times=#{admissTimes} and ledger_sn=#{ledgerSn}")
    int updateZifeiCostStatus(MedinsSettleFee settleFee);

    @Update("update zy_ledger_file set account_date=#{zjdzDatetime},settle_type='1',balance=0,last_balance=#{xjzf}, " +
            "settle=(deposit-#{xjzf}),ward_code=#{wardCode},dept_code=#{deptCode},op_id_code=#{staffId},charge_yb=0," +
            "fund_pay_sumamt=#{tczf},acct_pay=#{grzhzf},hosp_part_amt=#{hospitalPay} where inpatient_no=#{inpatientNo} " +
            "and admiss_times=#{admissTimes} and ledger_sn=#{ledgerSn}")
    int updateCostStatusWithoutInfant(MedinsSettleFee settleFee);

    @Update("update zy_ledger_file set account_date=#{zjdzDatetime},settle_type='1',balance=0,charge_yb=0, " +
            "last_balance=(total_charge-#{tczf}-#{grzhzf}),settle=(deposit-total_charge+#{tczf}+#{grzhzf}), " +
            "ward_code=#{wardCode},dept_code=#{deptCode},op_id_code=#{staffId},fund_pay_sumamt=#{tczf}, " +
            "acct_pay=#{grzhzf},hosp_part_amt=#{hospitalPay} " +
            "where inpatient_no=#{inpatientNo} and admiss_times=#{admissTimes} and ledger_sn=#{ledgerSn}")
    int updateCostStatusWithInfant(MedinsSettleFee settleFee);

    @Update("update zy_detail_charge set charge_status=3 where charge_status=2 and " +
            "inpatient_no=#{patNo} AND admiss_times=#{times} AND ledger_sn=#{ledgerSn}")
    void updateZyDetailCharge(@Param("patNo") String patNo,
                             @Param("times") Integer times, @Param("ledgerSn") int ledgerSn);

    @Select("select start_time from yz_act_order where inpatient_no=#{patNo} and admiss_times=#{times} " +
            "and status_flag > '1' and isnull(group_no, '00')='00' and order_code in " +
            "(select order_code from yz_zd_order_item_confirm where item_name in (N'出院',N'死亡'))")
    Date selectActOrderDisDate(@Param("patNo") String patNo, @Param("times") Integer times);

    @Select("select start_time from yz_inact_order where inpatient_no=#{patNo} and admiss_times=#{times} " +
            "and status_flag > '1' and isnull(group_no, '00')='00' and order_code in " +
            "(select order_code from yz_zd_order_item_confirm where item_name in (N'出院',N'死亡'))")
    Date selectActOrderDisDate2(@Param("patNo") String patNo, @Param("times") Integer times);

    @Update("update ${table} SET dis_date=#{disDate},dis_ward=ward,dis_dept=dept,job_nurse=#{codeRs}," +
            "bed_status='5' WHERE inpatient_no=#{patNo} AND admiss_times=#{times}")
    int updateZyActpatient(@Param("patNo") String patNo, @Param("times") Integer times,
                           @Param("disDate") Date disDate, @Param("codeRs") String codeRs, @Param("table") String table);

    @Delete("DELETE FROM zy_inactpatient WHERE inpatient_no=#{patNo} AND admiss_times=#{times}")
    void deleteZyInactpatient(@Param("patNo") String patNo, @Param("times") Integer times);

    @Insert("INSERT INTO zy_inactpatient SELECT * FROM zy_actpatient WHERE inpatient_no=#{patNo} AND admiss_times=#{times}")
    int insertZyInactpatient(@Param("patNo") String patNo, @Param("times") Integer times);

    @Update("update ${table} SET dis_date=#{disDate},dis_ward=ward,dis_dept=dept WHERE inpatient_no >=#{patNo1} " +
            "and inpatient_no <= #{patNo6} and admiss_times=#{times}")
    void updateZyActpatientAgain(@Param("patNo1") String patNo1,
                                 @Param("patNo6") String patNo6,
                                 @Param("times") Integer times,
                                 @Param("disDate") Date disDate, @Param("table") String table);

    @Insert("INSERT INTO zy_inactpatient SELECT * FROM zy_actpatient WHERE inpatient_no >=#{patNo1} " +
            "and inpatient_no <= #{patNo6} and admiss_times=#{times}")
    void insertZyInactpatientAgain(@Param("patNo1") String patNo1,
                                   @Param("patNo6") String patNo6,
                                   @Param("times") Integer times);

    @Update("update zy_adt SET dis_date=#{disDate},orig_ward=#{wardCode},orig_dept=#{deptCode},orig_bed=#{bedNo} " +
            "WHERE inpatient_no=#{patNo} AND admiss_times=#{times} AND trans_times=0")
    int updateZyAdt(@Param("patNo") String patNo, @Param("times") Integer times,
                    @Param("wardCode") String wardCode, @Param("deptCode") String deptCode,
                    @Param("bedNo") String bedNo, @Param("disDate") Date disDate);

    @Update("update zy_bed_mi SET bed_status='1',admiss_times=NULL,inpatient_no=NULL WHERE " +
            "inpatient_no=#{patNo} AND admiss_times=#{times}")
    void updateZyBedMi(@Param("patNo") String patNo, @Param("times") Integer times);

    @Delete("DELETE FROM zy_actpatient WHERE inpatient_no=#{patNo}")
    void deleteZyActpatient(@Param("patNo") String patNo);

    @Delete("DELETE FROM zy_actpatient WHERE inpatient_no >= #{patNo1} and inpatient_no <= #{patNo6} " +
            " and admiss_times=#{times}")
    void deleteZyActpatientAgain(@Param("patNo1") String patNo1, @Param("patNo6") String patNo6,
                                 @Param("times") Integer times);

    @Insert("insert into zy_work_log (inpatient_no,admiss_times,ledger_sn,op_id,op_date,log_type," +
            "dept_code,ward_code,name) values (#{patNo},#{times},#{ledgerSn},#{codeRs}," +
            "getDate(),#{logType},#{deptCode},#{wardCode},#{userName})")
    int insertNewZyWorkLog(@Param("patNo") String patNo, @Param("times") Integer times,
                           @Param("ledgerSn") int ledgerSn, @Param("wardCode") String wardCode,
                           @Param("logType") String logType, @Param("deptCode") String deptCode,
                           @Param("codeRs") String codeRs, @Param("userName") String userName);

    @Update("update ${table} set times_billed=(times_billed+1) where " +
            "inpatient_no=#{patNo} and admiss_times=#{times}")
    void updateTimesBilledByIncreaseOne(@Param("patNo") String patNo, @Param("times") Integer times, @Param("table") String table);

    @Insert("insert into zy_ledger_file (inpatient_no, admiss_times, ledger_sn, op_id_code, deposit, balance, " +
            "total_charge, charge1, charge2, charge3, charge4, charge5, charge6, charge7, charge8, charge9, " +
            "charge10, charge11, charge12, charge13, charge14, charge15, charge16, charge17, charge18, " +
            "charge19, charge20, last_balance, settle, settle_type) VALUES " +
            "(#{patNo}, #{times}, #{ledgerSn}, NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, " +
            "0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '0')")
    void insertNewLedgerFile(@Param("patNo") String patNo,
                             @Param("times") Integer times,
                             @Param("ledgerSn") int ledgerSn);

    @Update("update zy_ledger_file set total_charge=" +
            "(select isnull(sum(charge_fee),0) from zy_detail_charge where inpatient_no=#{patNo} and " +
            "admiss_times=#{times} and ledger_sn=#{ledgerSn}) where inpatient_no=#{patNo} " +
            "and admiss_times=#{times} and ledger_sn=#{ledgerSn}")
    void updateZyLedgerFileTotalCharge(@Param("patNo") String patNo,
                                       @Param("times") Integer times,
                                       @Param("ledgerSn") int ledgerSn);

    @Select("select top 1 inpatient_no, admiss_times, depo_times, depo_date, op_id_code, depo_amount, depo_type, " +
            "status, ledger_sn, window_no, deleted from zy_deposit_file where inpatient_no=#{patNo} " +
            "and admiss_times=#{times} order by depo_times desc")
    DepositFile selectLatestDepositFile(@Param("patNo") String patNo,
                                        @Param("times") Integer times);

    @Select("select settle from zy_ledger_file where inpatient_no=#{patNo} and admiss_times=#{times} and " +
            "ledger_sn=#{ledgerSn}")
    String selectLastLedgerSettle(@Param("patNo") String patNo,
                                  @Param("times") Integer times,
                                  @Param("ledgerSn") Integer ledgerSn);

    @Insert("insert into zy_deposit_file (inpatient_no, admiss_times, depo_times, depo_date, op_id_code, " +
            "depo_amount, depo_type, status, ledger_sn, window_no, receipt_no, deleted) " +
            "values (#{inpatientNo},#{admissTimes},#{depoTimes},#{depoDate},#{opIdCode},#{depoAmount}, " +
            "#{depoType},#{status},#{ledgerSn},#{windowNo},#{receiptNo},#{deleted})")
    void insertNewDeposit(DepositFile depositFile);

    @Delete("delete from zy_deposit_file where inpatient_no=#{patNo} and admiss_times=#{times} and depo_type='B' ")
    void deleteZjdzSettleDeposit(@Param("patNo") String patNo,
                                 @Param("times") Integer times);

    @Update("update zy_detail_charge set ledger_sn=#{ledgerSn} where inpatient_no=#{patNo} " +
            "and admiss_times=#{times} and ledger_sn=#{lastSn} and charge_date>#{zjdzDate}")
    void updateFeesLedgerSn(@Param("patNo") String patNo,
                            @Param("times") Integer times,
                            @Param("ledgerSn") int ledgerSn,
                            @Param("lastSn") int lastSn,
                            @Param("zjdzDate") Date zjdzDate);

    @Update("update zy_actpatient set yb_register_date=#{date} " +
            "where inpatient_no=#{patNo} and admiss_times=#{times}")
    void clearMedinsInfo(@Param("patNo") String patNo, @Param("times") Integer times, @Param("date") Date date);

    @Select("select rtrim(hic_no_new) from a_patient_mi where inpatient_no=#{zyh}")
    String getHicNoNew(@Param("zyh") String zyh);

    @Update("update a_patient_mi set cpy=#{hicNew} where inpatient_no=#{zyh}")
    void updateCpy(@Param("zyh") String zyh, @Param("hicNew") String hicNew);

    @Update("update a_patient_mi set hic_no=#{hicNew} where inpatient_no=#{zyh}")
    void updateHic(@Param("zyh") String zyh, @Param("hicNew") String hicNew);

    @Select("select mdtrt_id,med_type from t_si_pat_info where pat_no=#{patNo} and times=#{times} and ledger_sn=#{ledgerSn}")
    BriefMdtrtInfo selectMdtrtId(@Param("patNo") String patNo,
                                 @Param("times") Integer times,
                                 @Param("ledgerSn") int ledgerSn);

    @Select("select count(1) from zy_dis_diag_yb where inpatient_no=#{patNo} and admiss_times=#{times}")
    int hasDismissDiag(@Param("patNo") String patNo,
                       @Param("times") Integer times);

    @Update("update zy_actpatient set total_charge=#{map.total}, " +
            "charge1=#{map.32080209},charge2=#{map.32080210}, charge3=#{map.32080201},charge4=#{map.32080203}, " +
            "charge5=#{map.32080206},charge6=#{map.32080203},charge7=#{map.32080205}, charge8=#{map.32080203},charge9=#{map.32080203}, " +
            "charge10=#{map.32080204},charge11=#{map.32080203},charge12=#{map.32080206}, charge13=#{map.32080204},charge14=#{map.32080205}, " +
            "charge15=#{map.32080205},charge16=#{map.32080205},charge17=#{map.32080205}, charge18=#{map.32080203},charge19=#{map.32080207}, " +
            "charge20=#{map.32080299},charge21=#{map.32080205},charge22=#{map.32080205}, charge23=#{map.32080208},charge24=#{map.32080208}, " +
            "charge25=#{map.32080205},charge26=#{map.32080299},charge27=#{map.32080208}, charge28=#{map.32080212},charge29=0,charge30=0 " +
            "where inpatient_no>=#{patNo1} and inpatient_no<=#{patNo6} and " +
            "admiss_times=#{times} ")
    void updateInfantfee(@Param("patNo1") String patNo1,
                         @Param("patNo6") String patNo6,
                         @Param("times") int times,
                         @Param("map") Map<String, String> map);

    @Select("select b.bill_item_zy as bill_code, ledger_fee=sum(a.charge_fee), " +
            "infant_fee=sum(case when isnull(infant_flag,0)=1 then charge_fee else 0 end), " +
            "adult_fee=sum(case when isnull(infant_flag,0)!=1 then charge_fee else 0 end) " +
            "from zy_detail_charge a, zd_charge_item b " +
            "where a.inpatient_no=#{patNo} and a.admiss_times=#{times} and " +
            "a.ledger_sn=#{ledgerSn} and a.charge_status!='1' and " +
            "a.charge_date>=#{begntime} and a.charge_date<=#{endtime} and " +
            "a.charge_code=b.code group by b.bill_item_zy, a.ledger_sn")
    List<ReceiptFee> selectLedgerFees(@Param("patNo") String patNo,
                                      @Param("times") int times,
                                      @Param("ledgerSn") int ledgerSn,
                                      @Param("begntime") Date begntime,
                                      @Param("endtime") Date endtime);

    @Select("select code from zy_bill_item")
    List<String> selectBillCodes();

    @Select("select responce_type from zy_actpatient where inpatient_no=#{patNo} ")
    String selectResponceType(@Param("patNo") String patNo);

    @Select("select cast(isnull(sum(depo_amount),0) as varchar(16)) from zy_deposit_file where " +
            "inpatient_no=#{patNo} and admiss_times=#{times} and ledger_sn=#{ledgerSn} and " +
            "depo_type!='B' and status in (1,2) ")
    String selectDepositSumamt(@Param("patNo") String patNo,
                               @Param("times") int times,
                               @Param("ledgerSn") int ledgerSn);

    @Update("update zy_ledger_file set deposit=#{deposit}, balance=#{balance}, responce_type=#{restype}, " +
            "total_charge=#{map.total},charge1=#{map.32080209},charge2=#{map.32080210}, charge3=#{map.32080201},charge4=#{map.32080203}, " +
            "charge5=#{map.32080206},charge6=#{map.32080203},charge7=#{map.32080205}, charge8=#{map.32080203},charge9=#{map.32080203}, " +
            "charge10=#{map.32080204},charge11=#{map.32080203},charge12=#{map.32080206}, charge13=#{map.32080204},charge14=#{map.32080205}, " +
            "charge15=#{map.32080205},charge16=#{map.32080205},charge17=#{map.32080205}, charge18=#{map.32080203},charge19=#{map.32080207}, " +
            "charge20=#{map.32080299},charge21=#{map.32080205},charge22=#{map.32080205}, charge23=#{map.32080208},charge24=#{map.32080208}, " +
            "charge25=#{map.32080205},charge26=#{map.32080299},charge27=#{map.32080208}, charge28=#{map.32080212},charge29=0,charge30=0 " +
            "where inpatient_no=#{patNo} and admiss_times=#{times} and ledger_sn=#{ledgerSn}")
    void updateZyLedgerFileCharges(@Param("patNo") String patNo,
                                   @Param("times") int times,
                                   @Param("ledgerSn") int ledgerSn,
                                   @Param("deposit") String deposit,
                                   @Param("balance") String balance,
                                   @Param("restype") String restype,
                                   @Param("map") Map<String, String> map);

    @Update("update zy_actpatient set balance=#{balance}, total_charge=#{totalCost}, " +
            "charge1=#{map.32080209},charge2=#{map.32080210}, charge3=#{map.32080201},charge4=#{map.32080203}, " +
            "charge5=#{map.32080206},charge6=#{map.32080203},charge7=#{map.32080205}, charge8=#{map.32080203},charge9=#{map.32080203}, " +
            "charge10=#{map.32080204},charge11=#{map.32080203},charge12=#{map.32080206}, charge13=#{map.32080204},charge14=#{map.32080205}, " +
            "charge15=#{map.32080205},charge16=#{map.32080205},charge17=#{map.32080205}, charge18=#{map.32080203},charge19=#{map.32080207}, " +
            "charge20=#{map.32080299},charge21=#{map.32080205},charge22=#{map.32080205}, charge23=#{map.32080208},charge24=#{map.32080208}, " +
            "charge25=#{map.32080205},charge26=#{map.32080299},charge27=#{map.32080208}, charge28=#{map.32080212},charge29=0,charge30=0 " +
            " where inpatient_no=#{patNo} ")
    void updateZyActpatientCharges(@Param("patNo") String patNo,
                                   @Param("totalCost") String totalCost,
                                   @Param("balance") String balance,
                                   @Param("map") Map<String, String> map);

    @Select("select isnull(sum(charge_fee),0) from zy_detail_charge where inpatient_no=#{patNo} " +
            "and admiss_times=#{times} and ledger_sn>0 and isnull(charge_status,'1')!='1' ")
    String selectTotalCharge(@Param("patNo") String patNo,
                             @Param("times") int times);
}
