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

import cn.hnthyy.thmz.entity.his.YpMzFytj;
import cn.hnthyy.thmz.entity.his.mz.MzChargeDetail;
import cn.hnthyy.thmz.entity.thmz.RadSendRecord;
import cn.hnthyy.thmz.vo.*;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.Date;
import java.util.List;
import java.util.Map;
/**
 * @description: 门诊发药、配药、退药数据持久化
 * @author: WANGJIALIANG
 * @time: 2021/7/5 15:57
 */
public interface MzPharmacyMapper {

    /**
     * 查询发药处方信息
     *
     * @param chargeDetailPharmacyVo
     * @return
     */
    @Select({"<script>",
            "select  max(charge_item_code)charge_item_code,max(confirm_flag)confirm_flag,rtrim(patient_id)patient_id,rtrim(name)name,rtrim(serial_no)serial_no,rtrim(real_no)real_no,rtrim(order_no)order_no,charge_date,times,rtrim(receipt_no)receipt_no,rtrim(warn_dept)warn_dept " +
                    "from mz_charge_detail  where 1=1 ",
            " and serial_no > 0",
            " and isnull(confirm_zs,'') <![CDATA[<>]]>'1'",
            "<when test='mzChargeDetail.groupNo!=null'>",
            " and group_no =#{mzChargeDetail.groupNo,jdbcType=CHAR}",
            "</when>",
            "<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.windowsNoYf!=null'>",
            " and windows_no_yf = #{mzChargeDetail.windowsNoYf,jdbcType=CHAR}",
            "</when>",
            "<when test='mzChargeDetail.confirmFlag!=null'>",
            " and confirm_flag =#{mzChargeDetail.confirmFlag,jdbcType=CHAR}",
            "</when>",
            "<when test='mzChargeDetail.groupType!=null'>",
            " and group_type =#{mzChargeDetail.groupType,jdbcType=CHAR}",
            "</when>",
            "<when test='mzChargeDetail.printFlag!=null'>",
            " and print_flag =#{mzChargeDetail.printFlag,jdbcType=CHAR}",
            "</when>",
            "<when test='beginTime!=null'>",
            " and charge_date &gt;=#{beginTime,jdbcType=TIMESTAMP}",
            "</when>",
            "<when test='endTime!=null'>",
            " and charge_date &lt;=#{endTime,jdbcType=TIMESTAMP}",
            "</when>",
            " GROUP BY patient_id,name,serial_no,real_no,order_no,charge_date,times,receipt_no,warn_dept order by charge_date desc",
            "</script>"})
    List<MzChargeDetail> selectMzChargeOrder(ChargeDetailPharmacyVo chargeDetailPharmacyVo);

    /**
     * 查询发药统计(退药)
     *
     * @return
     */
    @Select({"<script>",
            "SELECT top ${pageSize} * ",
            "    FROM  (select ROW_NUMBER() OVER (ORDER BY charge_date desc)",
            "AS RowNumber, rtrim(patient_id) patient_id," ,
                    "times," ,
                    "receipt_no," ,
                    "order_no," ,
                    "rtrim(name) name," ,
                    "charge_date," ,
                    "rtrim(group_no) group_no," ,
                    "confirm_time," ,
                    "rtrim(doctor_id) doctor_id," ,
                    "rtrim(doctor_name) doctor_name," ,
                    "real_no," ,
                    "warn_dept, " ,
                    "confirm_flag " ,
                    "from yp_mz_fytj where serial_no <![CDATA[<]]> 0 ",
            "<when test='name!=null'>",
            " and name like #{name}",
            "</when>",
            "<when test='patientId!=null'>",
            " and patient_id = #{patientId}",
            "</when>",
            "<when test='serialNo!=null'>",
            " and serial_no = #{serialNo}",
            "</when>",
            "<when test='realNo!=null'>",
            " and real_no = #{realNo}",
            "</when>",
            "<when test='groupNoOut!=null'>",
            " and group_no_out = #{groupNoOut}",
            "</when>",
            "<when test='confirmFlag!=null'>",
            " and confirm_flag = #{confirmFlag}",
            "</when>",
            "<when test='beginDate!=null'>",
            " and charge_date <![CDATA[>=]]> #{beginDate}",
            "</when>",
            "<when test='endDate!=null'>",
            " and charge_date <![CDATA[<=]]> #{endDate}",
            "</when> group by patient_id,times,receipt_no,order_no,name,charge_date,group_no,confirm_time,doctor_id,doctor_name,real_no,warn_dept,confirm_flag",
            ") as A WHERE RowNumber >#{pageSize}*#{pageIndex} order by RowNumber ASC",
            "</script>"})
    List<YpMzFytjVo> selectYpMzRefundFytjPage(ChargeFeeParamsVo chargeFeeParamsVo);

    /**
     * 查询发药统计条数(退药)
     *
     * @return
     */
    @Select({"<script>",
            "select count(1) from",
            "(select DISTINCT rtrim(patient_id) patient_id,times,receipt_no,order_no,rtrim(name) name,charge_date,rtrim(group_no) group_no,confirm_time,rtrim(doctor_id) doctor_id,rtrim(doctor_name) doctor_name,real_no,warn_dept from yp_mz_fytj where serial_no <![CDATA[<]]> 0 ",
            "<when test='name!=null'>",
            " and name like #{name}",
            "</when>",
            "<when test='patientId!=null'>",
            " and patient_id = #{patientId}",
            "</when>",
            "<when test='serialNo!=null'>",
            " and serial_no = #{serialNo}",
            "</when>",
            "<when test='realNo!=null'>",
            " and real_no = #{realNo}",
            "</when>",
            "<when test='groupNoOut!=null'>",
            " and group_no_out = #{groupNoOut}",
            "</when>",
            "<when test='confirmFlag!=null'>",
            " and confirm_flag = #{confirmFlag}",
            "</when>",
            "<when test='beginDate!=null'>",
            " and charge_date <![CDATA[>=]]> #{beginDate}",
            "</when>",
            "<when test='endDate!=null'>",
            " and charge_date <![CDATA[<=]]> #{endDate}",
            "</when>",
            ")A",
            "</script>"})
    int selectYpMzRefundFytjPageCount(ChargeFeeParamsVo chargeFeeParamsVo);

    /**
     * 查询发药统计(退药成功)
     *
     * @return
     */
    @Select({"<script>",
            "SELECT top ${pageSize} * ",
            "   FROM  (select ROW_NUMBER() OVER (ORDER BY charge_date desc)" ,
                    "AS RowNumber, yp_mz_fytj.name,   " ,
                    "         yp_mz_fytj.real_no,   " ,
                    "         yp_mz_fytj.order_no,   " ,
                    "         rtrim(yp_mz_fytj.patient_id)patient_id,   " ,
                    "         yp_mz_fytj.times,   " ,
                    "         yp_mz_fytj.receipt_no ," ,
                    "         yp_mz_fytj.group_no ," ,
                    "         charge_date," ,
                    "         confirm_time," ,
                    "         confirm_flag," ,
                    "doctor_id," ,
                    "doctor_name," ,
                    "warn_dept from yp_mz_fytj where serial_no <![CDATA[<]]> 0 ",
            "<when test='name!=null'>",
            " and name like #{name}",
            "</when>",
            "<when test='patientId!=null'>",
            " and patient_id = #{patientId}",
            "</when>",
            "<when test='groupNoOut!=null'>",
            " and group_no_out = #{groupNoOut}",
            "</when>",
            "<when test='realNo!=null'>",
            " and real_no = #{realNo}",
            "</when>",
            "<when test='confirmFlag!=null'>",
            " and confirm_flag = #{confirmFlag}",
            "</when>",
            "<when test='beginDate!=null'>",
            " and confirm_time <![CDATA[>=]]> #{beginDate}",
            "</when>",
            "<when test='endDate!=null'>",
            " and confirm_time <![CDATA[<=]]> #{endDate}",
            "</when> group by name,real_no,order_no,patient_id,times,receipt_no ,group_no ,charge_date,confirm_time,doctor_id,doctor_name,warn_dept,confirm_flag",
            ") as A WHERE RowNumber >#{pageSize}*#{pageIndex} order by RowNumber ASC",
            "</script>"})
    List<YpMzFytjVo> selectYpMzRefundSucceedFytjPage(ChargeFeeParamsVo chargeFeeParamsVo);

    /**
     * 查询发药统计条数(退药成功)
     *
     * @return
     */
    @Select({"<script>",
            "select count(*) from",
            "(select DISTINCT yp_mz_fytj.name,   " ,
                    "         yp_mz_fytj.real_no,   " ,
                    "         yp_mz_fytj.order_no,   " ,
                    "         yp_mz_fytj.patient_id,   " ,
                    "         yp_mz_fytj.times,   " ,
                    "         yp_mz_fytj.receipt_no ," ,
                    "         yp_mz_fytj.group_no ," ,
                    "         charge_date," ,
                    "         confirm_time," ,
                    "doctor_id," ,
                    "doctor_name," ,
                    "warn_dept from yp_mz_fytj where serial_no <![CDATA[<]]> 0 ",
            "<when test='name!=null'>",
            " and name like #{name}",
            "</when>",
            "<when test='patientId!=null'>",
            " and patient_id = #{patientId}",
            "</when>",
            "<when test='groupNoOut!=null'>",
            " and group_no_out = #{groupNoOut}",
            "</when>",
            "<when test='realNo!=null'>",
            " and real_no = #{realNo}",
            "</when>",
            "<when test='confirmFlag!=null'>",
            " and confirm_flag = #{confirmFlag}",
            "</when>",
            "<when test='beginDate!=null'>",
            " and confirm_time <![CDATA[>=]]> #{beginDate}",
            "</when>",
            "<when test='endDate!=null'>",
            " and confirm_time <![CDATA[<=]]> #{endDate}",
            "</when>",
            ")A",
            "</script>"})
    int selectYpMzRefundSucceedFytjCount(ChargeFeeParamsVo chargeFeeParamsVo);

    /**
     * 查询处方药品明细
     *
     * @param chargeFeeParamsVo
     * @return
     */
    @Select({"<script>" +
            "SELECT DISTINCT yp_mz_fytj.serial_no,   " +
            "         yp_mz_fytj.order_no,   " +
            "         rtrim(yp_mz_fytj.patient_id)patient_id,   " +
            "         yp_mz_fytj.times,   " +
            "         yp_mz_fytj.receipt_no,   " +
            "         drugname=c.name+'('+isnull(d.abbr_name,isnull(d.name,''))+')',   " +
            "         specification=c.specification,   " +
            "         quantity= case yp_mz_fytj.group_no when 81 then yp_mz_fytj.quantity * cy_fy else yp_mz_fytj.quantity  end,   " +
            "         yp_mz_fytj.unit_price,   " +
            "         yp_mz_fytj.item_no,   " +
            "         yp_mz_fytj.drug_flag,   " +
            "         yp_mz_fytj.charge_item_code,   " +
            "         yp_mz_fytj.serial,   " +
            "         yp_mz_fytj.confirm_time,   " +
            "         yp_mz_fytj.group_no,   " +
            "         yp_mz_fytj.bill_item_code,   " +
            "         yp_mz_fytj.pay_mark,   " +
            "         yp_mz_fytj.charge_item_type,   " +
            "         yp_mz_fytj.name,   " +
            "         yp_mz_fytj.charge_date,   " +
            "         yp_mz_fytj.cy_fy,   " +
            "         yp_mz_fytj.specification,   " +
            "         yp_mz_fytj.drugname,   " +
            "         yp_mz_fytj.doctor_name,   " +
            "         yp_mz_fytj.doctor_id,   " +
            "         warn_dept=yp_mz_fytj.warn_dept,   " +
            "         yp_mz_fytj.country_flag,   " +
            "         yp_mz_fytj.confirm_flag,   " +
            "         yp_mz_fytj.self_flag,   " +
            "         yp_mz_fytj.response_type,   " +
            "         yp_mz_fytj.warn_dept  ," +
            "         real_no=yp_mz_fytj.real_no," +
            "         win_no = yp_mz_fytj.win_no " +
            "    FROM yp_mz_fytj  , " +
            "         yp_zd_dict c, " +
            "         yp_zd_manufactory d " +
            "   WHERE  yp_mz_fytj.patient_id = #{patientId}  AND  " +
            "          yp_mz_fytj.times = #{times}  AND  " +
            "          yp_mz_fytj.receipt_no = #{receiptNo}  AND  " +
            "          yp_mz_fytj.order_no = #{orderNo}  AND  " +
            "         yp_mz_fytj.group_no_out = #{groupNoOut} AND  " +
            "          yp_mz_fytj.charge_item_code = c.code  and  " +
            "          yp_mz_fytj.serial = c.serial  and  " +
            "         yp_mz_fytj.confirm_flag = #{confirmFlag}   and " +
            "         yp_mz_fytj.serial_no <![CDATA[<]]> 0 and " +
            "         c.manu_code *= d.code " +
            "ORDER BY yp_mz_fytj.item_no ASC ",
            "</script>"})
    List<Map<String, Object>> selectPrescriptionYpMx(ChargeFeeParamsVo chargeFeeParamsVo);

    /**
     * 更新发药统计状态
     *
     * @param mzRefundMedicineVo
     * @return
     */
    @Update("update yp_mz_fytj set confirm_flag =#{confirmFlag} where patient_id =#{patientId} and times =#{times} and receipt_no =-#{receiptNo}" +
            " and order_no =#{orderNo} and real_no =-#{realNo}  ")
    int updateFytjConfirmFlag(MzRefundMedicineVo mzRefundMedicineVo);

    /**
     * 更新收费明细状态信息
     *
     * @param mzRefundMedicineVo
     * @return
     */
    @Update("update mz_charge_detail set confirm_flag =#{confirmFlag} , confirm_time =#{confirmTime} , confirm_id =#{confirmId}, dec_amount =#{decAmount} " +
            "where patient_id =#{patientId} and serial_no =-#{serialNo} and order_no =#{orderNo} and item_no = #{itemNo} ")
    int updateChargeDetailConfirmFlag(MzRefundMedicineVo mzRefundMedicineVo);

    /**
     * 更新药房库存
     *
     * @param mzRefundMedicineVo
     * @return
     */
    @Update("update yp_base_yf set stock_amount =stock_amount + #{decAmount,jdbcType=DECIMAL} , stock_value =stock_value + round ( #{decAmount,jdbcType=DECIMAL} *  #{unitPrice,jdbcType=DECIMAL}, 2 ) " +
            "where charge_code =#{chargeCode} and serial =#{serial} and group_no =#{groupNo}  ")
    int updateBaseYf(MzRefundMedicineVo mzRefundMedicineVo);

    /**
     * 查询未发药处方
     *
     * @param chargeFeeParamsVo
     * @return
     */
    @Select({"<script>",
            "SELECT top ${pageSize} * ",
            "    FROM  (SELECT ROW_NUMBER() OVER (ORDER BY mz_charge_detail.charge_date desc) ",
            "AS RowNumber, rtrim(mz_charge_detail.patient_id)patient_id,",
            "         mz_charge_detail.times,",
            "         mz_charge_detail.receipt_no,",
            "         mz_charge_detail.order_no,",
            "         mz_charge_detail.group_no,",
            "         mz_charge_detail.real_no,",
            "         mz_charge_detail.name,",
            "         mz_charge_detail.doctor_code,",
            "         mz_charge_detail.charge_date,",
            "         mz_charge_detail.confirm_flag,",
            "         doctor_flag,",
            "         warn_dept,",
            "         windows_no_yf",
            " from mz_charge_detail,yp_zd_dict",
            "   WHERE   1=1 ",
            "<when test='name!=null'>",
            " and mz_charge_detail.name like '%${name}%'",
            "</when>",
            "<when test='patientId!=null'>",
            " and mz_charge_detail.patient_id = #{patientId}",
            "</when>",
            "<when test='realNo!=null'>",
            " and mz_charge_detail.real_no = #{realNo}",
            "</when>",
            "<when test='groupNoOut!=null'>",
            " and mz_charge_detail.group_no = #{groupNoOut}",
            "</when>",
            "<when test='beginDate!=null'>",
            " and charge_date <![CDATA[>=]]> #{beginDate}",
            "</when>",
            "<when test='endDate!=null'>",
            " and charge_date <![CDATA[<=]]> #{endDate}",
            "</when>",
            " and mz_charge_detail.charge_item_code = yp_zd_dict.code",
            " and mz_charge_detail.serial = yp_zd_dict.serial",
            " and isnull(yp_zd_dict.class_code,'') <![CDATA[<>]]> '210100'",
            " and mz_charge_detail.windows_no_yf  like (case #{groupNoOut} when '81' then '%' else '04' end ) ",
            " and ( mz_charge_detail.pay_mark='0' and mz_charge_detail.serial_no <![CDATA[>]]> 0)",
            "<when test='isSendAndMatch==0'>",
            " and (isnull(mz_charge_detail.confirm_flag,'0') in('2','5') OR mz_charge_detail.print_flag_yj='1') ",
            "</when>",
            "<when test='isSendAndMatch==1'>",
            " and (isnull(mz_charge_detail.confirm_flag,'0') in('0','2','5') OR mz_charge_detail.print_flag_yj='1') ",
            "</when>",
            " and (isnull(mz_charge_detail.confirm_zs,'') <![CDATA[<>]]> '1' ",
            " and (isnull(mz_charge_detail.confirm_zs,'') <![CDATA[<>]]> '4') ) group by confirm_flag,mz_charge_detail.patient_id,mz_charge_detail.times,mz_charge_detail.receipt_no,mz_charge_detail.order_no,mz_charge_detail.real_no,mz_charge_detail.charge_date,mz_charge_detail.group_no,mz_charge_detail.name,mz_charge_detail.doctor_code,mz_charge_detail.warn_dept,mz_charge_detail.charge_date,doctor_flag,windows_no_yf",
            ") as A WHERE RowNumber >#{pageSize}*#{pageIndex} order by RowNumber ASC",
            "</script>"})
    List<ChargeDetailInfoVo> selectFyclWfyPrescriptionPage(ChargeFeeParamsVo chargeFeeParamsVo);

    /**
     * 查询未发药处方条数
     *
     * @param chargeFeeParamsVo
     * @return
     */
    @Select({"<script>",
            "select count(1) from (SELECT DISTINCT mz_charge_detail.patient_id,",
            "         mz_charge_detail.times,",
            "         mz_charge_detail.receipt_no,",
            "         mz_charge_detail.order_no,",
            "         mz_charge_detail.group_no,",
            "         mz_charge_detail.real_no,",
            "         mz_charge_detail.name,",
            "         mz_charge_detail.doctor_code,",
            "         mz_charge_detail.charge_date,",
            "         mz_charge_detail.confirm_flag,",
            "         doctor_flag,",
            "         warn_dept,",
            "         windows_no_yf",
            " from mz_charge_detail,yp_zd_dict",
            "   WHERE   1=1 ",
            "<when test='name!=null'>",
            " and mz_charge_detail.name like '%${name}%'",
            "</when>",
            "<when test='patientId!=null'>",
            " and mz_charge_detail.patient_id = #{patientId}",
            "</when>",
            "<when test='realNo!=null'>",
            " and mz_charge_detail.real_no = #{realNo}",
            "</when>",
            "<when test='groupNoOut!=null'>",
            " and mz_charge_detail.group_no = #{groupNoOut}",
            "</when>",
            "<when test='beginDate!=null'>",
            " and charge_date <![CDATA[>=]]> #{beginDate}",
            "</when>",
            "<when test='endDate!=null'>",
            " and charge_date <![CDATA[<=]]> #{endDate}",
            "</when>",
            " and mz_charge_detail.charge_item_code = yp_zd_dict.code",
            " and mz_charge_detail.serial = yp_zd_dict.serial",
            " and isnull(yp_zd_dict.class_code,'') <![CDATA[<>]]> '210100'",
            " and mz_charge_detail.windows_no_yf  like (case #{groupNoOut} when '81' then '%' else '04' end ) ",
            " and ( mz_charge_detail.pay_mark='0' and mz_charge_detail.serial_no <![CDATA[>]]> 0)",
            " and (isnull(mz_charge_detail.confirm_flag,'0') in('2','5') OR mz_charge_detail.print_flag_yj='1') ",
            " and (isnull(mz_charge_detail.confirm_zs,'') <![CDATA[<>]]> '1' ",
            " and (isnull(mz_charge_detail.confirm_zs,'') <![CDATA[<>]]> '4') )) AS A ",
            "</script>"})
    int selectFyclWfyPrescriptionCount(ChargeFeeParamsVo chargeFeeParamsVo);

    /**
     * 查询门诊发票记录
     *
     * @param chargeDetailInfoVo
     * @return
     */
    @Select("select count(*) from mz_receipt_serial i where #{patientId} = i.patient_id" +
            " and #{times} = i.times" +
            " and #{realNo} = -i.receipt_sn")
    int selectMzReceiptSerialCount(ChargeDetailInfoVo chargeDetailInfoVo);

    /**
     * 查询已发药处方
     *
     * @param chargeFeeParamsVo
     * @return
     */
    @Select({"<script>",
            "SELECT top ${pageSize} * ",
            "    FROM  ( SELECT ROW_NUMBER() OVER (ORDER BY a.charge_date desc)  ",
            "AS RowNumber, rtrim(a.patient_id)patient_id,   ",
            "a.times,   ",
            "a.receipt_no,   ",
            "a.order_no,   ",
            "a.group_no,   ",
            "a.real_no,   ",
            "a.doctor_id as doctor_code,   ",
            "a.name     ,",
            "doctor_flag = '' ,",
            "warn_dept ,",
            "charge_date ,",
            "a.win_no,",
            "confirm_flag",
            " FROM yp_mz_fytj a",
            " WHERE   1=1     ",
            "<when test='name!=null'>",
            " and a.name like '%${name}%'",
            "</when>",
            "<when test='patientId!=null'>",
            " and a.patient_id = #{patientId}",
            "</when>",
            "<when test='realNo!=null'>",
            " and a.real_no = #{realNo}",
            "</when>",
            "<when test='groupNoOut!=null'>",
            " and a.group_no_out = #{groupNoOut}",
            "</when>",
            "<when test='beginDate!=null'>",
            " and charge_date <![CDATA[>=]]> #{beginDate}",
            "</when>",
            "<when test='endDate!=null'>",
            " and charge_date <![CDATA[<=]]> #{endDate}",
            "</when>",
            " and a.pay_mark='0' ",
            " and a.confirm_flag>'0'",
            "<when test='patientId!=null'>",
            " and a.patient_id = #{patientId}",
            "</when>",
            "<when test='name!=null'>",
            " and a.name = #{name}",
            "</when> group by a.patient_id,a.times,a.receipt_no,a.order_no,a.group_no,a.real_no,a.doctor_id,a.name,warn_dept ,charge_date ,a.win_no,confirm_flag",
            ") as A WHERE RowNumber >#{pageSize}*#{pageIndex} order by RowNumber ASC",
            "</script>"})
    List<ChargeDetailInfoVo> selectFyclYfyPrescriptionPage(ChargeFeeParamsVo chargeFeeParamsVo);

    /**
     * 查询已发药处方条数
     *
     * @param chargeFeeParamsVo
     * @return
     */
    @Select({"<script>",
            "SELECT count(1) ",
            "    FROM  ( SELECT DISTINCT a.patient_id,   ",
            "a.times,   ",
            "a.receipt_no,   ",
            "a.order_no,   ",
            "a.group_no,   ",
            "a.real_no,   ",
            "a.doctor_id,   ",
            "a.name     ,",
            "doctor_flag = '' ,",
            "warn_dept ,",
            "charge_date ,",
            "a.win_no,",
            "confirm_flag",
            " FROM yp_mz_fytj a",
            " WHERE   1=1     ",
            "<when test='name!=null'>",
            " and a.name like '%${name}%'",
            "</when>",
            "<when test='patientId!=null'>",
            " and a.patient_id = #{patientId}",
            "</when>",
            "<when test='realNo!=null'>",
            " and a.real_no = #{realNo}",
            "</when>",
            "<when test='groupNoOut!=null'>",
            " and a.group_no_out = #{groupNoOut}",
            "</when>",
            "<when test='beginDate!=null'>",
            " and charge_date <![CDATA[>=]]> #{beginDate}",
            "</when>",
            "<when test='endDate!=null'>",
            " and charge_date <![CDATA[<=]]> #{endDate}",
            "</when>",
            " and a.pay_mark='0' ",
            " and a.confirm_flag>'0'",
            "<when test='patientId!=null'>",
            " and a.patient_id = #{patientId}",
            "</when>",
            "<when test='name!=null'>",
            " and a.name = #{name}",
            "</when>",
            ") as A ",
            "</script>"})
    int selectFyclYfyPrescriptionCount(ChargeFeeParamsVo chargeFeeParamsVo);

    /**
     * 发药处理查询处方明细
     *
     * @param mzChargeDetail
     * @return
     */
    @Select({"<script>" +
            "SELECT rtrim(a.patient_id)patient_id,   " +
            "         a.price_time,   " +
            "         a.times,   " +
            "         a.receipt_no,   " +
            "         a.name  ," +
            "         a.order_no," +
            "         a.item_no," +
            "         drugname=case when a.pay_self='1' then '(免发)' else '' end  + c.name," +
            "         drug_flag=c.drug_flag," +
            "         specification=c.specification," +
            "         a.supply_code," +
            "         a.frequency," +
            "         a.drug_quan," +
            "         drug_unit=case when a.group_no='81' then c.pack_unit else isnull(a.drug_unit,'') end," +
            "yp_unit=c.pack_unit," +
            " c.mini_unit," +
            "         a.order_days," +
            "         doctor_code=a.doctor_code," +
            "         a.charge_date," +
            "         b.location," +
            "         quantity=a.quantity," +
            "         a.unit_price," +
            "         serial_no," +
            "         responce_type," +
            "         a.country_flag," +
            "         a.bill_item_code," +
            "         a.charge_item_code," +
            "         a.input_id," +
            "         a.input_date," +
            "         a.group_no," +
            "         a.serial," +
            "         a.self_flag," +
            "         a.drug_win," +
            "         a.warn_dept, " +
            "         confirm_flag=case when isnull(b.stock_amount,0) <![CDATA[>=]]> round(quantity * drug_win,2) then '1'else '0'end," +
            "         infusion=c.infusion_flag," +
            "         zj_flag=c.zj_flag," +
            "         zs_flag=''," +
            "         tc_no=a.tc_no," +
            "         cash_id," +
            "         confirm_zs," +
            "         a.windows_no_yf," +
            "         a.instruction_code," +
            "         a.instruction_text," +
            "         doctor_flag=isnull(a.doctor_flag,'')," +
            "         pay_self=isnull(a.pay_self,'')," +
            "         a.group_order," +
            "         try_flag," +
            "         yf_no=a.back_serial," +
            "         real_no=a.real_no," +
            "         cy_mtjs=a.cy_mtjs," +
            "         cy_jsl=a.cy_jsl," +
            "         cy_zql=a.cy_zql," +
            "         cy_fj=a.cy_fj  ," +
            "         cy_ffcs=a.cy_ffcs," +
            "         cy_fysm=a.cy_fysm," +
            "         cy_jssm=a.cy_jssm," +
            "         cy_dj=a.cy_dj," +
            "         b.location," +
            "         a.jz_flag," +
            "         a.group_type," +
            "         dec_amount=-a.dec_amount," +
            "         pack_unit = c.pack_unit," +
            "         stock_amount = isnull(b.stock_amount,0)," +
            "         abbr_name=case when isnull(d.abbr_name,'')='' then d.name else d.abbr_name end," +
            "         try_result = a.try_result," +
            "         yb_zf_flag=a.yb_zf_flag," +
            " a.confirm_id," +
            "         print_flag_yj=isnull(a.print_flag_yj,'')," +
            "         confirm_flag_b=confirm_flag" +
            "    FROM mz_charge_detail a, yp_base_yf b,yp_zd_dict c,yp_zd_manufactory d" +
            " where    a.patient_id=#{patientId} and" +
            "         a.times     =#{times} and" +
            "         a.receipt_no=#{receiptNo} and" +
            "         a.order_no =#{orderNo}   and" +
            "         a.charge_item_code *=b.charge_code and " +
            "         a.serial     *=b.serial and " +
            "         b.group_no   =#{groupNo} and  " +
            "         a.bill_item_code <![CDATA[<>]]>'TC'and" +
            "         a.charge_item_code=c.code and" +
            "         a.serial=c.serial and" +
            "         c.manu_code *= d.code and" +
            "         a.real_no = #{realNo} ",
            "</script>"})
    List<MzSendMedicineVo> selectFyclPrescriptionDetail(MzChargeDetail mzChargeDetail);

    /**
     * 查询药品单位名称
     *
     * @param code
     * @return
     */
    @Select("select name from yp_zd_unit where code =#{code}")
    String selectYpUnitName(String code);

    /**
     * 发药处理添加门诊发药统计
     *
     * @param ypMzFytj
     * @return
     */
    @Insert("INSERT INTO yp_mz_fytj (" +
            "patient_id," +
            "times," +
            "receipt_no," +
            "order_no," +
            "item_no," +
            "charge_item_code," +
            "bill_item_code," +
            "quantity," +
            "unit_price," +
            "pay_mark," +
            "serial," +
            "country_flag," +
            "confirm_flag," +
            "serial_no," +
            "name," +
            "charge_date," +
            "group_no," +
            "confirm_time," +
            "drugname," +
            "specification," +
            "confirm_id," +
            "confirm_name," +
            "doctor_id," +
            "doctor_name," +
            "cy_fy," +
            "input_id," +
            "input_date," +
            "response_type," +
            "self_flag," +
            "win_no," +
            "group_no_out," +
            "warn_dept," +
            "input_time," +
            "real_no " +
            ") SELECT " +
            "a.patient_id ," +
            "a.times ," +
            "a.receipt_no ," +
            "a.order_no ," +
            "a.item_no ," +
            "a.charge_item_code ," +
            "a.bill_item_code ," +
            " CASE" +
            " WHEN yb_zf_flag = 1 THEN" +
            " a.quantity- 1 ELSE a.quantity " +
            " END," +
            " CASE" +
            " WHEN yb_zf_flag = 1 " +
            " AND a.quantity > 1 THEN" +
            " ( a.unit_price * a.quantity ) / ( a.quantity- 1 ) ELSE a.unit_price " +
            " END," +
            " a.pay_mark ," +
            " a.serial ," +
            " a.country_flag ," +
            " '1'," +
            "a.serial_no ," +
            "a.name ," +
            "a.charge_date ," +
            "a.group_no ," +
            "#{confirmTime}," +
            "left(b.name,15) ," +
            "left(b.specification,8)specification ," +
            "#{confirmId}," +
            "#{confirmName}," +
            "#{doctorId}," +
            "#{doctorName}," +
            "a.drug_win ," +
            "a.input_id ," +
            "a.input_time ," +
            "a.responce_type ," +
            "a.self_flag ," +
            "#{winNo}," +
            "#{groupNo}," +
            "a.warn_dept ," +
            "#{inputTime}," +
            "a.real_no " +
            " FROM " +
            " mz_charge_detail a," +
            " yp_zd_dict b," +
            " yp_base_yf c " +
            " WHERE" +
            " a.patient_id = #{patientId} " +
            " AND a.times = #{times} " +
            " AND a.receipt_no = #{receiptNo} " +
            " AND a.order_no = #{orderNo} " +
            " AND a.item_no = #{itemNo} " +
            " AND a.charge_item_code = #{chargeItemCode} " +
            " AND a.serial = #{serial} " +
            " AND a.charge_item_code = b.code " +
            " AND a.serial = b.serial " +
            " AND a.charge_item_code = c.charge_code " +
            " AND a.serial = c.serial " +
            " AND c.group_no = #{groupNo} " +
            " AND ( IsNull( a.confirm_flag , '0' ) IN ( '0', '2','5' ) OR a.print_flag_yj = '1' )")
    int insertFyclFytj(YpMzFytj ypMzFytj);

    /**
     * 发药处理修改收费详细发药状态
     *
     * @param ypMzFytj
     * @return
     */
    @Update("UPDATE mz_charge_detail " +
            " SET confirm_flag = '1'," +
            " print_flag_yj =" +
            " CASE" +
            " WHEN print_flag_yj = '1' THEN" +
            " '2' ELSE print_flag_yj " +
            " END," +
            " confirm_time = #{confirmTime}," +
            " confirm_id = #{confirmId} " +
            " WHERE" +
            " patient_id = #{patientId} " +
            " AND times = #{times} " +
            " AND receipt_no = #{receiptNo} " +
            " AND order_no = #{orderNo} " +
            " AND item_no = #{itemNo} " +
            " AND charge_item_code = #{chargeItemCode} " +
            " AND serial = #{serial} " +
            " AND group_no = #{groupNo} " +
            " AND ( IsNull( confirm_flag, '0' ) IN ( '0', '2','5' ) OR print_flag_yj = '1' ) ")
    int updateFyclChargeDetailConfirmFlag(YpMzFytj ypMzFytj);

    /**
     * 配药处理修改收费详细发药状态
     *
     * @param mzChargeDetail
     * @return
     */
    @Update("UPDATE mz_charge_detail " +
            " SET confirm_flag = #{confirmFlag}" +
            " WHERE" +
            " patient_id = #{patientId} " +
            " AND order_no = #{orderNo} " +
            " AND times = #{times} " )
    int updateDispensingChargeDetailConfirmFlag(MzChargeDetail mzChargeDetail);

    /**
     * 查询放射科发药记录历史时间
     * @param radSendRecord
     * @return
     */
    @Select({"<script>",
            "SELECT top ${pageSize} * ",
            "    FROM  (SELECT ROW_NUMBER() OVER (ORDER BY c.confirm_time desc) ",
            "AS RowNumber, c.confirm_time as send_date,c.confirm_id ",
            " from mz_charge_detail c,yp_zd_dict y",
            "   WHERE   1=1 ",
            " and c.group_no = '71' ",
            " and c.confirm_flag = '1' ",
            " and c.charge_item_code = y.code and y.class_code = '210100' ",
            " group by c.confirm_time,c.confirm_id",
            ") as A WHERE RowNumber >#{pageSize}*#{pageIndex} order by RowNumber ASC",
            "</script>"})
    List<RadSendRecord> selectRadSendRecordPage(RadSendRecord radSendRecord);

    /**
     * 查询放射科发药记录总条数
     * @param radSendRecord
     * @return
     */
    @Select({"<script>",
            "SELECT count(1) ",
            " from (select c.confirm_time,c.confirm_id " ,
            " from mz_charge_detail c,yp_zd_dict y",
            " WHERE   1=1 ",
            " and c.group_no = '71' ",
            " and c.confirm_flag = '1' ",
            " and c.charge_item_code = y.code and y.class_code = '210100' ",
            " group by c.confirm_time,c.confirm_id) a",
            "</script>"})
    Integer selectRadSendRecordCount(RadSendRecord radSendRecord);

    /**
     * 查询放射科当前时间的上一次发药时间
     * @param date
     * @return
     */
    @Select("select top 1 c.confirm_time" +
            " from mz_charge_detail c,yp_zd_dict y" +
            " where c.group_no = '71' and c.confirm_time < #{date} and c.confirm_flag = '1'" +
            "  and c.charge_item_code = y.code and y.class_code = '210100'  order by confirm_time desc")
    Date selectLastRadSendDate(@Param("date") Date date);

    /**
     * 根据发药时间查询放射科发药统计
     * @param confirmTime
     * @return
     */
    @Select("select sum(c.quantity)quantity,y.code,y.name,y.manu_code,y.specification,y.serial" +
            " from mz_charge_detail c,yp_zd_dict y" +
            " where c.group_no = '71' and c.confirm_time = #{confirmTime} and c.confirm_flag = '1'" +
            "  and c.charge_item_code = y.code and c.serial = y.serial and y.class_code = '210100' group by y.code,y.name,y.manu_code,y.specification,y.serial")
    List<Map<String, Object>> selectRadSendStatByConfirmTime(@Param("confirmTime") String confirmTime);

    /**
     * 根据缴费时间范围查询放射科待发药统计
     * @param startTime
     * @param endTime
     * @return
     */
    @Select("select sum(c.quantity)quantity,y.code,y.name,y.manu_code,y.specification,y.serial,c.unit_price" +
            " from mz_charge_detail c,yp_zd_dict y " +
            " where c.group_no = '71' and c.charge_date > #{startTime} and c.charge_date < #{endTime} and c.confirm_flag in (0,2) and serial_no > 0" +
            "  and c.charge_item_code = y.code and c.serial = y.serial and y.class_code = '210100' group by y.code,y.name,y.manu_code,y.specification,y.serial,c.unit_price")
    List<Map<String, Object>> selectRadSendStat(@Param("startTime") String startTime,@Param("endTime") String endTime);

    /**
     * 根据缴费时间范围查询待发药处方
     * @param startTime
     * @param endTime
     * @return
     */
    @Select("select rtrim(c.patient_id)patient_id,c.times,c.order_no,c.quantity,c.unit_price,c.charge_item_code,y.serial " +
            " from mz_charge_detail c,yp_zd_dict y " +
            " where c.group_no = '71' and c.charge_date > #{startTime} and c.charge_date < #{endTime} and c.confirm_flag in (0,2) and serial_no > 0" +
            "  and c.charge_item_code = y.code and c.serial = y.serial and y.class_code = '210100' group by c.patient_id,c.times,c.order_no,c.quantity,c.unit_price,c.charge_item_code,y.serial")
    List<MzChargeDetail> selectRadSendPrescription(@Param("startTime") String startTime,@Param("endTime") String endTime);

    /**
     * 修改当前病人当前就诊次数下的西药房发药信息
     *
     * @param mzChargeDetail
     * @return
     */
    @Update({"<script>",
            "update mz_charge_detail ",
            "<trim prefix='set' prefixOverrides=',' suffix=' where rtrim(patient_id) = #{patientId} and times=#{times} and order_no=#{orderNo} and group_no=71 and confirm_flag in (0,2)' >",
            "<when test='confirmTime!=null'>",
            ",confirm_time =#{confirmTime,jdbcType=TIMESTAMP}",
            "</when>",
            "<when test='confirmId!=null'>",
            ",confirm_id =#{confirmId,jdbcType=TIMESTAMP}",
            "</when>",
            "<when test='confirmFlag!=null'>",
            ",confirm_flag =#{confirmFlag,jdbcType=CHAR}",
            "</when>",
            "</trim>",
            "</script>"})
    int updateMzChargeDetailDispensing(MzChargeDetail mzChargeDetail);

    /**
     * 是否为放射科发药
     * @param code
     * @return
     */
    @Select("select (case when count(1)>0 then 1 else 0 end ) from yp_zd_dict where code = #{code} and class_code = '210100'")
    int selectIsRadSend(@Param("code") String code);
}
