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

import cn.hnthyy.thmz.entity.his.mz.MzReceiptSerial;
import cn.hnthyy.thmz.entity.thmz.Mzmxsr;
import cn.hnthyy.thmz.enums.YesNoEnum;
import cn.hnthyy.thmz.vo.*;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

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

public interface ChargeFeeVoMapper {
    /**
     * 日结列表的收费清单 未结账
     *
     * @param userIdCode
     * @return
     */

    @Select("   select " +
            "   patient_id=max(m.patient_id), " +
            "  serial_no =max(m.serial_no), " +
            "   total_charge=isnull(max(m.amount),0), " +
            "   r.pay_mark," +
            "    charge_date=max(r.charge_date)," +
            "  r.receipt_bill," +
            "   receipt_sn=r.receipt_sn , " +
            "   m.cheque_type, " +
            "   m.op_id from mz_deposit_file  m left join mz_receipt_serial r on m.serial_no =r.serial_no where   r.pay_id=#{userIdCode} and" +
            "   m.dcount_no=0 and m.cheque_type <>'Z'  " +
            "  group by r.receipt_sn,r.receipt_bill,m.cheque_type,r.pay_mark,m.op_id " +
            "   order by abs(r.receipt_sn),r.receipt_bill")
    List<ChargeFeeVo> selectChargeFeeVoUnAccount(@Param("userIdCode") String userIdCode);

    /**
     * 日结汇总的收费清单 （正式表数据）
     *
     * @param caseType  0 结账基准  1  收费基准
     * @param beginDate 开始时间
     * @param endDate   结束时间
     * @return
     */
    @Select("   select " +
            "   patient_id=max(m.patient_id), " +
            "  serial_no =max(m.serial_no), " +
            "   total_charge=isnull(max(m.amount),0), " +
            "   r.pay_mark," +
            "    charge_date=max(r.charge_date)," +
            "  r.receipt_bill," +
            "   receipt_sn=r.receipt_sn , " +
            "   m.cheque_type, " +
            "   m.op_id from mz_deposit_file  m left join mz_receipt_serial r on m.serial_no =r.serial_no where " +
            "   case '1' when #{caseType} then r.charge_date else  CONVERT(varchar(100), m.dcount_date, 20) end >=#{beginDate,jdbcType=TIMESTAMP}  " +
            "  and  case '1' when #{caseType} then r.charge_date else  CONVERT(varchar(100), m.dcount_date, 20) end <=#{endDate,jdbcType=TIMESTAMP} " +
            "  and m.pay_mark <> 2 and m.cheque_type <>'Z' " +
            "  group by r.receipt_sn,r.receipt_bill,m.cheque_type,r.pay_mark,m.op_id " +
            "   order by abs(r.receipt_sn),r.receipt_bill")
    List<ChargeFeeVo> selectChargeFeeVoCollect(@Param("caseType") String caseType, @Param("beginDate") Date beginDate, @Param("endDate") Date endDate);


    /**
     * 日结汇总的收费清单 (历史表数据)
     *
     * @param caseType  0 结账基准  1  收费基准
     * @param beginDate 开始时间
     * @param endDate   结束时间
     * @return
     */
    @Select("   select " +
            "   patient_id=max(m.patient_id), " +
            "  serial_no =max(m.serial_no), " +
            "   total_charge=isnull(max(m.amount),0), " +
            "   r.pay_mark," +
            "    charge_date=max(r.charge_date)," +
            "  r.receipt_bill," +
            "   receipt_sn=r.receipt_sn , " +
            "   m.cheque_type, " +
            "   m.op_id from mz_deposit_file_b  m left join mz_receipt_serial_b r on m.serial_no =r.serial_no where " +
            "   case '1' when #{caseType} then r.charge_date else  CONVERT(varchar(100), m.dcount_date, 20) end >=#{beginDate,jdbcType=TIMESTAMP}  " +
            "  and  case '1' when #{caseType} then r.charge_date else  CONVERT(varchar(100), m.dcount_date, 20) end <=#{endDate,jdbcType=TIMESTAMP} " +
            "  and m.pay_mark <> 2 and m.cheque_type <>'Z' " +
            "  group by r.receipt_sn,r.receipt_bill,m.cheque_type,r.pay_mark,m.op_id " +
            "   order by abs(r.receipt_sn),r.receipt_bill")
    List<ChargeFeeVo> selectHisChargeFeeVoCollect(@Param("caseType") String caseType, @Param("beginDate") Date beginDate, @Param("endDate") Date endDate);

    /**
     * 重打结算单的收费清单 日结列表
     *
     * @param userIdCode
     * @param caseType
     * @param dcountDate
     * @return
     */
    @Select("   select " +
            "   patient_id=max(m.patient_id), " +
            "  serial_no =max(m.serial_no), " +
            "   total_charge=isnull(max(m.amount),0), " +
            "   r.pay_mark," +
            "    charge_date=max(r.charge_date)," +
            "  r.receipt_bill," +
            "   receipt_sn=r.receipt_sn , " +
            "   m.cheque_type, " +
            "   m.op_id from mz_deposit_file  m left join mz_receipt_serial r on m.serial_no =r.serial_no where  " +
            "   case '1' when #{caseType} then r.charge_date else  m.dcount_date end =#{dcountDate,jdbcType=TIMESTAMP}  " +
            "  and r.pay_mark <> 2 and m.cheque_type <>'Z' " +
            "  group by r.receipt_sn,r.receipt_bill,m.cheque_type,r.pay_mark,m.op_id " +
            "   order by abs(r.receipt_sn),r.receipt_bill")
    List<ChargeFeeVo> selectChargeFeeVoReprint(@Param("userIdCode") String userIdCode, @Param("caseType") String caseType, @Param("dcountDate") Date dcountDate);

    /**
     * 可以被重打结算列表用户与日期列表
     *
     * @param userIdCode 用户编码
     * @param beginDate  开始时间
     * @param endDate    结束时间
     * @return
     */
    @Select("  SELECT distinct mz_receipt_serial.operator_id," +
            "         mz_receipt_serial.dcount_date " +
            "    FROM mz_receipt_serial " +
            "where  operator_id = #{userIdCode} and " +
            "       CONVERT(varchar(100), dcount_date, 20) >=#{beginDate,jdbcType=TIMESTAMP} and CONVERT(varchar(100), dcount_date, 20) <= #{endDate,jdbcType=TIMESTAMP} " +
            "union " +
            "SELECT distinct mzy_reqrec.op_id," +
            "         mzy_reqrec.closing_date " +
            "    FROM mzy_reqrec " +
            "where  op_id =#{userIdCode} and " +
            "       CONVERT(varchar(100), closing_date, 20) >= #{beginDate,jdbcType=TIMESTAMP} and CONVERT(varchar(100), closing_date, 20) <= #{endDate,jdbcType=TIMESTAMP} " +
            " order by dcount_date desc")
    List<DcountDateListVo> selectDcountDateList(@Param("userIdCode") String userIdCode, @Param("beginDate") Date beginDate, @Param("endDate") Date endDate);


    /**
     * 日结列表的挂号清单 未结账
     *
     * @param userIdCode
     * @return
     */
    @Select("SELECT mzy_reqrec.receipt_bill," +
            "         mzy_reqrec.serial_no," +
            "mzy_reqrec.patient_id,   " +
            "         mzy_reqrec.name,  " +
            "         mzy_reqrec.unit_code,   " +
            "         mzy_reqrec.charge_type," +
            "         mzy_reqrec.cancel_mark," +
            "         sum(req_fee + clinic_fee + oth_fee + isnull(brochure_fee,0) + isnull(bl_fee,0) + isnull(ck_fee,0)) charge_fee," +
            "         max(mzy_reqrec.op_day) op_day, " +
            "         paymode=mzy_reqrec.paymode " +
            "    FROM mzy_reqrec   " +
            "   WHERE (op_id = #{userIdCode} ) and dcount_no = 0 " +
            "group by receipt_bill," +
            "         serial_no," +
            "         patient_id," +
            "         name," +
            "   mzy_reqrec.unit_code,   " +
            "         mzy_reqrec.charge_type," +
            "         mzy_reqrec.cancel_mark,mzy_reqrec.paymode " +
            " order by receipt_bill")
    List<ReqrecVo> selectReqrecVoUnAccount(@Param("userIdCode") String userIdCode);

    /**
     * 重打日结列表的挂号清单 已结账
     *
     * @param userIdCode
     * @param caseType
     * @param dcountDate
     * @return
     */
    @Select("  SELECT receipt_bill=mzy_reqrec.receipt_bill," +
            "         serial_no=mzy_reqrec.serial_no," +
            "patient_id=mzy_reqrec.patient_id,   " +
            "         name=mzy_reqrec.name,   " +
            "         unit_code =mzy_reqrec.unit_code,   " +
            "         charge_type=mzy_reqrec.charge_type," +
            "         cancel_mark=mzy_reqrec.cancel_mark," +
            "         charge_fee =sum(req_fee + clinic_fee + oth_fee + isnull(brochure_fee,0) + isnull(bl_fee,0) + isnull(ck_fee,0))," +
            "         op_day =max(mzy_reqrec.op_day), " +
            "         paymode=mzy_reqrec.paymode " +
            "    FROM mzy_reqrec " +
            "   WHERE   case when '1'=#{caseType} then 0 else 1 end = dcount_no and " +
            "         case when '1'=#{caseType} then op_day else closing_date end=#{dcountDate,jdbcType=TIMESTAMP} and " +
            "         mzy_reqrec.op_id=#{userIdCode} " +
            "group by receipt_bill," +
            "         serial_no," +
            "         patient_id," +
            "         name," +
            "   mzy_reqrec.unit_code,   " +
            "         mzy_reqrec.charge_type," +
            "         mzy_reqrec.cancel_mark,mzy_reqrec.paymode " +
            " order by receipt_bill")
    List<ReqrecVo> selectReqrecVoReprint(@Param("userIdCode") String userIdCode, @Param("caseType") String caseType, @Param("dcountDate") Date dcountDate);

    /**
     * 费用汇总 挂号清单 （正式表数据）
     *
     * @param caseType
     * @param beginDate
     * @param endDate
     * @return
     */
    @Select("  SELECT receipt_bill=mzy_reqrec.receipt_bill," +
            "         serial_no=mzy_reqrec.serial_no," +
            "patient_id=mzy_reqrec.patient_id, " +
            "         name=mzy_reqrec.name, " +
            "         unit_code =mzy_reqrec.unit_code, " +
            "         charge_type=mzy_reqrec.charge_type," +
            "         cancel_mark=mzy_reqrec.cancel_mark," +
            "         charge_fee =sum(req_fee + clinic_fee + oth_fee + isnull(brochure_fee,0) + isnull(bl_fee,0) + isnull(ck_fee,0))," +
            "         op_day =max(mzy_reqrec.op_day)," +
            "op_id, " +
            "         paymode=mzy_reqrec.paymode " +
            "    FROM mzy_reqrec " +
            "   WHERE case when '1'=#{caseType} then 0 else 1 end = dcount_no and " +
            "         case when '1'=#{caseType} then op_day else CONVERT(varchar(100), closing_date, 20) end >=#{beginDate,jdbcType=TIMESTAMP} and " +
            "         case when '1'=#{caseType} then op_day else CONVERT(varchar(100), closing_date, 20) end <=#{endDate,jdbcType=TIMESTAMP} " +
            "group by receipt_bill," +
            "         serial_no," +
            "         patient_id," +
            "         name," +
            "mzy_reqrec.unit_code," +
            "         mzy_reqrec.charge_type," +
            "         mzy_reqrec.cancel_mark," +
            " op_id,mzy_reqrec.paymode " +
            " order by receipt_bill")
    List<ReqrecVo> selectReqrecVoCollect(@Param("caseType") String caseType, @Param("beginDate") Date beginDate, @Param("endDate") Date endDate);


    /**
     * 费用汇总 挂号清单 （历史表数据）
     *
     * @param caseType
     * @param beginDate
     * @param endDate
     * @return
     */
    @Select("  SELECT receipt_bill=mzy_reqrec_b.receipt_bill," +
            "         serial_no=mzy_reqrec_b.serial_no," +
            "patient_id=mzy_reqrec_b.patient_id, " +
            "         name=mzy_reqrec_b.name, " +
            "         unit_code =mzy_reqrec_b.unit_code, " +
            "         charge_type=mzy_reqrec_b.charge_type," +
            "         cancel_mark=mzy_reqrec_b.cancel_mark," +
            "         charge_fee =sum(req_fee + clinic_fee + oth_fee + isnull(brochure_fee,0) + isnull(bl_fee,0) + isnull(ck_fee,0))," +
            "         op_day =max(mzy_reqrec_b.op_day)," +
            "op_id " +
            "         paymode=mzy_reqrec_b.paymode " +
            "    FROM mzy_reqrec_b " +
            "   WHERE case when '1'=#{caseType} then 0 else 1 end = dcount_no and " +
            "         case when '1'=#{caseType} then op_day else CONVERT(varchar(100), closing_date, 20) end >=#{beginDate,jdbcType=TIMESTAMP} and " +
            "         case when '1'=#{caseType} then op_day else CONVERT(varchar(100), closing_date, 20) end <=#{endDate,jdbcType=TIMESTAMP} " +
            " group by receipt_bill," +
            "         serial_no," +
            "         patient_id," +
            "         name," +
            "mzy_reqrec_b.unit_code," +
            "         mzy_reqrec_b.charge_type," +
            "         mzy_reqrec_b.cancel_mark," +
            " op_id,mzy_reqrec_b.paymode " +
            " order by receipt_bill")
    List<ReqrecVo> selectHisReqrecVoCollect(@Param("caseType") String caseType, @Param("beginDate") Date beginDate, @Param("endDate") Date endDate);

    /**
     * 费用日结 收费所用票号
     *
     * @param userIdCode
     * @return
     */
    @Select("  SELECT mz_receipt_serial.pay_mark," +
            "         rtrim(mz_receipt_serial.receipt_bill) receipt_bill," +
            "         rtrim(mz_receipt_serial.serial_no) serial_no " +
            "    FROM mz_receipt_serial " +
            "   WHERE mz_receipt_serial.operator_id =#{userIdCode}  and mz_receipt_serial.dcount_no=0 " +
            "         and mz_receipt_serial.receipt_no  > 0 " +
            "  order by mz_receipt_serial.receipt_bill ")
    List<ReceiptBillVo> selectSfReceiptBillVoUnAccount(@Param("userIdCode") String userIdCode);


    /**
     * 费用日结 挂号所用票号
     *
     * @param userIdCode
     * @return
     */
    @Select("    SELECT mzy_reqrec.cancel_mark," +
            "         rtrim(mzy_reqrec.receipt_bill) receipt_bill," +
            "         rtrim(mzy_reqrec.serial_no) serial_no " +
            "    FROM mzy_reqrec " +
            " where mzy_reqrec.printer_id like #{userIdCode} and print_dcount_no=0 and times  > 0 " +
            "order by op_id,mzy_reqrec.receipt_bill")
    List<ReceiptBillVo> selectGhReceiptBillVoUnAccount(@Param("userIdCode") String userIdCode);

    /**
     * 重打日结 收费所用票号
     *
     * @param userIdCode
     * @param dcountDate
     * @return
     */
    @Select("  SELECT mz_receipt_serial.pay_mark," +
            "          rtrim(mz_receipt_serial.receipt_bill) receipt_bill," +
            "         mz_receipt_serial.serial_no " +
            "    FROM mz_receipt_serial " +
            "   WHERE mz_receipt_serial.operator_id =#{userIdCode}  and mz_receipt_serial.dcount_no=1 " +
            "         and mz_receipt_serial.receipt_no >0 and dcount_date=#{dcountDate,jdbcType=TIMESTAMP}  and (cheque_type <> 'Y' or cheque_type is null) " +
            "  order by mz_receipt_serial.receipt_bill  ")
    List<ReceiptBillVo> selectSfReceiptBillVoReprint(@Param("userIdCode") String userIdCode, @Param("dcountDate") Date dcountDate);


    /**
     * 重打日结 挂号所用票号
     *
     * @param userIdCode
     * @param dcountDate
     * @return
     */
    @Select("    SELECT cancel_mark =mzy_reqrec.cancel_mark," +
            "        receipt_bill = rtrim(mzy_reqrec.receipt_bill)," +
            "         serial_no =mzy_reqrec.serial_no " +
            "    FROM mzy_reqrec " +
            " where mzy_reqrec.printer_id = #{userIdCode} and print_dcount_no=1 and times>0 and " +
            "       print_closing_date =#{dcountDate} " +
            "order by op_id,mzy_reqrec.receipt_bill ")
    List<ReceiptBillVo> selectGhReceiptBillVoReprint(@Param("userIdCode") String userIdCode, @Param("dcountDate") Date dcountDate);

    /**
     * 收款类别数据汇总，当参数只有用户编号时是查询未结算数据，有用户编号和 dcountDate 结算时间是查询 重新打印结算单  当只有开始和结束时间时是查询结算汇总
     *
     * @param userIdCode
     * @param dcountDate
     * @param beginDate
     * @param endDate
     * @return
     */
    @Select({
            "<script>",
            "select " +
                    "sum ( case cheque_type when '1' then amount else 0 end ) xjss," +
                    "sum ( case when cheque_type ='1' and pay_mark ='0'  then amount else 0 end ) xjsk," +
                    "sum ( case when cheque_type ='5' or cheque_type ='3' then amount else 0 end ) yhkss," +
                    "sum ( case when cheque_type in ('5' ,'3') and pay_mark ='0' then amount else 0 end ) yhksk," +
                    "sum ( case cheque_type when '6' then amount else 0 end ) sybkss," +
                    "sum ( case when cheque_type='6' and pay_mark ='0' then amount else 0 end ) sybksk," +
                    "sum ( case when cheque_type = 'Y' and op_id <![CDATA[<>]]> '99999' then amount else 0 end ) zfss," +
                    "sum ( case when cheque_type = 'Y' and op_id <![CDATA[<>]]> '99999' and pay_mark ='0' then amount else 0 end ) zfsk," +
                    "sum ( case when cheque_type ='2'   then amount else 0 end ) ybjzss," +
                    "sum ( case when cheque_type = '2'  and pay_mark ='0'  then amount else 0 end ) ybjzsk," +
                    "sum ( case when cheque_type ='1' and pay_mark ='1' then amount else 0 end ) xjtf," +
                    "sum ( case when ( cheque_type ='5' or cheque_type ='3' ) and pay_mark ='1' then amount else 0 end ) yhktf," +
                    "sum ( case when cheque_type ='6' and pay_mark ='1' then amount else 0 end ) sybtf," +
                    "sum ( case when cheque_type = 'Y' and op_id <![CDATA[<>]]> '99999' and pay_mark &gt; '0'  then amount else 0 end ) zftf," +
                    "sum ( case when cheque_type = '2' and pay_mark &gt; '0'  then amount else 0 end ) ybjztf," +
                    "sum ( amount) zjss," +
                    "sum ( case when pay_mark ='0'  then amount else 0 end ) zjsk," +
                    "sum ( case when pay_mark  <![CDATA[<>]]>'0'  then amount else 0 end ) zjtk," +
                    "sum ( case when ((cheque_type='W' or cheque_type='E' or cheque_type='Y') and op_id ='99999') then amount else 0 end ) wxzzss," +
                    "sum ( case when ((cheque_type='W' or cheque_type='E' or cheque_type='Y') and op_id ='99999') and pay_mark ='0'  then amount else 0 end ) wxzzsk," +
                    "sum ( case when ((cheque_type='M' or cheque_type='F') and op_id ='99999') then amount else 0 end ) zfbzzss," +
                    "sum ( case when ((cheque_type='M' or cheque_type='F') and op_id ='99999') and pay_mark ='0'  then amount else 0 end ) zfbzzsk," +
                    "count ( distinct case when ((cheque_type='W' or cheque_type='E' or cheque_type='Y') and op_id ='99999') and pay_mark ='0' then receipt_sn else null end ) wxzzbs," +
                    " count ( distinct case when ((cheque_type='W' or cheque_type='E' or cheque_type='Y') and op_id ='99999') and pay_mark &gt; '0' then receipt_sn else null end ) wxzztfbs," +
                    "sum ( case when ((cheque_type='W' or cheque_type='E' or cheque_type='Y') and op_id ='99999') and pay_mark ='1' then amount else 0 end ) wxzztf," +
                    "sum ( case when ((cheque_type='M' or cheque_type='F') and op_id ='99999') and pay_mark ='1' then amount else 0 end ) zfbzztf," +
                    "count ( distinct case when ((cheque_type='M' or cheque_type='F') and op_id ='99999') and pay_mark ='0' then receipt_sn else null end ) zfbzzbs," +
                    " count ( distinct case when ((cheque_type='M' or cheque_type='F') and op_id ='99999') and pay_mark &gt; '0' then receipt_sn else null end ) zfbzztfbs," +
                    "count ( distinct case when cheque_type ='1' and pay_mark ='0' then receipt_sn else null end ) xjbs," +
                    "  count ( distinct case when ( cheque_type ='5' or cheque_type ='3' ) and pay_mark ='0' then receipt_sn else null end ) yhsbs," +
                    "  count ( distinct case when cheque_type ='6' and pay_mark ='0' then receipt_sn else null end ) sybbs," +
                    " count ( distinct case when cheque_type = 'Y' and op_id <![CDATA[<>]]> '99999'  and pay_mark ='0'  then receipt_sn else null end ) zfbs," +
                    " count ( distinct case when cheque_type = '2'  and pay_mark ='0'  then receipt_sn else null end ) ybjzbs," +
                    " count ( distinct case when cheque_type ='1' and pay_mark &gt; '0' then receipt_sn else null end ) xjtfbs," +
                    "count ( distinct case when ( cheque_type ='5' or cheque_type ='3' ) and pay_mark &gt; '0' then receipt_sn else null end ) yhktfbs," +
                    "count ( distinct case when cheque_type ='6' and pay_mark &gt; '0' then receipt_sn else null end ) sybtfbs," +
                    "   count ( distinct case when cheque_type = 'Y' and op_id <![CDATA[<>]]> '99999' and pay_mark &gt; '0'  then receipt_sn else null end ) zftfbs," +
                    "   count ( distinct case when cheque_type ='2'  and pay_mark &gt; '0'  then receipt_sn else null end ) ybjztfbs," +
                    "    count ( distinct case when pay_mark ='0' then cast(serial_no as char)+cheque_type else null end ) zjbs," +
                    "count ( distinct case when pay_mark  <![CDATA[<>]]> '0' then cast(serial_no as char)+cheque_type else null end ) ztfbs," +
                    "count ( distinct case when cheque_type ='E' and op_id <![CDATA[<>]]> '99999' and pay_mark ='0' then receipt_sn else null end ) wxbs," +
                    "count ( distinct case when cheque_type ='F' and op_id <![CDATA[<>]]> '99999' and pay_mark ='0' then receipt_sn else null end ) zfbbs," +
                    "count ( distinct case when cheque_type ='E' and op_id <![CDATA[<>]]> '99999' and pay_mark  <![CDATA[<>]]> '0' then receipt_sn else null end ) wxtfbs," +
                    "count ( distinct case when cheque_type ='F' and op_id <![CDATA[<>]]> '99999' and pay_mark  <![CDATA[<>]]> '0' then receipt_sn else null end ) zfbtfbs," +
                    "sum ( case when cheque_type= 'E' and op_id <![CDATA[<>]]> '99999' then amount else 0 end ) wxss," +
                    "sum ( case when cheque_type ='E' and op_id <![CDATA[<>]]> '99999' and pay_mark ='0' then amount else 0 end ) wxsk," +
                    "sum ( case when cheque_type= 'F' and op_id <![CDATA[<>]]> '99999' then amount else 0 end ) zfbss," +
                    "sum ( case when cheque_type ='F' and op_id <![CDATA[<>]]> '99999' and pay_mark ='0' then amount else 0 end ) zfbsk," +
                    "sum ( case when cheque_type ='E' and op_id <![CDATA[<>]]> '99999' and pay_mark ='1' then amount else 0 end ) wxtf," +
                    "sum ( case when cheque_type ='F' and op_id <![CDATA[<>]]> '99999' and pay_mark ='1' then amount else 0 end ) zfbtf " +
                    " from mz_deposit_file " +
                    " where 1=1 and pay_mark <![CDATA[<>]]> 2 ",
            "<when test='userIdCode!=null'>",
            " and op_id=#{userIdCode} ",
            "</when>",
            "<when test='dcountDate!=null'>",
            " and dcount_date=#{dcountDate,jdbcType=TIMESTAMP} ",
            "</when>",
            "<when test='beginDate!=null'>",
            " and CONVERT(varchar(100), dcount_date, 20) &gt;=#{beginDate,jdbcType=TIMESTAMP} ",
            "</when>",
            "<when test='endDate!=null'>",
            " and CONVERT(varchar(100), dcount_date, 20) &lt;=#{endDate,jdbcType=TIMESTAMP} ",
            "</when>",
            "<when test='dcountDate == null and beginDate == null and endDate == null'>",
            " and dcount_no =0 ",
            "</when>",
            "</script>"})
    List<ChargeCountVo> selectChargeCountVo(@Param("userIdCode") String userIdCode, @Param("dcountDate") Date dcountDate, @Param("beginDate") Date beginDate, @Param("endDate") Date endDate);


    /**
     * 各种费用数据汇总，当参数只有用户编号时是查询未结算数据，有用户编号和 dcountDate 结算时间是查询 重新打印结算单  当只有开始和结束时间时是查询结算汇总
     *
     * @param userIdCode
     * @param dcountDate
     * @param beginDate
     * @param endDate
     * @return
     */
    @Select({
            "<script>",
            "SELECT " +
                    "isnull ( sum ( charge1 ) , 0 ) charge1," +
                    "isnull ( sum ( charge2 ) , 0 ) charge2," +
                    "isnull ( sum ( charge3 ) , 0 ) charge3," +
                    "isnull ( sum ( charge4 ) , 0 ) charge4," +
                    "isnull ( sum ( charge5 ) , 0 ) charge5," +
                    "isnull ( sum ( charge6 ) , 0 ) charge6," +
                    "isnull ( sum ( charge7 ) , 0 ) charge7," +
                    "isnull ( sum ( charge8 ) , 0 ) charge8," +
                    "isnull ( sum ( charge9 ) , 0 ) charge9," +
                    "isnull ( sum ( charge10 ) , 0 ) charge10," +
                    "isnull ( sum ( charge11 ) , 0 ) charge11," +
                    "isnull ( sum ( charge12 ) , 0 ) charge12," +
                    "isnull ( sum ( charge13 ) , 0 ) charge13," +
                    "isnull ( sum ( charge14 ) , 0 ) charge14," +
                    "isnull ( sum ( charge15 ) , 0 ) charge15," +
                    "isnull ( sum ( charge16 ) , 0 ) charge16," +
                    "isnull ( sum ( charge17 ) , 0 ) charge17," +
                    "isnull ( sum ( charge18 ) , 0 ) charge18," +
                    "isnull ( sum ( charge19 ) , 0 ) charge19," +
                    "isnull ( sum ( charge20 ) , 0 ) charge20," +
                    "isnull ( sum ( total_charge ) , 0 ) total_charge," +
                    "isnull (sum ( case mz_receipt_serial.pay_mark when '0' then 1 when '2' then 1 else 0 end ) ,0)total_count " +
                    "FROM mz_receipt_serial " +
                    " where 1=1 and pay_mark <![CDATA[<>]]> 2 " +
                    //"and (cheque_type not in ('W','M') or cheque_type is null) "+
                    "<when test='userIdCode!=null'>",
            " and pay_id=#{userIdCode} ",
            "</when>",
            "<when test='dcountDate!=null'>",
            " and charge_dcount_date=#{dcountDate,jdbcType=TIMESTAMP} ",
            "</when>",
            "<when test='beginDate!=null'>",
            " and CONVERT(varchar(100), charge_dcount_date, 20) &gt;=#{beginDate,jdbcType=TIMESTAMP} ",
            "</when>",
            "<when test='endDate!=null'>",
            " and CONVERT(varchar(100), charge_dcount_date, 20) &lt;=#{endDate,jdbcType=TIMESTAMP} ",
            "</when>",
            "<when test='dcountDate == null and beginDate == null and endDate == null'>",
            " and charge_dcount_no =0 ",
            "</when>",
            "</script>"})
    List<MzReceiptSerial> selectMzReceiptSerial(@Param("userIdCode") String userIdCode, @Param("dcountDate") Date dcountDate, @Param("beginDate") Date beginDate, @Param("endDate") Date endDate);

    /**
     * 查询收费发票数  payMark = 0  总票数  payMark = 1 退号数  payMark =2 作废票数
     *
     * @param userIdCode
     * @param payMark
     * @return
     */
    @Select({"<script>",
            "select count ( distinct receipt_bill ) from mz_receipt_serial where operator_id =#{userIdCode} and len(receipt_bill)=8 and receipt_bill <![CDATA[<>]]> '00000000' ",
            "<when test='payMark!=null and payMark == 1'>",
            " and receipt_no  <![CDATA[<]]> 0 and pay_mark =1",
            "</when>",
            "<when test='payMark!=null and payMark == 0'>",
            " and receipt_no <![CDATA[>]]> 0 and pay_mark in (0,2) ",
            "</when>",
            "<when test='payMark!=null and payMark == 2'>",
            " and receipt_no <![CDATA[>]]> 0 and pay_mark =2 ",
            "</when>",
            "<when test='dcountDate!=null'>",
            " and dcount_date=#{dcountDate,jdbcType=TIMESTAMP} ",
            "</when>",
            "<when test='dcountDate == null'>",
            " and dcount_no =0 ",
            "</when>",
            "</script>"})
    Integer countReceipt(@Param("userIdCode") String userIdCode, @Param("payMark") String payMark, @Param("dcountDate") Date dcountDate);

    /**
     * 查询收费作废发票明细
     *
     * @param userIdCode
     * @param dcountDate
     * @return
     */
    @Select({"<script>",
            "select a.name," +
                    "     receipt_bill=rtrim(b.receipt_bill)," +
                    "       flag=(select case when count(*)> 0 then '取消'else '' end from mz_charge_detail where real_no=b.receipt_sn and isnull(confirm_flag_qx,'')='1')," +
                    "       total_charge=sum(b.total_charge) " +
                    "from mz_patient_mi a," +
                    "     mz_receipt_serial b " +
                    "where b.operator_id=#{userIdCode} and b.pay_mark='1' and total_charge <![CDATA[<]]> 0   " +
                    "<when test='dcountDate!=null'>",
            " and dcount_date=#{dcountDate,jdbcType=TIMESTAMP} ",
            "</when>",
            "<when test='dcountDate == null'>",
            " and dcount_no =0 ",
            "</when>",
            "    and  (b.patient_id=a.patient_id) " +
                    "group by a.name,b.receipt_bill,b.receipt_sn " +
                    "union " +
                    "select  a.name," +
                    "       receipt_bill=rtrim(b.receipt_bill)," +
                    "       flag=''," +
                    "      total_charge= b.total_charge " +
                    "from mz_receipt_serial b left join  mz_patient_mi a on b.patient_id = a.patient_id " +
                    "where operator_id=#{userIdCode} and pay_mark='2' ",
            "<when test='dcountDate!=null'>",
            " and dcount_date=#{dcountDate,jdbcType=TIMESTAMP} ",
            "</when>",
            "<when test='dcountDate == null'>",
            " and dcount_no =0 ",
            "</when>",
            "</script>"})
    List<ReceiptVo> selectReceiptVo(@Param("userIdCode") String userIdCode, @Param("dcountDate") Date dcountDate);


    /**
     * 查询自助发票明细
     *
     * @param userIdCode
     * @param dcountDate
     * @return
     */
    @Select({"<script>",
            "select a.name, " +
                    "receipt_bill=rtrim(b.receipt_bill), " +
                    "total_charge=sum(b.total_charge) " +
                    "from mz_patient_mi a, mz_receipt_serial b " +
                    "where b.operator_id=#{userIdCode} and b.pay_mark='0' " +
                    "<when test='dcountDate!=null'>",
            " and dcount_date=#{dcountDate,jdbcType=TIMESTAMP} ",
            "</when>",
            "<when test='dcountDate == null'>",
            " and dcount_no =0 ",
            "</when>",
            "and pay_id='99999' and cheque_type in ('E','F','M','W') and (b.patient_id=a.patient_id) group by a.name,b.receipt_bill,b.receipt_sn ",
            "</script>"})
    List<ReceiptVo> selectZiZhuReceiptVo(@Param("userIdCode") String userIdCode, @Param("dcountDate") Date dcountDate);


    /**
     * 查询挂号发票数   cancelMark = 0  总票数  cancelMark = 1 退号数
     *
     * @param userIdCode
     * @param cancelMark
     * @return
     */
    @Select({"<script>",
            "select count ( *) from mzy_reqrec where  len(receipt_bill)=8 and receipt_bill <![CDATA[<>]]> '00000000' ",
            "<when test='cancelMark!=null and cancelMark == 1'>",
            " and times <![CDATA[<]]> 0 ",
            "</when>",
            "<when test='cancelMark!=null and cancelMark == 0'>",
            " and times <![CDATA[>]]> 0 ",
            "</when>",
            "<when test='userIdCode!=null'>",
            " and printer_id=#{userIdCode} ",
            "</when>",
            "<when test='dcountDate!=null'>",
            " and print_closing_date=#{dcountDate,jdbcType=TIMESTAMP} ",
            "</when>",
            "<when test='beginDate!=null'>",
            " and CONVERT(varchar(100), print_closing_date, 20) &gt;=#{beginDate,jdbcType=TIMESTAMP} ",
            "</when>",
            "<when test='endDate!=null'>",
            " and CONVERT(varchar(100), print_closing_date, 20) &lt;=#{endDate,jdbcType=TIMESTAMP} ",
            "</when>",
            "<when test='dcountDate == null and beginDate == null and endDate == null'>",
            " and print_dcount_no =0 ",
            "</when>",
            "</script>"})
    Integer countReqrecReceipt(@Param("userIdCode") String userIdCode, @Param("cancelMark") String cancelMark, @Param("dcountDate") Date dcountDate, @Param("beginDate") Date beginDate, @Param("endDate") Date endDate);

    /**
     * 查询挂号收入构成表格数据
     *
     * @param userIdCode
     * @param dcountDate
     * @param beginDate
     * @param endDate
     * @return
     */
    @Select({
            "<script>",
            "SELECT " +
                    " count ( case when times  <![CDATA[>]]> 0 and (paymode ='1' or paymode is null) then 1 else null end ) xjhs," +
                    " sum ( case when times  <![CDATA[>]]> 0 and (paymode ='1' or paymode is null) then req_fee + oth_fee + clinic_fee + isnull ( brochure_fee , 0 ) + isnull ( bl_fee , 0 ) + isnull ( ck_fee , 0 ) else 0 end ) xjsk, " +
                    " count ( case when  times  <![CDATA[<]]> 0 and (paymode ='1' or paymode is null) then 1 else null end ) xjthhs," +
                    " ABS( sum ( case when times  <![CDATA[<]]> 0 and (paymode ='1' or paymode is null) then req_fee + oth_fee + clinic_fee + isnull ( brochure_fee , 0 ) + isnull ( bl_fee , 0 ) + isnull ( ck_fee , 0 ) else 0 end )) xjth, " +
                    " count ( case when times  <![CDATA[>]]> 0 and (paymode ='1' or paymode is null) then 1 else null end ) - count ( case when times  <![CDATA[<]]> 0 and (paymode ='1' or paymode is null) then 1 else null end ) xjsshs," +
                    " sum ( case when paymode ='1' or paymode is null then req_fee + oth_fee + clinic_fee + isnull ( brochure_fee , 0 ) + isnull ( bl_fee , 0 ) + isnull ( ck_fee , 0 ) else 0 end ) xjss, " +
                    " count ( case when times  <![CDATA[>]]> 0 and paymode ='3' then 1 else null end ) yhkhs," +
                    " sum ( case when times  <![CDATA[>]]> 0 and paymode ='3'  then req_fee + oth_fee + clinic_fee + isnull ( brochure_fee , 0 ) + isnull ( bl_fee , 0 ) + isnull ( ck_fee , 0 ) else 0 end ) yhksk, " +
                    " count ( case when  times  <![CDATA[<]]> 0 and paymode ='3' then 1 else null end )  yhkthhs," +
                    " ABS(sum ( case when times  <![CDATA[<]]> 0 and paymode ='3' then req_fee + oth_fee + clinic_fee + isnull ( brochure_fee , 0 ) + isnull ( bl_fee , 0 ) + isnull ( ck_fee , 0 ) else 0 end )) yhkth, " +
                    " count ( case when times  <![CDATA[>]]> 0 and paymode ='3'  then 1 else null end ) - count ( case when times  <![CDATA[<]]> 0 and paymode ='3'  then 1 else null end ) yhksshs," +
                    " sum ( case when paymode ='3'  then req_fee + oth_fee + clinic_fee + isnull ( brochure_fee , 0 ) + isnull ( bl_fee , 0 ) + isnull ( ck_fee , 0 ) else 0 end ) yhkss," +
                    " count ( case when times  <![CDATA[>]]> 0 and paymode ='6' then 1 else null end ) ybkhs," +
                    " sum ( case when times  <![CDATA[>]]> 0 and paymode ='6'  then req_fee + oth_fee + clinic_fee + isnull ( brochure_fee , 0 ) + isnull ( bl_fee , 0 ) + isnull ( ck_fee , 0 ) else 0 end ) ybksk, " +
                    " count ( case when times  <![CDATA[<]]> 0 and paymode ='6' then 1 else null end ) ybkthhs," +
                    " ABS(sum ( case when times  <![CDATA[<]]> 0 and paymode ='6' then req_fee + oth_fee + clinic_fee + isnull ( brochure_fee , 0 ) + isnull ( bl_fee , 0 ) + isnull ( ck_fee , 0 ) else 0 end )) ybkth, " +
                    " count ( case when times  <![CDATA[>]]> 0 and paymode ='6'  then 1 else null end ) - count ( case when times  <![CDATA[<]]> 0 and paymode ='6'  then 1 else null end ) ybksshs," +
                    " sum ( case when paymode ='6'  then req_fee + oth_fee + clinic_fee + isnull ( brochure_fee , 0 ) + isnull ( bl_fee , 0 ) + isnull ( ck_fee , 0 ) else 0 end ) ybkss," +
                    " count ( case when times  <![CDATA[>]]> 0 and paymode ='F' then 1 else null end ) zfbhs," +
                    " sum ( case when times  <![CDATA[>]]> 0 and paymode ='F'  then req_fee + oth_fee + clinic_fee + isnull ( brochure_fee , 0 ) + isnull ( bl_fee , 0 ) + isnull ( ck_fee , 0 ) else 0 end ) zfbsk, " +
                    " count ( case when times  <![CDATA[<]]> 0 and paymode ='F' then 1 else null end )  zfbthhs," +
                    " ABS(sum ( case when times  <![CDATA[<]]> 0 and paymode ='F' then req_fee + oth_fee + clinic_fee + isnull ( brochure_fee , 0 ) + isnull ( bl_fee , 0 ) + isnull ( ck_fee , 0 ) else 0 end )) zfbth, " +
                    " count ( case when times  <![CDATA[>]]> 0 and paymode ='F'  then 1 else null end ) - count ( case when  times  <![CDATA[<]]> 0 and paymode ='F'  then 1 else null end ) zfbsshs," +
                    " sum ( case when paymode ='F'  then req_fee + oth_fee + clinic_fee + isnull ( brochure_fee , 0 ) + isnull ( bl_fee , 0 ) + isnull ( ck_fee , 0 ) else 0 end ) zfbss," +
                    " count ( case when times  <![CDATA[>]]> 0 and paymode ='E' then 1 else null end )  wxhs," +
                    " sum ( case when times  <![CDATA[>]]> 0 and paymode ='E'  then req_fee + oth_fee + clinic_fee + isnull ( brochure_fee , 0 ) + isnull ( bl_fee , 0 ) + isnull ( ck_fee , 0 ) else 0 end ) wxsk, " +
                    " count ( case when times  <![CDATA[<]]> 0 and paymode ='E' then 1 else null end ) wxthhs," +
                    " ABS(sum ( case when times  <![CDATA[<]]> 0 and paymode ='E' then req_fee + oth_fee + clinic_fee + isnull ( brochure_fee , 0 ) + isnull ( bl_fee , 0 ) + isnull ( ck_fee , 0 ) else 0 end )) wxth, " +
                    " count ( case when times  <![CDATA[>]]> 0 and paymode ='E'  then 1 else null end ) -count ( case when times  <![CDATA[<]]> 0 and paymode ='E' then 1 else null end ) wxsshs," +
                    " sum ( case when paymode ='E'  then req_fee + oth_fee + clinic_fee + isnull ( brochure_fee , 0 ) + isnull ( bl_fee , 0 ) + isnull ( ck_fee , 0 ) else 0 end ) wxss," +
                    " count ( case when times  <![CDATA[>]]> 0 and paymode ='Y' then 1 else null end ) - count ( case when  times  <![CDATA[<]]> 0 and paymode ='Y'  then 1 else null end ) byjzhs," +
                    " sum ( case when times  <![CDATA[>]]> 0 and paymode ='Y'  then req_fee + oth_fee + clinic_fee + isnull ( brochure_fee , 0 ) + isnull ( bl_fee , 0 ) + isnull ( ck_fee , 0 ) else 0 end ) byjzsk, " +
                    " count ( case when times  <![CDATA[<]]> 0 and paymode ='Y' then 1 else null end ) byjzthhs," +
                    " ABS(sum ( case when times  <![CDATA[<]]> 0 and paymode ='Y' then req_fee + oth_fee + clinic_fee + isnull ( brochure_fee , 0 ) + isnull ( bl_fee , 0 ) + isnull ( ck_fee , 0 ) else 0 end )) byjzth, " +
                    " count ( case when times  <![CDATA[>]]> 0 and paymode ='Y'  then 1 else null end ) - count ( case when times  <![CDATA[<]]> 0 and paymode ='Y'  then 1 else null end ) byjzsshs," +
                    " sum ( case when paymode ='Y'  then req_fee + oth_fee + clinic_fee + isnull ( brochure_fee , 0 ) + isnull ( bl_fee , 0 ) + isnull ( ck_fee , 0 ) else 0 end ) byjzss," +
                    " count ( case when times  <![CDATA[>]]> 0 and (paymode <![CDATA[<>]]> 'hospital_z' or paymode is null) then 1 else null end ) hjhs," +
                    " sum ( case when times  <![CDATA[>]]> 0 and (paymode <![CDATA[<>]]> 'hospital_z' or paymode is null) then req_fee + oth_fee + clinic_fee + isnull ( brochure_fee , 0 ) + isnull ( bl_fee , 0 ) + isnull ( ck_fee , 0 ) else 0 end ) hjsk, " +
                    " count ( case when (paymode <![CDATA[<>]]> 'hospital_z' or paymode is null)  and times  <![CDATA[<]]> 0 then 1 else null end ) hjthhs," +
                    " ABS(sum ( case when (paymode <![CDATA[<>]]> 'hospital_z' or paymode is null) and times  <![CDATA[<]]> 0 then req_fee + oth_fee + clinic_fee + isnull ( brochure_fee , 0 ) + isnull ( bl_fee , 0 ) + isnull ( ck_fee , 0 ) else 0 end )) hjth, " +
                    " count ( case when times  <![CDATA[>]]> 0 and (paymode <![CDATA[<>]]> 'hospital_z' or paymode is null) then 1 else null end ) -count ( case when  (paymode <![CDATA[<>]]> 'hospital_z' or paymode is null)  and times  <![CDATA[<]]> 0 then 1 else null end ) hjsshs," +
                    " sum ( case when paymode <![CDATA[<>]]> 'hospital_z' or paymode is null then req_fee + oth_fee + clinic_fee + isnull ( brochure_fee , 0 ) + isnull ( bl_fee , 0 ) + isnull ( ck_fee , 0 ) else 0 end ) hjss," +
                    " count ( case when times  <![CDATA[>]]> 0 and paymode ='WX' then 1 else null end ) wxzzhs," +
                    " sum ( case when times  <![CDATA[>]]> 0 and paymode ='WX'  then req_fee + oth_fee + clinic_fee + isnull ( brochure_fee , 0 ) + isnull ( bl_fee , 0 ) + isnull ( ck_fee , 0 ) else 0 end )  wxzzsk, " +
                    " count ( case when op_id='99999' and  times  <![CDATA[<]]> 0 and paymode ='WX' then 1 else null end ) wxzzthhs," +
                    " ABS( sum ( case when op_id='99999' and   times  <![CDATA[<]]> 0 and paymode ='WX'  then req_fee + oth_fee + clinic_fee + isnull ( brochure_fee , 0 ) + isnull ( bl_fee , 0 ) + isnull ( ck_fee , 0 ) else 0 end )) wxzzth, " +
                    " count ( case when times  <![CDATA[>]]> 0 and paymode ='WX' then 1 else null end )  - count ( case when  op_id='99999' and times  <![CDATA[<]]> 0 and paymode ='WX' then 1 else null end ) wxzzsshs," +
                    " sum ( case when op_id='99999' and paymode ='WX' then req_fee + oth_fee + clinic_fee + isnull ( brochure_fee , 0 ) + isnull ( bl_fee , 0 ) + isnull ( ck_fee , 0 ) else 0 end ) wxzzss, " +
                    " count ( case when times  <![CDATA[>]]> 0 and paymode ='ZFB' then 1 else null end ) zfbzzhs," +
                    " sum ( case when times  <![CDATA[>]]> 0 and paymode ='ZFB'  then req_fee + oth_fee + clinic_fee + isnull ( brochure_fee , 0 ) + isnull ( bl_fee , 0 ) + isnull ( ck_fee , 0 ) else 0 end )  zfbzzsk, " +
                    " count ( case when op_id='99999' and    times  <![CDATA[<]]> 0 and paymode ='ZFB' then 1 else null end ) zfbzzthhs," +
                    " ABS( sum ( case when  op_id='99999' and   times  <![CDATA[<]]> 0 and paymode ='ZFB'  then req_fee + oth_fee + clinic_fee + isnull ( brochure_fee , 0 ) + isnull ( bl_fee , 0 ) + isnull ( ck_fee , 0 ) else 0 end )) zfbzzth, " +
                    " count ( case when times  <![CDATA[>]]> 0 and paymode ='ZFB' then 1 else null end )  - count ( case when op_id='99999' and times  <![CDATA[<]]> 0 and paymode ='ZFB' then 1 else null end ) zfbzzsshs," +
                    " sum ( case when op_id='99999' and paymode ='ZFB' then req_fee + oth_fee + clinic_fee + isnull ( brochure_fee , 0 ) + isnull ( bl_fee , 0 ) + isnull ( ck_fee , 0 ) else 0 end ) zfbzzss, " +
                    " sum ( case when isnull ( paymode , '1' ) = 'Y' then req_fee + oth_fee + clinic_fee + isnull ( brochure_fee , 0 ) + isnull ( bl_fee , 0 ) + isnull ( ck_fee , 0 ) else 0 end ) ghjejz," +
                    " sum ( case when isnull ( paymode , '1' ) <![CDATA[<>]]> 'Y' then req_fee + oth_fee + clinic_fee + isnull ( brochure_fee , 0 ) + isnull ( bl_fee , 0 ) + isnull ( ck_fee , 0 ) else 0 end ) ghjebhjz," +
                    " sum ( clinic_fee ) zcsr," +
                    " sum ( oth_fee ) jcsr," +
                    " sum ( req_fee ) ghsr " +
                    "FROM mzy_reqrec " +
                    " where 1=1 " +
                    "<when test='userIdCode!=null'>",
            " and op_id=#{userIdCode} ",
            "</when>",
            "<when test='dcountDate!=null'>",
            " and closing_date=#{dcountDate,jdbcType=TIMESTAMP} ",
            "</when>",
            "<when test='beginDate!=null'>",
            " and CONVERT(varchar(100), closing_date, 20) &gt;=#{beginDate,jdbcType=TIMESTAMP} ",
            "</when>",
            "<when test='endDate!=null'>",
            " and CONVERT(varchar(100), closing_date, 20) &lt;=#{endDate,jdbcType=TIMESTAMP} ",
            "</when>",
            "<when test='dcountDate == null and beginDate == null and endDate == null'>",
            " and dcount_no =0 ",
            "</when>",
            "</script>"})
    List<ReqrecVo> selectMzReceiptSerialCount(@Param("userIdCode") String userIdCode, @Param("dcountDate") Date dcountDate, @Param("beginDate") Date beginDate, @Param("endDate") Date endDate);


    /**
     * 查询退号明细
     *
     * @param userIdCode
     * @param dcountDate
     * @return
     */
    @Select({
            "<script>",
            "SELECT " +
                    "receipt_bill= rtrim(mzy_reqrec.receipt_bill)," +
                    "         rtrim(mzy_reqrec.name) name," +
                    "         mzy_reqrec.req_fee+mzy_reqrec.oth_fee+mzy_reqrec.clinic_fee  as total_charge " +
                    "FROM mzy_reqrec " +
                    "where cancel_mark <![CDATA[<>]]> '0'  and times <![CDATA[<]]> 0 " +
                    "<when test='userIdCode!=null'>",
            " and op_id=#{userIdCode} ",
            "</when>",
            "<when test='dcountDate!=null'>",
            " and closing_date=#{dcountDate,jdbcType=TIMESTAMP} ",
            "</when>",
            "<when test='dcountDate == null'>",
            " and dcount_no =0 ",
            "</when>",
            "</script>"})
    List<ReceiptVo> selectThMzReceiptSerial(@Param("userIdCode") String userIdCode, @Param("dcountDate") Date dcountDate);

    /**
     * 判断是否有退费后没重收的数据
     *
     * @param userIdCode
     * @return
     */
    @Select("select count ( *) " +
            "from mz_charge_detail " +
            "where cash_id =#{userIdCode} " +
            "and isnull ( pay_mark , '5' ) ='5' " +
            "and isnull ( print_flag_yj , '' ) ='1' " +
            "and isnull ( bill_item_code , '' )  <> '020' ")
    Integer selectTFWDFPCount(@Param("userIdCode") String userIdCode);

    /**
     * 查询是否有可以日结的收费数据
     *
     * @param userIdCode
     * @return
     */
    @Select("select count(1) from mz_receipt_serial where operator_id =#{userIdCode} and dcount_no =0 ")
    Integer selectChargeCountCanDaily(@Param("userIdCode") String userIdCode);


    /**
     * 查询是否有可以日结的挂号数据
     *
     * @param userIdCode
     * @return
     */
    @Select("select count(1) from mzy_reqrec where op_id =#{userIdCode} and dcount_no =0 ")
    Integer selectRequecCountCanDaily(@Param("userIdCode") String userIdCode);

    /**
     * 查询总计金额
     *
     * @param userIdCode
     * @return
     */
    @Select("select sum ( total_charge ) from mz_receipt_serial where operator_id =#{userIdCode} and dcount_no =0 ")
    BigDecimal selectTotalCharge(@Param("userIdCode") String userIdCode);

    /**
     * 发票表结算  所用发票号码
     *
     * @param userIdCode
     * @param dcountDate
     * @return
     */
    @Update("update mz_receipt_serial set dcount_no =1 , dcount_date =#{dcountDate} where operator_id =#{userIdCode} and dcount_no =0 ")
    int dcountReceipt(@Param("userIdCode") String userIdCode, @Param("dcountDate") Date dcountDate);


    /**
     * 发票表结算  各种费用结算
     *
     * @param userIdCode
     * @param dcountDate
     * @return
     */
    @Update("update mz_receipt_serial set charge_dcount_no =1 , charge_dcount_date =#{dcountDate} where pay_id =#{userIdCode} and (charge_dcount_no =0 or charge_dcount_no is null) ")
    int dcountReceiptChargeCount(@Param("userIdCode") String userIdCode, @Param("dcountDate") Date dcountDate);

    /**
     * 收费方式表结算
     *
     * @param userIdCode
     * @param dcountDate
     * @return
     */
    @Update("update mz_deposit_file set dcount_no =1 , dcount_date =#{dcountDate} where op_id =#{userIdCode} and dcount_no =0")
    int dcountDeposit(@Param("userIdCode") String userIdCode, @Param("dcountDate") Date dcountDate);

    /**
     * 挂号表收费结算
     *
     * @param userIdCode
     * @param dcountDate
     * @return
     */
    @Update("update mzy_reqrec set dcount_no =1 , closing_date =#{dcountDate} where op_id =#{userIdCode} and dcount_no =0 ")
    int dcountReqrec(@Param("userIdCode") String userIdCode, @Param("dcountDate") Date dcountDate);


    /**
     * 挂号表打印发票结算
     *
     * @param userIdCode
     * @param dcountDate
     * @return
     */
    @Update("update mzy_reqrec set print_dcount_no =1 , print_closing_date =#{dcountDate} where printer_id =#{userIdCode} and print_dcount_no =0 ")
    int dcountReqrecReceipt(@Param("userIdCode") String userIdCode, @Param("dcountDate") Date dcountDate);

    /**
     * 在发票表中查询当前日期范围内有收款记录的收费员
     *
     * @param beginDate
     * @param endDate
     * @return
     */
    @Select("select operator_id from  mz_receipt_serial where CONVERT(varchar(100), dcount_date, 20) >=#{beginDate,jdbcType=TIMESTAMP} and CONVERT(varchar(100), dcount_date, 20) <=#{endDate,jdbcType=TIMESTAMP} group by operator_id")
    List<String> selectAllOperatorFromReceipt(@Param("beginDate") Date beginDate, @Param("endDate") Date endDate);

    /**
     * 在挂号表中查询当前日期范围内有收款记录的收费员
     *
     * @param beginDate
     * @param endDate
     * @return
     */
    @Select("select op_id from  mzy_reqrec where CONVERT(varchar(100), closing_date, 20) >=#{beginDate,jdbcType=TIMESTAMP} and CONVERT(varchar(100), closing_date, 20) <=#{endDate,jdbcType=TIMESTAMP} group by op_id")
    List<String> selectAllOperatorFromReqrec(@Param("beginDate") Date beginDate, @Param("endDate") Date endDate);

    /**
     * 挂号工作量一览表
     *
     * @param beginDate
     * @param endDate
     * @return
     */
    @Select({"<script>" +
            "    SELECT b.op_id," +
            "<when test='orderByDcountDate!=null'>" +
            "           closing_date = b.closing_date," +
            "</when>" +
            "           pt_count =count((case when  b.cancel_mark = '0' or (b.cancel_mark = '1' and times <![CDATA[>]]> 0 ) then b.patient_id else null end)), " +
            "           th_count =count(case when b.cancel_mark='1' and times <![CDATA[<]]> 0 then b.patient_id else null end),            " +
            "           pt_amount =sum((case  when b.cancel_mark = '0' or ( b.cancel_mark = '1' and times <![CDATA[>]]> 0)  then  b.req_fee + b.clinic_fee + b.oth_fee + isnull(b.bl_fee,0) + isnull(b.ck_fee,0) else 0 end))," +
            "           th_amount =sum(case when b.cancel_mark='1' and times <![CDATA[<]]> 0  then b.req_fee + b.clinic_fee + b.oth_fee + isnull(b.bl_fee,0) + isnull(b.ck_fee,0) else 0 end)," +
            "           total_amount=sum((case  when b.cancel_mark in ('0','1') then  b.req_fee + b.clinic_fee + b.oth_fee + isnull(b.bl_fee,0) + isnull(b.ck_fee,0) else 0 end)) " +
            "    FROM ${tableName}   b " +
            "   WHERE " +
            "         (case when 1=0 then CONVERT(varchar(100), b.op_day, 20) else CONVERT(varchar(100), b.closing_date, 20) end) <![CDATA[>=]]> #{beginDate,jdbcType=TIMESTAMP} and " +
            "         (case when 1=0 then CONVERT(varchar(100), b.op_day, 20) else CONVERT(varchar(100), b.closing_date, 20) end) <![CDATA[<=]]> #{endDate,jdbcType=TIMESTAMP}  " +
            "<when test='cashId!=null'>" +
            " and b.op_id =#{cashId}" +
            "</when>" +
            " and b.unit_code <![CDATA[<>]]> '0000000' " +
            "group by b.op_id " +
            "<when test='orderByDcountDate!=null'>" +
            ",b.closing_date " +
            "</when>" +
            "order by b.op_id " +
            "</script>"})
    List<WorkloadVo> selectGhWorkloadVo(@Param("tableName") String tableName, @Param("beginDate") Date beginDate, @Param("endDate") Date endDate, @Param("cashId") String cashId,@Param("orderByDcountDate") YesNoEnum orderByDcountDate);

    /**
     * 门诊收费一览表
     *
     * @param beginDate
     * @param endDate
     * @return
     */
    @Select({"<script>" +
            "select op_id," +
            "<when test='orderByDcountDate!=null'>" +
            "       dcount_date closing_date," +
            "</when>" +
            "       pt_count=count(distinct case when pay_mark='0' then receipt_sn else null end)," +
            "       pt_amount=sum(case when pay_mark='0' then amount else 0 end)," +
            "       th_count=count(distinct case when pay_mark='1' then receipt_sn else null end)," +
            "       th_amount=sum(case when pay_mark='1' then amount else 0 end)," +
            "       total_amount=sum(case when pay_mark in ('0','1') then amount else 0 end) " +
            " from " +
            " ${tableName} " +
            " where CONVERT(varchar(100),dcount_date, 20) <![CDATA[>=]]> #{beginDate} and " +
            "      CONVERT(varchar(100),dcount_date, 20) <![CDATA[<=]]> #{endDate}  and pay_mark <![CDATA[<>]]> 2 " +
            "<when test='cashId!=null'>" +
            " and op_id =#{cashId} " +
            "</when>" +
            " group by op_id " +
            "<when test='orderByDcountDate!=null'>" +
            ",dcount_date " +
            "</when>" +
            "order by op_id " +
            "</script>"})
    List<WorkloadVo> selectSfWorkloadVo(@Param("tableName") String tableName, @Param("beginDate") Date beginDate, @Param("endDate") Date endDate, @Param("cashId") String cashId,@Param("orderByDcountDate") YesNoEnum orderByDcountDate);


    /**
     * 门诊收据分类统计表
     *
     * @param beginDate
     * @param endDate
     * @return
     */
    @Select("  SELECT parent=max(d.parent_code), " +
            "         total_charge=isnull(sum(round(convert(decimal(18,5),((convert(decimal(18,5),unit_price)) * (quantity * isnull(drug_win,1)))),2)),0)," +
            "         audit_code=max(d.name), " +
            "         amount = count(distinct a.patient_id + cast(a.times as char) + cast(a.receipt_no as char) + cast(a.order_no as char) + cast(isnull(order_bill,100) as char)) " +
            "    FROM ${tableName} a, " +
            "         (select distinct patient_id,times,receipt_no, serial_no,receipt_sn from ${tableName1} " +
            "          where (case '1' when '0' then charge_date " +
            "                 else CONVERT(varchar(100), dcount_date, 20) end) >= #{beginDate,jdbcType=TIMESTAMP} and  " +
            "                (case '1' when '0' then charge_date " +
            "                 else CONVERT(varchar(100), dcount_date, 20) end) <= #{endDate,jdbcType=TIMESTAMP} and " +
            "                (case '1' when '0' then isnull(dcount_no,0) " +
            "                 else 1 end) = isnull(dcount_no,0) ) as b, " +
            "         zd_charge_item c, " +
            "         zy_zd_audit_item d " +
            "   WHERE ( a.patient_id = b.patient_id )    and  " +
            "           a.times=b.times and  " +
            "           a.receipt_no=b.receipt_no and " +
            "           a.real_no=b.receipt_sn and  " +
            "           case a.charge_bill_code when 'BILL10' then 'BILL01' when 'BILL11' then 'BILL28' when 'BILL12' then 'BILL02' else a.charge_bill_code end*=c.code and " +
            "           isnull(c.audit_code,'81')=d.code and " +
            "           a.serial_no=b.serial_no and " +
            "            a.bill_item_code<>'TC' " +
            "group by c.audit_code " +
            "order by c.audit_code  ")
    List<ChargeDetailVo> selectChargeDetailVos(@Param("tableName") String tableName, @Param("tableName1") String tableName1, @Param("beginDate") Date beginDate, @Param("endDate") Date endDate);


    /**
     * 数据清洗，将在老系统日结的普通收费员数据清洗一次
     *
     * @return
     */
    @Update("update mz_receipt_serial set pay_id=operator_id,charge_dcount_date=dcount_date,charge_dcount_no=dcount_no  where operator_id =#{userIdCode}  and dcount_date=#{dcountDate}  and (receipt_no>0  and (pay_id is null or pay_id ='')  or receipt_no<0 ) ")
    int refeshNomalPayManData(@Param("userIdCode") String userIdCode, @Param("dcountDate") Date dcountDate);


    /**
     * 刷新退费产生的新数据
     *
     * @return
     */
    @Update("update mz_receipt_serial set pay_id=operator_id,charge_dcount_date=dcount_date,charge_dcount_no=dcount_no  where operator_id =#{userIdCode}   and dcount_date=#{dcountDate} and receipt_no>0 and isnull(cheque_type,1) not in ('Y','E','F','W','M','Z') and pay_id='99999' ")
    int refeshRefundNewData(@Param("userIdCode") String userIdCode, @Param("dcountDate") Date dcountDate);


    /**
     * 查询发票表和收费表中不一致的数据
     *
     * @param beginDate
     * @param endDate
     * @return
     */
    @Select("SELECT serial_no FROM mz_receipt_serial where 1=1 and pay_mark <> 2 and charge_dcount_date >=#{beginDate} and charge_dcount_date <=#{endDate} and serial_no not in ( " +
            "select serial_no from mz_deposit_file where 1=1 and pay_mark <> 2 and dcount_date >=#{beginDate} and dcount_date <=#{endDate})  union " +
            "select serial_no from mz_deposit_file where 1=1 and pay_mark <> 2 and dcount_date >=#{beginDate} and dcount_date <=#{endDate} and serial_no not in ( " +
            " SELECT serial_no FROM mz_receipt_serial where 1=1 and pay_mark <> 2 and charge_dcount_date >=#{beginDate} and charge_dcount_date <=#{endDate} " +
            ")")
    List<Integer> selectInconformityData(@Param("beginDate") Date beginDate, @Param("endDate") Date endDate);

    /**
     * 清洗payId
     *
     * @return
     */
    @Update("update mz_receipt_serial set pay_id ='99999' where operator_id='99999' and pay_id=''")
    int setPayId();

    /**
     * 清洗急症记账数据
     *
     * @return
     */
    @Update("update mzy_reqrec set paymode='Y' where  paymode is null and charge_type  in (19,20,21)")
    int refeshJZReqrecData();

    /**
     * 清洗老系统收费方式表退费方式
     *
     * @return
     */
    @Update("update mz_deposit_file set cheque_type='1' where pay_mark = 1 and op_id <> '99999' and cheque_type in ('W','M') ")
    int refeshDepositFile();

    /**
     * 清洗老系统发票表退费方式
     *
     * @return
     */
    @Update("update mz_receipt_serial set cheque_type='1' where pay_mark = 1 and pay_id <> '99999' and cheque_type in ('W','M') ")
    int refeshreceiptSerial();

    /**
     * 查询泰和医院门诊明细收入
     *
     * @param thmzmxsrParamsVo
     * @return
     */
    @Select({"<script>" +
            "select xm,patient_id,jz_sq_dept,jz_sq_doctor,exec_dept,xmm,hsm,je,charge_date,dcount_date,quty,price,parent_code " +
            " from (select xm=a.name,  " +
            "   a.patient_id,  " +
            " jz_sq_dept=a.warn_dept,  " +
            " jz_sq_doctor = a.doctor_code,  " +
            " exec_dept=a.exec_dept,  " +
            " xmm=isnull((select name from zd_charge_item where code = a.charge_item_code)," +
            " (select name from yp_zd_dict where code = a.charge_item_code and serial = a.serial )),  " +
            " hsm=isnull((select bb.name from zd_charge_item aa,zy_zd_audit_item bb where aa.audit_code = bb.code and aa.code = a.charge_item_code)," +
            " (select dd.name from yp_zd_dict cc, yp_zd_drug_kind dd where cc.drug_kind = dd.code and cc.code = a.charge_item_code and cc.serial = a.serial)),  " +
            " je=round(CAST(quantity * ( case when a.group_no =81 and round(unit_price,2) = 0.00 then 0.01 else round(unit_price,2) end ) * isnull(drug_win,1) AS NUMERIC ( 10, 3 )),2),  " +
            " c.charge_date,  " +
            " dcount_date=c.charge_dcount_date,   " +
            "      quty = a.quantity, " +
            "      price = ( case when a.group_no =81 and round(a.unit_price,2) = 0.00 then 0.01 else round(a.unit_price,2) end ), " +
            " e.parent_code " +
            "  from   ${firstTableName} a ," +
            "  (select distinct patient_id,times,receipt_no, serial_no,receipt_sn,charge_date,charge_dcount_date from ${secondTableName} where fp_flag <![CDATA[<>]]> 'Y' or fp_flag is null ) c," +
            "  zd_charge_item d," +
            "  zy_zd_audit_item  e " +
            "   where " +
            "    a.patient_id = c.patient_id " +
            " and  a.times = c.times " +
            "   and a.receipt_no = c.receipt_no " +
            "  and  a.serial_no = c.serial_no  " +
            "    and         a.real_no = c.receipt_sn  " +
            "  and         case a.charge_bill_code " +
            "  when 'BILL10' then 'BILL01' when 'BILL11' then 'BILL28' when 'BILL12' then 'BILL02' else a.charge_bill_code end *=d.code " +
            "  and         isnull(d.audit_code,e.code)=e.code) mzsr  where mzsr.je <![CDATA[<>]]> 0 " +
            "  and  case '1' when #{caseType} then mzsr.charge_date else  CONVERT(varchar(100), mzsr.dcount_date, 20) end <![CDATA[>=]]>#{beginDate,jdbcType=TIMESTAMP} " +
            "  and  case '1' when #{caseType} then mzsr.charge_date else  CONVERT(varchar(100), mzsr.dcount_date, 20) end <![CDATA[<]]>#{endDate,jdbcType=TIMESTAMP} " +
            "<when test='patientIds!=null'>" +
            " and mzsr.patient_id in" +
            "<foreach item='item' index='index' collection='patientIds' open='(' separator=',' close=')'>" +
            "#{item}" +
            "</foreach>" +
            "</when>" +
            "<when test='jzSqDept!=null'>" +
            " and mzsr.jz_sq_dept =#{jzSqDept}" +
            "</when>" +
            "<when test='jzSqDoctor!=null'>" +
            " and mzsr.jz_sq_doctor =#{jzSqDoctor}" +
            "</when>" +
            "<when test='execDept!=null'>" +
            " and mzsr.exec_dept =#{execDept}" +
            "</when>" +
            "<when test='hsm!=null'>" +
            " and mzsr.hsm =#{hsm}" +
            "</when>" +
            "<when test='parentCode!=null'>" +
            " and mzsr.parent_code =#{parentCode}" +
            "</when>" +
            "<when test='xmm!=null'>" +
            " and mzsr.xmm =#{xmm}" +
            "</when>"
            + "</script>"})
    List<Mzmxsr> selectThmzmxsr(ThmzmxsrParamsVo thmzmxsrParamsVo);


    /**
     * 统计选定日期内费用总额 用于判断是否需要插入收费明细
     * @param thmzmxsrParamsVo
     * @return
     */
    @Select("select patient_id,SUM(total_charge) total_charge from mz_receipt_serial where CONVERT(varchar(100), charge_dcount_date, 20) >=#{beginDate,jdbcType=TIMESTAMP} and  CONVERT(varchar(100), charge_dcount_date, 20) <#{endDate,jdbcType=TIMESTAMP}  GROUP BY patient_id order by patient_id desc ")
    List<Map<String,Object>> selectSumAmount(ThmzmxsrParamsVo thmzmxsrParamsVo);

}
