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

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.*;
import thyyxxk.webserver.entity.datamodify.GetDropdownBox;
import thyyxxk.webserver.entity.datamodify.ZyDetailCharge;
import thyyxxk.webserver.entity.inpatient.FeeSelect;
import thyyxxk.webserver.entity.inpatient.WeiGuiFeiYongFenXi;
import thyyxxk.webserver.entity.inpatient.ZyActpatient;
import thyyxxk.webserver.entity.projectEntry.DrugReturnForm;
import thyyxxk.webserver.entity.xmlr.vo.FeeResult;
import thyyxxk.webserver.service.inpatient.xmlr.XiangMuLuRuService;

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

/**
 * <p>
 * 描述： 项目录入
 * </p>
 *
 * @author xc
 * @date 2021-08-02 10:37
 */
@Mapper
public interface XiangMuLuRuDao extends BaseMapper<ZyDetailCharge> {

    @Select("<script>" +
            "select a.admiss_times, a.inpatient_no, a.order_no, a.gen_time, a.ledger_sn, a.detail_sn,  " +
            "                   a.trans_flag_yb,    " +
            "       a.charge_date, a.op_id_code, d.name op_name, a.charge_code, a.charge_fee, a.charge_status ,  " +
            "       bill_item_code= b.bill_item_zy, bill_item_name=(select name from zy_bill_item where code = b.bill_item_zy),  " +
            "       charge_name=b.name, spec=b.charge_unit, a.charge_code_mx, a.serial, price=0.0000, charge_amount=a.charge_amount,  " +
            "       exec_dept=(select name from zd_unit_code where code = a.exec_unit),  " +
            "       dept_code = (select name from zd_unit_code where code = a.ward_code) ,  " +
            "       self_flag=isnull(a.self_flag,0), suppress_flag=a.suppress_flag, group_no='00',  " +
            "       separate_flag=a.separate_flag, ope_flag=a.ope_flag, drugname=c.name, isnull(a.ori_detail_sn,0) ori_detail_sn  " +
            "    FROM  (select top ${pageSize} * from (select ROW_NUMBER() OVER (order by charge_date ${riQiPaiXu} ) as RowNumber,* from zy_detail_charge a " +
            "  ${ew.customSqlSegment} " +
            ") temp where RowNumber > ${pageSize} * (${currentPage} - 1 )) a  " +
            "        left join zd_charge_item b on (a.charge_code = b.code)  " +
            "        left join  yp_zd_dict c on (a.serial = c.serial and c.code= a.charge_code_mx)  " +
            "        left join a_employee_mi d on (d.code = a.op_id_code)  " +
            "</script>")
    List<ZyDetailCharge> huanZheYaoPinFeiYongV2(@Param(Constants.WRAPPER) QueryWrapper<?> queryWrapper,
                                                @Param("currentPage") long currentPage,
                                                @Param("pageSize") long pageSize,
                                                @Param("riQiPaiXu") String riQiPaiXu);

    @Select("select count(*) total,sum(charge_fee) charge_fee" +
            "    FROM zy_detail_charge a  " +
            " ${ew.customSqlSegment} ")
    XiangMuLuRuService.FeiYongTotal huanZheYaoPinFeiYongTotalV2(@Param(Constants.WRAPPER) QueryWrapper<?> queryWrapper);

    @Select(" select count(*) total,sum(charge_fee) charge_fee" +
            " from zy_detail_charge ${ew.customSqlSegment} ")
    XiangMuLuRuService.FeiYongTotal huanZheFeiYongToatalV2(@Param(Constants.WRAPPER) QueryWrapper<?> queryWrapper);


    @Select("            SELECT " +
            "                   a.admiss_times, " +
            "                   a.inpatient_no, " +
            "                   a.order_no,    " +
            "                   a.gen_time,      " +
            "                   a.ledger_sn," +
            "                   a.trans_flag_yb,    " +
            "                   a.detail_sn,    " +
            "                   a.charge_date,    " +
            "                   a.op_id_code,    " +
            "                   c.name op_name,    " +
            "                   a.charge_code,    " +
            "                   a.charge_fee,    " +
            "                   a.charge_status  ,    " +
            "                   bill_item_code = b.bill_item_zy,    " +
            "                   bill_item_name=(select name from zy_bill_item where code = b.bill_item_zy),    " +
            "                   charge_name=b.name,    " +
            "                   spec=b.charge_unit,    " +
            "                   a.charge_code_mx,    " +
            "                   a.serial,    " +
            "                   price=0.0000,    " +
            "                   charge_amount=a.charge_amount,    " +
            "                   exec_dept=(select name from zd_unit_code where code = a.exec_unit),    " +
            "                   dept_code = (select name from zd_unit_code  where code = a.ward_code) ,    " +
            "                   self_flag=a.self_flag,    " +
            "                   suppress_flag=a.suppress_flag,    " +
            "                   group_no='00',    " +
            "                   separate_flag=a.separate_flag,    " +
            "                   ope_flag=a.ope_flag," +
            "                   specification,    " +
            "                   drugname=b.name,    " +
            "                   isnull(a.ori_detail_sn,0) ori_detail_sn    " +
            "                   FROM  (select top ${pageSize} * from (select ROW_NUMBER() OVER (order by charge_date ${riQiPaiXu} ) as RowNumber,*  " +
            "from zy_detail_charge " +
            "${ew.customSqlSegment}" +
            ") temp where RowNumber > ${pageSize} * (${currentPage}-1) )  a " +
            "  left join  zd_charge_item b on (a.charge_code=b.code) " +
            "  left join a_employee_mi c on (c.code = a.op_id_code)")
    List<ZyDetailCharge> huanZheXiangMuFeiYongV2(@Param(Constants.WRAPPER) QueryWrapper<?> queryWrapper,
                                                 @Param("currentPage") long currentPage,
                                                 @Param("pageSize") long pageSize,
                                                 @Param("riQiPaiXu") String riQiPaiXu);


    @Select("select detail_sn,ori_detail_sn from zy_detail_charge where inpatient_no=#{inpatientNo} and admiss_times = #{admissTimes} " +
            "and charge_fee <= 0 and ori_detail_sn is not null and infant_flag = #{infantFlag}")
    List<ZyDetailCharge> tuiFeiList(@Param("inpatientNo") String inpatientNo,
                                    @Param("admissTimes") Integer admissTimes,
                                    @Param("infantFlag") int infantFlag);

    /**
     * @param pyCode 拼音编码
     * @return 获取项目的名称和code
     */
    @Select("select distinct code, " +
            "                name, " +
            "                specification=N'诊疗项目', " +
            "                manufactor   = (select name from yp_zd_manufactory where manu_code = yp_zd_manufactory.code) from  zd_charge_item where (py_code like #{pyCode} or code like #{pyCode} or name like #{pyCode})" +
            "union all " +
            "select distinct code, " +
            "                name, " +
            "                specification, " +
            "                manufactor = (select name from yp_zd_manufactory where manu_code = yp_zd_manufactory.code) from  yp_zd_dict where (py_code like #{pyCode} or code like #{pyCode} or name like #{pyCode})")
    List<FeeSelect> getChargeCode(String pyCode);

    /**
     * 退药汇总单
     */
    @Insert("insert into yz_yp_page_no (page_no, dept_code, ward_code, submit_time, submit_name, " +
            "                           submit_flag, page_class, page_name, group_no,refund_reason) " +
            "values (#{pageNo}, #{wardCode}, #{wardCode}, getdate(), #{userCode}, 1, 'HT', '护士退药单', #{groupNo},#{refundReason})")
    void drugReturnForm(@Param("pageNo") Integer pageNo,
                        @Param("wardCode") String wardCode,
                        @Param("userCode") String userCode,
                        @Param("groupNo") String groupNo,
                        @Param("refundReason") Integer refundReason);

    /**
     * 获取模板
     *
     * @param queryWrapper 根据科室来搜索
     * @return 返回模板信息
     */
    @Select("SELECT rtrim(pattern_name) as name, " +
            "       max(group_no)       as group_no, " +
            "       max(op_id_code)     as op_id_code, " +
            "       op_dept " +
            "FROM zy_charge_pattern " +
            " ${ew.customSqlSegment} " +
            "group by pattern_name, op_dept ")
    Page<ZyDetailCharge> getMuBan(IPage<ZyDetailCharge> page,
                                  @Param(Constants.WRAPPER) QueryWrapper<?> queryWrapper);

    /**
     * 这个是获取模板下面的那些项目具体信息
     *
     * @param patternName 模板名称
     * @param opIdCode    录入人id
     * @return 返回模板 下面的详细信息
     */
    @Select("SELECT charge_name=RTRIM(b.name), " +
            "       charge_code_mx=b.code, " +
            "       amount=a.amount," +
            "       rtrim(b.yb_comment) as yb_comment," +
            "       rtrim(b.yb_comment) as manu_name, " +
            "       charge_amount=b.charge_amount, " +
            "       op_id_code=a.op_id_code, " +
            "       bill_item_code=b.bill_item_zy, " +
            "       bill_item_name=(select name from zy_bill_item where code = b.bill_item_zy)," +
            "       national_code," +
            "       national_name," +
            "       descriptions," +
            "       a.serial,a.group_no  " +
            "FROM zy_charge_pattern a, " +
            "     zd_charge_item b " +
            "WHERE (a.pattern_name =  #{patternName}) " +
            "  and a.op_id_code =  #{opIdCode} " +
            "  and a.charge_code = b.code " +
            "  and isnull(a.serial, '01') = '00' " +
            "  and (isnull(b.del_flag, '0') <> '1') " +
            "union all " +
            "SELECT charge_name=RTRIM(d.name), " +
            "       charge_code_mx=b.charge_code, " +
            "       amount=a.amount," +
            "       '' as yb_comment," +
            "       manu_name = (select name from yp_zd_manufactory where yp_zd_manufactory.code = manu_code),  " +
            "       charge_amount=d.pack_retprice, " +
            "       op_id_code=a.op_id_code, " +
            "       bill_item_code=d.bill_item_zy, " +
            "       bill_item_name=(select name from zy_bill_item where code = d.bill_item_zy)," +
            "       national_code," +
            "       national_name," +
            "       specification as descriptions," +
            "       a.serial,a.group_no " +
            "FROM zy_charge_pattern a, " +
            "     yp_base_yf b, " +
            "     yp_zd_dict d " +
            "WHERE (a.pattern_name =  #{patternName}) " +
            "  and a.op_id_code =  #{opIdCode} " +
            "  and a.group_no = b.group_no " +
            "  and a.charge_code = b.charge_code " +
            "  and isnull(a.serial, '01') = b.serial " +
            "  and b.charge_code = d.code " +
            "  and b.serial = d.serial " +
            "  and isnull(d.del_flag, '0') <> '1' " +
            "  and isnull(b.visible_flag_zy, '0') <> '1'")
    List<ZyDetailCharge> getMuBanXinXi(@Param("patternName") String patternName, @Param("opIdCode") String opIdCode);

    /**
     * 搜索项目信息
     *
     * @param pyCode 拼音码
     * @return 返回模板信息
     */
    @Select("SELECT a.code charge_code," +
            "       a.code charge_code_mx, " +
            "       a.exec_unit dept_code," +
            "       rtrim(a.yb_comment) as  yb_comment," +
            "       rtrim(a.yb_comment) as manu_name," +
            "       RTRIM(a.name) charge_name, " +
            "       a.charge_amount," +
            "       amount = 1, " +
            "       bill_item_code=a.bill_item_zy, " +
            "       bill_item_name=(select name from zy_bill_item where code = a.bill_item_zy)," +
            "       a.py_code, " +
            "       a.d_code, " +
            "       a.suppress_flag, " +
            "       a.bill_item_out, " +
            "       a.n_code, " +
            "       self_flag =isnull(a.self_flag,'0'), " +
            "       a.separate_flag , " +
            "       spec=charge_unit, " +
            "       exec_unit_name=b.name, " +
            "       serial='00', " +
            "       group_no='00'," +
            "       national_code," +
            "       national_name," +
            "       descriptions " +
            "FROM zd_charge_item a left join zd_unit_code b on (a.exec_unit = b.code) " +
            "where    isnull(a.del_flag,'') <> '1' and " +
            "         isnull(a.in_out_flag,'') <> '1' and " +
            "         (a.py_code like #{pyCode} or a.code like #{pyCode} or a.name like #{pyCode})")
    List<ZyDetailCharge> queryXiangMu(String pyCode);


    @Select("SELECT distinct a.code charge_code_mx, " +
            "                charge_code = (select charge_code from zy_bill_item where a.bill_item_zy = zy_bill_item.code), " +
            "                a.serial," +
            "                manu_name = (select name from yp_zd_manufactory where yp_zd_manufactory.code = manu_code), " +
            "                RTRIM(a.name) charge_name, " +
            "                a.pack_size amount, " +
            "                charge_amount=a.pack_retprice, " +
            "                spec=a.specification, " +
            "                a.drug_flag  , " +
            "                a.fix_price, " +
            "                c.stock_amount, " +
            "                self_flag=isnull(a.self_flag,'0'), " +
            "                suppress_flag=a.suprice_flag, " +
            "                group_no=c.group_no, " +
            "                bill_item_code=a.bill_item_zy, " +
            "                bill_item_name=(select name from zy_bill_item where code = a.bill_item_zy), " +
            "                ybbz=a.yb_comment_new, " +
            "                yblx=(select name from yp_zd_yb_type where code=a.yb_flag_new), " +
            "                xnh=a.xnh_flag," +
            "                national_code," +
            "                national_name " +
            "FROM yp_zd_dict a ,yp_print_name b  , " +
            "     yp_base_yf c " +
            "where " +
            "(a.py_code like #{pyCode} or a.name like #{pyCode} or a.code like #{pyCode}) and" +
            "        a.code=b.charge_code and " +
            "        a.code=c.charge_code and " +
            "        a.serial=c.serial    and " +
            "        c.visible_flag_zy <> '1' and" +
            "        isnull(a.del_flag,0) = 0 and group_no = '73' ")
    List<ZyDetailCharge> queryYaoPin(String pyCode);

    /**
     * 插入项目录入费用
     *
     * @param zyDetailCharge 插入的信息
     * @param list           费用详情
     */
    @Insert("<script>" +
            "INSERT INTO zy_detail_charge (inpatient_no, admiss_times, ledger_sn, detail_sn, charge_date  " +
            "  , op_id_code, charge_code, infant_flag, charge_status, charge_fee  " +
            "  , self_flag, separate_flag, suppress_flag, ward_code, dept_code  " +
            "  , order_no, ope_flag, exec_unit, charge_amount, gen_time  " +
            "  , zy_serial_no, charge_code_mx, serial,ss_code,yb_self_flag,refer_physician,doctor_code,receipt_no)  " +
            "VALUES " +
            "<foreach collection='list' item='item' separator=','>" +
            "(#{zyDetailCharge.inpatientNo}, #{zyDetailCharge.admissTimes}, #{ledgerSn}, #{item.detailSn}, #{zyDetailCharge.chargeDate}  " +
            "  , #{zyDetailCharge.opIdCode}, #{item.chargeCode}, #{infantFlag}, '2', cast(#{item.chargeAmount,jdbcType=DECIMAL} as decimal(14,5))  " +
            "  , '0', '0', NULL, #{zyDetailCharge.ward}, #{zyDetailCharge.dept}  " +
            "  , #{item.orderNo}, '0', #{item.deptCode}, cast(#{item.amount,jdbcType=DECIMAL} as decimal(14,5)), getdate()  " +
            "  , #{zyDetailCharge.zySerialNo}, #{item.chargeCodeMx}, #{item.serial},#{zyDetailCharge.ssCode}" +
            "  ,#{item.ybSelfFlag} " +
            ",#{zyDetailCharge.referPhysician},null,#{receiptNo})" +
            "</foreach>" +
            "</script>")
    void chaRuFeiYong(@Param("zyDetailCharge") ZyDetailCharge zyDetailCharge,
                      @Param("list") List<ZyDetailCharge> list,
                      @Param("infantFlag") int infantFlag,
                      @Param("ledgerSn") Integer ledgerSn,
                      @Param("chargeDate") String chargeDate,
                      @Param("receiptNo") String receiptNo);

    @Insert("<script>" +
            "insert into zy_drug ( inpatient_no , admiss_times , ledger_sn , detail_sn , charge_date , op_id_code ," +
            " charge_code , infant_flag , " +
            "charge_fee , self_flag , separate_flag , supress_flag , ward_code , dept_code , ope_flag , exec_unit ," +
            " retprice , amount , zy_serial_no , " +
            "serial , group_no , occ_cate ) values " +
            "<foreach collection='list' item='item' index='index' separator=','>" +
            "( #{zyDetailCharge.inpatientNo} , #{zyDetailCharge.admissTimes} , #{ledgerSn} , #{item.detailSn} , DATEADD(second, ${index}, GETDATE())  , #{opIdCode} , " +
            "#{item.chargeCodeMx} ,  #{infantFlag} , " +
            // 金额
            "cast(#{item.chargeFee,jdbcType=DECIMAL} as decimal(14,5))," +
            " '0' , '0' , null , #{zyDetailCharge.ward} , #{zyDetailCharge.dept} , '0' , #{item.deptCode} ," +
            // 单价
            "  cast(#{item.chargeFee,jdbcType=DECIMAL} / #{item.chargeAmount,jdbcType=DECIMAL} as decimal(14,5)) , " +
            // 数量
            "cast(#{item.chargeAmount,jdbcType=DECIMAL} as decimal(14,5)) " +
            ", #{zyDetailCharge.zySerialNo} , #{item.serial} , #{item.groupNo} ,'1' )" +
            "</foreach>" +
            "</script>")
    void shenQingYaoPing(@Param("zyDetailCharge") ZyDetailCharge zyDetailCharge,
                         @Param("list") List<ZyDetailCharge> list,
                         @Param("infantFlag") int infantFlag,
                         @Param("ledgerSn") Integer ledgerSn,
                         @Param("opIdCode") String opIdCode);

    /**
     * 查看模板名称是否存在
     *
     * @param patternName 模板名字
     * @param opIdCode    创建模板的人 code
     * @return 返回条数
     */
    @Select("select count(*) from zy_charge_pattern where pattern_name =  #{patternName} and op_id_code <> #{opIdCode} ")
    Integer chaKanMuBanMingChengSFcunZi(@Param("patternName") String patternName,
                                        @Param("opIdCode") String opIdCode);

    /**
     * 删除模板
     *
     * @param patternName 模板名字
     * @param opIdCode    创建模板的人 code
     */
    @Select("delete zy_charge_pattern where pattern_name = #{patternName} and op_id_code = #{opIdCode} ")
    Integer delMuBan(@Param("patternName") String patternName,
                     @Param("opIdCode") String opIdCode);

    /**
     * 保存模板
     *
     * @param param 保存的数据
     */
    @Insert("<script>" +
            "INSERT INTO zy_charge_pattern ( charge_name, " +
            "                               pattern_name, " +
            "                               charge_code,   " +
            "                               group_no," +
            "                               serial,  " +
            "                               amount, " +
            "                               self,  " +
            "                               separate," +
            "                               suppress, " +
            "                               infant,  " +
            "                               per1," +
            "                               per2," +
            "                               per3," +
            "                               per4," +
            "                               per5," +
            "                               item_no,  " +
            "                               op_id_code, " +
            "                               py_code, " +
            "                               d_code, " +
            "                               charge_fee, " +
            "                               op_dept, " +
            "                               default_no " +
            "                               )" +
            "                               VALUES " +
            "<foreach collection='param.list' item='item' index='index' separator=','>" +
            "( substring(#{item.chargeName},0,15) , #{param.name} , #{item.chargeCodeMx} , #{item.groupNo}, #{item.serial}, cast(#{item.amount,jdbcType=DECIMAL} as decimal(14,5)) , " +
            "'0', '0', '0', '0', 1, 1, 1, 1, 1, #{index} + 1 , #{param.opIdCode} ,  #{param.pyCode} , #{param.dCode} , cast(#{item.chargeAmount,jdbcType=DECIMAL} as decimal(14,5)) ," +
            " #{param.deptCode} , " +
            "#{param.defaultNo} )" +
            "</foreach>" +
            "</script>")
    void baoCunMuBan(@Param("param") ZyDetailCharge param);

    /**
     * 获取医嘱的费用
     *
     * @param inpatientNo 住院号
     * @param admissTimes 住院次数
     * @param ZF          正的费用还是负的费用
     * @return 返回list 费用集合
     */
    @Select("<script>" +
            "select charge_date,charge_fee,charge_amount,charge_code_mx,detail_sn,ori_detail_sn,ABS(order_no),order_no,ledger_sn,exec_unit " +
            "from zy_detail_charge where  inpatient_no = #{inpatientNo} and admiss_times = #{admissTimes} and  ledger_sn = #{ledgerSn}  " +
            "and ori_detail_sn is null and charge_amount ${ZF} 0 and charge_fee ${ZF} 0 and infant_flag = #{infantFlag} and trans_flag_yb &lt;&gt; 2 " +
            "<if test=\"list.size >0 \">" +
            "and exec_unit in " +
            "<foreach collection='list' item='item' index='index' open='(' close=')' separator=','>" +
            "#{item}" +
            "</foreach>" +
            "</if>" +
            "</script>")
    List<ZyDetailCharge> getYiZhuFeiYong(@Param("inpatientNo") String inpatientNo,
                                         @Param("admissTimes") Integer admissTimes,
                                         @Param("ledgerSn") Integer ledgerSn,
                                         @Param("ZF") String ZF,
                                         @Param("infantFlag") int infantFlag,
                                         @Param("list") List<String> list);

    /**
     * @param inpatientNo 住院号
     * @param admissTimes 住院次数
     * @param yiZhuPiPei  需要匹配的流水信息
     */
    @Update("<script>" +
            "<foreach collection='list' item='item' index='index' open='' close='' separator=';'>" +
            "update zy_detail_charge set ori_detail_sn = #{item.oriDetailSn} where inpatient_no= #{inpatientNo} and admiss_times = #{admissTimes} " +
            "and detail_sn = #{item.detailSn} and infant_flag = #{infantFlag} and ledger_sn = #{item.ledgerSn} " +
            "</foreach>" +
            "</script>")
    void yiZhuTuiFeiPiPei(@Param("inpatientNo") String inpatientNo,
                          @Param("admissTimes") Integer admissTimes,
                          @Param("list") List<ZyDetailCharge> yiZhuPiPei,
                          @Param("infantFlag") int infantFlag);

    @Select("SELECT small_dept FROM zd_dept_all where dept = #{dept}")
    Set<String> chaXunZhiZXinKeShi(@Param("dept") String dept);


    @Select("select role_id from dj_user_role where  user_code = #{code}")
    List<Integer> huoQuJueSe(@Param("code") String code);


    @Select("select code,rtrim(name) name from zd_unit_code where code not like '8%'")
    List<GetDropdownBox> getDept();

    @Select("select code,rtrim(name) name from zd_unit_code where code like '8%'")
    List<GetDropdownBox> getWard();

    @Select("SELECT small_dept code,name=(select name from zd_unit_code where code = small_dept) FROM zd_dept_all where dept = #{dept}")
    List<GetDropdownBox> getBingQuDuiYingKeShi(@Param("dept") String dept);


    @Select("select count(*) from zy_actpatient where inpatient_no = #{inpatientNo} and admiss_times = #{admissTimes}")
    Integer getHuanZheSFZaiYuan(@Param("inpatientNo") String inpatientNo,
                                @Param("admissTimes") Integer admissTimes);

    @Select("select case settle_type when '1' then '0' else settle_type end as settle_type from zy_ledger_file where " +
            "inpatient_no = #{inpatientNo} and admiss_times = #{admissTimes} " +
            "and ledger_sn = (select max(ledger_sn)ledger_sn from zy_ledger_file where  zy_ledger_file.inpatient_no = #{inpatientNo} " +
            "and zy_ledger_file.admiss_times = #{admissTimes}) ")
    Integer getHuanZheSFJieSuan(@Param("inpatientNo") String inpatientNo,
                                @Param("admissTimes") Integer admissTimes);

    @Select("<script>" +
            "select code from yp_zd_dict where code in " +
            "<foreach collection='list' item='item' index='index' open='(' close=')' separator=','>" +
            " #{item}" +
            "</foreach> " +
            " and serial = '01' and isnull(del_flag,0)  = 1 " +
            "union all " +
            "select code from zd_charge_item where code in " +
            "<foreach collection='list' item='item' index='index' open='(' close=')' separator=','>" +
            " #{item}" +
            "</foreach> " +
            " and isnull(del_flag,0)  = 1" +
            "</script>")
    List<String> chaXunFeiYongShiFouTingYong(List<String> bianMa);

    @Select("select act_order_no as order_no,exec_unit from yz_erase_order where act_order_no in  " +
            "(select order_no from zy_detail_charge where  inpatient_no = #{inpatientNo} and admiss_times = #{admissTimes} and ledger_sn = #{ledger}   " +
            "and exec_unit is null AND charge_fee < 0 and ori_detail_sn is null)")
    List<ZyDetailCharge> huoQuYaoPingZhongKeShiWeiKongDe(@Param("inpatientNo") String inpatientNo,
                                                         @Param("admissTimes") Integer admissTimes,
                                                         @Param("ledger") Integer ledger);

    @Update("<script>" +
            " <foreach collection='list' item='item' separator=';' >" +
            "update zy_detail_charge set exec_unit = #{item.execUnit}  where inpatient_no = #{inpatientNo} and admiss_times = #{admissTimes} and ledger_sn = #{ledger} " +
            "and order_no = #{item.orderNo} and exec_unit is null  and charge_fee &lt; 0" +
            "</foreach> " +
            "</script> ")
    void yiZhuTuiFeiGenXingZhiXingKeShi(@Param("inpatientNo") String inpatientNo,
                                        @Param("admissTimes") Integer admissTimes,
                                        @Param("ledger") Integer ledger,
                                        @Param("list") List<ZyDetailCharge> genXingDeZhiXinKeShi);

    @Select("select b.code from zy_detail_charge a, yp_zd_dict b " +
            "where order_no <> 6 and trans_flag_yb not in (1,2)  and charge_code like 'BILL%' " +
            "  and  isnull(b.national_code,'') = '' and inpatient_no = #{inpatientNo} and admiss_times = #{admissTimes} and ledger_sn = #{ledger} " +
            "and a.infant_flag = #{infantFlag} " +
            "  and a.charge_code_mx = b.code " +
            "group by  b.code ")
    List<String> weiGuiYaoPinInt(@Param("inpatientNo") String inpatientNo,
                                 @Param("admissTimes") Integer admissTimes,
                                 @Param("ledger") Integer ledger,
                                 @Param("infantFlag") Integer infantFlag);

    @Update("<script>" +
            "<foreach collection='list' item='item' separator=';'>" +
            "update yp_zd_dict set national_code = (select top(1) national_code from yp_zd_dict where  code = #{item} and national_code is not null ) " +
            "where code = #{item} and national_code is null" +
            "</foreach>" +
            "</script>")
    void genXinYaoPingPiPeiXinXi(@Param("list") List<String> list);


    @Select("select a.charge_code_mx,case when (select top(1) rtrim(name) from zd_charge_item where code = a.charge_code_mx) != '' then " +
            "(select top(1) rtrim(name) from zd_charge_item where code = a.charge_code_mx) else " +
            "(select top(1) rtrim(name) from yp_zd_dict where code = a.charge_code_mx) end as name " +
            "from zy_detail_charge a left join zd_charge_item_hnsgs b " +
            "on (a.charge_code_mx = b.hosp_code) where " +
            "inpatient_no = #{inpatientNo} and admiss_times = #{admissTimes} and ledger_sn = #{ledger} and infant_flag = #{infantFlag} and ori_detail_sn is null and isnull(b.hnsyb_item_code,'') = '' " +
            "group by a.charge_code_mx ")
    List<WeiGuiFeiYongFenXi> gongShangWeiPiPei(@Param("inpatientNo") String inpatientNo,
                                               @Param("admissTimes") Integer admissTimes,
                                               @Param("ledger") Integer ledger,
                                               @Param("infantFlag") Integer infantFlag);

    /**
     * 违规药品 信息 没有匹配的
     *
     * @param inpatientNo 住院号
     * @param admissTimes 住院次数
     * @param ledger      账页号
     * @return 返回违规药品 没有匹配的
     */
    @Select("select charge_code_mx,b.name from zy_detail_charge a, yp_zd_dict b " +
            "where  trans_flag_yb not in (1,2)  and charge_code like 'BILL%' " +
            "  and  isnull(b.national_code,'') = '' and inpatient_no = #{inpatientNo} and admiss_times = #{admissTimes} and ledger_sn = #{ledger} " +
            "and a.infant_flag = #{infantFlag}   " +
            "  and a.charge_code_mx = b.code " +
            "group by  charge_code_mx, b.name")
    List<WeiGuiFeiYongFenXi> weiGuiYaoPin(@Param("inpatientNo") String inpatientNo,
                                          @Param("admissTimes") Integer admissTimes,
                                          @Param("ledger") Integer ledger,
                                          @Param("infantFlag") Integer infantFlag);


    @Select("select charge_code_mx,b.name from zy_detail_charge a, zd_charge_item b  " +
            "where  trans_flag_yb not in (1,2)  and charge_code not like 'BILL%'  " +
            "  and  isnull(b.national_code,'') = '' and inpatient_no = #{inpatientNo} and admiss_times = #{admissTimes} and ledger_sn = #{ledger}  " +
            " and a.infant_flag = #{infantFlag}  " +
            "  and a.charge_code_mx = b.code  " +
            "group by  charge_code_mx, b.name")
    List<WeiGuiFeiYongFenXi> weiGuiXiangMu(@Param("inpatientNo") String inpatientNo,
                                           @Param("admissTimes") Integer admissTimes,
                                           @Param("ledger") Integer ledger,
                                           @Param("infantFlag") Integer infantFlag);


    @Select("<script>" +
            "select rtrim(inpatient_no) as inpatient_no,admiss_times,ledger_sn,charge_code_mx,detail_sn,ori_detail_sn,order_no,charge_fee,charge_amount, " +
            "       exec_unit_name = (select rtrim(name) name from zd_unit_code where exec_unit = code),charge_date,trans_flag_yb, exec_unit, " +
            "       op_id_code = (select rtrim(name) name from a_employee_mi where code  = op_id_code)," +
            "       charge_code_name = (select rtrim(name) name from zd_charge_item where code = charge_code_mx),charge_code = 'xm' from zy_detail_charge " +
            "where inpatient_no = #{inpatientNo} and admiss_times = #{admissTimes} and ledger_sn = #{ledger} and infant_flag = #{infantFlag} " +
            "  and charge_fee &lt; 0 and charge_code not like 'BILL%' " +
            "  and  trans_flag_yb not in (1,2) " +
            "  and ori_detail_sn is null " +
            "<if test=\"chuYuanYiZhu == 0\">" +
            "  and CONVERT(varchar(100), charge_date, 23) &lt;= CONVERT(varchar(100), GETDATE(), 23)" +
            "</if>" +
            "<if test=\"list.size >0 \">" +
            " and exec_unit in " +
            "<foreach collection='list' item='item' index='index' open='(' close=')' separator=','>" +
            "#{item}" +
            "</foreach>" +
            "</if>" +
            "</script>")
    List<ZyDetailCharge> weiXieDaiYuanLiuShuiXiangMu(@Param("inpatientNo") String inpatientNo,
                                                     @Param("admissTimes") Integer admissTimes,
                                                     @Param("ledger") Integer ledger,
                                                     @Param("infantFlag") Integer infantFlag,
                                                     @Param("list") List<String> execUnit,
                                                     @Param("chuYuanYiZhu") Integer chuYuanYiZhu);

    @Select("<script>" +
            "select rtrim(inpatient_no) inpatient_no,admiss_times,ledger_sn,charge_code_mx,detail_sn,order_no,ori_detail_sn,charge_fee,charge_amount, " +
            "       exec_unit_name = (select rtrim(name) name from zd_unit_code where exec_unit = code),charge_date,trans_flag_yb,exec_unit, " +
            "       op_id_code = (select rtrim(name) name from a_employee_mi where code  = op_id_code)," +
            "       charge_code_name = (select top(1) rtrim(name) name from yp_zd_dict where code = charge_code_mx),charge_code = 'yp' from zy_detail_charge " +
            "where inpatient_no = #{inpatientNo} and admiss_times = #{admissTimes} and ledger_sn = #{ledger} and infant_flag = #{infantFlag} " +
            "  and charge_fee &lt; 0 and charge_code  like 'BILL%' " +
            "  and  trans_flag_yb not in (1,2) " +
            "  and ori_detail_sn is null" +
            "<if test=\"chuYuanYiZhu == 0\">" +
            "  and CONVERT(varchar(100), charge_date, 23) &lt;= CONVERT(varchar(100), GETDATE(), 23)" +
            "</if>" +
            "<if test=\"list.size > 0 \">" +
            " and exec_unit in " +
            "<foreach collection='list' item='item' index='index' open='(' close=')' separator=','>" +
            "#{item}" +
            "</foreach>" +
            "</if>" +
            "</script>")
    List<ZyDetailCharge> weiXieDaiYuanLiuShuiYaoPing(@Param("inpatientNo") String inpatientNo,
                                                     @Param("admissTimes") Integer admissTimes,
                                                     @Param("ledger") Integer ledger,
                                                     @Param("infantFlag") Integer infantFlag,
                                                     @Param("list") List<String> execUnit,
                                                     @Param("chuYuanYiZhu") Integer chuYuanYiZhu);

    @Select("<script>" +
            "select inpatient_no,admiss_times,ledger_sn,detail_sn,charge_amount,charge_fee,order_no,charge_code_mx,charge_date,ori_detail_sn " +
            "from zy_detail_charge where inpatient_no = #{inpatientNo} and admiss_times = #{admissTimes} and ledger_sn = #{ledgerSn} and charge_code_mx = #{chargeCodeMx} " +
            "<choose>" +
            "<when test=\"execUnit != null and execUnit != ''\">" +
            "and exec_unit = #{execUnit} " +
            "</when>" +
            "<otherwise>" +
            "and order_no = #{orderNo}" +
            "</otherwise>" +
            "</choose>" +
            "and trans_flag_yb &lt;&gt; 2 and order_no &lt;&gt; 6 and charge_fee &gt; 0 and  charge_amount &gt; 0 " +
            "and infant_flag = #{infantFlag}  " +
            "<if test=\"startTime != null and startTime != '' \">" +
            "and charge_date &gt;= #{startTime} and charge_date &lt;= #{endTime} " +
            "</if>" +
            " order by charge_date ${riQiPaiXu}" +
            "</script>")
    List<ZyDetailCharge> weiGuiZhenShuPiPei(@Param("inpatientNo") String inpatientNo,
                                            @Param("admissTimes") Integer admissTimes,
                                            @Param("ledgerSn") Integer ledgerSn,
                                            @Param("infantFlag") Integer infantFlag,
                                            @Param("chargeCodeMx") String chargeCodeMx,
                                            @Param("execUnit") String execUnit,
                                            @Param("orderNo") String orderNo,
                                            @Param("startTime") String startTime,
                                            @Param("endTime") String endTime,
                                            @Param("riQiPaiXu") String riQiPaiXu);


    @Select("select inpatient_no,admiss_times,ledger_sn,detail_sn,charge_code_mx,sum(charge_fee) yi_tui_fei from (  " +
            "select inpatient_no,admiss_times,ledger_sn,detail_sn,charge_code_mx,charge_fee  " +
            "from zy_detail_charge where " +
            "inpatient_no = #{inpatientNo} and admiss_times = #{admissTimes} and ledger_sn = #{ledgerSn} and charge_code_mx = #{chargeCodeMx} " +
            "and exec_unit = #{execUnit} and trans_flag_yb <> 2 and order_no <> 6 and" +
            "    detail_sn in(select ori_detail_sn from zy_detail_charge where inpatient_no = #{inpatientNo} and admiss_times = #{admissTimes} and " +
            "ledger_sn = #{ledgerSn} and charge_code_mx = #{chargeCodeMx}" +
            " and charge_fee <0 and ori_detail_sn is not null)  " +
            "union all  " +
            "select inpatient_no,admiss_times,ledger_sn,ori_detail_sn,charge_code_mx,charge_fee  " +
            "from zy_detail_charge where " +
            "inpatient_no = #{inpatientNo} and admiss_times = #{admissTimes} and ledger_sn = #{ledgerSn} and charge_code_mx = #{chargeCodeMx} " +
            "and exec_unit = #{execUnit} and trans_flag_yb <> 2 and order_no <> 6 and " +
            "ori_detail_sn in(select ori_detail_sn from zy_detail_charge where inpatient_no = #{inpatientNo} and admiss_times = #{admissTimes} " +
            "and ledger_sn = #{ledgerSn} and charge_code_mx = #{chargeCodeMx} and charge_fee <0 and ori_detail_sn is not null)  " +
            ") temp  " +
            "group by inpatient_no, admiss_times, ledger_sn, detail_sn, charge_code_mx")
    List<ZyDetailCharge> tuiFeiYuEr1(@Param("inpatientNo") String inpatientNo,
                                     @Param("admissTimes") Integer admissTimes,
                                     @Param("ledgerSn") Integer ledgerSn,
                                     @Param("chargeCodeMx") String chargeCodeMx,
                                     @Param("execUnit") String execUnit);


    @Select("<script>" +
            "select detail_sn,ori_detail_sn,order_no,charge_fee,charge_amount,charge_code_mx,charge_date, " +
            "       exec_unit_name = (select rtrim(name) from zd_unit_code where code =exec_unit), " +
            "       ward_code = (select rtrim(name) from zd_unit_code where code = ward_code) " +
            "       from zy_detail_charge where " +
            "       inpatient_no = #{inpatientNo} and admiss_times = #{admissTimes} and ledger_sn = #{ledgerSn}  " +
            "and infant_flag = #{infantFlag} " +
            "and charge_code_mx = #{chargeCodeMx} and trans_flag_yb &lt;&gt; 2 and order_no &lt;&gt; 6 " +
            "and charge_amount &gt; 0 " +
            "<if test=\"startTime != null and startTime != '' \">" +
            "and charge_date &gt;= #{startTime} and charge_date &lt;= #{endTime} " +
            "</if>" +
            "order by charge_date ${riQiPaiXu}" +
            "</script>")
    List<ZyDetailCharge> genJuBianMaChaZhaoZhenShuFeiYong(@Param("inpatientNo") String inpatientNo,
                                                          @Param("admissTimes") Integer admissTimes,
                                                          @Param("ledgerSn") Integer ledgerSn,
                                                          @Param("infantFlag") Integer infantFlag,
                                                          @Param("chargeCodeMx") String chargeCodeMx,
                                                          @Param("startTime") String startTime,
                                                          @Param("endTime") String endTime,
                                                          @Param("riQiPaiXu") String riQiPaiXu);

    @Select("<script>" +
            "select * from zy_detail_charge " +
            "where inpatient_no = #{inpatientNo} and admiss_times = #{admissTimes} and ledger_sn = #{ledgerSn} and infant_flag = #{infantFlag} and detail_sn in " +
            "<foreach collection='list' item='item' index='index' open='(' close=')' separator=','>" +
            "#{item}" +
            "</foreach>" +
            " and charge_fee &gt; 0 and charge_amount &gt; 0 " +
            "</script>")
    List<ZyDetailCharge> piPeiXinXi(@Param("inpatientNo") String inpatientNo,
                                    @Param("admissTimes") Integer admissTimes,
                                    @Param("ledgerSn") Integer ledgerSn,
                                    @Param("infantFlag") Integer infantFlag,
                                    @Param("list") int[] detailSnList);


    @Select("<script>" +
            "select inpatient_no,admiss_times,ledger_sn,ori_detail_sn,sum(charge_fee) charge_fee from zy_detail_charge  " +
            "              where  inpatient_no = #{inpatientNo} and admiss_times = #{admissTimes} and ledger_sn = #{ledgerSn} and infant_flag = #{infantFlag} and ori_detail_sn in  " +
            "<foreach collection='list' item='item' index='index' open='(' close=')' separator=','>" +
            "#{item} " +
            "</foreach>" +
            "and charge_fee &lt; 0 and charge_amount &lt; 0  " +
            "group by inpatient_no, admiss_times, ledger_sn, ori_detail_sn  " +
            "</script>")
    List<ZyDetailCharge> tuiFeiYuEr2(@Param("inpatientNo") String inpatientNo,
                                     @Param("admissTimes") Integer admissTimes,
                                     @Param("ledgerSn") Integer ledgerSn,
                                     @Param("infantFlag") Integer infantFlag,
                                     @Param("list") int[] detailSnList);


    @Select("select inpatient_no,admiss_times,ledger_sn,detail_sn,charge_fee,charge_amount,ori_detail_sn,trans_flag_yb,exec_unit,isnull(yb_self_flag,0) as yb_self_flag from zy_detail_charge " +
            "where inpatient_no = #{inpatientNo} and admiss_times = #{admissTimes} and ledger_sn = #{ledgerSn} and detail_sn = #{detailSn} " +
            "and infant_flag = #{infantFlag} and charge_fee < 0 and charge_amount < 0 ")
    ZyDetailCharge piPeiXinXiFuShu(@Param("inpatientNo") String inpatientNo,
                                   @Param("admissTimes") Integer admissTimes,
                                   @Param("ledgerSn") Integer ledgerSn,
                                   @Param("infantFlag") Integer infantFlag,
                                   @Param("detailSn") Integer detailSn);

    @Update("update zy_detail_charge set ori_detail_sn = #{oriDetailSn} where inpatient_no = #{inpatientNo} and admiss_times = #{admissTimes} and ledger_sn = #{ledgerSn} " +
            "and detail_sn = #{detailSn} ")
    void zhiYouYiGeJiuGenXingFuShuLiuShui(@Param("inpatientNo") String inpatientNo,
                                          @Param("admissTimes") Integer admissTimes,
                                          @Param("ledgerSn") Integer ledgerSn,
                                          @Param("detailSn") Integer detailSn,
                                          @Param("oriDetailSn") Integer oriDetailSn);

    @Update("update zy_detail_charge set yb_self_flag = 0 ${ew.customSqlSegment}")
    void xiuGaiZiFeiZhiYiBaoBiaoZhi(@Param(Constants.WRAPPER) Wrapper<?> wq);


    @Update("update yz_yp_zy_order set status_flag = '2' " +
            "where inpatient_no = #{patNo} " +
            "and admiss_times = #{times} " +
            "and isnull(page_no , 0) = 0")
    void rejectNotGeneratedMedicationList(String patNo, Integer times);


    @Select("select a.charge_code, " +
            "       b.name         as drug_name, " +
            "       b.specification, " +
            "       a.amount, " +
            "       (-a.amount * b.pack_retprice) as retprice, " +
            "       b.pack_retprice, " +
            "       a.inpatient_no as pat_no, " +
            "       a.name, " +
            "       page_class, " +
            "       page_no_ty " +
            "from yz_yp_zy_order a," +
            "yp_zd_dict b " +
            "where page_no_ty = -1 " +
            "  and dept_code = '${dept}' " +
            "  and group_no = #{groupNo} " +
            "  and page_class = 'HT' " +
            "  AND charge_date >= #{start} AND charge_date <= #{end}" +
            "  and a.charge_code = b.code " +
            "  and a.serial = b.serial ")
    List<DrugReturnForm> selectNoMedicineReturnOrderGenerated(String start,
                                                              String end,
                                                              String dept,
                                                              String groupNo);


    @Update("update yz_yp_zy_order set page_no_ty = #{pageNo} " +
            "where page_no_ty = -1 " +
            "  and dept_code = '${dept}'" +
            "  and group_no = #{groupNo} " +
            "  and page_class = 'HT' " +
            "  AND charge_date >= #{start} AND charge_date <= #{end}")
    int generateMedicationList(String start,
                               String end,
                               String dept,
                               String groupNo,
                               int pageNo);


    @Select("select page_no, " +
            "       group_no, " +
            "       group_name = (select rtrim(group_name) from yp_zd_group_name b where b.group_no = yz_yp_page_no.group_no) " +
            "from yz_yp_page_no " +
            "where dept_code = #{dept} " +
            "  and page_class = 'HT' " +
            "  AND submit_time >= #{start} AND submit_time <= #{end}")
    List<DrugReturnForm> getPrescription(String start, String end, String dept);


    @Select("select a.charge_code, " +
            "       b.name         as drug_name, " +
            "       b.specification, " +
            "       a.amount, " +
            "       a.retprice, " +
            "       b.pack_retprice, " +
            "       a.inpatient_no as pat_no, " +
            "       a.name, " +
            "       page_class, " +
            "       page_no_ty " +
            "from yz_yp_zy_order a," +
            "yp_zd_dict b " +
            " where a.page_no_ty = #{pageNo} " +
            "  and a.charge_code = b.code " +
            "  and a.serial = b.serial")
    List<DrugReturnForm> getAPrescriptionByPageNo(Integer pageNo);


    @Select("select refund_reason from yz_yp_page_no where page_no = #{pageNo}")
    Integer selectRefundReasonByPageNo(Integer pageNo);

    @Select("SELECT                  rtrim(a.inpatient_no)       inpatient_no,\n" +
            "                        a.admiss_times,\n" +
            "    ledger_sn         = (select max(ledger_sn) ledger_sn\n" +
            "                         from zy_ledger_file\n" +
            "                         where zy_ledger_file.inpatient_no = a.inpatient_no\n" +
            "                           and zy_ledger_file.admiss_times = a.admiss_times),\n" +
            "                        a.admiss_dept,\n" +
            "                        rtrim(a.name)            as name,\n" +
            "                        rtrim(a.bed_no)          as bed_no,\n" +
            "                        a.ward,\n" +
            "    ward_name=(select rtrim(name) name\n" +
            "               from zd_unit_code\n" +
            "               where zd_unit_code.code = a.ward),\n" +
            "                        a.responce_type,\n" +
            "    responce_type_name=(select rtrim(name) name\n" +
            "                        from zy_zd_responce_type\n" +
            "                        where a.responce_type = zy_zd_responce_type.code),\n" +
            "                        a.balance,\n" +
            "                        a.dept,\n" +
            "                        a.refer_physician,\n" +
            "                        a.total_charge,\n" +
            "                        rtrim(a.charge_type)     as charge_type,\n" +
            "                        a.times_billed,\n" +
            "                        rtrim(a.admiss_diag_str) as admiss_diag_str,\n" +
            "                        rtrim(a.admiss_diag)     as admiss_diag,\n" +
            "                        a.admiss_date,\n" +
            "                        a.sex,\n" +
            "                        a.zy_serial_no,\n" +
            "                        a.mz_no,\n" +
            "                        rtrim(a.intern_code)     as intern_code,\n" +
            "                        a.zk_ward,birthDate=(select t.birth_date from a_patient_mi t where t.inpatient_no = a.inpatient_no) " +
            "FROM ${tableName} a\n" +
            "${ew.customSqlSegment};")
    ZyActpatient getPatientInfo(@Param("ew") QueryWrapper<?> qw, @Param("tableName") String tableName);


    @Select("SELECT inpatient_no,\n" +
            "       admiss_times,\n" +
            "       ledger_sn,\n" +
            "       detail_sn,\n" +
            "       order_no,\n" +
            "       charge_code_mx,\n" +
            "       charge_date,\n" +
            "       exec_dept=(select name\n" +
            "                  from zd_unit_code\n" +
            "                  where code = a.exec_unit),\n" +
            "       dept_code = (select name\n" +
            "                    from zd_unit_code\n" +
            "                    where code = a.ward_code),\n" +
            "       charge_fee,\n" +
            "       charge_amount,\n" +
            "       opIdName = (select rtrim(name) from a_employee_mi where code = op_id_code),\n" +
            "       name as chargeName,\n" +
            "       bill_item_name=(select name\n" +
            "                       from zy_bill_item\n" +
            "                       where code = charge_item.bill_item_zy),ori_detail_sn,gen_time,charge_item.serial,descriptions " +
            "from zy_detail_charge a\n" +
            "         left join (select code, '00' as serial, name, 'xm' as flag, bill_item_zy,descriptions\n" +
            "                    from zd_charge_item\n" +
            "                    union\n" +
            "                    select code, serial, name, 'yp' as flag, bill_item_zy,specification\n" +
            "                    from yp_zd_dict) charge_item\n" +
            "                   on (charge_code_mx = code and isnull(nullif(rtrim(a.serial), ''), '00') = charge_item.serial)" +
            "${ew.customSqlSegment}")
    List<FeeResult.Detail> getFee(@Param("ew") QueryWrapper<?> qw);
}
