package cn.hnthyy.thmz.mapper.his.mz;

import cn.hnthyy.thmz.entity.his.mz.MzReceiptSerial;
import org.apache.ibatis.annotations.*;

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

public interface MzReceiptSerialMapper {
    /**
     * 查询当前病人的所有记账费用明细
     *
     * @param patientId
     * @return
     */
    @Select(" select rtrim(patient_id) patient_id,times,receipt_no,serial_no,rtrim(operator_id) operator_id,charge1,charge2,charge3,charge4,charge5,charge6,charge7,charge8,charge9," +
            "charge10,charge11,charge12,charge13,charge14,charge15,charge16,charge17,charge18,charge19,charge20,rtrim(pay_mark) pay_mark,total_charge,charge_date,rtrim(receipt_bill) receipt_bill," +
            "receipt_sn,dcount_date,rtrim(windows_no) windows_no,dcount_no,serial_yb,rtrim(fp_flag) fp_flag,rtrim(responce_type) responce_type,rtrim(cheque_type) cheque_type," +
            "cheque_no,rtrim(tf_flag) tf_flag,dept_no,rtrim(pay_id) pay_id,charge_dcount_date,charge_dcount_no from mz_receipt_serial where patient_id =#{patientId,jdbcType=CHAR} and cheque_type ='Y' and operator_id <> '99999' " +
            "and receipt_sn not in ( select 0 - receipt_sn from mz_receipt_serial where cheque_type ='Y' and patient_id =#{patientId,jdbcType=CHAR} and tf_flag =1 and operator_id <> '99999' ) " +
            "and receipt_sn not in ( select receipt_sn from mz_receipt_serial where cheque_type ='Y' and patient_id =#{patientId,jdbcType=CHAR} and tf_flag =1 and operator_id <> '99999' ) ")
    List<MzReceiptSerial> selectTallyReceiptSerial(@Param("patientId") String patientId);


    /**
     * 根据就诊次数查询当前病人的记账费用明细
     *
     * @param patientId
     * @return
     */
    @Select(" select rtrim(patient_id) patient_id,times,receipt_no,serial_no,rtrim(operator_id) operator_id,charge1,charge2,charge3,charge4,charge5,charge6,charge7,charge8,charge9," +
            "charge10,charge11,charge12,charge13,charge14,charge15,charge16,charge17,charge18,charge19,charge20,rtrim(pay_mark) pay_mark,total_charge,charge_date,rtrim(receipt_bill) receipt_bill," +
            "receipt_sn,dcount_date,rtrim(windows_no) windows_no,dcount_no,serial_yb,rtrim(fp_flag) fp_flag,rtrim(responce_type) responce_type,rtrim(cheque_type) cheque_type," +
            "cheque_no,rtrim(tf_flag) tf_flag,dept_no,rtrim(pay_id) pay_id,charge_dcount_date,charge_dcount_no from mz_receipt_serial where patient_id =#{patientId,jdbcType=CHAR} and times = #{times} and receipt_no = #{receiptNo} and cheque_type ='Y' and operator_id <> '99999' " +
            "and receipt_sn not in ( select 0 - receipt_sn from mz_receipt_serial where cheque_type ='Y' and patient_id =#{patientId,jdbcType=CHAR} and times = #{times} and receipt_no = #{receiptNo} and tf_flag =1 and operator_id <> '99999' ) " +
            "and receipt_sn not in ( select receipt_sn from mz_receipt_serial where cheque_type ='Y' and patient_id =#{patientId,jdbcType=CHAR} and times = #{times}  and receipt_no = #{receiptNo} and tf_flag =1 and operator_id <> '99999' ) ")
    List<MzReceiptSerial> selectTallyReceiptSerialByIdAndTimes(@Param("patientId") String patientId, @Param("times") Integer times,@Param("receiptNo") Integer receiptNo);


    /**
     * 查询当前病人的所有记账费用
     *
     * @param patientId
     * @return
     */
    @Select(" select sum ( total_charge ) from mz_receipt_serial where patient_id =#{patientId,jdbcType=CHAR} and cheque_type ='Y'  and operator_id <> '99999' " +
            "and receipt_sn not in ( select 0 - receipt_sn from mz_receipt_serial where cheque_type ='Y' and patient_id =#{patientId,jdbcType=CHAR} and tf_flag =1 and operator_id <> '99999' ) " +
            "and receipt_sn not in ( select receipt_sn from mz_receipt_serial where cheque_type ='Y' and patient_id =#{patientId,jdbcType=CHAR} and tf_flag =1 and operator_id <> '99999' ) ")
    BigDecimal selectTallyTotalCharge(@Param("patientId") String patientId);


    /**
     * 新增门诊发票记录
     *
     * @param mzReceiptSerial
     * @return
     */
    @Insert("INSERT INTO  mz_receipt_serial(patient_id,times,receipt_no,serial_no,operator_id,charge1,charge2,charge3,charge4,charge5,charge6,charge7,charge8,charge9," +
            "charge10,charge11,charge12,charge13,charge14,charge15,charge16,charge17,charge18,charge19,charge20,pay_mark,total_charge,charge_date,receipt_bill,receipt_sn," +
            "dcount_date,windows_no,dcount_no,serial_yb,fp_flag,responce_type,cheque_type,cheque_no,tf_flag,dept_no,charge_dcount_date,charge_dcount_no,pay_id) VALUES " +
            "(#{patientId,jdbcType=CHAR},#{times,jdbcType=INTEGER},#{receiptNo,jdbcType=SMALLINT},#{serialNo,jdbcType=INTEGER},#{operatorId,jdbcType=CHAR},#{charge1,jdbcType=DOUBLE}," +
            "#{charge2,jdbcType=DOUBLE},#{charge3,jdbcType=DOUBLE},#{charge4,jdbcType=DOUBLE},#{charge5,jdbcType=DOUBLE},#{charge6,jdbcType=DOUBLE},#{charge7,jdbcType=DOUBLE}," +
            "#{charge8,jdbcType=DOUBLE},#{charge9,jdbcType=DOUBLE},#{charge10,jdbcType=DOUBLE},#{charge11,jdbcType=DOUBLE},#{charge12,jdbcType=DOUBLE},#{charge13,jdbcType=DOUBLE}," +
            "#{charge14,jdbcType=DOUBLE},#{charge15,jdbcType=DOUBLE},#{charge16,jdbcType=DOUBLE},#{charge17,jdbcType=DOUBLE},#{charge18,jdbcType=DOUBLE},#{charge19,jdbcType=DOUBLE}," +
            "#{charge20,jdbcType=DOUBLE},#{payMark,jdbcType=CHAR},#{totalCharge,jdbcType=DOUBLE},#{chargeDate,jdbcType=TIMESTAMP},#{receiptBill,jdbcType=CHAR},#{receiptSn,jdbcType=INTEGER}," +
            "#{dcountDate,jdbcType=TIMESTAMP},#{windowsNo,jdbcType=CHAR},#{dcountNo,jdbcType=INTEGER},#{serialYb,jdbcType=INTEGER},#{fpFlag,jdbcType=CHAR},#{responceType,jdbcType=CHAR}," +
            "#{chequeType,jdbcType=CHAR},#{chequeNo,jdbcType=VARCHAR},#{tfFlag,jdbcType=CHAR},#{deptNo,jdbcType=VARCHAR},#{chargeDcountDate,jdbcType=TIMESTAMP},#{chargeDcountNo,jdbcType=INTEGER},#{payId,jdbcType=CHAR})")
    int insertMzReceiptSerial(MzReceiptSerial mzReceiptSerial);


    /**
     * 批量保存门诊发票记录
     *
     * @param mzReceiptSerials
     * @return
     */
    @Insert({
            "<script>",
            "INSERT INTO  mz_receipt_serial(patient_id,times,receipt_no,serial_no,operator_id,charge1,charge2,charge3,charge4,charge5,charge6,charge7,charge8,charge9,charge10,charge11," +
                    "charge12,charge13,charge14,charge15,charge16,charge17,charge18,charge19,charge20,pay_mark,total_charge,charge_date,receipt_bill,receipt_sn,dcount_date,windows_no," +
                    "dcount_no,serial_yb,fp_flag,responce_type,cheque_type,cheque_no,tf_flag,dept_no,charge_dcount_date,charge_dcount_no,pay_id) VALUES ",
            "<foreach collection='mzReceiptSerials' item='item' index='index' separator=','>",
            " (#{item.patientId,jdbcType=CHAR},#{item.times,jdbcType=INTEGER},#{item.receiptNo,jdbcType=SMALLINT},#{item.serialNo,jdbcType=INTEGER},#{item.operatorId,jdbcType=CHAR}," +
                    "#{item.charge1,jdbcType=DOUBLE},#{item.charge2,jdbcType=DOUBLE},#{item.charge3,jdbcType=DOUBLE},#{item.charge4,jdbcType=DOUBLE},#{item.charge5,jdbcType=DOUBLE}," +
                    "#{item.charge6,jdbcType=DOUBLE},#{item.charge7,jdbcType=DOUBLE},#{item.charge8,jdbcType=DOUBLE},#{item.charge9,jdbcType=DOUBLE},#{item.charge10,jdbcType=DOUBLE}," +
                    "#{item.charge11,jdbcType=DOUBLE},#{item.charge12,jdbcType=DOUBLE},#{item.charge13,jdbcType=DOUBLE},#{item.charge14,jdbcType=DOUBLE},#{item.charge15,jdbcType=DOUBLE}," +
                    "#{item.charge16,jdbcType=DOUBLE},#{item.charge17,jdbcType=DOUBLE},#{item.charge18,jdbcType=DOUBLE},#{item.charge19,jdbcType=DOUBLE},#{item.charge20,jdbcType=DOUBLE}," +
                    "#{item.payMark,jdbcType=CHAR},#{item.totalCharge,jdbcType=DOUBLE},#{item.chargeDate,jdbcType=TIMESTAMP},#{item.receiptBill,jdbcType=CHAR},#{item.receiptSn,jdbcType=INTEGER}," +
                    "#{item.dcountDate,jdbcType=TIMESTAMP},#{item.windowsNo,jdbcType=CHAR},#{item.dcountNo,jdbcType=INTEGER},#{item.serialYb,jdbcType=INTEGER},#{item.fpFlag,jdbcType=CHAR}," +
                    "#{item.responceType,jdbcType=CHAR},#{item.chequeType,jdbcType=CHAR},#{item.chequeNo,jdbcType=VARCHAR},#{item.tfFlag,jdbcType=CHAR},#{item.deptNo,jdbcType=VARCHAR}," +
                    "#{item.chargeDcountDate,jdbcType=TIMESTAMP},#{item.chargeDcountNo,jdbcType=INTEGER},#{item.payId,jdbcType=CHAR}) ",
            "</foreach>",
            "</script>"
    })
    int batchInsertMzReceiptSerial(@Param(value = "mzReceiptSerials") List<MzReceiptSerial> mzReceiptSerials);


    /**
     * 查询所有的发票记录
     *
     * @param patientId
     * @param times
     * @return
     */
    @Select({"<script>",
            "select rtrim(patient_id) patient_id,times,receipt_no,serial_no,rtrim(operator_id) operator_id,charge1,charge2,charge3,charge4,charge5,charge6,charge7,charge8,charge9,",
            "charge10,charge11,charge12,charge13,charge14,charge15,charge16,charge17,charge18,charge19,charge20,rtrim(pay_mark) pay_mark,total_charge,charge_date,rtrim(receipt_bill) receipt_bill,",
            "receipt_sn,dcount_date,rtrim(windows_no) windows_no,dcount_no,serial_yb,rtrim(fp_flag) fp_flag,rtrim(responce_type) responce_type,rtrim(cheque_type) cheque_type,cheque_no," +
                    "rtrim(tf_flag) tf_flag,dept_no,rtrim(pay_id) pay_id,charge_dcount_date,charge_dcount_no",
            "from ${tableName} where patient_id =#{patientId,jdbcType=CHAR} and pay_mark=0  and times =#{times}  ",
            "<when test='receiptNo!=null'>",
            " and receipt_no=#{receiptNo}",
            "</when>",
            " order by receipt_bill desc ",
            "</script>"})
    List<MzReceiptSerial> selectReceiptDetails(@Param(value = "patientId") String patientId, @Param("times") Integer times, @Param("receiptNo") Integer receiptNo,@Param(value = "tableName") String tableName);

    /**
     * 根据病人编号和就诊次数查询其当次缴费对应的所有发票
     *
     * @param patientId
     * @param times
     * @return
     */
    @Select({"<script>",
            "select top 1 rtrim(patient_id) patient_id,times,receipt_no,serial_no,rtrim(operator_id) operator_id,charge1,charge2,charge3,charge4,charge5,charge6,charge7,charge8,charge9,",
            "charge10,charge11,charge12,charge13,charge14,charge15,charge16,charge17,charge18,charge19,charge20,rtrim(pay_mark) pay_mark,total_charge,charge_date,rtrim(receipt_bill) receipt_bill,",
            "receipt_sn,dcount_date,rtrim(windows_no) windows_no,dcount_no,serial_yb,rtrim(fp_flag) fp_flag,rtrim(responce_type) responce_type,rtrim(cheque_type) cheque_type,cheque_no," +
                    "rtrim(tf_flag) tf_flag,dept_no,rtrim(pay_id) pay_id,charge_dcount_date,charge_dcount_no",
            "from mz_receipt_serial where patient_id=#{patientId,jdbcType=CHAR} and times = #{times,jdbcType=INTEGER} and receipt_no = #{receiptNo,jdbcType=INTEGER} and ((cheque_type  <![CDATA[<>]]> 'Y' and cheque_type  <![CDATA[<>]]> '2' ) or cheque_type is null or operator_id =99999) order by receipt_no desc",
            "</script>"})
    MzReceiptSerial selectAllSerialForThisTime(@Param(value = "patientId") String patientId, @Param("times") Integer times,@Param("receiptNo") Integer receiptNo);


    /**
     * 根据病人编号和就诊次数查询其当次缴费已经打印的发票
     *
     * @param patientId
     * @param times
     * @return
     */
    @Select({"<script>",
            "select rtrim(patient_id) patient_id,times,receipt_no,serial_no,rtrim(operator_id) operator_id,charge1,charge2,charge3,charge4,charge5,charge6,charge7,charge8,charge9,",
            "charge10,charge11,charge12,charge13,charge14,charge15,charge16,charge17,charge18,charge19,charge20,rtrim(pay_mark) pay_mark,total_charge,charge_date,rtrim(receipt_bill) receipt_bill,",
            "receipt_sn,dcount_date,rtrim(windows_no) windows_no,dcount_no,serial_yb,rtrim(fp_flag) fp_flag,rtrim(responce_type) responce_type,rtrim(cheque_type) cheque_type,cheque_no,rtrim(tf_flag) tf_flag," +
                    "dept_no,rtrim(pay_id) pay_id,charge_dcount_date,charge_dcount_no",
            "from ${tableName} where patient_id=#{patientId,jdbcType=CHAR} and times = #{times,jdbcType=INTEGER} and (cheque_type <![CDATA[<>]]>  'Y' or cheque_type is null or operator_id ='99999' ) ",
            "<when test='receiptBill!=null'>",
            " and receipt_bill =#{receiptBill}",
            "</when>",
            " and receipt_bill is not null and pay_mark =0 order by charge_date desc",
            "</script>"})
    List<MzReceiptSerial> selectPrintedSerialForThisTime(@Param(value = "patientId") String patientId, @Param("times") Integer times, @Param("receiptBill") String receiptBill,@Param(value = "tableName") String tableName);


    /**
     * 根据病人编号和就诊次数查询其当前就诊次数对应的发票
     *
     * @param patientId
     * @param times
     * @param receiptNo
     * @return
     */
    @Select({"<script>",
            "select rtrim(patient_id) patient_id,times,receipt_no,serial_no,rtrim(operator_id) operator_id,charge1,charge2,charge3,charge4,charge5,charge6,charge7,charge8,charge9,",
            "charge10,charge11,charge12,charge13,charge14,charge15,charge16,charge17,charge18,charge19,charge20,rtrim(pay_mark) pay_mark,total_charge,charge_date,rtrim(receipt_bill) receipt_bill,",
            "receipt_sn,dcount_date,rtrim(windows_no) windows_no,dcount_no,serial_yb,rtrim(fp_flag) fp_flag,rtrim(responce_type) responce_type,rtrim(cheque_type) cheque_type,cheque_no," +
                    "rtrim(tf_flag) tf_flag,dept_no,rtrim(pay_id) pay_id,charge_dcount_date,charge_dcount_no",
            "from mz_receipt_serial where  patient_id=#{patientId,jdbcType=CHAR} and times = #{times,jdbcType=INTEGER} and receipt_no = #{receiptNo,jdbcType=INTEGER}",
            "</script>"})
    List<MzReceiptSerial> selectSerialForThisTime(@Param(value = "patientId") String patientId, @Param("times") Integer times, @Param("receiptNo") Integer receiptNo);



    /**
     * 根据病人编号和就诊次数查询其当前就诊次数对应的真实发票
     *
     * @param patientId
     * @param times
     * @param receiptNo
     * @return
     */
    @Select({"<script>",
            "select rtrim(patient_id) patient_id,times,receipt_no,serial_no,rtrim(operator_id) operator_id,charge1,charge2,charge3,charge4,charge5,charge6,charge7,charge8,charge9,",
            "charge10,charge11,charge12,charge13,charge14,charge15,charge16,charge17,charge18,charge19,charge20,rtrim(pay_mark) pay_mark,total_charge,charge_date,rtrim(receipt_bill) receipt_bill,",
            "receipt_sn,dcount_date,rtrim(windows_no) windows_no,dcount_no,serial_yb,rtrim(fp_flag) fp_flag,rtrim(responce_type) responce_type,rtrim(cheque_type) cheque_type,cheque_no," +
                    "rtrim(tf_flag) tf_flag,dept_no,rtrim(pay_id) pay_id,charge_dcount_date,charge_dcount_no",
            "from ${tableName} where  patient_id=#{patientId,jdbcType=CHAR} and times = #{times,jdbcType=INTEGER} and receipt_no = #{receiptNo,jdbcType=INTEGER} and receipt_bill is not null",
            "</script>"})
    MzReceiptSerial selectReceiptBillForThisTime(@Param(value = "patientId") String patientId, @Param("times") Integer times, @Param("receiptNo") Short receiptNo,@Param(value = "tableName") String tableName);


    /**
     * 根据机制号查询发票信息
     *
     * @param serialNo
     * @return
     */
    @Select({"<script>",
            "select rtrim(patient_id) patient_id,times,receipt_no,serial_no,rtrim(operator_id) operator_id,charge1,charge2,charge3,charge4,charge5,charge6,charge7,charge8,charge9,",
            "charge10,charge11,charge12,charge13,charge14,charge15,charge16,charge17,charge18,charge19,charge20,rtrim(pay_mark) pay_mark,total_charge,charge_date,rtrim(receipt_bill) receipt_bill,",
            "receipt_sn,dcount_date,rtrim(windows_no) windows_no,dcount_no,serial_yb,rtrim(fp_flag) fp_flag,rtrim(responce_type) responce_type,rtrim(cheque_type) cheque_type,cheque_no," +
                    "rtrim(tf_flag) tf_flag,dept_no,rtrim(pay_id) pay_id,charge_dcount_date,charge_dcount_no",
            "from mz_receipt_serial where serial_no =#{serialNo}  ",
            "</script>"})
    MzReceiptSerial selectReceiptDetailsBySerialNo(@Param(value = "serialNo") Integer serialNo);

    /**
     * 修改发票表
     *
     * @param mzReceiptSerial
     * @return
     */
    @Update({"<script>",
            "update mz_receipt_serial ",
            "<trim prefix='set' prefixOverrides=',' suffix=' where patient_id = #{patientId} and times=#{times}' >",
            "<when test='receiptBill!=null'>",
            "receipt_bill =#{receiptBill}",
            "</when>",
            "<when test='payMark!=null'>",
            ",pay_mark =#{payMark}",
            "</when>",
            "<when test='tfFlag!=null'>",
            ",tf_flag =#{tfFlag}",
            "</when>",
            "</trim>",
            "</script>"})
    int updateMzReceiptSerial(MzReceiptSerial mzReceiptSerial);


    /**
     * 修改发票表备注字段 实际是为了防止已经打了发票继续打发票 根据收费次数
     *
     * @param mzReceiptSerial
     * @return
     */
    @Update("update mz_receipt_serial set bz =1 where patient_id = #{patientId} and times=#{times} and receipt_no=#{receiptNo} and cheque_type=#{chequeType}  and bz is null")
    int updateBzByReceiptNo(MzReceiptSerial mzReceiptSerial);

    /**
     * 修改发票表 根据收费次数
     *
     * @param mzReceiptSerial
     * @return
     */
    @Update({"<script>",
            "update mz_receipt_serial ",
            "<trim prefix='set' prefixOverrides=',' suffix=' where patient_id = #{patientId} and times=#{times} and receipt_no=#{receiptNo} and cheque_type=#{chequeType} ' >",
            "<when test='receiptBill!=null'>",
            "receipt_bill =#{receiptBill},dcount_date=null,dcount_no=0",
            "</when>",
            "<when test='payMark!=null'>",
            ",pay_mark =#{payMark}",
            "</when>",
            "<when test='tfFlag!=null'>",
            ",tf_flag =#{tfFlag}",
            "</when>",
            "<when test='operatorId!=null'>",
            ",operator_id =#{operatorId}",
            "</when>",
            "<when test='newChequeType!=null'>",
            ",cheque_type =#{newChequeType}",
            "</when>",
            "<when test='payId!=null'>",
            ",pay_id =#{payId}",
            "</when>",
            "<when test='chargeDcountDate!=null'>",
            ",charge_dcount_date =#{chargeDcountDate,jdbcType=TIMESTAMP}",
            "</when>",
            "<when test='chargeDcountNo!=null'>",
            ",charge_dcount_no =#{chargeDcountNo}",
            "</when>",
            "</trim>",
            "</script>"})
    int updateMzReceiptSerialByReceiptNo(MzReceiptSerial mzReceiptSerial);


    /**
     * 根据发票流水号查询发票信息
     *
     * @param receiptSn
     * @return
     */
    @Select({"<script>",
            "select top 1 rtrim(patient_id) patient_id,times,receipt_no,serial_no,rtrim(operator_id) operator_id,charge1,charge2,charge3,charge4,charge5,charge6,charge7,charge8,charge9,",
            "charge10,charge11,charge12,charge13,charge14,charge15,charge16,charge17,charge18,charge19,charge20,rtrim(pay_mark) pay_mark,total_charge,charge_date,rtrim(receipt_bill) receipt_bill,",
            "receipt_sn,dcount_date,rtrim(windows_no) windows_no,dcount_no,serial_yb,rtrim(fp_flag) fp_flag,rtrim(responce_type) responce_type,rtrim(cheque_type) cheque_type,cheque_no," +
                    "rtrim(tf_flag) tf_flag,dept_no,rtrim(pay_id) pay_id,charge_dcount_date,charge_dcount_no",
            "from ${tableName} where receipt_sn =#{receiptSn}  and receipt_bill is not null and receipt_bill  <![CDATA[<>]]>  '' ",
            "</script>"})
    MzReceiptSerial selectReceiptDetailsByReceiptSn(@Param(value = "receiptSn") Integer receiptSn,@Param("tableName") String tableName);




    /**
     * 根据发票流水号修改发票表
     *
     * @param mzReceiptSerial
     * @return
     */
    @Update("update mz_receipt_serial set charge_dcount_date =#{chargeDcountDate},charge_dcount_no =#{chargeDcountNo} where serial_no=#{serialNo}")
    int updateMzReceiptSerialBySerialNo(MzReceiptSerial mzReceiptSerial);

    /**
     * 清洗门诊部数据
     * @return
     */
    @Update("update mz_receipt_serial set pay_id =operator_id,charge_dcount_date=dcount_date,charge_dcount_no=dcount_no where operator_id in ('01893','01894','01895','01942') and charge_dcount_date is null  ")
    int updateMzService();



    /**
     * 根据病人编号和就诊次数查询其当前就诊次数对应的本院记账退费的发票
     *
     * @param patientId
     * @param times
     * @return
     */
    @Select({"<script>",
            "select rtrim(patient_id) patient_id,times,receipt_no,serial_no,rtrim(operator_id) operator_id,charge1,charge2,charge3,charge4,charge5,charge6,charge7,charge8,charge9,",
            "charge10,charge11,charge12,charge13,charge14,charge15,charge16,charge17,charge18,charge19,charge20,rtrim(pay_mark) pay_mark,total_charge,charge_date,rtrim(receipt_bill) receipt_bill,",
            "receipt_sn,dcount_date,rtrim(windows_no) windows_no,dcount_no,serial_yb,rtrim(fp_flag) fp_flag,rtrim(responce_type) responce_type,rtrim(cheque_type) cheque_type,cheque_no," +
                    "rtrim(tf_flag) tf_flag,dept_no,rtrim(pay_id) pay_id,charge_dcount_date,charge_dcount_no",
            "from mz_receipt_serial where  patient_id=#{patientId,jdbcType=CHAR} and times = #{times,jdbcType=INTEGER} and cheque_type ='Y' and tf_flag = 1 ",
            "</script>"})
    List<MzReceiptSerial> selectSerialForJzTfThisTime(@Param(value = "patientId") String patientId, @Param("times") Integer times);






    /**
     * 查询病人缴费记录
     *
     * @param patientIds
     * @param serialNo
     * @param beginDate
     * @param endDate
     * @return
     */
    @Select({"<script>",
            "SELECT rtrim(patient_id) patient_id,times,receipt_no,serial_no,charge_date=convert(char(10),charge_date,111) ,serial_no," +
                    " cheque_type=case when cheque_type='Y' " +
                    " and (len(ltrim(rtrim(receipt_bill))) >= 10 or len(ltrim(rtrim(receipt_bill)))=0) then '本院记账' else '自费' end " +
                    "  ,serial_no_order=ABS(serial_no)  FROM ${tableName} " +
                    "  WHERE charge_date <![CDATA[>=]]> #{beginDate,jdbcType=TIMESTAMP} and charge_date <![CDATA[<=]]> #{endDate,jdbcType=TIMESTAMP}  " +
                    "<when test='patientIds!=null'>",
            " and patient_id in ",
            "<foreach item='item' index='index' collection='patientIds' open='(' separator=',' close=')'>",
            "#{item}",
            "</foreach>",
            "</when>",
            "<when test='serialNo!=null'>",
            " and serial_no like #{serialNo}",
            "</when>",
            " order by serial_no_order desc" +
                    "</script>"})
    List<Map<String, Object>> selectChargeListByPatient(@Param("patientIds") List<String> patientIds, @Param("serialNo") String serialNo, @Param("beginDate") Date beginDate, @Param("endDate") Date endDate,@Param("tableName") String tableName);


    /**
     * 查询病人当天的费用
     * @param beginDate
     * @param endDate
     * @param patientId
     * @return
     */
    @Select("select  sum(total_charge) from  mz_receipt_serial where charge_dcount_date >=#{beginDate}  and charge_dcount_date<=#{endDate} and patient_id=#{patientId} and pay_mark <> 2")
    BigDecimal selectSumByPatientId(@Param("beginDate") String beginDate, @Param("endDate") String endDate,@Param("patientId") String patientId);

    /**
     * 将备份库的发票数据回写进正式库
     * @param patientId
     * @param times
     * @param receiptNo
     * @return
     */
    @Insert("insert into mz_receipt_serial select  * from mz_receipt_serial_b where patient_id=#{patientId} and times = #{times} and receipt_no =#{receiptNo,jdbcType=INTEGER}")
    int repairDataFromBack(@Param(value = "patientId") String patientId, @Param("times") Integer times,@Param("receiptNo") Integer receiptNo);


    /**
     * 将备份库数据删除
     * @param patientId
     * @param times
     * @param receiptNo
     * @return
     */
    @Delete("delete from mz_receipt_serial_b where patient_id=#{patientId} and times = #{times} and receipt_no =#{receiptNo,jdbcType=INTEGER} ")
    int deleteFromBak(@Param(value = "patientId") String patientId, @Param("times") Integer times,@Param("receiptNo") Integer receiptNo);



    /**
     * 根据病人编号和就诊次数查询其在备份库中记录对应的缴费日期
     *
     * @param patientId
     * @param times
     * @param receiptNo
     * @return
     */
    @Select("select charge_date from mz_receipt_serial_b where  patient_id=#{patientId,jdbcType=CHAR} and times = #{times,jdbcType=INTEGER} and receipt_no = #{receiptNo,jdbcType=INTEGER}")
    Date selectChargeDateThisTime(@Param(value = "patientId") String patientId, @Param("times") Integer times, @Param("receiptNo") Integer receiptNo);



    /**
     * 根据病人的收费日期查询备份库中存在的所有记录
     *
     * @param patientId
     * @param beginDate
     * @param endDate
     * @return
     */
    @Select("select  rtrim(patient_id) patient_id,times,receipt_no from mz_receipt_serial_b where patient_id=#{patientId} and charge_date >=#{beginDate} and charge_date <=#{endDate} ")
    List<MzReceiptSerial> selectAllItemByChargeDate(@Param(value = "patientId") String patientId, @Param("beginDate") Date beginDate, @Param("endDate") Date endDate);
}
