package thyyxxk.webserver.dao.his.zhuyuanyisheng;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import org.apache.ibatis.annotations.*;
import thyyxxk.webserver.entity.datamodify.GetDropdownBox;
import thyyxxk.webserver.entity.datamodify.YzActOrder;
import thyyxxk.webserver.entity.zhuyuanyisheng.DoctorSOrderFee;
import thyyxxk.webserver.entity.zhuyuanyisheng.ZyOrderZk;
import thyyxxk.webserver.entity.zhuyuanyisheng.yizhuluru.*;

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

/**
 * <p>
 * 描述： 医嘱录入dao
 * </p>
 *
 * @author xc
 * @date 2022-01-04 17:00
 */
@Mapper
public interface YiZhuLuRuDao {

    @Select("select isnull(balance,0) balance, " +
            "       total_charge, " +
            "       charge1, " +
            "       charge2, " +
            "       charge3, " +
            "       charge4, " +
            "       charge5, " +
            "       charge6, " +
            "       charge7, " +
            "       charge8, " +
            "       charge9, " +
            "       charge10, " +
            "       charge11, " +
            "       charge12, " +
            "       charge13, " +
            "       charge14, " +
            "       charge15, " +
            "       charge16, " +
            "       charge17, " +
            "       charge18, " +
            "       charge19, " +
            "       charge20, " +
            "       charge21, " +
            "       charge22, " +
            "       charge23, " +
            "       charge24, " +
            "       charge25, " +
            "       charge26, " +
            "       charge27, " +
            "       charge28, " +
            "       charge29, " +
            "       charge30, " +
            "       yp   = (isnull(charge1 + charge2 + charge28, 0))," +
            "       jyjc = (isnull(charge8 + charge9 + charge11 + charge18 + charge13, 0))," +
            "       isnull(charge_yb, 0) charge_yb " +
            "from zy_ledger_file with (NOLOCK) " +
            "where inpatient_no = #{inpatientNo} " +
            "  and admiss_times = #{admissTimes} " +
            "  and ledger_sn = #{ledgerSn} ")
    HuanZheFeiYong feiYongXinXi(@Param("inpatientNo") String inpatientNo,
                                @Param("admissTimes") Integer admissTimes,
                                @Param("ledgerSn") Integer ledgerSn);

    @Select("select distinct rtrim(order_name) code, rtrim(order_name) name " +
            "from yz_act_order with (NOLOCK) " +
            "where inpatient_no = #{inpatientNo} " +
            "  and admiss_times = #{admissTimes} " +
            "  and (order_name like #{orderName} or order_code like #{orderName})")
    List<GetDropdownBox> huoQuYiZhuMingZi(@Param("inpatientNo") String inpatientNo,
                                          @Param("admissTimes") Integer admissTimes,
                                          @Param("orderName") String orderName);

    @Select("select     rtrim(frequ_code) code, " +
            "    name = (select rtrim(comm) from yz_order_frequency where frequ_code = code) " +
            "from yz_act_order with (NOLOCK) " +
            "where inpatient_no = #{patNo} " +
            "  and admiss_times = #{times} " +
            "group by frequ_code")
    List<GetDropdownBox> huoQuGeRenPinLv(@Param("patNo") String patNo,
                                         @Param("times") Integer times);


    @Select("<script>" +
            "select rowIndex, " +
            "       act_order_no," +
            "       cast(cast(act_order_no as decimal) as varchar) id, " +
            "       order_code, " +
            "       order_name, " +
            "       rtrim(frequ_code) frequ_code," +
            "       frequ_code_name  = (select rtrim(comm) from yz_order_frequency where code = frequ_code), " +
            "       instruction, " +
            "       discription, " +
            "       infant_flag, " +
            "       order_time, " +
            "       physician, " +
            "       start_time, " +
            "       drug_specification, " +
            "       drug_quan, " +
            "       drug_quan_name = (select name from yp_zd_unit with (NOLOCK) where code = mini_unit)," +
            "       supply_code, " +
            "       supply_code_name     = (select supply_name " +
            "                               from yz_supply_type with (NOLOCK) " +
            "                               where yz_supply_type.supply_code = temp.supply_code)," +
            "       inpatient_no, " +
            "       admiss_times, " +
            "       drug_flag, " +
            "       enter_oper, " +
            "       enter_time, " +
            "       mini_unit, " +
            "       drug_weight, " +
            "       drug_weight_unit, " +
            "       status_flag, " +
            "       status_time, " +
            "       parent_no, " +
            "       self_buy, " +
            "       dose, " +
            "       dose_unit, " +
            "       dose_unit_name       = (select name from yp_zd_unit with (NOLOCK) where dose_unit = code)," +
            "       drug_occ, " +
            "       drug_volume, " +
            "       drug_vol_unit, " +
            "       exclu_back_time, " +
            "       exclu_act_order_no, " +
            "       pay_self, " +
            "       serial, " +
            "       group_no, " +
            "       signer, " +
            "       cl_code, " +
            "       doctor_flag, " +
            "       emergency_flag, " +
            "       exclu_type='', " +
            "       end_time_b=end_time, " +
            "       exec_unit, " +
            "       dept_code, " +
            "       ward_code, " +
            "       new_flag='0', " +
            "       order_no_pattern=0, " +
            "       suprice_flag='', " +
            "       self_flag ='', " +
            "       pack_size=0.0, " +
            "       parent_no_c= -1000, " +
            "       reg_flag, " +
            "       parent_flag='', " +
            "       yb_self_flag, " +
            "       kf_flag, " +
            "       refer_physician, " +
            "       consult_physician, " +
            "       physician_name = (select  top(1) rtrim(name) from a_employee_mi with (NOLOCK) where code = physician) /* 开这个医嘱的医生 */, " +
            "       exec_unit_name = (select  top(1) rtrim(name) from zd_unit_code with (NOLOCK) where code = exec_unit), " +
            "    /*管床医生*/ " +
            "       refer_physician_name = (select rtrim(name) from a_employee_mi with (NOLOCK) where code = refer_physician), " +
            "       group_no_name = (select rtrim(group_name) from yp_zd_group_name with (NOLOCK) where temp.group_no = group_no), " +
            "    signer_name          = (select top (1) rtrim(name) from a_employee_mi with (NOLOCK) where code = signer) /*确认人*/, " +
            "                           confirm_time /*确认时间*/, " +
            "                           modifier, " +
            "    modifier_name        = (select top (1) rtrim(name) from a_employee_mi with (NOLOCK) where code = modifier) /*停止人*/, " +
            "                           end_time /*停止时间*/ " +
            "from ( " +
            "         select top ${pageSize} * " +
            "         from (select row_number() over (order by act_order_no) rowIndex, * " +
            "               from yz_act_order a with (NOLOCK) " +
            "               ${ew.customSqlSegment} " +
            "              ) page " +
            "         where rowIndex > (${currentPage} - 1) * ${pageSize} " +
            "     ) temp" +
            "</script>")
    List<YzActOrder> huoQuYiZhuShuJu(@Param("currentPage") long currentPage,
                                     @Param("pageSize") long pageSize,
                                     @Param(Constants.WRAPPER) Wrapper<?> wq);


    @Select("<script>" +
            "select count(1) " +
            "from yz_act_order a with (NOLOCK) " +
            " ${ew.customSqlSegment} " +
            "</script>")
    long huoQuYiZhuShuJuTotal(@Param(Constants.WRAPPER) Wrapper<?> wq);


    @Select("SELECT distinct py_code=a.py_code, " +
            "                d_code =a.d_code, " +
            "                order_code =a.code, " +
            "                order_name =a.name, " +
            "                drug_specification=a.specification, " +
            "                order_type =case a.infusion_flag " +
            "                                when '1' then 'i' " +
            "                                else " +
            "                                    (case " +
            "                                         when a.code >= '30000' and a.code < '50000' then 'z' " +
            "                                         else 'd' end) end, " +
            "                parent_code=null, " +
            "                exclu_group_type=null, " +
            "                discription=case a.infusion_flag " +
            "                                when '1' then '大输液' " +
            "                                else (case " +
            "                                          when a.code >= '30000' and a.code < '50000' then '中药' " +
            "                                          else '西药  ' end) end + convert(char(10), a.pack_retprice) + '元' + '(医保类别：' + " +
            "                            ltrim(rtrim(yb_flag_new)) + ';报销比例：' + ltrim(rtrim(yb_bl_new)) + ';医保说明：' + " +
            "                            ltrim(rtrim(yb_comment_new)) + ')', " +
            "                serial=a.serial, " +
            "                group_no=a.group_no, " +
            "                infusion_flag=a.infusion_flag, " +
            "                drug_flag =a.drug_flag, " +
            "                group_name =a.group_name, " +
            "                pack_size=a.pack_size, " +
            "                order_class='Y', " +
            "                spec_pack=a.spec_pack, " +
            "                jsy_flag =a.jsy_flag, " +
            "                xnh_flag, " +
            "                yb_comment, " +
            "                dosage = a.dosage, " +
            "                yb_type, " +
            "                yb_flag_new," +
            "                father_flag, " +
            "                manu_name=(select name from yp_zd_manufactory with (NOLOCK) where code *= a.manu_code), " +
            "                stock_amount=a.stock_amount, " +
            "                national_code," +
            "                national_name " +
            "FROM view_yp_zd_dict_base_yf a with (NOLOCK) " +
            "WHERE isnull(a.visible_flag_zy, '') <> '1' " +
            "  and (a.py_code like #{code} or a.name like #{code} or code like #{code}) " +
            "  and group_no = #{groupNo}  ")
    List<YiZhuMingChen> yiZhuYaoPing(@Param("code") String code,
                                     @Param("groupNo") String groupNo);


    @Select("SELECT a.py_code, " +
            "       a.d_code, " +
            "       a.order_code , " +
            "       rtrim(a.order_name) order_name, " +
            "       drug_specification='诊疗项目', " +
            "       order_type   = isnull(operation,'o') , " +
            "       a.parent_code , " +
            "       a.exclu_group_type, " +
            "       discription=a.discription + '  ' + a.yb_comment, " +
            "       serial       ='00', " +
            "       group_no='00', " +
            "       infusion_flag='', " +
            "       drug_flag    ='', " +
            "       group_name   ='', " +
            "       pack_size=1, " +
            "       order_class=a.order_class, " +
            "       spec_pack='', " +
            "       jsy_flag     ='', " +
            "       xnh_flag='', " +
            "       yb_comment   =null, " +
            "       dosage='', " +
            "       yb_type='', " +
            "       father_flag='0', " +
            "       manu_name='', " +
            "       stock_amount=0 " +
            "FROM yz_order_item a with (NOLOCK) " +
            "WHERE isnull(a.del_flag, '') <> '1' " +
            "  and (py_code like #{code} or order_name like #{code} or order_code like #{code}) " +
            "  and order_code not in (select zy_order_code from jc_zd_item) " +
            "  and isnull(exec_dept, '') <> '2010000' ")
    List<YiZhuMingChen> yiZhuXiangMu(String code);


    @Select("SELECT cast(rtrim(isnull(nullif(del_flag,\n" +
            "                                ''),\n" +
            "                         0)) as int) as del_flag,\n" +
            "       national_code                 as national_code,\n" +
            "       name                          as order_name\n" +
            "FROM yz_order_occurence a with (NOLOCK),\n" +
            "     zd_charge_item b with (NOLOCK)\n" +
            "where a.occ_code = b.code\n" +
            "  and a.order_code = #{code}")
    List<XinZhenYzActOrder> huoQuXiangMu(String code);


    @Select("select isnull(exclu_group_type, 0) " +
            "from yz_order_item with (NOLOCK) where order_code = #{code}")
    Integer shiFouPaiChiYiZhu(String code);


    @Select("select\n" +
            "    /* 毒麻标志 */     drug_flag,\n" +
            "                       a.serial,\n" +
            "    /* 医保自费 */\n" +
            "                       cast(isnull(NULLIF(self_flag_yb,\n" +
            "                                          ''),\n" +
            "                                   0) as int)                      as self_flag_yb,\n" +
            "    /* 重量 */\n" +
            "                       weight,\n" +
            "    /* 重量单位 */\n" +
            "                       weigh_unit,\n" +
            "    weigh_unit_name  = (select name\n" +
            "                        from yp_zd_unit with (NOLOCK)\n" +
            "                        where code = weigh_unit),\n" +
            "    /*体积*/\n" +
            "                       volum,\n" +
            "    /*体积单位*/\n" +
            "                       vol_unit,\n" +
            "    vol_unit_name    = (select name\n" +
            "                        from yp_zd_unit with (NOLOCK)\n" +
            "                        where code = vol_unit),\n" +
            "    /*包装体积*/\n" +
            "                       pack_size,\n" +
            "    /*包装单位*/\n" +
            "                       pack_unit,\n" +
            "    pack_unit_name   = (select name\n" +
            "                        from yp_zd_unit with (NOLOCK)\n" +
            "                        where code = pack_unit),\n" +
            "    /*最小单位*/\n" +
            "                       mini_unit,\n" +
            "    mini_unit_name   = (select name\n" +
            "                        from yp_zd_unit with (NOLOCK)\n" +
            "                        where code = mini_unit),\n" +
            "    /* 需要判断医生的权限 在 select\n" +
            "        doctor_xz_yp\n" +
            "    from\n" +
            "        a_employee_mi */\n" +
            "                       cast(isnull(NULLIF(yp_level,\n" +
            "                                          ''),\n" +
            "                                   0) as int)                      as yp_level,\n" +
            "    /*需要皮试*/\n" +
            "                       cast(isnull(NULLIF(ps_flag,\n" +
            "                                          ''),\n" +
            "                                   0) as int)                      as ps_flag,\n" +
            "                       cast(isnull(NULLIF(kss_flag,\n" +
            "                                          ''),\n" +
            "                                   0) as int)                      as kss_flag,\n" +
            "                       cast(isnull(NULLIF(kjyw_flag,\n" +
            "                                          ''),\n" +
            "                                   0) as int)                      as kjyw_flag,\n" +
            "                       code                                        as order_code,\n" +
            "                       name                                        as order_name,\n" +
            "                       frequency                                   as frequCode,\n" +
            "                       (select isnull(rtrim(comm),\n" +
            "                                      '')\n" +
            "                        from yz_order_frequency\n" +
            "                        where yz_order_frequency.code = frequency) as frequCodeName,\n" +
            "                       supply_type                                 as supply_code,\n" +
            "    supply_code_name = (select supply_name\n" +
            "                        from yz_supply_type with (NOLOCK)\n" +
            "                        where yz_supply_type.supply_code = supply_type),\n" +
            "                       national_code,\n" +
            "                       stock_amount," +
            "                       cast(isnull(nullif(visible_flag_zy, ''), 0) as int) as visible_flag_zy \n" +
            "from yp_zd_dict a with (NOLOCK)\n" +
            "         left join yp_base_yf b on (code = b.charge_code and a.serial = b.serial) " +
            "where  rtrim(code) + rtrim(a.serial) = #{code}  " +
            "  and cast(isnull(NULLIF(del_flag, ''), 0) as int) = 0 " +
            "  and group_no = #{groupNo} ")
    YaoPinXinXi huoQuYaoPin(@Param("code") String code,
                            @Param("groupNo") String groupNo);

    @Select("select count(1)\n" +
            "from yp_zd_dept\n" +
            "where charge_code = #{code} \n" +
            "  and dept_code = #{dept} ")
    Integer restrictedUseInTheDepartment(@Param("code") String code,
                                         @Param("dept") String dept);


    @Select("select cast(isnull(NULLIF(doctor_xz_yp, ''), 0) as int) from a_employee_mi with (NOLOCK) where code = #{code}")
    Integer huoQuYiShenDengJi(String code);

    @Select("select rtrim(supply_code) code,rtrim(supply_name) name " +
            "from yz_supply_type with (NOLOCK) " +
            "where ps_flag in ('1', '0')")
    List<GetDropdownBox> piShiGeiYaoFangShi();

    @Select("select rtrim(code) code, rtrim(comm) name " +
            "from yz_order_frequency with (NOLOCK) " +
            "where (code like #{code} or comm like #{code})")
    List<GetDropdownBox> huoQuZhuYuanPinLv(String code);


    @Select("select rtrim(supply_code) code, rtrim(supply_name) name " +
            "from yz_supply_type with (NOLOCK) " +
            "where (supply_code like #{code} or supply_name like #{code} or py_code like #{code} )")
    List<GetDropdownBox> huoQuGeiYaoFangShi(String code);


    @Select("SELECT a.code code, " +
            "       a.name name " +
            "FROM zd_unit_code a with (NOLOCK), " +
            "     zd_dept_all b with (NOLOCK) " +
            "where isnull(a.del_flag, '0') <> '1' " +
            "  and a.code = b.small_dept " +
            "  and (a.code like #{code} or a.name like #{code} or py_code like #{code}) " +
            "group by code,name")
    List<GetDropdownBox> huoQuZhiXinKeShi(String code);


    @Select("<script>" +
            "select rtrim(code)                       order_code, " +
            "       rtrim(a.serial)                     serial, " +
            "       rtrim(name)                       order_name," +
            "       case infusion_flag " +
            "           when '1' then 'i' " +
            "           else " +
            "               (case " +
            "                    when rtrim(code) &gt;= '30000' and rtrim(code) &lt; '50000' then 'z' " +
            "                    else 'd' end) end as drug_flag, " +
            "       mini_unit, " +
            "       isnull(weight,1)                  drug_weight, " +
            "       weigh_unit                        drug_weight_unit, " +
            "       isnull(volum,1)                   drug_volume, " +
            "       vol_unit                          drug_vol_unit," +
            "       isnull(pack_size,1) pack_size,  " +
            "       pack_unit,  " +
            "       national_code," +
            "       cast(isnull(NULLIF(kjyw_flag, ''), 0) as int) as kjyw_flag," +
            "       cast(isnull(NULLIF(yp_level, ''), 0) as int)  as yp_level," +
            "       cast(isnull(NULLIF(del_flag, ''), 0) as int)  as del_flag,  " +
            "       stock_amount," +
            "       cast(isnull(nullif(visible_flag_zy, ''), 0) as int) as visible_flag_zy," +
            "       deptRestrictions = (select count(1) from yp_zd_dept where code = yp_zd_dept.charge_code and dept_code = '${dept}')  " +
            " from yp_zd_dict a with (NOLOCK)  " +
            " left join  yp_base_yf b on ( code = b.charge_code and  a.serial = b.serial) " +
            "where rtrim(code) + rtrim(a.serial) in " +
            "<foreach collection='code' item='item' index='index' open='(' close=')' separator=','>" +
            "#{item}" +
            "</foreach>" +
            " and group_no = #{groupNo} " +
            "</script>")
    List<XinZhenYzActOrder> huoQuYaoPinXinXi(@Param("code") Set<String> code,
                                             @Param("groupNo") String groupNo,
                                             @Param("dept") String dept);


    @Select("<script>" +
            "select          b.order_code,\n" +
            "                rtrim(c.name)                 order_name,\n" +
            "                national_code,\n" +
            "                kjyw_flag = 0,\n" +
            "                cast(rtrim(isnull(nullif(c.del_flag,\n" +
            "                                         ''),\n" +
            "                                  0)) as int) del_flag " +
            " from " +
            "     yz_order_occurence b with (NOLOCK) , " +
            "     zd_charge_item c with (NOLOCK) " +
            "where b.order_code in " +
            "<foreach collection='code' item='item' index='index' open='(' close=')' separator=','>" +
            "#{item}" +
            "</foreach>" +
            "  and b.occ_code = c.code" +
            "</script>")
    List<XinZhenYzActOrder> huoQuXiangMuXinXi(Set<String> code);


    @Select("select          rtrim(inpatient_no) as inpatient_no,\n" +
            "                admiss_times,\n" +
            "                rtrim(name)         as name,\n" +
            "                rtrim(dept)         as dept_code,\n" +
            "                rtrim(zk_ward)      as ward_code,\n" +
            "                refer_physician,\n" +
            "                times_billed,\n" +
            "                admiss_date,\n" +
            "                small_dept,\n" +
            "                bed_no,\n" +
            "    ledger_sn = (select max(ledger_sn)\n" +
            "                 from zy_ledger_file with (NOLOCK)\n" +
            "                 where zy_actpatient.inpatient_no = zy_ledger_file.inpatient_no\n" +
            "                   and zy_actpatient.admiss_times = zy_ledger_file.admiss_times)\n" +
            " from zy_actpatient with (NOLOCK) " +
            "where inpatient_no = #{patNo} " +
            "  and admiss_times = #{times} ")
    XinZhenYiZhu huoQuHuanZheXinXi(@Param("patNo") String patNo,
                                   @Param("times") Integer times);


    @Delete("<script>" +
            "delete yz_act_record_kss " +
            "where act_order_no in " +
            "<foreach collection='list' item='item' index='index' open='(' close=')' separator=','>" +
            "#{item.actOrderNo}" +
            "</foreach>" +
            "</script>")
    void shanChuYuanKuangJunYaoWu(List<YzActRecordKss> list);

    @Insert("<script>" +
            "insert into yz_act_record_kss (act_order_no, charge_code, input_id, inpatient_no, admiss_times, yyfs, ssqk, yysj) " +
            "values " +
            "<foreach collection='list' item='item' separator=','>" +
            "(#{item.actOrderNo},#{item.chargeCode},#{inputId},#{patNo},#{times},#{item.yyfs},#{item.ssqk},#{item.yysj})" +
            "</foreach>" +
            "</script>")
    void chaRuKuangJunYaoWuXinXi(@Param("patNo") String patNo,
                                 @Param("times") Integer times,
                                 @Param("inputId") String inputId,
                                 @Param("list") List<YzActRecordKss> list);


    @Insert("<script>" +
            "INSERT INTO yz_act_order (order_code, order_name, frequ_code, instruction, discription, infant_flag, order_time, " +
            "    /*录入人*/physician, start_time, drug_specification, drug_quan, supply_code, act_order_no, inpatient_no, " +
            "                          admiss_times, drug_flag/*药品中找这个是 药品的类型 a i d*/, enter_oper, /*录入时间*/ enter_time, " +
            "                          mini_unit /*药品中找 最小包装规格*/, " +
            "                          drug_weight/*药品的重量*/, drug_weight_unit/*药品重量单位*/, " +
            "                          status_flag/*2*/, self_buy, dose, dose_unit, /*这个是算出来的 输入的 dose 除以选中的单位 */drug_occ, drug_volume/*药品中的 volum 找*/, " +
            "                          drug_vol_unit/* 药品中的 vol_unit */, pay_self/*0*/, " +
            "                          serial/*前端传*/, group_no/*药房 */, doctor_flag/*写死为 1*/, exec_unit, dept_code/* 在院病人表中的 dept */, " +
            "                          ward_code /* 在院病人表中的 zk_ward*/, " +
            "                          reg_flag/* 0 */, " +
            "                          yb_self_flag/*医保自费标志*/,emergency_flag, " +
            "                          kf_flag /*饭前饭后*/,parent_no, refer_physician/*患者的管床医生*/, confirm_time/*确认时间*/, signer/*确认人*/,end_time,modifier) " +
            "VALUES " +
            "<foreach collection='list' item='item' separator=','>" +
            "(#{item.orderCode,jdbcType=VARCHAR}, #{item.orderName,jdbcType=VARCHAR}, #{item.frequCode,jdbcType=VARCHAR}," +
            " #{item.instruction,jdbcType=VARCHAR}, #{item.discription,jdbcType=VARCHAR}, #{infantFlag,jdbcType=VARCHAR}, " +
            "#{item.orderTime,jdbcType=VARCHAR}, " +
            "        #{inputCode,jdbcType=TIMESTAMP}, #{item.startTime,jdbcType=TIMESTAMP}, #{item.drugSpecification}, " +
            "#{item.drugQuan,jdbcType=DECIMAL}," +
            " #{item.supplyCode}," +
            " #{item.actOrderNo,jdbcType=DECIMAL}, #{patNo}, #{times}, " +
            "#{item.drugFlag}," +
            " #{inputCode}, " +
            " getdate(), #{item.miniUnit}, " +
            "#{item.drugWeight,jdbcType=DECIMAL}, " +
            "#{item.drugWeightUnit}, '2', #{item.selfBuy}, " +
            "cast(#{item.dose,jdbcType=DECIMAL} as decimal(10,2))," +
            " #{item.doseUnit}, " +
            "cast(#{item.drugOcc,jdbcType=DECIMAL} as decimal(10,2))," +
            "cast(#{item.drugVolume,jdbcType=DECIMAL} as decimal(10,2))," +
            " #{item.drugVolUnit}, 0, #{item.serial}, " +
            "#{item.groupNo}," +
            "'1', #{item.execUnit}, " +
            "#{deptCode}, #{wardCode}, '0', #{item.ybSelfFlag},#{item.emergencyFlag}, #{item.kfFlag}," +
            "#{item.parentNo,jdbcType=DECIMAL}," +
            " #{referPhysician}, getdate(), #{inputCode},#{item.endTime,jdbcType=TIMESTAMP},#{item.modifier})" +
            "</foreach>" +
            "</script>")
    void chaRuYiZhu(@Param("patNo") String patNo,
                    @Param("times") Integer times,
                    @Param("inputCode") String inputCode,
                    @Param("infantFlag") Integer infantFlag,
                    @Param("deptCode") String deptCode,
                    @Param("wardCode") String wardCode,
                    @Param("referPhysician") String referPhysician,
                    @Param("list") List<XinZhenYzActOrder> list);


    @Select("<script>" +
            "select top 20 * from (" +
            "SELECT row_number() over (order by sort_no ) row_number," +
            "       rtrim(pattern_code) pattern_code, " +
            "       rtrim(pattern_name) pattern_name," +
            "       rtrim(input_id) input_id," +
            "       input_date," +
            "       rtrim(input_type) input_type, " +
            "       sort_no," +
            "       yiBeiShouCang = (select count(1) from yz_OrderPattern b with (NOLOCK) where b.collect_code = a.pattern_code " +
            "                           and b.input_id = #{inputId})," +
            "       collect_code " +
            "FROM yz_OrderPattern a with (NOLOCK) " +
            "where 1=1 " +
            "<if test=\"code != null and code != '' \">" +
            "and (py_code like #{code} " +
            "    or pattern_code like #{code} " +
            "    or pattern_name like #{code}) " +
            "</if>" +
            "<if test=\"muBanLeiXing == 2 \">" +
            " and dept_code = #{deptCode} AND input_type = '2' " +
            "</if>" +
            "<if test=\"muBanLeiXing == 3 \">" +
            "  and input_id = #{inputId} AND input_type = '3' " +
            "</if>" +
            "<if test=\"muBanLeiXing == 1\">" +
            " and input_type = '1' " +
            "</if>" +
            "<if test=\"muBanLeiXing == 4\">" +
            " and input_id = #{inputId} AND input_type = '4' " +
            "</if>" +
            "<if test=\"muBanLeiXing == 0\">" +
            " AND input_type &lt;&gt; '4' " +
            "</if>" +
            ") temp where row_number > (${currentPage} - 1) * 20" +
            "</script>")
    List<YzOrderPattern> huoQuMuBan(@Param("code") String code,
                                    @Param("inputId") String inputId,
                                    @Param("deptCode") String deptCode,
                                    @Param("muBanLeiXing") Integer muBanLeiXing,
                                    @Param("currentPage") long currentPage);

    @Select("<script>" +
            "SELECT count(*) " +
            "FROM yz_OrderPattern with (NOLOCK) " +
            "where 1=1 " +
            "<if test=\"code != null and code != '' \">" +
            "and (py_code like #{code} " +
            "    or pattern_code like #{code} " +
            "    or pattern_name like #{code}) " +
            "</if>" +
            "<if test=\"muBanLeiXing == 2 \">" +
            " and dept_code = #{deptCode} AND input_type = '2' " +
            "</if>" +
            "<if test=\"muBanLeiXing == 3 \">" +
            "  and input_id = #{inputId} AND input_type = '3' " +
            "</if>" +
            "<if test=\"muBanLeiXing == 1\">" +
            " and input_type = '1' " +
            "</if>" +
            "<if test=\"muBanLeiXing == 4\">" +
            " and input_id = #{inputId} AND input_type = '4' " +
            "</if>" +
            "<if test=\"muBanLeiXing == 0\">" +
            " AND input_type &lt;&gt; '4' " +
            "</if>" +
            "</script>")
    long huoQuMuBanTotal(@Param("code") String code,
                         @Param("inputId") String inputId,
                         @Param("deptCode") String deptCode,
                         @Param("muBanLeiXing") Integer muBanLeiXing);


    @Select("SELECT                 cast(cast(act_order_no as decimal) as varchar) id, " +
            "                       act_order_no,  " +
            "                       rtrim(a.order_name) order_name, " +
            "                       rtrim(a.order_code) order_code, " +
            "                       a.frequ_code, " +
            "    frequ_code_name  = (select rtrim(comm) " +
            "                        from yz_order_frequency with (NOLOCK) " +
            "                        where yz_order_frequency.code = a.frequ_code), " +
            "                       a.drug_specification, " +
            "                       a.supply_code, " +
            "    supply_code_name = (select rtrim(supply_name) " +
            "                        from yz_supply_type with (NOLOCK) " +
            "                        where yz_supply_type.supply_code = a.supply_code), " +
            "                       a.drug_quan, " +
            "                       a.instruction, " +
            "                       a.mini_unit, " +
            "    mini_unit_name   = (select rtrim(name) from yp_zd_unit with (NOLOCK) where code = mini_unit), " +
            "                       a.drug_weight, " +
            "                       a.drug_weight_unit, " +
            "                       a.parent_no, " +
            "                       a.drug_volume, " +
            "                       a.drug_vol_unit, " +
            "                       a.dose, " +
            "                       a.dose_unit, " +
            "    dose_unit_name   = (select rtrim(name) from yp_zd_unit with (NOLOCK) where code = dose_unit), " +
            "                       a.exclu_act_order_no, " +
            "                       a.discription, " +
            "                       a.pattern_code, " +
            "                       a.order_type, " +
            "                       a.serial, " +
            "    self_buy=isnull(a.self_buy, '0'), " +
            "    del_flag=isnull(b.del_flag, ''), " +
            "                       a.drug_flag, " +
            "    suprice_flag='', " +
            "    self_flag        ='', " +
            "    parent_flag='', " +
            "                       a.exec_unit, " +
            "    exec_unit_name   = (select rtrim(name) from zd_unit_code with (NOLOCK) where code = exec_unit) " +
            "FROM yz_pattern_order a with (NOLOCK), " +
            "     yz_order_item b with (NOLOCK) " +
            "WHERE a.pattern_code = #{code} " +
            "  and isnull(a.group_no, '00') = '00' " +
            "  and a.order_code = b.order_code " +
            "UNION " +
            "SELECT                 cast(cast(act_order_no as decimal) as varchar) id," +
            "                       act_order_no,  " +
            "                       a.order_name, " +
            "                       a.order_code, " +
            "                       a.frequ_code, " +
            "    frequ_code_name  = (select rtrim(comm) " +
            "                        from yz_order_frequency with (NOLOCK) " +
            "                        where yz_order_frequency.code = a.frequ_code), " +
            "                       a.drug_specification, " +
            "                       a.supply_code, " +
            "    supply_code_name = (select rtrim(supply_name) " +
            "                        from yz_supply_type with (NOLOCK) " +
            "                        where yz_supply_type.supply_code = a.supply_code), " +
            "                       a.drug_quan, " +
            "                       a.instruction, " +
            "                       a.mini_unit, " +
            "    mini_unit_name   = (select rtrim(name) from yp_zd_unit with (NOLOCK) where code = mini_unit), " +
            "                       a.drug_weight, " +
            "                       a.drug_weight_unit, " +
            "                       a.parent_no, " +
            "                       a.drug_volume, " +
            "                       a.drug_vol_unit, " +
            "                       a.dose, " +
            "                       a.dose_unit, " +
            "    dose_unit_name   = (select rtrim(name) from yp_zd_unit with (NOLOCK) where code = dose_unit), " +
            "                       a.exclu_act_order_no, " +
            "                       a.discription, " +
            "                       a.pattern_code, " +
            "                       a.order_type, " +
            "                       a.serial, " +
            "    self_buy=isnull(a.self_buy, case when b.jsy_flag = '1' then '3' else '0' end), " +
            "    del_flag=isnull(b.visible_flag_zy, ''), " +
            "                       a.drug_flag, " +
            "    suprice_flag=b.suprice_flag, " +
            "    self_flag        =b.self_flag, " +
            "    parent_flag='', " +
            "                       a.exec_unit, " +
            "    exec_unit_name   = (select rtrim(name) from zd_unit_code with (NOLOCK) where code = exec_unit) " +
            "FROM yz_pattern_order a with (NOLOCK), " +
            "     view_yp_zd_dict_base_yf b with (NOLOCK) " +
            "WHERE a.pattern_code = #{code} " +
            "  and a.group_no = b.group_no " +
            "  and a.order_code = b.code " +
            "  and a.serial = b.serial " +
            "  and isnull(b.visible_flag_zy, '') <> '1'")
    List<YzActOrder> huoQuMuBanShuJu(String code);


    @Select("select pattern_code," +
            "       rtrim(input_type) as input_type," +
            "       input_id  " +
            "from yz_OrderPattern with (NOLOCK) " +
            "where pattern_name = #{patternName} " +
            "  and input_id = #{inputId}" +
            "  and input_type <> 4 ")
    YzOrderPattern yuanLaiDeMuBanBianMa(@Param("patternName") String patternName,
                                        @Param("inputId") String inputId);


    @Select("select top 1 * " +
            "from yz_OrderPattern with (NOLOCK) " +
            "where pattern_code = #{patternCode} " +
            "  and input_id = #{inputId}" +
            "  and input_type <> 4 ")
    YzOrderPattern getDataAccordingToTemplateCode(@Param("patternCode") String patternCode,
                                                  @Param("inputId") String input);

    @Select("select pattern_code," +
            "       rtrim(input_type) input_type," +
            "       input_id  " +
            "from yz_OrderPattern with (NOLOCK) " +
            "where pattern_name = #{patternName} " +
            "and input_type not in ('3','4') ")
    YzOrderPattern gongGongMuBan(@Param("patternName") String patternName);


    @Delete("<script>" +
            "delete yz_OrderPattern " +
            "where pattern_code = #{patternCode}; " +
            "delete yz_pattern_order " +
            "where pattern_code = #{patternCode} " +
            "</script>")
    void shanChuLaoMuBan(@Param("patternCode") String patternCode);


    @Insert("insert yz_OrderPattern (pattern_code, pattern_name, py_code, d_code, dept_code, " +
            "                        ward_code, input_id, " +
            "                        input_type, input_date, sort_no) " +
            "values (#{patternCode},#{patternName},#{pyCode},#{dCode},#{deptCode},#{deptCode}," +
            "#{inputId},#{inputType}," +
            "getdate(),#{sortNo})")
    void chaRuMuBan(YzOrderPattern param);


    @Select("select count(1) " +
            "from yz_OrderPattern " +
            "where pattern_name = #{name} and input_id = #{userCode} ")
    Integer duplicateTemplateName(@Param("name") String name,
                                  @Param("userCode") String userCode);

    @Insert("<script>" +
            "INSERT INTO yz_pattern_order (act_order_no, order_code, frequ_code, drug_flag, supply_code, drug_quan, instruction, " +
            "                              drug_specification, mini_unit, drug_weight, drug_weight_unit, parent_no, drug_volume, " +
            "                              drug_vol_unit, dose, dose_unit, drug_occ,  order_name, discription, " +
            "                              pattern_code, serial, self_buy, exec_unit) " +
            "VALUES " +
            "<foreach collection='list' item='item' separator=','>" +
            "(#{item.actOrderNo}, #{item.orderCode}, #{item.frequCode}," +
            "<if test=\"item.drugFlag !=null and item.drugFlag != '' \">" +
            "#{item.drugFlag}," +
            "</if>" +
            "<if test=\"item.drugFlag == null or item.drugFlag == '' \">" +
            "'o'," +
            "</if>" +
            " #{item.supplyCode}," +
            "<if test=\"item.drugQuan != null \">" +
            "#{item.drugQuan}," +
            "</if>" +
            "<if test=\"item.drugQuan == null \">" +
            "null," +
            "</if>" +
            "#{item.instruction}, #{item.drugSpecification}, #{item.miniUnit}, " +
            "<if test=\"item.drugWeight != null\">" +
            "#{item.drugWeight}, " +
            "</if>" +
            "<if test=\"item.drugWeight == null\">" +
            "null, " +
            "</if>" +
            "#{item.drugWeightUnit}, " +
            "<if test=\"item.parentNo != null \">" +
            "#{item.parentNo}," +
            "</if>" +
            "<if test=\"item.parentNo == null \">" +
            "null," +
            "</if>" +
            "<if test=\"item.drugVolume != null\">" +
            "cast(#{item.drugVolume,jdbcType=DECIMAL} as decimal(10,2))," +
            "</if>" +
            "<if test=\"item.drugVolume == null\">" +
            "null," +
            "</if>" +
            " #{item.drugVolUnit}, " +
            "<if test=\"item.dose != null\">" +
            "cast(#{item.dose,jdbcType=DECIMAL} as decimal(10,2))," +
            "</if>" +
            "<if test=\"item.dose == null\">" +
            "null," +
            "</if>" +
            "#{item.doseUnit}," +
            "<if test=\"item.drugOcc != null \">" +
            "cast(#{item.drugOcc,jdbcType=DECIMAL} as decimal(10,2))," +
            "</if>" +
            "<if test=\"item.drugOcc == null \">" +
            "null," +
            "</if>" +
            " #{item.orderName},#{item.discription},#{patternCode},#{item.serial},#{item.selfBuy},#{item.execUnit})" +
            "</foreach>" +
            "</script>")
    void chaRuMuBanShuJu(List<XinZhenYzActOrder> list, String patternCode);


    @Select("select * " +
            "from yz_OrderPattern with (NOLOCK) " +
            "where pattern_code = #{code}")
    YzOrderPattern huoQuMuBanXinXi(@Param("code") String code);

    @Update("update yz_OrderPattern " +
            "set pattern_name = #{patternName}, " +
            "    py_code      = #{pyCode}, " +
            "    d_code       = #{dCode}, " +
            "    input_date   = getdate()," +
            "    sort_no = #{sortNo} " +
            "where pattern_code = #{patternCode} ")
    void genXingMuBan(@Param("patternName") String patternName,
                      @Param("pyCode") String pyCode,
                      @Param("dCode") String dCode,
                      @Param("patternCode") String patternCode,
                      @Param("sortNo") Integer sortNo);

    @Select("select rtrim(dept_code) from a_employee_mi where code = '${code}'")
    String userDeptCode(@Param("code") String code);

    @Delete("/* 父模板 */  " +
            "delete yz_OrderPattern " +
            "where pattern_code = #{patternCode}; " +
            "/* 模板下面的数据 */  " +
            "delete yz_pattern_order " +
            "where pattern_code = #{patternCode} ")
    void shanChuMuBan(@Param("patternCode") String patternCode);

    @Select("select pattern_code " +
            "from yz_OrderPattern " +
            "where collect_code = #{patternCode} and input_id = #{userId}")
    String whetherToSaveTheDoctorSOrderTemplate(@Param("patternCode") String patternCode,
                                                @Param("userId") String userId);

    @Select("select collect_code " +
            "from yz_OrderPattern with (NOLOCK) " +
            "where input_id = #{inputId} " +
            "  and collect_code = #{collectCode} ")
    String chongFuShouCang(@Param("inputId") String inputId,
                           @Param("collectCode") String collectCode);


    @Insert("insert into yz_OrderPattern (pattern_code, pattern_name, py_code, d_code, dept_code, ward_code, " +
            "                             input_id, input_type, input_date, sort_no, collect_code) " +
            "values (#{patternCode},#{patternName},#{pyCode},#{dCode},#{deptCode},#{deptCode},#{inputId},'4',getdate(),0,#{collectCode});")
    void chaRuShouCang(@Param("patternCode") String patternCode,
                       @Param("patternName") String patternName,
                       @Param("pyCode") String pyCode,
                       @Param("dCode") String dCode,
                       @Param("deptCode") String deptCode,
                       @Param("inputId") String inputId,
                       @Param("collectCode") String collectCode);

    @Select("select * " +
            "from yz_act_order with (NOLOCK) " +
            "where act_order_no = #{actOrderNo} ")
    YzActOrder chaXunYiZhuXinXin(BigDecimal actOrderNo);

    @Update("update yz_act_order set emergency_flag = #{emergencyFlag} " +
            "where act_order_no = #{actOrderNo} ")
    void sheZhiJinJi(@Param("actOrderNo") BigDecimal actOrderNo,
                     @Param("emergencyFlag") String emergencyFlag);

    @Insert("insert into yz_erase_order_comm (act_order_no, inpatient_no, admiss_times, comment, order_code, sys_date) " +
            "values (#{actOrderNo}, #{patNo}, #{times}, #{content}, #{orderCode}, getdate())")
    void chaRuCheXiaoYuanYin(@Param("actOrderNo") BigDecimal actOrderNo,
                             @Param("patNo") String patNo,
                             @Param("times") Integer times,
                             @Param("content") String content,
                             @Param("orderCode") String orderCode);

    @Delete("/*删除费用*/ " +
            "delete yz_zy_patient_fee " +
            "where act_order_no = #{actOrderNo}  " +
            "  and charge_status = '3';" +
            "/*删除药品的药单*/  " +
            "delete yz_yp_zy_order " +
            "where act_order_no = #{actOrderNo} " +
            "  and status_flag = '1';" +
            "/*删除医嘱表*/" +
            "DELETE " +
            "FROM yz_act_order " +
            "WHERE act_order_no = #{actOrderNo};" +
            "/*删除医技*/  " +
            "delete ysh_yj_req " +
            "where act_order_no = #{actOrderNo};")
    void cheXiaoYiZhu(@Param("actOrderNo") BigDecimal actOrderNo);


    @Insert("insert into yz_erase_order " +
            "select * " +
            "from yz_act_order " +
            "where act_order_no = #{actOrderNo}")
    void chaRuShanChuBiao(@Param("actOrderNo") BigDecimal actOrderNo);

    @Update("update yz_erase_order " +
            "set modifier           =#{userCode}, " +
            "    status_time        =getdate(), " +
            "    print_order_change =case when isnull(print_order_change, '') = '' then 'd' else '' end " +
            "where act_order_no = #{actOrderNo};" +
            "update op_record " +
            "set status ='d' " +
            "where act_order_no = #{actOrderNo};")
    void genXingShanChuBiaoZhi(@Param("userCode") String userCode,
                               @Param("actOrderNo") BigDecimal actOrderNo);


    @Update("update yz_act_order " +
            "set modifier = #{userCode}, " +
            "    end_time = #{endTime} " +
            "where act_order_no = #{actOrderNo} ")
    void sheZhiTingZhiShiJian(@Param("userCode") String userCode,
                              @Param("actOrderNo") BigDecimal actOrderNo,
                              @Param("endTime") String endTime);

    @Select("<script>" +
            "select * from yz_act_order with (NOLOCK) where act_order_no in " +
            "<foreach collection='list' item='item' index='index' open='(' close=')' separator=','>" +
            " #{item} " +
            "</foreach>" +
            "</script>")
    List<YzActOrder> yiZhuXinXiJiHe(@Param("list") List<BigDecimal> list);


    @Update("<script>" +
            "<foreach collection='list' item='item' separator=';'>" +
            "update yz_act_order " +
            "set modifier = #{userCode}, " +
            "    end_time = #{endTime} " +
            " where act_order_no = #{item} " +
            "</foreach>" +
            "</script>")
    void piLiangSheZhiTingZhiShiJian(@Param("list") List<BigDecimal> actOrderNo,
                                     @Param("userCode") String userCode,
                                     @Param("endTime") String endTime);

    @Insert("insert into zy_order_zk (act_order_no, old_ward, " +
            "old_dept, new_ward, new_dept) " +
            "values (#{actOrderNo},#{oldWard},#{oldDept},#{newWard},#{newDept})")
    void insertDoctorSOrder(ZyOrderZk zk);


    @Select("execute dbo.xc_zy_order_fee;1 @in_no = #{patNo}, @in_times = #{times}")
    List<DoctorSOrderFee> expensesForGettingADoctorSOrder(@Param("patNo") String patNo,
                                                          @Param("times") Integer times);

    @Select("select admiss_date " +
            "from zy_actpatient " +
            "where inpatient_no = #{patNo}")
    Date patientAdmissionTime(String patNo);
}
