package thyyxxk.webserver.dao.his.inpatient.charge;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import thyyxxk.webserver.entity.executeItem.ZyLedgerFile;
import thyyxxk.webserver.entity.inpatient.ZyActpatient;
import thyyxxk.webserver.entity.inpatient.charge.PatientParam;
import thyyxxk.webserver.entity.inpatient.charge.ZyDepositFile;
import thyyxxk.webserver.entity.inpatient.charge.ZyReceipt;

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

/**
 * @Description:
 * @Author:lihong
 * @Date: 2024/9/9
 */
@Mapper
public interface CashierProcessDao {

    @Select("<script>" +
            "                       SELECT   " +
            "                              zy_actpatient.inpatient_no, " +
            "                              zy_actpatient.admiss_times, " +
            "                              zy_actpatient.name, " +
            "                              zy_actpatient.bed_no, " +
            "                              zy_actpatient.ward, " +
            "                              zy_actpatient.responce_type, " +
            "                              zy_actpatient.balance, " +
            "                              zy_actpatient.dept, " +
            "                              zy_actpatient.total_charge, " +
            "                              zy_actpatient.charge_type, " +
            "                              zy_actpatient.times_billed, " +
            "                              zy_actpatient.admiss_diag_str, " +
            "                              zy_actpatient.admiss_diag, " +
            "                              zy_actpatient.admiss_date, " +
            "                              zy_actpatient.sex, " +
            "                              zy_serial_no, " +
            "                              mz_no, " +
            "                              code=inpatient_no, " +
            "                              intern_code, " +
            "                              ward_name=b.name, " +
            "                              responce_type_name=c.name, " +
            "                              charge_name=d.name, " +
            "                              dis_date " +
            "                       FROM ${tableName} zy_actpatient, " +
            "                            zd_unit_code b (nolock), " +
            "                            zy_zd_responce_type c (nolock), " +
            "                            zd_charge_type d " +
            "                       WHERE 1=1" +
            "<if test=\"inpatientNo != null and inpatientNo != '' \">" +
            " and (zy_actpatient.inpatient_no like #{inpatientNo} or zy_actpatient.name like #{inpatientNo} or zy_actpatient.mz_no like #{inpatientNo}  ) " +
            "</if>" +
            "<if test=\"wardCode != null and wardCode != '' \">" +
            " and zy_actpatient.ward = #{wardCode} " +
            "</if>" +
            "                         and zy_actpatient.ward = b.code " +
            "                         and zy_actpatient.responce_type = c.code " +
            "                         and zy_actpatient.charge_type = d.code " +
            "  order by  inpatient_no ,admiss_times desc" +
            "</script>")
    List<ZyActpatient> selectPatientInfoPage(PatientParam param);

    @Select("<script>" +
            "                       SELECT   count(*) " +
            "                       FROM ${tableName} zy_actpatient, " +
            "                            zd_unit_code b (nolock), " +
            "                            zy_zd_responce_type c (nolock), " +
            "                            zd_charge_type d " +
            "                       WHERE 1=1" +
            "<if test=\"inpatientNo != null and inpatientNo != '' \">" +
            " and (zy_actpatient.inpatient_no like #{inpatientNo} or zy_actpatient.name like #{inpatientNo} ) " +
            "</if>" +
            "<if test=\"wardCode != null and wardCode != '' \">" +
            " and zy_actpatient.ward = #{wardCode} " +
            "</if>" +
            "                         and zy_actpatient.ward = b.code " +
            "                         and zy_actpatient.responce_type = c.code " +
            "                         and zy_actpatient.charge_type = d.code " +
            "</script>")
    Integer selectPatientInfoCount(PatientParam param);
    @Select("<script>" +
            "select *," +
            "  op_id_name=(select name from a_employee_mi where code=op_id_code)," +
            "       ward_name = (select name from zd_unit_code where code = ward_code)," +
            "       dept_name = (select name from zd_unit_code where code = dept_code) ," +
            " responce_type_name=(select top 1 name from zy_zd_responce_type where code = responce_type ) from zy_ledger_file where " +
            " inpatient_no= #{inpatientNo} " +
            " and admiss_times= #{admissTimes} " +
            "<if test=\"ledgerSn != null \">" +
            " and ledger_sn = #{ledgerSn}"+
            "</if>" +
            " order by ledger_sn "+
            "</script>")
    List<ZyLedgerFile> selectZyLedgerFile(PatientParam param);
    @Select(" select top 1 rtrim(mz_no) mz_no from a_patient_mi where inpatient_no =#{inpatientNo} ")
    String selectMzNo(String inpatientNo);
    @Select(" select sum ( total_charge ) total_charge  from mz_receipt_serial where cheque_type ='Y' and patient_id =#{mzNo} ")
    BigDecimal selectMzByJz(String mzNo);
    @Select(" select total_charge from zy_ledger_file where inpatient_no =#{inpatientNo} and admiss_times =#{admissTimes} and ledger_sn =#{ledgerSn} ")
    BigDecimal selectZyLedgerFileTotal(PatientParam param);

    @Select(" select sum ( charge_fee ) charge_fee from zy_detail_charge where inpatient_no =#{inpatientNo} and admiss_times =#{admissTimes} and ledger_sn =#{ledgerSn} and isnull ( charge_status , '1' ) <> '1' ")
    BigDecimal selectZyDetailChargeTotal(PatientParam param);

    @Select("<script>" +
            " SELECT  *, " +
            "        responce_unit_name = (select name from zy_zd_responce_type where responce_unit = code)  " +
            " from  zy_receipt " +
            " where    inpatient_no=#{inpatientNo}  " +
            "      and  admiss_times=#{admissTimes}  " +
            " <if test ='ledgerSn !=null'>" +
            "      and  ledger_sn=#{ledgerSn} " +
            " </if>" +
            "</script>")
    List<ZyReceipt> selectZyReceipt(PatientParam param);
    @Select(" select code,name  from zd_cheque_type where zy_flag= '0' ")
    List<Map<String, Object>> selectZdChequeType();
    @Select(" select account_date, " +
            "       cash_date, " +
            "       settle, " +
            "       settle_type, " +
            "       isnull(dcount_no, '') dcount_no, " +
            "       datediff(hour, cash_date, getdate()) diff_cash_date, " +
            "       last_balance, " +
            "       deposit, " +
            "       hosp_part_amt, " +
            "       total_charge - (deposit - settle) yb_jz_je " +
            "from zy_ledger_file " +
            "where inpatient_no = #{inpatientNo} " +
            "  and admiss_times = #{admissTimes} " +
            "  and ledger_sn = #{ledgerSn} ")
    ZyLedgerFile selectSettData(PatientParam param);
    
    @Select(" <script>" +
            "SELECT inpatient_no, " +
            "       admiss_times, " +
            "       depo_times, " +
            "       depo_date, " +
            "       depo_amount, " +
            "       depo_type, " +
            "       depo_type_name = (select top 1 name from zd_cheque_type where code = depo_type), "+
            "       case " +
            "           when window_no = '2' and status = '2' then '4' " +
            "           when window_no = '2' and status = '4' then '3' " +
            "           else status end status, " +
            "       cheque_no, " +
            "       receipt_no, " +
            "       op_id_code, " +
            "        op_id_code_name = (select top 1 name from a_employee_mi where code = op_id_code), "+
            "       ledger_sn, " +
            "       printed, " +
            "       window_no " +
            " FROM zy_deposit_file " +
            " WHERE " +
            "   inpatient_no = #{inpatientNo} " +
            "  and admiss_times = #{admissTimes} " +
            "<if test='inOutFlag == 1'>"+
            " and status in ('1','2','6' ) "+
            "</if>"+
            "<if test='ledgerSn !=null'>"+
            "  and ledger_sn = #{ledgerSn} " +
            " </if> " +
            " order by ledger_sn,status, depo_date" +
            "</script>")
    List<ZyDepositFile> selectZyDepositFile(PatientParam param);

    @Select(" SELECT inpatientNo=a.inpatient_no, " +
            "       accountDate=a.account_date, " +
            "       ledgerSn=a.ledger_sn, " +
            "       opIdCode=a.op_id_code, " +
            "       settle    =a.settle    , " +
            "       wardCode =a.ward_code  , " +
            "       wardName =(select name from zd_unit_code where code =a.ward_code )  , " +
            "       name=b.name, " +
            "       bedNo=b.bed_no, " +
            "       admissDate=b.admiss_date, " +
            "       admissTimes=b.admiss_times, " +
            "       actFlag   ='0' ," +
            "       actFlagName   ='在院' " +
            " FROM zy_ledger_file a  ,zy_actpatient b " +
            " where    a.account_date >= #{startDate} and " +
            "        a.account_date <= #{endDate} and " +
            "    (a.cash_date is null or a.cash_date=null) and " +
            "        a.inpatient_no=b.inpatient_no  and " +
            "        a.admiss_times=b.admiss_times " +
            " union " +
            " SELECT inpatient_no=a.inpatient_no, " +
            "       account_date=a.account_date, " +
            "       ledgerSn=a.ledger_sn, " +
            "       op_id_code=a.op_id_code, " +
            "       settle    =a.settle    , " +
            "       ward_code =a.ward_code  , " +
            "       wardName =(select name from zd_unit_code where code =a.ward_code )  , " +
            "       name=b.name, " +
            "       bed_no=b.bed_no, " +
            "       admiss_date=b.admiss_date, " +
            "       admiss_times=b.admiss_times, " +
            "       act_flag   ='1' ," +
            "       act_flag_name   ='出院' " +
            " FROM zy_ledger_file a  ,zy_inactpatient b " +
            " where    a.account_date >= #{startDate} and " +
            "        a.account_date <= #{endDate} and " +
            "    (a.cash_date is null or a.cash_date=null) and " +
            "        a.inpatient_no=b.inpatient_no  and " +
            "        a.admiss_times=b.admiss_times  and " +
            "        a.ledger_sn   =b.times_billed " +
            "order by inpatientNo ")
    List<Map<String, Object>> queryKyCashierList(PatientParam param);

    @Insert("insert into zy_deposit_file ( inpatient_no , admiss_times , depo_times , depo_date , op_id_code , depo_amount , depo_type , status , cheque_no  , receipt_no , ledger_sn , window_no , deleted,psordnum,agtordnum,par_channel,trans_date,trace_no ) values" +
            " ( #{inpatientNo} , ${admissTimes} , ${depoTimes} , #{depoDate} , #{opIdCode} , #{depoAmount} , #{depoType} , #{status} , #{chequeNo}  , #{receiptNo} , ${ledgerSn} , #{windowNo}, #{deleted},#{psordnum},#{agtordnum},#{parChannel},#{transDate},#{traceNo} ) ")
    int insetZyDepositFile(ZyDepositFile zyDepositFile);
    @Select(" select cast(isnull(max ( depo_times ),0) as int)   from zy_deposit_file where inpatient_no =#{inpatientNo} and admiss_times =#{admissTimes} ")
    Integer selectMaxDepoTimes(PatientParam param);

    @Select(" SELECT count ( *) FROM zy_detail_charge WHERE inpatient_no =#{inpatientNo} AND admiss_times =#{admissTimes} AND ledger_sn =#{ledgerSn} AND charge_fee <> 0 and charge_status ='1' ")
    int countWsjFee(PatientParam param);

    @Select(" SELECT count ( *) FROM yp_zy_patient WHERE inpatient_no =#{inpatientNo} AND admiss_times =#{admissTimes} AND amount <> 0 and acct_sign <> '2' ")
    int countWjsYp(PatientParam param);

    @Select(" SELECT count ( *) FROM yz_zy_patient_fee WHERE inpatient_no =#{inpatientNo} AND admiss_times =#{admissTimes} AND charge_fee <> 0 and charge_status <> '0' and charge_status <> '2' ")
    int countWjYzFee(PatientParam param);

    @Select(" SELECT count ( *) FROM yz_yp_zy_order ( nolock ) WHERE inpatient_no =#{inpatientNo} AND admiss_times =#{admissTimes} AND ( amount > 0 or ( amount < 0 and drug_class <> 'd' ) ) and status_flag ='1' and page_no =0 ")
    int countWtjYp(PatientParam param);

    @Select(" SELECT count ( *) FROM yz_yp_zy_order ( nolock ) WHERE inpatient_no =#{inpatientNo} AND admiss_times =#{admissTimes} AND ( amount > 0 or ( amount < 0 and drug_class <> 'd' ) ) and status_flag ='1' and ( case when amount > 0 then page_no else page_no_ty end > 0 ) ")
    int countWclYp(PatientParam param);

    @Select(" SELECT count ( *) FROM yz_zy_patient_fee WHERE inpatient_no =#{inpatientNo} AND admiss_times =#{admissTimes} AND charge_fee <> 0 and charge_status ='3' ")
    int countWqrFy(PatientParam param);

    @Update(" update zy_ledger_file " +
            "set cash_date   = #{opDate}, " +
            "    settle_type ='4', " +
            "    cash_id     =#{opId}, " +
            "    balance     =0 " +
            "where inpatient_no = #{inpatientNo} " +
            "  and admiss_times = #{admissTimes} " +
            "  and ledger_sn = #{ledgerSn} ")
    int updateCnZyLedgerFile(PatientParam param);

    @Update(" update zy_deposit_file " +
            " SET hsr     =#{opId}, " +
            "    hs_time =#{opDate} " +
            " WHERE inpatient_no = #{inpatientNo} " +
            "  AND admiss_times = #{admissTimes} " +
            "  AND (hs_time is null or hs_time = null) " +
            "  AND window_no = '1' " +
            "  and status in ('1', '2')")
    int updateCnZyDepositFile(PatientParam param);
    @Update(" update ${tableName} set balance =0 where inpatient_no =#{inpatientNo} and admiss_times =#{admissTimes} ")
    int updateZyPatientBalance(PatientParam param);

    @Select(" select count ( *) from zy_actpatient where inpatient_no =#{inpatientNo} and admiss_times =#{admissTimes} ")
    int countZy(PatientParam param);

    @Update(" update zy_ledger_file " +
            "set settle_type ='1', " +
            "    cash_date   =null, " +
            "    cash_id     =null, " +
            "    receipt_no  ='00000000' " +
            "where inpatient_no = #{inpatientNo} " +
            "  and admiss_times = #{admissTimes} " +
            "  and ledger_sn = #{ledgerSn} ")
    int clearZyLedgerFileCashier(PatientParam param);

    @Update(" update zy_receipt " +
            "set receipt_no ='00000000' " +
            "where inpatient_no = #{inpatientNo} " +
            "  and admiss_times = #{admissTimes} " +
            "  and ledger_sn = #{ledgerSn}  ")
    int clearZyReceiptCashier(PatientParam param);

    @Update(" update zy_deposit_file " +
            "SET hsr     ='', " +
            "    hs_time =null " +
            "WHERE inpatient_no = #{inpatientNo} " +
            "  AND admiss_times = #{admissTimes} " +
            "  AND window_no = '1' " +
            "  and status in ('1', '2') ")
    int clearZyDepositFileHsr(PatientParam param);

    @Delete(" delete zy_deposit_file " +
            " where inpatient_no = #{inpatientNo} " +
            "  and admiss_times = #{admissTimes} " +
            "  and ledger_sn = #{ledgerSn} " +
            "  and window_no = 2 ")
    int delZyDepositFileCashier(PatientParam param);

    @Update(" update zy_ledger_file set receipt_no =#{receiptNo} where inpatient_no =#{inpatientNo} and admiss_times =#{admissTimes} and ledger_sn =#{ledgerSn} ")
    int updateZyLedgerFileReceiptNo(PatientParam param );

    @Update(" update zy_deposit_file set receipt_no =#{receiptNo}   where inpatient_no =#{inpatientNo} and admiss_times =#{admissTimes} and ledger_sn =#{ledgerSn} and window_no ='2' ")
    int updateZyDepositFileReceiptNo(PatientParam param);

    @Update(" update zy_receipt set receipt_no =case when #{printFlag} ='1' then #{receiptNo} else receipt_no end , print_date =getdate ( ) where inpatient_no =#{inpatientNo} and admiss_times =#{admissTimes} and ledger_sn =#{ledgerSn} and ( case when 1 > 0 then receipt_sn else 1 end ) =1 ")
    int updatezyReceipt(PatientParam param);

    @Update(" update zy_receipt set dzfp_receipt_no =#{receiptNo} , print_date =getdate ( ) where inpatient_no =#{inpatientNo} and admiss_times =#{admissTimes} and ledger_sn =#{ledgerSn} and ( case when 1 > 0 then receipt_sn else 1 end ) =1 ")
    int updatezyReceiptDzfpNo(PatientParam param);

    @Select(" SELECT a.inpatient_no, " +
            "       a.admiss_times, " +
            "       a.ledger_sn, " +
            "       date_1=max(a.date_1), " +
            "       date_2=max(a.date_2), " +
            "       ward=max(a.ward), " +
            "  ward_name=(select top 1 ward_name from zy_adtward where ward_code = max(a.ward)  ),"+
            "       dept=max(a.dept), " +
            "       total_charge=sum(a.total_charge), " +
            "       charge1=sum(a.charge1), " +
            "       charge2=sum(a.charge2), " +
            "       charge3=sum(a.charge3), " +
            "       charge4=sum(a.charge4), " +
            "       charge5=sum(a.charge5), " +
            "       charge6=sum(a.charge6), " +
            "       charge7=sum(a.charge7), " +
            "       charge8=sum(a.charge8), " +
            "       charge9=sum(a.charge9), " +
            "       charge10=sum(a.charge10), " +
            "       charge11=sum(a.charge11), " +
            "       charge12=sum(a.charge12), " +
            "       charge13=sum(a.charge13), " +
            "       charge14=sum(a.charge14), " +
            "       charge15=sum(a.charge15), " +
            "       charge16=sum(a.charge16), " +
            "       charge17=sum(a.charge17), " +
            "       charge18=sum(a.charge18), " +
            "       charge19=sum(a.charge19), " +
            "       charge20=sum(a.charge20), " +
            "       charge21=sum(a.charge21), " +
            "       charge22=sum(a.charge22), " +
            "       charge23=sum(a.charge23), " +
            "       charge24=sum(a.charge24), " +
            "       charge25=sum(a.charge25), " +
            "       charge26=sum(a.charge26), " +
            "       charge27=sum(a.charge27), " +
            "       charge28=sum(a.charge28), " +
            "       charge29=sum(a.charge29), " +
            "       charge30=sum(a.charge30), " +
            "       res_sn=0, " +
            "       receipt_no=max(receipt_no), " +
            "       responce_unit=isnull(a.responce_unit,''), " +
            "       receipt_sn=max(receipt_sn) " +
            "FROM zy_receipt a " +
            "where    a.inpatient_no=#{inpatientNo}    and " +
            "        a.admiss_times=#{admissTimes} and " +
            "        a.ledger_sn   =#{ledgerSn}    and " +
            "        isnull(a.responce_unit,'')  = #{responceUnit} " +
            "group by a.inpatient_no,a.admiss_times,a.ledger_sn,a.receipt_sn,isnull(a.responce_unit,'') ")
    ZyReceipt selectPrintZyFpData(PatientParam param);
    @Select("select rtrim(name) name ,cast(sex as int ) sex  from a_patient_mi where inpatient_no = #{inpatientNo} ")
    ZyActpatient selectApatienInfo(String inpatientNo);
    @Select(" select top 1 name ,ward,ward_name = (select name from zd_unit_code where code =ward ) from view_zy_patient_all where inpatient_no=#{inpatientNo} ")
    ZyActpatient selectNameAndWardName(String inpatientNo);

    @Select(" select max(ledger_sn) from zy_ledger_file where inpatient_no =#{inpatientNo} AND admiss_times =#{admissTimes} and cash_date is not null and ledger_sn > 0 ")
    Integer hasCashierDataTx(PatientParam param);
    @Select(" SELECT min ( ledger_sn ) FROM zy_ledger_file WHERE inpatient_no =#{inpatientNo} AND admiss_times =#{admissTimes} ")
    Integer selectMinLedgerSn(PatientParam param);
    
    @Insert(" INSERT INTO zy_ledger_file (inpatient_no, admiss_times, ledger_sn, account_date, cash_date, op_id_code, cash_id, " +
            "                            deposit, balance, 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, settle, settle_type, receipt_no, " +
            "                            dept_code, ward_code, dept_x, ward_x, last_balance, dcount_no, dcount_date, zy_serial_no, " +
            "                            tf_flag) " +
            "select inpatient_no, " +
            "       admiss_times, " +
            "       #{oneLedgerSn}, " +
            "       case when #{flag} = '1' then getdate() else account_date end, " +
            "       case when #{flag} = '1' then getdate() else cash_date end, " +
            "       case when #{flag} = '1' then #{info.opId} else op_id_code end, " +
            "       case when #{flag} = '1' then #{info.opId} else op_id_code end, " +
            "       deposit  * #{num}, " +
            "       balance  * #{num}, " +
            "       total_charge  * #{num}, " +
            "       charge1  * #{num}, " +
            "       charge2  * #{num}, " +
            "       charge3  * #{num}, " +
            "       charge4  * #{num}, " +
            "       charge5  * #{num}, " +
            "       charge6  * #{num}, " +
            "       charge7  * #{num}, " +
            "       charge8  * #{num}, " +
            "       charge9  * #{num}, " +
            "       charge10  * #{num}, " +
            "       charge11  * #{num}, " +
            "       charge12  * #{num}, " +
            "       charge13  * #{num}, " +
            "       charge14  * #{num}, " +
            "       charge15  * #{num}, " +
            "       charge16  * #{num}, " +
            "       charge17  * #{num}, " +
            "       charge18  * #{num}, " +
            "       charge19  * #{num}, " +
            "       charge20  * #{num}, " +
            "       charge21  * #{num}, " +
            "       charge22  * #{num}, " +
            "       charge23  * #{num}, " +
            "       charge24  * #{num}, " +
            "       charge25  * #{num}, " +
            "       charge26  * #{num}, " +
            "       charge27  * #{num}, " +
            "       charge28  * #{num}, " +
            "       charge29  * #{num}, " +
            "       charge30  * #{num}, " +
            "       settle  * #{num}, " +
            "       settle_type, " +
            "       receipt_no, " +
            "       dept_code, " +
            "       ward_code, " +
            "       dept_x, " +
            "       ward_x, " +
            "       last_balance, " +
            "       case when #{flag} = '1' then '0' else dcount_no end, " +
            "       case when #{flag} = '1' then null else dcount_date end, " +
            "       zy_serial_no, " +
            "       '1' " +
            "from zy_ledger_file " +
            "WHERE inpatient_no = #{info.inpatientNo} " +
            "  AND admiss_times =  #{info.admissTimes} " +
            "  AND ledger_sn =  #{info.ledgerSn} ")
    void zyLedgerFileTx(@Param("info") PatientParam param, @Param("oneLedgerSn") Integer oneLedgerSn, @Param("flag") String flag, @Param("num") Integer num);
    @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) " +
            "SELECT inpatient_no, " +
            "       admiss_times, " +
            "       #{oneLedgerSn}, " +
            "       receipt_sn, " +
            "       receipt_no, " +
            "       case when #{flag} = '1' then getdate() else account_date end, " +
            "       date_1, " +
            "       date_2, " +
            "       ward, " +
            "       dept, " +
            "       case when  #{flag} = '1' then '1900-01-01' else print_date end, " +
            "       responce_unit, " +
            "       case when  #{flag} = '1' then #{info.opId} else op_id_code end, " +
            "       total_charge * #{num} , " +
            "       charge1 * #{num} , " +
            "       charge2 * #{num} , " +
            "       charge3 * #{num} , " +
            "       charge4 * #{num} , " +
            "       charge5 * #{num} , " +
            "       charge6 * #{num} , " +
            "       charge7 * #{num} , " +
            "       charge8 * #{num} , " +
            "       charge9 * #{num} , " +
            "       charge10 * #{num} , " +
            "       charge11 * #{num} , " +
            "       charge12 * #{num} , " +
            "       charge13 * #{num} , " +
            "       charge14 * #{num} , " +
            "       charge15 * #{num} , " +
            "       charge16 * #{num} , " +
            "       charge17 * #{num} , " +
            "       charge18 * #{num} , " +
            "       charge19 * #{num} , " +
            "       charge20 * #{num} , " +
            "       charge21 * #{num} , " +
            "       charge22 * #{num} , " +
            "       charge23 * #{num} , " +
            "       charge24 * #{num} , " +
            "       charge25 * #{num} , " +
            "       charge26 * #{num} , " +
            "       charge27 * #{num} , " +
            "       charge28 * #{num} , " +
            "       charge29 * #{num} , " +
            "       charge30 * #{num}  " +
            "FROM zy_receipt " +
            "WHERE inpatient_no = #{info.inpatientNo} " +
            "  AND admiss_times = #{info.admissTimes} " +
            "  AND ledger_sn = #{info.ledgerSn} ")
    void zyReceiptTx(@Param("info") PatientParam param, @Param("oneLedgerSn") Integer oneLedgerSn, @Param("flag") String flag, @Param("num") Integer num);

    @Insert(" INSERT INTO zy_detail_charge (inpatient_no, admiss_times, ledger_sn, detail_sn, charge_date, op_id_code, charge_code, " +
            "                              infant_flag, charge_status, charge_fee, self_flag, separate_flag, suppress_flag, " +
            "                              ward_code, dept_code, order_no, ope_flag, exec_unit, charge_amount, charge_code_mx, " +
            "                              serial, zy_serial_no, gen_time) " +
            "select inpatient_no, " +
            "       admiss_times, " +
            "       #{oneLedgerSn}, " +
            "       detail_sn, " +
            "       charge_date, " +
            "       op_id_code, " +
            "       charge_code, " +
            "       infant_flag, " +
            "       charge_status, " +
            "       charge_fee * #{num}, " +
            "       self_flag, " +
            "       separate_flag, " +
            "       suppress_flag, " +
            "       ward_code, " +
            "       dept_code, " +
            "       order_no, " +
            "       ope_flag, " +
            "       exec_unit, " +
            "       charge_amount * #{num}, " +
            "       charge_code_mx, " +
            "       serial, " +
            "       zy_serial_no, " +
            "       getdate() " +
            "from zy_detail_charge " +
            "WHERE inpatient_no = #{info.inpatientNo} " +
            "  AND admiss_times = #{info.admissTimes} " +
            "  AND ledger_sn = #{info.ledgerSn} ")
    void zyDetailChargeTx(@Param("info") PatientParam param, @Param("oneLedgerSn") Integer oneLedgerSn,  @Param("num") Integer num);
    
    @Insert(" INSERT INTO zy_deposit_file (zy_serial_no, inpatient_no, admiss_times, depo_times, depo_date, depo_amount, depo_type, " +
            "                             cheque_no, status, op_id_code, ledger_sn, receipt_no, window_no, deleted) " +
            " SELECT zy_serial_no, " +
            "       inpatient_no, " +
            "       admiss_times, " +
            "       depo_times, " +
            "       case when #{flag} = '1' then getdate() else depo_date end, " +
            "       depo_amount * #{num}, " +
            "       depo_type, " +
            "       cheque_no, " +
            "       status, " +
            "       case when #{flag} = '1' then #{info.opId} else op_id_code end, " +
            "       #{oneLedgerSn}, " +
            "       receipt_no, " +
            "       window_no, " +
            "       '1' " +
            " FROM zy_deposit_file " +
            " WHERE inpatient_no = #{info.inpatientNo} " +
            "  AND admiss_times = #{info.admissTimes} " +
            "  AND ledger_sn = #{info.ledgerSn} " +
            "  AND window_no = '2' ")
    void zyDepositFileTx(@Param("info") PatientParam param, @Param("oneLedgerSn") Integer oneLedgerSn, @Param("flag") String flag, @Param("num") Integer num);
    @Select(" SELECT " +
            "    zy_actpatient.balance, " +
            "    zy_actpatient.total_charge, " +
            "    zy_actpatient.charge1, " +
            "    zy_actpatient.charge2, " +
            "    zy_actpatient.charge3, " +
            "    zy_actpatient.charge4, " +
            "    zy_actpatient.charge5, " +
            "    zy_actpatient.charge6, " +
            "    zy_actpatient.charge7, " +
            "    zy_actpatient.charge8, " +
            "    zy_actpatient.charge9, " +
            "    zy_actpatient.charge10, " +
            "    zy_actpatient.charge11, " +
            "    zy_actpatient.charge12, " +
            "    zy_actpatient.charge13, " +
            "    zy_actpatient.charge14, " +
            "    zy_actpatient.charge15, " +
            "    zy_actpatient.charge16, " +
            "    zy_actpatient.charge17, " +
            "    zy_actpatient.charge18, " +
            "    zy_actpatient.charge19, " +
            "    zy_actpatient.charge20, " +
            "    zy_actpatient.charge21, " +
            "    zy_actpatient.charge22, " +
            "    zy_actpatient.charge23, " +
            "    zy_actpatient.charge24, " +
            "    zy_actpatient.charge25, " +
            "    zy_actpatient.charge26, " +
            "    zy_actpatient.charge27, " +
            "    zy_actpatient.charge28, " +
            "    zy_actpatient.charge29, " +
            "    zy_actpatient.charge30, " +
            "    zy_actpatient.times_billed , " +
            "    inpatient_no,name,admiss_times " +
            "FROM zy_actpatient " +
            "WHERE ( zy_actpatient.inpatient_no = #{inpatientNo}) AND " +
            "    ( zy_actpatient.admiss_times = #{admissTimes} ) " +
            " " +
            "UNION " +
            "SELECT " +
            "    zy_inactpatient.balance, " +
            "    zy_inactpatient.total_charge, " +
            "    zy_inactpatient.charge1, " +
            "    zy_inactpatient.charge2, " +
            "    zy_inactpatient.charge3, " +
            "    zy_inactpatient.charge4, " +
            "    zy_inactpatient.charge5, " +
            "    zy_inactpatient.charge6, " +
            "    zy_inactpatient.charge7, " +
            "    zy_inactpatient.charge8, " +
            "    zy_inactpatient.charge9, " +
            "    zy_inactpatient.charge10, " +
            "    zy_inactpatient.charge11, " +
            "    zy_inactpatient.charge12, " +
            "    zy_inactpatient.charge13, " +
            "    zy_inactpatient.charge14, " +
            "    zy_inactpatient.charge15, " +
            "    zy_inactpatient.charge16, " +
            "    zy_inactpatient.charge17, " +
            "    zy_inactpatient.charge18, " +
            "    zy_inactpatient.charge19, " +
            "    zy_inactpatient.charge20, " +
            "    zy_inactpatient.charge21, " +
            "    zy_inactpatient.charge22, " +
            "    zy_inactpatient.charge23, " +
            "    zy_inactpatient.charge24, " +
            "    zy_inactpatient.charge25, " +
            "    zy_inactpatient.charge26, " +
            "    zy_inactpatient.charge27, " +
            "    zy_inactpatient.charge28, " +
            "    zy_inactpatient.charge29, " +
            "    zy_inactpatient.charge30, " +
            "    zy_inactpatient.times_billed , " +
            "    inpatient_no,name,admiss_times " +
            "FROM zy_inactpatient " +
            "WHERE ( zy_inactpatient.inpatient_no = #{inpatientNo} ) AND " +
            "    ( zy_inactpatient.admiss_times = #{admissTimes} )")
    ZyActpatient selectZyActpatientFeeDetail(PatientParam param);
    @Select(" select max ( ledger_sn ) maxLedgerSn , min ( ledger_sn ) minLedgerSn  from zy_ledger_file where inpatient_no =#{inpatientNo} and admiss_times =#{admissTimes}  ")
    Map<String,Object> selectMaxAndMinLedgerSn(PatientParam param);

    @Select("select * from mz_dzfp_upload where patient_id = #{patientId} and  times = #{times}")
    Map<String,Object> selectDzfpUploadAboutSerialNo(@Param("patientId") String patientId, @Param("times")String times);
    @Select(" select fp_no,qrcode from mz_dzfp_upload where del_flag = 0 and  type_flag = #{typeFlag} and patient_id = #{inpatientNo} and times = #{admissTimes} and receipt_no = #{ledgerSn} ")
    Map<String,Object> selectDzfpNo(PatientParam param);
    @Update("update zy_receipt set dzfp_receipt_no =null  where inpatient_no =#{inpatientNo} and admiss_times =#{admissTimes} and ledger_sn =#{ledgerSn} and ( case when 1 > 0 then receipt_sn else 1 end ) =1 ")
    void clearZyReceiptDzfpNo(PatientParam param);
}
