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

import cn.hnthyy.thmz.entity.his.mz.MzChargeDetail;
import cn.hnthyy.thmz.pageDto.MzChargeDetailPageDto;
import cn.hnthyy.thmz.vo.ThmzmxsrParamsVo;
import org.apache.ibatis.annotations.*;

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

/**
 * 门诊收费记录表的数据层
 */
public interface MzChargeDetailMapper {
    /**
     * 统计符合条件的费用信息
     *
     * @param mzChargeDetailPageDto
     * @return
     */
    @Select({"<script>",
            "select count(*) from (",
            "select patient_id from ${tableName}  where bill_item_code <![CDATA[<>]]> 'TC'  ",
            "<when test='mzChargeDetail.payMark!=null'>",
            " and pay_mark =#{mzChargeDetail.payMark,jdbcType=CHAR}",
            "</when>",
            "<when test='mzChargeDetail.payMark!=null and mzChargeDetail.payMark eq 0'>",
            " and  confirm_flag <![CDATA[<>]]> 4 ",
            "</when>",
            "<when test='mzChargeDetail.patientId!=null'>",
            " and patient_id =#{mzChargeDetail.patientId,jdbcType=CHAR}",
            "</when>",
            "<when test='mzChargeDetail.times!=null'>",
            " and times =#{mzChargeDetail.times,jdbcType=INTEGER}",
            "</when>",
            "<when test='mzChargeDetail.receiptNo!=null'>",
            " and receipt_no =#{mzChargeDetail.receiptNo,jdbcType=INTEGER}",
            "</when>",
            "<when test='mzChargeDetail.name!=null'>",
            " and name like #{mzChargeDetail.name,jdbcType=CHAR}",
            "</when>",
            "<when test='mzChargeDetail.warnDept!=null'>",
            " and warn_dept = #{mzChargeDetail.warnDept,jdbcType=CHAR}",
            "</when>",
            "<when test='mzChargeDetail.doctorCode!=null'>",
            " and doctor_code =#{mzChargeDetail.doctorCode,jdbcType=CHAR}",
            "</when>",
            "<when test='opId!=null and mzChargeDetail.payMark eq 0 '>",
            " and cash_id =#{opId,jdbcType=CHAR}",
            "</when>",
            "<when test='opId!=null and mzChargeDetail.payMark eq 1 '>",
            " and input_id =#{opId,jdbcType=CHAR}",
            "</when>",
            "<when test='beginTime!=null and mzChargeDetail.payMark eq 5'>",
            " and price_time &gt;=#{beginTime,jdbcType=TIMESTAMP}",
            "</when>",
            "<when test='endTime!=null and mzChargeDetail.payMark eq 5'>",
            " and price_time &lt;=#{endTime,jdbcType=TIMESTAMP}",
            "</when>",
            "<when test='beginTime!=null and mzChargeDetail.payMark eq 0'>",
            " and charge_date &gt;=#{beginTime,jdbcType=TIMESTAMP}",
            "</when>",
            "<when test='endTime!=null and mzChargeDetail.payMark eq 0'>",
            " and charge_date &lt;=#{endTime,jdbcType=TIMESTAMP}",
            "</when>",
            "<when test='beginTime!=null and mzChargeDetail.payMark eq 1'>",
            " and input_date &gt;=#{beginTime,jdbcType=TIMESTAMP}",
            "</when>",
            "<when test='endTime!=null and mzChargeDetail.payMark eq 1'>",
            " and input_date &lt;=#{endTime,jdbcType=TIMESTAMP}",
            "</when>",
            " group by patient_id,times,receipt_no,name,",
            "<when test='mzChargeDetail.payMark eq 0'>",
            " charge_date,",
            "</when>",
            "<when test='mzChargeDetail.payMark eq 1'>",
            " input_date,",
            "</when>",
            "<when test='mzChargeDetail.payMark eq 5'>",
            " price_time,",
            "</when>",
            "real_no)As mz_charge_detail_page",
            "</script>"})
    int countMzChargeDetail(MzChargeDetailPageDto mzChargeDetailPageDto);

    /**
     * 按照费用类型查询费用明细
     *
     * @param patientId
     * @param times
     * @param payMark
     * @param billItemCode  0 ，1 都是in 2 not in
     * @param billItemTypes
     * @return
     */
    @Select({"<script>",
            "select rtrim(patient_id) patient_id,times,receipt_no,order_no,item_no,rtrim(charge_item_code) charge_item_code,rtrim(bill_item_code) bill_item_code,",
            "quantity,unit_price,rtrim(pay_mark) pay_mark,rtrim(serial) serial,rtrim(charge_item_type) charge_item_type,rtrim(country_flag) country_flag,rtrim(drug_flag) drug_flag,",
            "serial_no,rtrim(name) name,charge_date,confirm_time,rtrim(group_no) group_no,rtrim(confirm_id) confirm_id,drug_win,rtrim(input_id) input_id,input_date,",
            "rtrim(cashier_id) cashier_id,rtrim(windows_no) windows_no,rtrim(price_id) price_id,price_time,rtrim(exec_dept) exec_dept,rtrim(audit_code) audit_code,",
            "rtrim(gongnks_flag) gongnks_flag,rtrim(print_flag) print_flag,confirm_flag,rtrim(warn_dept) warn_dept,rtrim(jz_flag) jz_flag,group_order,rtrim(cash_id) cash_id,",
            "rtrim(doctor_code) doctor_code,rtrim(supply_code) supply_code,rtrim(frequency) frequency,drug_quan,rtrim(drug_unit) drug_unit,rtrim(doctor_flag) doctor_flag,inject_res,inject_times,",
            "rtrim(confirm_zs) confirm_zs,real_no,windows_no_yf,ybbx_flag,tc_no,tc_name,rtrim(detail_flag) detail_flag,gl_percent,rtrim(order_type) order_type,supply_no,instruction_code,",
            "instruction_text,order_days,rtrim(pay_self) pay_self,rtrim(try_flag) try_flag,rtrim(req_yj) req_yj,req_no,rtrim(group_type) group_type,order_bill,charge_bill_code,",
            "rtrim(self_flag) self_flag,rtrim(responce_type) responce_type,dec_amount,back_serial,input_time,cy_mtjs,cy_jsl,cy_zql,cy_fj,cy_ffcs,cy_fysm,supply_amount,rtrim(cy_jssm) cy_jssm,",
            "dept_no,gl_percent_ft,rtrim(yb_zf_flag) yb_zf_flag,orig_price,rtrim(try_result) try_result,rtrim(supply_exec) supply_exec,rtrim(cy_dj) cy_dj,rtrim(receive_flag) receive_flag,",
            "rtrim(chequ_type) chequ_type,rtrim(print_flag_yj) print_flag_yj,rtrim(confirm_flag_qx) confirm_flag_qx ",
            "from dbo.mz_charge_detail where pay_mark=#{payMark,jdbcType=CHAR} and patient_id =#{patientId,jdbcType=CHAR}  and times = #{times,jdbcType=INTEGER} and receipt_no=#{receiptNo,jdbcType=INTEGER}",
            "<choose>",
            "<when test='billItemCode==2'>",
            " and bill_item_code not in",
            "</when>",
            "<otherwise>",
            " and bill_item_code in",
            "</otherwise>",
            " </choose>",
            "<foreach item='item' index='index' collection='billItemTypes' open='(' separator=',' close=')'>",
            "#{item}",
            "</foreach>",
            "</script>"})
    List<MzChargeDetail> selectMzChargeDetailByBillItemCode(@Param("patientId") String patientId, @Param("times") Integer times, @Param("receiptNo") Integer receiptNo, @Param("payMark") String payMark, @Param("billItemCode") String billItemCode, @Param("billItemTypes") List<String> billItemTypes);


    /**
     * 分页查询收费合计信息
     *
     * @param mzChargeDetailPageDto
     * @return
     */
    @Select({"<script>",
            "select top ${pageSize} * FROM (SELECT ROW_NUMBER() OVER (ORDER BY mz_charge_detail_page.",
            "<when test='mzChargeDetail.payMark eq 5'>",
            "price_time",
            "</when>",
            "<when test='mzChargeDetail.payMark eq 0'>",
            "charge_date",
            "</when>",
            "<when test='mzChargeDetail.payMark eq 1'>",
            "input_date",
            "</when>",
            " desc) AS RowNumber,* from  (" +
                    "select  rtrim(patient_id) patient_id,times,receipt_no,rtrim(name) name,",
            "<when test='mzChargeDetail.payMark eq 0'>",
            " charge_date,",
            "</when>",
            "<when test='mzChargeDetail.payMark eq 1'>",
            " input_date,",
            "</when>",
            "<when test='mzChargeDetail.payMark eq 5'>",
            " price_time,",
            "</when>",
            "real_no,sum(( case when convert(decimal(18,2),unit_price)=0.00 then 0.01 else convert(decimal(18,2),unit_price) end )*quantity*drug_win) amount" ,
            ",sum(( case when convert(decimal(18,2),orig_price)=0.00 then 0.01 else convert(decimal(18,2),orig_price) end )*quantity*drug_win) not_discount_amount ",
                    " from ${tableName}  where bill_item_code <![CDATA[<>]]> 'TC' ",
            "<when test='mzChargeDetail.payMark!=null'>",
            " and pay_mark =#{mzChargeDetail.payMark,jdbcType=CHAR}",
            "</when>",
            "<when test='mzChargeDetail.payMark!=null and mzChargeDetail.payMark eq 0'>",
            " and  confirm_flag <![CDATA[<>]]> 4 ",
            "</when>",
            "<when test='mzChargeDetail.patientId!=null'>",
            " and patient_id =#{mzChargeDetail.patientId,jdbcType=CHAR}",
            "</when>",
            "<when test='mzChargeDetail.times!=null'>",
            " and times =#{mzChargeDetail.times,jdbcType=INTEGER}",
            "</when>",
            "<when test='mzChargeDetail.receiptNo!=null'>",
            " and receipt_no =#{mzChargeDetail.receiptNo,jdbcType=INTEGER}",
            "</when>",
            "<when test='mzChargeDetail.name!=null'>",
            " and name like #{mzChargeDetail.name,jdbcType=CHAR}",
            "</when>",
            "<when test='mzChargeDetail.warnDept!=null'>",
            " and warn_dept = #{mzChargeDetail.warnDept,jdbcType=CHAR}",
            "</when>",
            "<when test='mzChargeDetail.doctorCode!=null'>",
            " and doctor_code =#{mzChargeDetail.doctorCode,jdbcType=CHAR}",
            "</when>",
            "<when test='opId!=null and mzChargeDetail.payMark eq 0 '>",
            " and cash_id =#{opId,jdbcType=CHAR}",
            "</when>",
            "<when test='opId!=null and mzChargeDetail.payMark eq 1 '>",
            " and input_id =#{opId,jdbcType=CHAR}",
            "</when>",
            "<when test='beginTime!=null and mzChargeDetail.payMark eq 5'>",
            " and price_time &gt;=#{beginTime,jdbcType=TIMESTAMP}",
            "</when>",
            "<when test='endTime!=null and mzChargeDetail.payMark eq 5'>",
            " and price_time &lt;=#{endTime,jdbcType=TIMESTAMP}",
            "</when>",
            "<when test='beginTime!=null and mzChargeDetail.payMark eq 0'>",
            " and charge_date &gt;=#{beginTime,jdbcType=TIMESTAMP}",
            "</when>",
            "<when test='endTime!=null and mzChargeDetail.payMark eq 0'>",
            " and charge_date &lt;=#{endTime,jdbcType=TIMESTAMP}",
            "</when>",
            "<when test='beginTime!=null and mzChargeDetail.payMark eq 1'>",
            " and input_date &gt;=#{beginTime,jdbcType=TIMESTAMP}",
            "</when>",
            "<when test='endTime!=null and mzChargeDetail.payMark eq 1'>",
            " and input_date &lt;=#{endTime,jdbcType=TIMESTAMP}",
            "</when>",
            " group by patient_id,times,receipt_no,name,",
            "<when test='mzChargeDetail.payMark eq 0'>",
            " charge_date,",
            "</when>",
            "<when test='mzChargeDetail.payMark eq 1'>",
            " input_date,",
            "</when>",
            "<when test='mzChargeDetail.payMark eq 5'>",
            " price_time,",
            "</when>",
            "real_no) As mz_charge_detail_page",
            ") as A WHERE RowNumber >#{pageSize}*#{pageIndex} order by RowNumber asc",
            "</script>"})
    List<MzChargeDetail> selectMzChargeDetailWithPage(MzChargeDetailPageDto mzChargeDetailPageDto);


    /**
     * 查询当前病人已经存在的收费记录（药品记录） 未缴费
     *
     * @param patientId
     * @return
     */
    @Select({"<script>",
            "select rtrim(patient_id) patient_id,times,receipt_no,order_no,item_no,rtrim(charge_item_code) charge_item_code,rtrim(bill_item_code) bill_item_code,",
            "quantity,unit_price,rtrim(pay_mark) pay_mark,rtrim(serial) serial,rtrim(charge_item_type) charge_item_type,rtrim(country_flag) country_flag,rtrim(drug_flag) drug_flag,",
            "serial_no,rtrim(name) name,charge_date,confirm_time,rtrim(group_no) group_no,rtrim(confirm_id) confirm_id,drug_win,rtrim(input_id) input_id,input_date,",
            "rtrim(cashier_id) cashier_id,rtrim(windows_no) windows_no,rtrim(price_id) price_id,price_time,rtrim(exec_dept) exec_dept,rtrim(audit_code) audit_code,",
            "rtrim(gongnks_flag) gongnks_flag,rtrim(print_flag) print_flag,confirm_flag,rtrim(warn_dept) warn_dept,rtrim(jz_flag) jz_flag,group_order,rtrim(cash_id) cash_id,",
            "rtrim(doctor_code) doctor_code,rtrim(supply_code) supply_code,rtrim(frequency) frequency,drug_quan,rtrim(drug_unit) drug_unit,rtrim(doctor_flag) doctor_flag,inject_res,inject_times,",
            "rtrim(confirm_zs) confirm_zs,real_no,windows_no_yf,ybbx_flag,tc_no,tc_name,rtrim(detail_flag) detail_flag,gl_percent,rtrim(order_type) order_type,supply_no,instruction_code,",
            "instruction_text,order_days,rtrim(pay_self) pay_self,rtrim(try_flag) try_flag,rtrim(req_yj) req_yj,req_no,rtrim(group_type) group_type,order_bill,charge_bill_code,",
            "rtrim(self_flag) self_flag,rtrim(responce_type) responce_type,dec_amount,back_serial,input_time,cy_mtjs,cy_jsl,cy_zql,cy_fj,cy_ffcs,cy_fysm,supply_amount,rtrim(cy_jssm) cy_jssm,",
            "dept_no,gl_percent_ft,rtrim(yb_zf_flag) yb_zf_flag,orig_price,rtrim(try_result) try_result,rtrim(supply_exec) supply_exec,rtrim(cy_dj) cy_dj,rtrim(receive_flag) receive_flag,",
            "rtrim(chequ_type) chequ_type,rtrim(print_flag_yj) print_flag_yj,rtrim(confirm_flag_qx) confirm_flag_qx ",
            "from dbo.mz_charge_detail where  patient_id =#{patientId,jdbcType=CHAR} and pay_mark = 5 ",
            "</script>"})
    List<MzChargeDetail> selectNotPayMzChargeDetailByPatientId(@Param("patientId") String patientId);

    /**
     * 删除未缴费收费明细
     *
     * @param patientId
     * @return
     */
    @Delete("delete  from  mz_charge_detail where patient_id =#{patientId,jdbcType=CHAR} and pay_mark = 5 ")
    int deleteNotPayMzChargeDetailByPatientId(@Param("patientId") String patientId);


    /**
     * 查询收费合计信息
     *
     * @param mzChargeDetailPageDto
     * @return
     */
    @Select({"<script>",
            "select  rtrim(patient_id) patient_id,times,receipt_no,rtrim(name) name,price_time," +
                    "receipt_sn,sum(unit_price*quantity) amount from mz_charge_detail  where 1=1 ",
            "<when test='mzChargeDetail.payMark!=null'>",
            " and pay_mark =#{mzChargeDetail.payMark,jdbcType=CHAR}",
            "</when>",
            "<when test='mzChargeDetail.patientId!=null'>",
            " and patient_id =#{mzChargeDetail.patientId,jdbcType=CHAR}",
            "</when>",
            "<when test='mzChargeDetail.name!=null'>",
            " and name like #{mzChargeDetail.name,jdbcType=CHAR}",
            "</when>",
            "<when test='mzChargeDetail.warnDept!=null'>",
            " and warn_dept = #{mzChargeDetail.warnDept,jdbcType=CHAR}",
            "</when>",
            "<when test='mzChargeDetail.doctorCode!=null'>",
            " and doctor_code =#{mzChargeDetail.doctorCode,jdbcType=CHAR}",
            "</when>",
            "<when test='beginTime!=null'>",
            " and price_time &gt;=#{beginTime,jdbcType=TIMESTAMP}",
            "</when>",
            "<when test='endTime!=null'>",
            " and price_time &lt;=#{endTime,jdbcType=TIMESTAMP}",
            "</when>",
            " group by  patient_id,times,receipt_no,name,price_time,receipt_sn",
            "</script>"})
    List<MzChargeDetail> selectMzChargeDetail(MzChargeDetailPageDto mzChargeDetailPageDto);

    /**
     * 查询当前病人的就诊次数对应的最大的处方号
     *
     * @param patientId
     * @param times
     * @return
     */
    @Select("select max(order_no) from mz_charge_detail where  patient_id =#{patientId,jdbcType=CHAR} and times =#{times,jdbcType=INTEGER}")
    Integer selectMaxOrderNo(@Param("patientId") String patientId, @Param("times") Integer times);


    /**
     * 查询当前病人的就诊次数对应的已经存在的收费记录（药品记录） 未缴费
     *
     * @param patientId
     * @param times
     * @return
     */
    @Select({"<script>",
            "select rtrim(patient_id) patient_id,times,receipt_no,order_no,item_no,rtrim(charge_item_code) charge_item_code,rtrim(bill_item_code) bill_item_code,",
            "quantity,unit_price,rtrim(pay_mark) pay_mark,rtrim(serial) serial,rtrim(charge_item_type) charge_item_type,rtrim(country_flag) country_flag,rtrim(drug_flag) drug_flag,",
            "serial_no,rtrim(name) name,charge_date,confirm_time,rtrim(group_no) group_no,rtrim(confirm_id) confirm_id,drug_win,rtrim(input_id) input_id,input_date,",
            "rtrim(cashier_id) cashier_id,rtrim(windows_no) windows_no,rtrim(price_id) price_id,price_time,rtrim(exec_dept) exec_dept,rtrim(audit_code) audit_code,",
            "rtrim(gongnks_flag) gongnks_flag,rtrim(print_flag) print_flag,confirm_flag,rtrim(warn_dept) warn_dept,rtrim(jz_flag) jz_flag,group_order,rtrim(cash_id) cash_id,",
            "rtrim(doctor_code) doctor_code,rtrim(supply_code) supply_code,rtrim(frequency) frequency,drug_quan,rtrim(drug_unit) drug_unit,rtrim(doctor_flag) doctor_flag,inject_res,inject_times,",
            "rtrim(confirm_zs) confirm_zs,real_no,windows_no_yf,ybbx_flag,tc_no,tc_name,rtrim(detail_flag) detail_flag,gl_percent,rtrim(order_type) order_type,supply_no,instruction_code,",
            "instruction_text,order_days,rtrim(pay_self) pay_self,rtrim(try_flag) try_flag,rtrim(req_yj) req_yj,req_no,rtrim(group_type) group_type,order_bill,charge_bill_code,",
            "rtrim(self_flag) self_flag,rtrim(responce_type) responce_type,dec_amount,back_serial,input_time,cy_mtjs,cy_jsl,cy_zql,cy_fj,cy_ffcs,cy_fysm,supply_amount,rtrim(cy_jssm) cy_jssm,",
            "dept_no,gl_percent_ft,rtrim(yb_zf_flag) yb_zf_flag,orig_price,rtrim(try_result) try_result,rtrim(supply_exec) supply_exec,rtrim(cy_dj) cy_dj,rtrim(receive_flag) receive_flag,",
            "rtrim(chequ_type) chequ_type,rtrim(print_flag_yj) print_flag_yj,rtrim(confirm_flag_qx) confirm_flag_qx ",
            "from dbo.mz_charge_detail where  patient_id =#{patientId,jdbcType=CHAR} and times =#{times,jdbcType=INTEGER}" +
                    "<choose>" +
                    "<when test='payMark!=null and payMark ==0 '>",
            " and pay_mark=#{payMark,jdbcType=CHAR}  and confirm_flag <![CDATA[<>]]> 4 ",
            "</when>",
            "<otherwise>" +
                    " and pay_mark=#{payMark,jdbcType=CHAR} " +
                    "</otherwise>" +
                    "</choose>" +
                    "<when test='receiptNo!=null'>",
            " and receipt_no=#{receiptNo} ",
            "</when>",
                    "order by order_no desc",
            "</script>"})
    List<MzChargeDetail> selectMzChargeDetailByPatientId(@Param("patientId") String patientId, @Param("times") Integer times,@Param("receiptNo") Integer receiptNo, @Param("payMark") String payMark);

    /**
     * 根据当期那病人与就诊次数以及收费编码查询对应的药品数量与退药量
     *
     * @param patientId
     * @param times
     * @param chargeItemCode
     * @return
     */
    @Select("select * from mz_charge_detail where patient_id =#{patientId,jdbcType=CHAR} and times =#{times,jdbcType=INTEGER} and charge_item_code=#{chargeItemCode,jdbcType=CHAR} and order_no =#{orderNo} and quantity>0")
    List<MzChargeDetail> selectMzChargeDetailByChargeItemCode(@Param("patientId") String patientId, @Param("times") Integer times, @Param("chargeItemCode") String chargeItemCode, @Param("orderNo") Integer orderNo);

    /**
     * 新增收费明细记录
     *
     * @param mzChargeDetail
     * @return
     */
    @Insert("INSERT INTO  mz_charge_detail(patient_id,times,receipt_no,order_no,item_no,charge_item_code,bill_item_code,quantity,unit_price,pay_mark,serial,charge_item_type," +
            "country_flag,drug_flag,serial_no,name,charge_date,confirm_time,group_no,confirm_id,drug_win,input_id,input_date,cashier_id,windows_no,price_id,price_time,exec_dept," +
            "audit_code,gongnks_flag,print_flag,confirm_flag,warn_dept,jz_flag,group_order,cash_id,doctor_code,supply_code,frequency,drug_quan,drug_unit,doctor_flag,inject_res,inject_times," +
            "confirm_zs,real_no,windows_no_yf,ybbx_flag,tc_no,tc_name,detail_flag,gl_percent,order_type,supply_no,instruction_code,instruction_text,order_days,pay_self,try_flag,req_yj," +
            "req_no,group_type,order_bill,charge_bill_code,self_flag,responce_type,dec_amount,back_serial,input_time,cy_mtjs,cy_jsl,cy_zql,cy_fj,cy_ffcs,cy_fysm,supply_amount,cy_jssm," +
            "dept_no,gl_percent_ft,yb_zf_flag,orig_price,try_result,supply_exec,cy_dj,receive_flag,chequ_type,print_flag_yj,confirm_flag_qx) " +
            " VALUES (#{patientId,jdbcType=CHAR},#{times,jdbcType=INTEGER},#{receiptNo,jdbcType=SMALLINT},#{orderNo,jdbcType=TINYINT},#{itemNo,jdbcType=TINYINT}," +
            "#{chargeItemCode,jdbcType=CHAR},#{billItemCode,jdbcType=CHAR},#{quantity,jdbcType=DOUBLE},#{unitPriceStr,jdbcType=DECIMAL},#{payMark,jdbcType=CHAR}," +
            "#{serial,jdbcType=CHAR},#{chargeItemType,jdbcType=CHAR},#{countryFlag,jdbcType=CHAR},#{drugFlag,jdbcType=CHAR},#{serialNo,jdbcType=INTEGER},#{name,jdbcType=CHAR}," +
            "#{chargeDate,jdbcType=TIMESTAMP},#{confirmTime,jdbcType=TIMESTAMP},#{groupNo,jdbcType=CHAR},#{confirmId,jdbcType=CHAR},#{drugWin,jdbcType=TINYINT},#{inputId,jdbcType=CHAR}," +
            "#{inputDate,jdbcType=TIMESTAMP},#{cashierId,jdbcType=CHAR},#{windowsNo,jdbcType=CHAR},#{priceId,jdbcType=CHAR},#{priceTime,jdbcType=TIMESTAMP},#{execDept,jdbcType=CHAR}," +
            "#{auditCode,jdbcType=CHAR},#{gongnksFlag,jdbcType=CHAR},#{printFlag,jdbcType=CHAR},#{confirmFlag,jdbcType=INTEGER},#{warnDept,jdbcType=CHAR},#{jzFlag,jdbcType=CHAR}," +
            "#{groupOrder,jdbcType=SMALLINT},#{cashId,jdbcType=CHAR},#{doctorCode,jdbcType=CHAR},#{supplyCode,jdbcType=CHAR},#{frequency,jdbcType=CHAR},#{drugQuan,jdbcType=DOUBLE}," +
            "#{drugUnit,jdbcType=CHAR},#{doctorFlag,jdbcType=CHAR},#{injectRes,jdbcType=DOUBLE},#{injectTimes,jdbcType=SMALLINT},#{confirmZs,jdbcType=CHAR},#{realNo,jdbcType=INTEGER}," +
            "#{windowsNoYf,jdbcType=VARCHAR},#{ybbxFlag,jdbcType=VARCHAR},#{tcNo,jdbcType=VARCHAR},#{tcName,jdbcType=VARCHAR},#{detailFlag,jdbcType=CHAR},#{glPercent,jdbcType=DECIMAL}," +
            "#{orderType,jdbcType=CHAR},#{supplyNo,jdbcType=VARCHAR},#{instructionCode,jdbcType=VARCHAR},#{instructionText,jdbcType=VARCHAR},#{orderDays,jdbcType=SMALLINT}," +
            "#{paySelf,jdbcType=CHAR},#{tryFlag,jdbcType=CHAR},#{reqYj,jdbcType=CHAR},#{reqNo,jdbcType=INTEGER},#{groupType,jdbcType=CHAR},#{orderBill,jdbcType=SMALLINT}," +
            "#{chargeBillCode,jdbcType=VARCHAR},#{selfFlag,jdbcType=CHAR},#{responceType,jdbcType=CHAR},#{decAmount,jdbcType=DECIMAL},#{backSerial,jdbcType=INTEGER},#{inputTime,jdbcType=TIMESTAMP}," +
            "#{cyMtjs,jdbcType=SMALLINT},#{cyJsl,jdbcType=SMALLINT},#{cyZql,jdbcType=SMALLINT},#{cyFj,jdbcType=SMALLINT},#{cyFfcs,jdbcType=SMALLINT},#{cyFysm,jdbcType=SMALLINT}," +
            "#{supplyAmount,jdbcType=SMALLINT},#{cyJssm,jdbcType=CHAR},#{deptNo,jdbcType=VARCHAR},#{glPercentFt,jdbcType=DECIMAL},#{ybZfFlag,jdbcType=CHAR},#{origPriceStr,jdbcType=DECIMAL}," +
            "#{tryResult,jdbcType=CHAR},#{supplyExec,jdbcType=CHAR},#{cyDj,jdbcType=CHAR},#{receiveFlag,jdbcType=CHAR},#{chequType,jdbcType=CHAR},#{printFlagYj,jdbcType=CHAR},#{confirmFlagQx,jdbcType=CHAR}) ")
    int insertMzChargeDetail(MzChargeDetail mzChargeDetail);


    /**
     * 批量保存门诊收费明细记录
     *
     * @param mzChargeDetails
     * @return
     */
    @Insert({
            "<script>",
            "INSERT INTO  mz_charge_detail(patient_id,times,receipt_no,order_no,item_no,charge_item_code,bill_item_code,quantity,unit_price,pay_mark,serial,charge_item_type,",
            "country_flag,drug_flag,serial_no,name,charge_date,confirm_time,group_no,confirm_id,drug_win,input_id,input_date,cashier_id,windows_no,price_id,price_time,exec_dept,",
            "audit_code,gongnks_flag,print_flag,confirm_flag,warn_dept,jz_flag,group_order,cash_id,doctor_code,supply_code,frequency,drug_quan,drug_unit,doctor_flag,inject_res,inject_times,",
            "confirm_zs,real_no,windows_no_yf,ybbx_flag,tc_no,tc_name,detail_flag,gl_percent,order_type,supply_no,instruction_code,instruction_text,order_days,pay_self,try_flag,req_yj,",
            "req_no,group_type,order_bill,charge_bill_code,self_flag,responce_type,dec_amount,back_serial,input_time,cy_mtjs,cy_jsl,cy_zql,cy_fj,cy_ffcs,cy_fysm,supply_amount,cy_jssm,",
            "dept_no,gl_percent_ft,yb_zf_flag,orig_price,try_result,supply_exec,cy_dj,receive_flag,chequ_type,print_flag_yj,confirm_flag_qx) VALUES ",
            "<foreach collection='mzChargeDetails' item='item' index='index' separator=','>",
            " (#{item.patientId,jdbcType=CHAR},#{item.times,jdbcType=INTEGER},#{item.receiptNo,jdbcType=SMALLINT},#{item.orderNo,jdbcType=TINYINT},#{item.itemNo,jdbcType=TINYINT},",
            "#{item.chargeItemCode,jdbcType=CHAR},#{item.billItemCode,jdbcType=CHAR},#{item.quantity,jdbcType=DOUBLE},#{item.unitPriceStr,jdbcType=DECIMAL},#{item.payMark,jdbcType=CHAR},",
            "#{item.serial,jdbcType=CHAR},#{item.chargeItemType,jdbcType=CHAR},#{item.countryFlag,jdbcType=CHAR},#{item.drugFlag,jdbcType=CHAR},#{item.serialNo,jdbcType=INTEGER},#{item.name,jdbcType=CHAR},",
            "#{item.chargeDate,jdbcType=TIMESTAMP},#{item.confirmTime,jdbcType=TIMESTAMP},#{item.groupNo,jdbcType=CHAR},#{item.confirmId,jdbcType=CHAR},#{item.drugWin,jdbcType=TINYINT},#{item.inputId,jdbcType=CHAR},",
            "#{item.inputDate,jdbcType=TIMESTAMP},#{item.cashierId,jdbcType=CHAR},#{item.windowsNo,jdbcType=CHAR},#{item.priceId,jdbcType=CHAR},#{item.priceTime,jdbcType=TIMESTAMP},#{item.execDept,jdbcType=CHAR},",
            "#{item.auditCode,jdbcType=CHAR},#{item.gongnksFlag,jdbcType=CHAR},#{item.printFlag,jdbcType=CHAR},#{item.confirmFlag,jdbcType=INTEGER},#{item.warnDept,jdbcType=CHAR},#{item.jzFlag,jdbcType=CHAR},",
            "#{item.groupOrder,jdbcType=SMALLINT},#{item.cashId,jdbcType=CHAR},#{item.doctorCode,jdbcType=CHAR},#{item.supplyCode,jdbcType=CHAR},#{item.frequency,jdbcType=CHAR},#{item.drugQuan,jdbcType=DOUBLE},",
            "#{item.drugUnit,jdbcType=CHAR},#{item.doctorFlag,jdbcType=CHAR},#{item.injectRes,jdbcType=DOUBLE},#{item.injectTimes,jdbcType=SMALLINT},#{item.confirmZs,jdbcType=CHAR},#{item.realNo,jdbcType=INTEGER},",
            "#{item.windowsNoYf,jdbcType=VARCHAR},#{item.ybbxFlag,jdbcType=VARCHAR},#{item.tcNo,jdbcType=VARCHAR},#{item.tcName,jdbcType=VARCHAR},#{item.detailFlag,jdbcType=CHAR},#{item.glPercent,jdbcType=DECIMAL},",
            "#{item.orderType,jdbcType=CHAR},#{item.supplyNo,jdbcType=VARCHAR},#{item.instructionCode,jdbcType=VARCHAR},#{item.instructionText,jdbcType=VARCHAR},#{item.orderDays,jdbcType=SMALLINT},",
            "#{item.paySelf,jdbcType=CHAR},#{item.tryFlag,jdbcType=CHAR},#{item.reqYj,jdbcType=CHAR},#{item.reqNo,jdbcType=INTEGER},#{item.groupType,jdbcType=CHAR},#{item.orderBill,jdbcType=SMALLINT},",
            "#{item.chargeBillCode,jdbcType=VARCHAR},#{item.selfFlag,jdbcType=CHAR},#{item.responceType,jdbcType=CHAR},#{item.decAmount,jdbcType=DECIMAL},#{item.backSerial,jdbcType=INTEGER},#{item.inputTime,jdbcType=TIMESTAMP},",
            "#{item.cyMtjs,jdbcType=SMALLINT},#{item.cyJsl,jdbcType=SMALLINT},#{item.cyZql,jdbcType=SMALLINT},#{item.cyFj,jdbcType=SMALLINT},#{item.cyFfcs,jdbcType=SMALLINT},#{item.cyFysm,jdbcType=SMALLINT},",
            "#{item.supplyAmount,jdbcType=SMALLINT},#{item.cyJssm,jdbcType=CHAR},#{item.deptNo,jdbcType=VARCHAR},#{item.glPercentFt,jdbcType=DECIMAL},#{item.ybZfFlag,jdbcType=CHAR},#{item.origPriceStr,jdbcType=DECIMAL},",
            "#{item.tryResult,jdbcType=CHAR},#{item.supplyExec,jdbcType=CHAR},#{item.cyDj,jdbcType=CHAR},#{item.receiveFlag,jdbcType=CHAR},#{item.chequType,jdbcType=CHAR},#{item.printFlagYj,jdbcType=CHAR},#{item.confirmFlagQx,jdbcType=CHAR}) ",
            "</foreach>",
            "</script>"
    })
    int batchInsertMzChargeDetail(@Param(value = "mzChargeDetails") List<MzChargeDetail> mzChargeDetails);

    /**
     * 查询近三天内没有缴费的药品处方
     *
     * @param patientId
     * @return
     */
    @Select({"<script>",
            "select rtrim(patient_id) patient_id,times  =times,doctor_code=min(doctor_code), cf_type='1', order_count=count(distinct order_no), receipt_no =min(receipt_no),visit_date =min(price_time)," +
                    "name=min(name) from   mz_charge_detail where 1=1",
            "<when test='patientId!=null'>",
            " and patient_id=#{patientId,jdbcType=CHAR}",
            "</when>",
            " and pay_mark='5' and (datediff(day,price_time,getdate()) <![CDATA[ <= ]]> 3) group by patient_id,times",
            "</script>"})
    List<Map<String, Object>> selectUnPaidChargeDetail(@Param(value = "patientId") String patientId);

    /**
     * 将临时数据的receiptNo设置成最新的数据
     *
     * @param patientId
     * @param times
     * @param receiptNo
     * @return
     */
    @Update("update mz_charge_detail set receipt_no =#{receiptNo,jdbcType=INTEGER} where patient_id = #{patientId} and times=#{times} and pay_mark=5 ")
    int setTempDataReceiptNo(@Param(value = "patientId") String patientId, @Param(value = "times") Integer times, @Param(value = "receiptNo") Integer receiptNo);

    /**
     * 设置医技项目的新编码
     *
     * @param patientId
     * @param times
     * @param reqNo
     * @param newReqNo
     * @return
     */
    @Update("update mz_charge_detail set req_no =#{newReqNo,jdbcType=INTEGER} where patient_id = #{patientId} and times=#{times} and req_no=#{reqNo,jdbcType=INTEGER} ")
    int updateReqNo(@Param(value = "patientId") String patientId, @Param(value = "times") Integer times, @Param(value = "reqNo") Integer reqNo, @Param(value = "newReqNo") Integer newReqNo);


    /**
     * 修改当前病人当前就诊次数下的数据
     *
     * @param mzChargeDetail
     * @return
     */
    @Update({"<script>",
            "update mz_charge_detail ",
            "<trim prefix='set' prefixOverrides=',' suffix=' where patient_id = #{patientId} and times=#{times} and receipt_no=#{receiptNo}' >",
            "<when test='name!=null'>",
            ",name =#{name,jdbcType=CHAR}",
            "</when>",
            "<when test='payMark!=null'>",
            ",pay_mark =#{payMark,jdbcType=CHAR}",
            "</when>",
            "<when test='printFlag!=null'>",
            ",print_flag =#{printFlag,jdbcType=CHAR}",
            "</when>",
            "<when test='newReceiptNo!=null'>",
            ",receipt_no =#{newReceiptNo,jdbcType=INTEGER}",
            "</when>",
            "<when test='confirmFlag!=null'>",
            ",confirm_flag =#{confirmFlag,jdbcType=CHAR}",
            "</when>",
            "<when test='serialNo!=null'>",
            ",serial_no =#{serialNo,jdbcType=INTEGER}",
            "</when>",
            "<when test='realNo!=null'>",
            ",real_no =#{realNo,jdbcType=INTEGER}",
            "</when>",
            "<when test='chequType!=null'>",
            ",chequ_type =#{chequType}",
            "</when>",
            "</trim>",
            "</script>"})
    int updateMzChargeDetailFull(MzChargeDetail mzChargeDetail);



    /**
     * 修改缴费明细记录
     *
     * @param mzChargeDetail
     * @return
     */
    @Update({"<script>",
            "update mz_charge_detail ",
            "<trim prefix='set' prefixOverrides=',' suffix=' where patient_id = #{patientId} and times=#{times} and charge_item_code=#{chargeItemCode,jdbcType=CHAR} and order_no = #{orderNo} and  item_no=#{itemNo} ' >",
            "<when test='serialNo!=null'>",
            "serial_no =#{serialNo,jdbcType=INTEGER}",
            "</when>",
            "<when test='realNo!=null'>",
            ",real_no =#{realNo,jdbcType=INTEGER}",
            "</when>",
            "<when test='cashId!=null'>",
            ",cash_id =#{cashId,jdbcType=CHAR}",
            "</when>",
            "<when test='chargeDate!=null'>",
            ",charge_date =#{chargeDate,jdbcType=TIMESTAMP}",
            "</when>",
            "<when test='payMark!=null'>",
            ",pay_mark =#{payMark,jdbcType=CHAR}",
            "</when>",
            "<when test='printFlag!=null'>",
            ",print_flag =#{printFlag,jdbcType=CHAR}",
            "</when>",
            "<when test='windowsNoYf!=null'>",
            ",windows_no_yf =#{windowsNoYf,jdbcType=VARCHAR}",
            "</when>",
            "<when test='backSerial!=null'>",
            ",back_serial =#{backSerial,jdbcType=INTEGER}",
            "</when>",
            "<when test='unitPriceStr!=null'>",
            ",unit_price =#{unitPriceStr,jdbcType=DECIMAL}",
            "</when>",
            "<when test='origPriceStr!=null'>",
            ",orig_price =#{origPriceStr,jdbcType=DECIMAL}",
            "</when>",
            "</trim>",
            "</script>"})
    int updateMzChargeDetail(MzChargeDetail mzChargeDetail);


    /**
     * 查询所有的记账记录
     *
     * @param patientId
     * @param times
     * @return
     */
    @Select({"<script>",
            "select rtrim(patient_id) patient_id,times,receipt_no,order_no,item_no,rtrim(charge_item_code) charge_item_code,rtrim(bill_item_code) bill_item_code,",
            "quantity,unit_price,rtrim(pay_mark) pay_mark,rtrim(serial) serial,rtrim(charge_item_type) charge_item_type,rtrim(country_flag) country_flag,rtrim(drug_flag) drug_flag,",
            "serial_no,rtrim(name) name,charge_date,confirm_time,rtrim(group_no) group_no,rtrim(confirm_id) confirm_id,drug_win,rtrim(input_id) input_id,input_date,",
            "rtrim(cashier_id) cashier_id,rtrim(windows_no) windows_no,rtrim(price_id) price_id,price_time,rtrim(exec_dept) exec_dept,rtrim(audit_code) audit_code,",
            "rtrim(gongnks_flag) gongnks_flag,rtrim(print_flag) print_flag,confirm_flag,rtrim(warn_dept) warn_dept,rtrim(jz_flag) jz_flag,group_order,rtrim(cash_id) cash_id,",
            "rtrim(doctor_code) doctor_code,rtrim(supply_code) supply_code,rtrim(frequency) frequency,drug_quan,rtrim(drug_unit) drug_unit,rtrim(doctor_flag) doctor_flag,inject_res,inject_times,",
            "rtrim(confirm_zs) confirm_zs,real_no,windows_no_yf,ybbx_flag,tc_no,tc_name,rtrim(detail_flag) detail_flag,gl_percent,rtrim(order_type) order_type,supply_no,instruction_code,",
            "instruction_text,order_days,rtrim(pay_self) pay_self,rtrim(try_flag) try_flag,rtrim(req_yj) req_yj,req_no,rtrim(group_type) group_type,order_bill,charge_bill_code,",
            "rtrim(self_flag) self_flag,rtrim(responce_type) responce_type,dec_amount,back_serial,input_time,cy_mtjs,cy_jsl,cy_zql,cy_fj,cy_ffcs,cy_fysm,supply_amount,rtrim(cy_jssm) cy_jssm,",
            "dept_no,gl_percent_ft,rtrim(yb_zf_flag) yb_zf_flag,orig_price,rtrim(try_result) try_result,rtrim(supply_exec) supply_exec,rtrim(cy_dj) cy_dj,rtrim(receive_flag) receive_flag,",
            "rtrim(chequ_type) chequ_type,rtrim(print_flag_yj) print_flag_yj,rtrim(confirm_flag_qx) confirm_flag_qx ",
            "from dbo.mz_charge_detail where patient_id =#{patientId,jdbcType=CHAR} and pay_mark=0  and times =#{times} and  receipt_no =#{receiptNo} ",
            "</script>"})
    List<MzChargeDetail> selectJiZhangDetails(@Param(value = "patientId") String patientId, @Param("times") Integer times, @Param("receiptNo") Integer receiptNo);

    /**
     * 查询当前就诊次数的所有取药号与药房信息
     *
     * @param patientId
     * @param times
     * @return
     */
    @Select("select back_serial,rtrim(group_no) group_no,windows_no_yf from mz_charge_detail where patient_id=#{patientId,jdbcType=CHAR} and times = #{times,jdbcType=INTEGER} and back_serial>0 group by back_serial,group_no,windows_no_yf")
    List<MzChargeDetail> selectFyJl(@Param("patientId") String patientId, @Param("times") Integer times);


    /**
     * 修改发票打印标记
     *
     * @param patientId
     * @param times
     * @return
     */
    @Update({"<script>",
            "update mz_charge_detail set print_flag =1 where patient_id = #{patientId} and times in ",
            "<foreach item='item' index='index' collection='times' open='(' separator=',' close=')'>",
            "#{item}",
            "</foreach>",
            "</script>"})
    int updatePrintFlag(@Param("patientId") String patientId, @Param("times") List<Integer> times);


    /**
     * 修改发票流水号与缴费次数  发票作废重打时调用
     *
     * @param receiptNo
     * @param serialNo
     * @param oriSerialNo
     * @return
     */
    @Update("update mz_charge_detail set receipt_no =#{receiptNo},serial_no=#{serialNo},real_no=#{serialNo} where serial_no = #{oriSerialNo}")
    int updateBySerialNo(@Param("receiptNo") Integer receiptNo, @Param("serialNo") Integer serialNo, @Param("oriSerialNo") Integer oriSerialNo);


    /**
     * 修改病人姓名
     *
     * @param name
     * @param patientId
     * @param times 120病人缴费时调用时需传
     * @return
     */
    @Update({"<script>","update mz_charge_detail set name =#{name} where patient_id = #{patientId} " +
            "<when test='times!=null'>",
            "and times = #{times}",
            "</when>",
            "</script>"})
    int updateNameByPatientIdAndTimes(@Param("name") String name, @Param("patientId") String patientId, @Param("times") Integer times);

    /**
     * 查询病人缴费记录
     *
     * @param patientId
     * @param name
     * @param serialNo
     * @param beginDate
     * @param endDate
     * @return
     */
    @Select({"<script>",
            "SELECT distinct rtrim(a.patient_id) patient_id,name,a.times,a.receipt_no,charge_date=convert(char(10),a.charge_date,111) ,a.serial_no,real_no ,a.responce_type," +
                    " cheque_type=case when mz_receipt_serial.cheque_type='Y' and cash_id <![CDATA[<>]]> '99999' " +
                    " and (len(ltrim(rtrim(mz_receipt_serial.receipt_bill))) >= 10 or len(ltrim(rtrim(mz_receipt_serial.receipt_bill)))=0) then '本院记账' else '自费' end, " +
                    " serial_no_order=ABS(a.serial_no)  " +
                    "    FROM mz_charge_detail a,mz_receipt_serial " +
                    " WHERE "+
                    " a.patient_id=mz_receipt_serial.patient_id " +
                    " and a.times = mz_receipt_serial.times " +
                    " and a.receipt_no = mz_receipt_serial.receipt_no "+
                    "    and a.charge_date <![CDATA[>=]]> #{beginDate,jdbcType=TIMESTAMP} and a.charge_date <![CDATA[<=]]> #{endDate,jdbcType=TIMESTAMP}  and a.pay_mark <![CDATA[<>]]> 9 " +
                    "<when test='patientId!=null'>",
            " and a.patient_id like #{patientId}",
            "</when>",
            "<when test='name!=null'>",
            " and a.name like #{name}",
            "</when>",
            "<when test='serialNo!=null'>",
            " and mz_receipt_serial.serial_no like #{serialNo}",
            "</when>",
            " and  real_no=receipt_sn and a.name is not null and a.responce_type is not null order by serial_no_order desc" +
                    "</script>"})
    List<Map<String, Object>> selectChargeListByPatient(@Param("patientId") String patientId, @Param("name") String name, @Param("serialNo") String serialNo, @Param("beginDate") Date beginDate, @Param("endDate") Date endDate);

    /**
     * 查询某个病人当月购买某一药品的总数量
     *
     * @param patientId
     * @param chargeItemCode
     * @param serial
     * @param beginDate
     * @return
     */
    @Select("select sum(quantity*drug_win) from mz_charge_detail where patient_id =#{patientId} and charge_date>=#{beginDate,jdbcType=TIMESTAMP} and pay_mark in (0,1) and charge_item_code=#{chargeItemCode,jdbcType=CHAR} and  serial=#{serial} ")
    Integer selectTotalQuantityForMonth(@Param("patientId") String patientId, @Param("chargeItemCode") String chargeItemCode, @Param("serial") String serial, @Param("beginDate") Date beginDate);

    /**
     * 根据病人编码和收费流水号查询缴费明细
     *
     * @param patientId
     * @param serialNo
     * @return
     */
    @Select("SELECT   patient_id=a.patient_id," +
            "         times     =a.times," +
            "         receipt_no=a.receipt_no," +
            "         order_no  =a.order_no," +
            "         quantity  =a.quantity * drug_win," +
            "         unit_price=a.unit_price," +
            "         name      =a.name," +
            "         drugname  =b.name," +
            "         specification=b.specification," +
            "         frequency    =a.frequency," +
            "         supply_code  =a.supply_code," +
            "         amount       =a.drug_quan," +
            "         drug_unit    =a.drug_unit," +
            "         item_no      =a.item_no," +
            "         doctor_code  =a.doctor_code," +
            "         op_id        =a.cash_id," +
            "         real_no      =a.real_no," +
            "         group_no=a.group_no," +
            "         total_price=a.quantity * drug_win* round(a.unit_price,2)," +
            "         tybm=b.code ," +
            "         unit=b.pack_unit," +
            "         warn_dept=warn_dept," +
            "         charge_date," +
            " print_flag = (select max(cheque_type) from mz_receipt_serial where mz_receipt_serial.receipt_sn = a.real_no) " +
            "    FROM mz_charge_detail a,yp_zd_dict b " +
            "   WHERE  a.patient_id = #{patientId}     AND  " +
            "          a.real_no=#{serialNo}  and " +
            "         a.charge_item_code=b.code and " +
            "         a.serial=b.serial  " +
            " union all " +
            "SELECT   patient_id=a.patient_id," +
            "         times     =a.times," +
            "         receipt_no=a.receipt_no," +
            "         order_no  =a.order_no," +
            "         quantity  =a.quantity * drug_win," +
            "         unit_price=a.unit_price," +
            "         name      =a.name," +
            "         drugname  =b.name," +
            "         specification=b.charge_unit," +
            "         frequency    =a.frequency," +
            "         supply_code  =a.supply_code," +
            "         amount       =a.drug_quan," +
            "         drug_unit    =a.drug_unit," +
            "         item_no      =a.item_no," +
            "         doctor_code  =a.doctor_code," +
            "         op_id        =a.cash_id," +
            "         real_no      =a.real_no," +
            "         group_no=a.group_no," +
            "         total_price=a.quantity * drug_win* round(a.unit_price,2)," +
            "         tybm=isnull(b.s_code,b.code)," +
            "         unit=charge_unit," +
            "         warn_dept=warn_dept," +
            "         charge_date," +
            " print_flag = (select max(cheque_type) from mz_receipt_serial where mz_receipt_serial.receipt_sn = a.real_no) " +
            "    FROM mz_charge_detail a,zd_charge_item b" +
            "   WHERE  a.patient_id = #{patientId}   AND  " +
            "          a.real_no=#{serialNo}  and " +
            "         a.charge_item_code=b.code   and " +
            "         a.serial <>'01' and " +
            "         a.serial <>'99'")
    List<Map<String, Object>> selectChargeDetailByPatient(@Param("patientId") String patientId, @Param("serialNo") String serialNo);


    /**
     * 查询病人缴费记录 （历史数据）
     *
     * @param patientId
     * @param name
     * @param serialNo
     * @param beginDate
     * @param endDate
     * @return
     */
    @Select({"<script>",
            "SELECT distinct rtrim(a.patient_id) patient_id,name,a.times,a.receipt_no,charge_date=convert(char(10),a.charge_date,111) ,a.serial_no,real_no ,a.responce_type," +
                    " cheque_type=case when mz_receipt_serial_b.cheque_type='Y' " +
                    " and (len(ltrim(rtrim(mz_receipt_serial_b.receipt_bill))) >= 10 or len(ltrim(rtrim(mz_receipt_serial_b.receipt_bill)))=0) then '本院记账' else '自费' end, " +
                    " serial_no_order=ABS(a.serial_no)  " +
                    "    FROM mz_charge_detail_b a,mz_receipt_serial_b " +
                    "WHERE"+
                    " a.patient_id=mz_receipt_serial_b.patient_id " +
                    " and a.times = mz_receipt_serial_b.times " +
                    " and a.receipt_no = mz_receipt_serial_b.receipt_no "+
                    "  and a.charge_date <![CDATA[>=]]> #{beginDate,jdbcType=TIMESTAMP} and a.charge_date <![CDATA[<=]]> #{endDate,jdbcType=TIMESTAMP}  and a.pay_mark <![CDATA[<>]]> 9  " +
                    "<when test='patientId!=null'>",
            " and a.patient_id like #{patientId}",
            "</when>",
            "<when test='name!=null'>",
            " and a.name like #{name}",
            "</when>",
            "<when test='serialNo!=null'>",
            " and mz_receipt_serial_b.serial_no =#{serialNo}",
            "</when>",
            " and  real_no=receipt_sn and a.name is not null and a.responce_type is not null order by serial_no_order desc" +
                    "</script>"})
    List<Map<String, Object>> selectHisChargeListByPatient(@Param("patientId") String patientId, @Param("name") String name, @Param("serialNo") String serialNo, @Param("beginDate") Date beginDate, @Param("endDate") Date endDate);


    /**
     * 根据病人编码和收费流水号查询缴费明细 （历史数据）
     *
     * @param patientId
     * @param serialNo
     * @return
     */
    @Select("SELECT   patient_id=a.patient_id," +
            "         times     =a.times," +
            "         receipt_no=a.receipt_no," +
            "         order_no  =a.order_no," +
            "         quantity  =a.quantity * drug_win," +
            "         unit_price=a.unit_price," +
            "         name      =a.name," +
            "         drugname  =b.name," +
            "         specification=b.specification," +
            "         frequency    =a.frequency," +
            "         supply_code  =a.supply_code," +
            "         amount       =a.drug_quan," +
            "         drug_unit    =a.drug_unit," +
            "         item_no      =a.item_no," +
            "         doctor_code  =a.doctor_code," +
            "         op_id        =a.cash_id," +
            "         real_no      =a.real_no," +
            "         group_no=a.group_no," +
            "         total_price=a.quantity * drug_win* a.unit_price," +
            "         tybm=b.code ," +
            "         unit=b.pack_unit," +
            "         warn_dept=warn_dept," +
            "         charge_date " +
            "    FROM mz_charge_detail_b a,yp_zd_dict b " +
            "   WHERE  a.patient_id = #{patientId}     AND  " +
            "          a.real_no=#{serialNo}  and " +
            "         a.charge_item_code=b.code and " +
            "         a.serial=b.serial  " +
            " union all " +
            "SELECT   patient_id=a.patient_id," +
            "         times     =a.times," +
            "         receipt_no=a.receipt_no," +
            "         order_no  =a.order_no," +
            "         quantity  =a.quantity * drug_win," +
            "         unit_price=a.unit_price," +
            "         name      =a.name," +
            "         drugname  =b.name," +
            "         specification=b.charge_unit," +
            "         frequency    =a.frequency," +
            "         supply_code  =a.supply_code," +
            "         amount       =a.drug_quan," +
            "         drug_unit    =a.drug_unit," +
            "         item_no      =a.item_no," +
            "         doctor_code  =a.doctor_code," +
            "         op_id        =a.cash_id," +
            "         real_no      =a.real_no," +
            "         group_no=a.group_no," +
            "         total_price=a.quantity * drug_win* a.unit_price," +
            "         tybm=isnull(b.s_code,b.code)," +
            "         unit=charge_unit," +
            "         warn_dept=warn_dept," +
            "         charge_date " +
            "    FROM mz_charge_detail_b a,zd_charge_item b" +
            "   WHERE  a.patient_id = #{patientId}   AND  " +
            "          a.real_no=#{serialNo}  and " +
            "         a.charge_item_code=b.code   and " +
            "         a.serial <>'01' and " +
            "         a.serial <>'99'")
    List<Map<String, Object>> selectHisChargeDetailByPatient(@Param("patientId") String patientId, @Param("serialNo") String serialNo);

    /**
     * 根据病人就诊次数和门诊号查询有效的申请科室和医生编码
     *
     * @param patientId
     * @param times
     * @return
     */
    @Select("select top 1 rtrim(warn_dept) warn_dept,rtrim(doctor_code) doctor_code,rtrim(print_flag) print_flag from ${tableName} where  patient_id = #{patientId} and times = #{times} and warn_dept is not null and doctor_code is not null and warn_dept<> '' and doctor_code<> '' ")
    MzChargeDetail selectWarnDeptAndDoctor(@Param("patientId") String patientId, @Param("times") Integer times, @Param("tableName") String tableName);

    /**
     * 清洗老系统打印发票数据
     *
     * @return
     */
    @Update("update mz_charge_detail set confirm_flag=4  where real_no in (select abs(real_no) from mz_charge_detail where real_no <0 group by real_no)")
    int refshOldSystemReceiptData();

    /**
     * 查询120 病人姓名
     *
     * @param patientId
     * @param times
     * @return
     */
    @Select("select top 1 name from mz_charge_detail where  patient_id = #{patientId} and times = #{times} and name is not null and  name <> '' ")
    String selectJZName(@Param("patientId") String patientId, @Param("times") Integer times);

    /**
     * @param thmzmxsrParamsVo caseType = 1 申请科室核算报表  0 执行科室核算报表
     * @return
     */
    @Select({"<script>",
            "SELECT " +
                    " exec_dept = (" +
                    " CASE " +
                    " 1 " +
                    " WHEN ${caseType} THEN " +
                    " CASE " +
                    " WHEN isnull( a.warn_dept, '' ) = '' THEN " +
                    " a.exec_dept ELSE a.warn_dept " +
                    " END ELSE " +
                    " CASE " +
                    " WHEN isnull( a.exec_dept, '' ) = '' THEN " +
                    " a.warn_dept ELSE a.exec_dept " +
                    " END " +
                    " END " +
                    " )," +
                    " charge_bill_code = " +
                    " CASE " +
                    " a.charge_bill_code " +
                    " WHEN 'BILL10' THEN " +
                    " 'BILL01' " +
                    " WHEN 'BILL11' THEN " +
                    " 'BILL28' " +
                    " WHEN 'BILL12' THEN " +
                    " 'BILL02' ELSE a.charge_bill_code " +
                    " END, " +
                    " total_charge = isnull(" +
                    "SUM(" +
                    " round( " +
                    " CONVERT (" +
                    "DECIMAL ( 18, 2 )," +
                    " ( " +
                    " ( " +
                    " CASE " +
                    " WHEN CONVERT ( DECIMAL ( 18, 2 ), unit_price ) = 0.00 THEN " +
                    " 0.01 ELSE CONVERT ( DECIMAL ( 18, 2 ), unit_price ) " +
                    " END  " +
                    " ) * ( quantity * isnull( drug_win, 1 ) )  " +
                    " )  " +
                    " ), " +
                    " 2  " +
                    " )  " +
                    " ), " +
                    " 0  " +
                    " ), " +
                    " charge_dcount_date = b.charge_dcount_date  " +
                    " FROM " +
                    " ${firstTableName} a ( nolock ), " +
                    " ( " +
                    " SELECT DISTINCT " +
                    " patient_id, " +
                    " times, " +
                    " receipt_no, " +
                    " serial_no, " +
                    " receipt_sn, " +
                    " charge_dcount_date, " +
                    " pay_id  " +
                    " FROM " +
                    " ${secondTableName} " +
                    " WHERE " +
                    " ( CASE 1 WHEN 0 THEN charge_date ELSE charge_dcount_date END ) <![CDATA[>=]]> #{beginDate}  " +
                    " AND ( CASE 1 WHEN 0 THEN charge_date ELSE charge_dcount_date END ) <![CDATA[<=]]> #{endDate}  " +
                    " AND ( fp_flag  <![CDATA[<>]]> 'Y' OR fp_flag IS NULL )  " +
                    " AND ( CASE 1 WHEN 0 THEN isnull( charge_dcount_no, 0 ) ELSE 1 END ) = isnull( charge_dcount_no, 0 )  " +
                    " ) AS b  " +
                    " WHERE " +
                    " ( a.patient_id = b.patient_id )  " +
                    " AND a.times = b.times  " +
                    " AND a.receipt_no = b.receipt_no  " +
                    " AND a.serial_no = b.serial_no " +
                    " AND a.real_no = b.receipt_sn  " +
                    " AND a.bill_item_code <![CDATA[<>]]> 'TC'  " +
                    "<when test='execDept!=null'>",
                    "          and ( case '1' when #{caseType} then " +
                    "      case when isnull(a.warn_dept,'')='' then  a.exec_dept else a.warn_dept end " +
                    "      else  " +
                    "     case when isnull(a.exec_dept,'') ='' then a.warn_dept else a.exec_dept end " +
                    "  end " +
                    ") =#{execDept} " +
                    "</when>" +
                    " GROUP BY " +
                    " isnull( a.group_no, '00' ), " +
                    " CASE " +
                    " a.charge_bill_code  " +
                    " WHEN 'BILL10' THEN " +
                    " 'BILL01'  " +
                    " WHEN 'BILL11' THEN " +
                    " 'BILL28'  " +
                    " WHEN 'BILL12' THEN " +
                    " 'BILL02' ELSE a.charge_bill_code  " +
                    " END, " +
                    " b.charge_dcount_date, " +
                    " b.pay_id, " +
                    " ( " +
                    " CASE " +
                    " 1  " +
                    " WHEN ${caseType} THEN " +
                    " CASE " +
                    " WHEN isnull( a.warn_dept, '' ) = '' THEN " +
                    " a.exec_dept ELSE a.warn_dept  " +
                    " END ELSE " +
                    " CASE " +
                    " WHEN isnull( a.exec_dept, '' ) = '' THEN " +
                    " a.warn_dept ELSE a.exec_dept " +
                    " END  " +
                    " END  " +
                    ")",
            "</script>"})
    List<Map<String, Object>> selectDepetBusinessReport(ThmzmxsrParamsVo thmzmxsrParamsVo);


    /**
     * 根据病人id和次数及缴费次数查询缴费记录
     * @param patientId
     * @param times
     * @param receiptNo
     * @return
     */
    @Select({"<script>",
            "select rtrim(patient_id) patient_id,times,receipt_no,order_no,item_no,rtrim(charge_item_code) charge_item_code,rtrim(bill_item_code) bill_item_code,",
            "quantity,unit_price,rtrim(pay_mark) pay_mark,rtrim(serial) serial,rtrim(charge_item_type) charge_item_type,rtrim(country_flag) country_flag,rtrim(drug_flag) drug_flag,",
            "serial_no,rtrim(name) name,charge_date,confirm_time,rtrim(group_no) group_no,rtrim(confirm_id) confirm_id,drug_win,rtrim(input_id) input_id,input_date,",
            "rtrim(cashier_id) cashier_id,rtrim(windows_no) windows_no,rtrim(price_id) price_id,price_time,rtrim(exec_dept) exec_dept,rtrim(audit_code) audit_code,",
            "rtrim(gongnks_flag) gongnks_flag,rtrim(print_flag) print_flag,confirm_flag,rtrim(warn_dept) warn_dept,rtrim(jz_flag) jz_flag,group_order,rtrim(cash_id) cash_id,",
            "rtrim(doctor_code) doctor_code,rtrim(supply_code) supply_code,rtrim(frequency) frequency,drug_quan,rtrim(drug_unit) drug_unit,rtrim(doctor_flag) doctor_flag,inject_res,inject_times,",
            "rtrim(confirm_zs) confirm_zs,real_no,windows_no_yf,ybbx_flag,tc_no,tc_name,rtrim(detail_flag) detail_flag,gl_percent,rtrim(order_type) order_type,supply_no,instruction_code,",
            "instruction_text,order_days,rtrim(pay_self) pay_self,rtrim(try_flag) try_flag,rtrim(req_yj) req_yj,req_no,rtrim(group_type) group_type,order_bill,charge_bill_code,",
            "rtrim(self_flag) self_flag,rtrim(responce_type) responce_type,dec_amount,back_serial,input_time,cy_mtjs,cy_jsl,cy_zql,cy_fj,cy_ffcs,cy_fysm,supply_amount,rtrim(cy_jssm) cy_jssm,",
            "dept_no,gl_percent_ft,rtrim(yb_zf_flag) yb_zf_flag,orig_price,rtrim(try_result) try_result,rtrim(supply_exec) supply_exec,rtrim(cy_dj) cy_dj,rtrim(receive_flag) receive_flag,",
            "rtrim(chequ_type) chequ_type,rtrim(print_flag_yj) print_flag_yj,rtrim(confirm_flag_qx) confirm_flag_qx ",
            "from dbo.mz_charge_detail where  patient_id =#{patientId,jdbcType=CHAR} and times =#{times} and receipt_no =#{receiptNo} ",
            "</script>"})
    List<MzChargeDetail> selectByPatientIdAndTimesAndReceiptNo(@Param("patientId") String patientId,@Param("times") Integer times,@Param("receiptNo") Integer receiptNo);



    /**
     * 查询药房
     * @param
     * @return
     */
    @Select("select  rtrim(group_no) from mz_charge_detail where  patient_id = #{patientId} and times = #{times} and receipt_no=#{receiptNo} and group_no<> '00' GROUP BY group_no")
    List<String> selectGroupNo(MzChargeDetail mzChargeDetail);

    /**
     * 更新收费明细的付款方式 当付款方式有本院记账时设置
     * @return
     */
    @Update("update mz_charge_detail set chequ_type=#{chequType} where serial_no=#{serialNo}")
    int updateChequeTypeByserialNo(@Param("chequType") String chequType,@Param("serialNo") Integer serialNo);

    /**
     * 同步备份库数据到正式库 修复记账的数据
     * @param mzChargeDetail
     * @return
     */
    @Insert("insert into mz_charge_detail select * from mz_charge_detail_b where patient_id = #{patientId} and times = #{times} and receipt_no=#{receiptNo}")
    int reInsertDetail(MzChargeDetail mzChargeDetail);


    /**
     * 将备份库数据删除
     * @return
     */
    @Delete("delete from mz_charge_detail_b where patient_id=#{patientId} and times = #{times} and receipt_no =#{receiptNo,jdbcType=INTEGER} ")
    int deleteFromBak(MzChargeDetail mzChargeDetail);
}
