package thyyxxk.webserver.dao.his.medicaladvice.drugmanage;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import thyyxxk.webserver.entity.medicaladvice.medicamanage.common.YzYpPageNo;
import thyyxxk.webserver.entity.medicaladvice.medicamanage.drugmanage.CyDetailVo;
import thyyxxk.webserver.entity.medicaladvice.medicamanage.drugmanage.DispenseDrugTotalVo;
import thyyxxk.webserver.entity.medicaladvice.medicamanage.drugmanage.DrugDetailTimeVo;
import thyyxxk.webserver.entity.medicaladvice.medicamanage.drugmanage.DrugDetailVo;
import thyyxxk.webserver.entity.medicaladvice.medicamanage.drugmanage.DrugSubmitParam;
import thyyxxk.webserver.entity.medicaladvice.medicamanage.drugmanage.DrugTotalVo;
import thyyxxk.webserver.entity.zhuyuanyisheng.caoyaoyizhu.YzActOrderCy;

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

/**
 * @Description:提交药品单
 * @Author:lihong
 * @Date: 2023/9/22
 */
@Mapper
public interface DrugSubmitDao {

 /**
  * @description:查询汇总
  * @author: lihong
  * @date: 2023/9/25 9:50
  * @param: param
  * @return: java.util.List<thyyxxk.webserver.entity.medicaladvice.medicamanage.drugmanage.DrugTotalVo>
  **/
 @Select({
         "<script>",
         "SELECT charge_code=b.charge_code     ," ,
                 "       serial     =b.serial   ," ,
                 "       drug_name   =max(d.name)," ,
                 "       spec       =max(d.specification)," ,
                 "       amount     =sum(a.amount)  ," ,
                 "       retprice   =max(b.pack_retprice)," ,
                 "       fee = convert(decimal(18,2),sum(a.amount)*max(b.pack_retprice))," ,
                 "       page_no    =max(isnull(a.page_no,0))," ,
                 "       confirm_flag='1'," ,
                 "       jy_flag     ='0'," ,
                 "       amount_jf   =0," ,
                 "       dept_code=max(a.dept_code)," ,
                 "       ward_code=max(a.ward_code)," ,
         "       ward_name = (select name from zy_ward_code where code = max(a.ward_code) )," ,
         "       group_no =max(a.group_no)," ,
                 "       pay_self =max(pay_self)" ,
                 " FROM yz_yp_zy_order a,yp_base_yf b,zy_actpatient c,yp_zd_dict d" ,
                 " where" ,
                 "    a.status_flag in ('1','3','4')  and a.amount <![CDATA[>]]> 0" ,
                 "  and     a.inpatient_no=c.inpatient_no" ,
                 "  and      a.admiss_times=c.admiss_times" ,
                 "  and      a.charge_code=b.charge_code" ,
                 "  and      a.group_no   =b.group_no" ,
                 "  and      isnull(a.serial,'01')=b.serial" ,
                 "  and      a.charge_code = d.code" ,
                 "  and      a.serial = d.serial" ,
                 "<when test='wardCode!=null' >",
                 "  and      a.ward_code=#{wardCode}" ,
                 "</when>",
               "<when test='pageNo!=null'>",
                       "  and  a.page_no =#{pageNo}" ,
               "</when>",
              "<when test='groupNo!=null' >",
                      "  and     a.group_no=#{groupNo}" ,
              "</when>",
              "<when test='pageClass!=null' >",
                 "  and     case when d.drug_flag <![CDATA[>]]> 0 and a.page_class != '1' then '2' else a.page_class end =#{pageClass}" ,
              "</when>",

                 "  and     (case when ${pageNo} <![CDATA[>]]> 0 then #{startDate} else occ_time end) <![CDATA[>=]]> #{startDate}" ,
                 "  and     (case when ${pageNo} <![CDATA[>]]> 0 then #{endDate} else occ_time end) <![CDATA[<=]]> #{endDate}" ,
            "<when test='alwaysFlag!=null' >",
                    "  and      a.always_flag = #{alwaysFlag}" ,
            "</when>",
            "<when test='startBedNo!=null' >",
                    "  and      c.bed_no <![CDATA[>=]]> #{startBedNo} " ,
            "</when>",
            "<when test='endBedNo!=null' >",
                            "and c.bed_no <![CDATA[<=]]> #{endBedNo}" ,
            "</when>",
            "<when test='jsyFlag!=null'>",
                    "  and       isnull(d.js_flag,'0')=#{jsyFlag}" ,
            "</when>",
                 "group by b.charge_code,b.serial" ,
                 "order by b.charge_code desc",
          "</script>"
 })
 List<DrugTotalVo> selectDrugSumitTotal(DrugSubmitParam param);

 /**
  * @description:查询明细按时间
  * @author: lihong
  * @date: 2023/9/25 9:50
  * @param: param
  * @return: java.util.List<thyyxxk.webserver.entity.medicaladvice.medicamanage.drugmanage.DrugDetailTimeVo>
  **/
 @Select({
         "<script>",
         "SELECT charge_code=b.charge_code     ," ,
                 "       serial     =b.serial   ," ,
                 "       drug_name   =d.name     ," ,
                 "       spec       =d.specification," ,
                 "       amount     =a.amount  ," ,
                 "       retprice   =b.pack_retprice," ,
                 "       page_no    =a.page_no," ,
                 "       amount_b   =a.amount," ,
                 "       fee = convert(decimal(18,2),a.amount*b.pack_retprice)," ,
                 "       confirm_flag='1'," ,
                 "       jy_flag     ='0'," ,
                 "       print_date  =getdate()," ,
                 "       inpatient_no=a.inpatient_no," ,
                 "       admiss_times=a.admiss_times," ,
                 "       act_order_no=a.act_order_no," ,
                 "       occ_time    =a.occ_time," ,
                 "       name        =a.name," ,
                 "       bed_no      =c.bed_no," ,
                 "       ward_code   =a.ward_code," ,
         "       ward_name = (select name from zy_ward_code where code = a.ward_code )," ,
         "       dept_code   =a.dept_code," ,
                 "       infant_flag =a.infant_flag," ,
                 "       drawer      =a.drawer," ,
                 "       always_flag =a.always_flag," ,
                 "       self_flag   =a.self_flag," ,
                 "       separate_flag=a.separate_flag," ,
                 "       suprice_flag =a.suprice_flag," ,
                 "       drug_flag    =a.drug_flag," ,
                 "       pay_self     =a.pay_self," ,
                 "       doctor_name  =a.doctor_name," ,
                 "       zy_serial_no =a.zy_serial_no," ,
                 "       group_no=a.group_no," ,
                 "       status_flag=a.status_flag," ,
                 "       drug_class=a.drug_class" ,
                 "  FROM yz_yp_zy_order a,yp_base_yf b,zy_actpatient c,yp_zd_dict d" ,
         "  where   a.status_flag in ('1','3','4')  and a.amount <![CDATA[>]]> 0" ,
         "  and     a.inpatient_no=c.inpatient_no" ,
         "  and      a.admiss_times=c.admiss_times" ,
         "  and      a.charge_code=b.charge_code" ,
         "  and      a.group_no   =b.group_no" ,
         "  and      isnull(a.serial,'01')=b.serial" ,
         "  and      a.charge_code = d.code" ,
         "  and      a.serial = d.serial" ,
         "<when test='wardCode!=null' >",
         "  and      a.ward_code=#{wardCode}" ,
         "</when>",
         "<when test='pageNo!=null'>",
         "  and  a.page_no =#{pageNo}" ,
         "</when>",
         "<when test='groupNo!=null' >",
         "  and     a.group_no=#{groupNo}" ,
         "</when>",
         "<when test='pageClass!=null' >",
         "  and     case when d.drug_flag <![CDATA[>]]> 0 and a.page_class != '1' then '2' else a.page_class end =#{pageClass}" ,
         "</when>",

         "  and     (case when ${pageNo} <![CDATA[>]]> 0 then #{startDate} else occ_time end) <![CDATA[>=]]> #{startDate}" ,
         "  and     (case when ${pageNo} <![CDATA[>]]> 0 then #{endDate} else occ_time end) <![CDATA[<=]]> #{endDate}" ,
         "<when test='alwaysFlag!=null' >",
         "  and      a.always_flag = #{alwaysFlag}" ,
         "</when>",
         "<when test='startBedNo!=null' >",
         "  and      c.bed_no <![CDATA[>=]]> #{startBedNo} " ,
         "</when>",
         "<when test='endBedNo!=null' >",
         "and c.bed_no <![CDATA[<=]]> #{endBedNo}" ,
         "</when>",
         "<when test='jsyFlag!=null' >",
         "  and       isnull(d.js_flag,'0')=#{jsyFlag}" ,
         "</when>",
                 "order by c.bed_no,a.inpatient_no,a.admiss_times,a.charge_code,a.occ_time",
         "</script>"
 })
 List<DrugDetailTimeVo> selectDrugDetailTime(DrugSubmitParam param);
/**
 * @description:查询明细
 * @author: lihong
 * @date: 2023/9/25 9:50
 * @param: param
 * @return: java.util.List<thyyxxk.webserver.entity.medicaladvice.medicamanage.drugmanage.DrugDetailVo>
 **/
@Select({
        "<script>",
        " SELECT charge_code=b.charge_code,",
        "       drug_name=e.name,",
        "       spec=e.specification,",
        "       amount=sum(a.amount),",
        "       page_no=a.page_no,",
        "       inpatient_no=a.inpatient_no,",
        "       admiss_times=a.admiss_times,",
        "       name=a.name,",
        "       bed_no=d.bed_no,",
        "       ward_code=a.ward_code,",
        "       freq_code=c.frequ_code,",
        "       dose=c.dose,",
        "       dose_unit=c.dose_unit,",
        "       dose_unit_name=(select name from yp_zd_unit where code=dose_unit)",
        " FROM yz_yp_zy_order a",
        " INNER JOIN yp_base_yf b ON a.charge_code=b.charge_code",
        "   AND a.group_no=b.group_no",
        "   AND isnull(a.serial,'01')=b.serial",
        " LEFT JOIN yz_act_order c ON a.act_order_no=c.act_order_no",
        " INNER JOIN yp_zd_dict e ON a.charge_code=e.code",
        "   AND a.serial=e.serial",
        " INNER JOIN zy_actpatient d ON a.inpatient_no=d.inpatient_no",
        "   AND a.admiss_times=d.admiss_times",
        " WHERE a.status_flag in ('1','3','4')",
        "   AND a.amount <![CDATA[>]]> 0",
        "<when test='wardCode!=null'>",
        "   AND a.ward_code=#{wardCode}",
        "</when>",
        "<when test='pageNo!=null'>",
        "   AND a.page_no=#{pageNo}",
        "</when>",
        "<when test='groupNo!=null'>",
        "   AND a.group_no=#{groupNo}",
        "</when>",
        "<when test='pageClass!=null'>",
        "   AND case when e.drug_flag <![CDATA[>]]> 0 and a.page_class != '1' then '2' else a.page_class end=#{pageClass}",
        "</when>",
        "   AND (case when ${pageNo} <![CDATA[>]]> 0 then #{startDate} else occ_time end) <![CDATA[>=]]> #{startDate}",
        "   AND (case when ${pageNo} <![CDATA[>]]> 0 then #{endDate} else occ_time end) <![CDATA[<=]]> #{endDate}",
        "<when test='alwaysFlag!=null'>",
        "   AND a.always_flag=#{alwaysFlag}",
        "</when>",
        "<when test='startBedNo!=null'>",
        "   AND d.bed_no <![CDATA[>=]]> #{startBedNo}",
        "</when>",
        "<when test='endBedNo!=null'>",
        "   AND d.bed_no <![CDATA[<=]]> #{endBedNo}",
        "</when>",
        "<when test='jsyFlag!=null'>",
        "   AND isnull(e.js_flag,'0')=#{jsyFlag}",
        "</when>",
        " GROUP BY b.charge_code, e.name, e.specification, a.page_no, a.inpatient_no, a.admiss_times, a.name, d.bed_no, a.ward_code, c.frequ_code, c.dose, c.dose_unit",
        " ORDER BY e.name, e.specification, d.bed_no",
        "</script>"
})
 List<DrugDetailVo> selectDrugDetail(DrugSubmitParam param);

 @Select("SELECT charge_code=a.charge_code," +
         "       serial=a.serial," +
         "       group_no=a.group_no," +
         "       quantity=a.quantity," +
         "       quantity_str=cast(a.quantity as varchar) + g.name," +
         "       supply_code=a.supply_code," +
         "       instruction=a.instruction," +
         "       inpatient_no=c.inpatient_no," +
         "       admiss_times=c.admiss_times," +
         "       bed_no=d.bed_no," +
         "       name=d.name," +
         "       ward_code=d.ward," +
         "       ward_name=(select name from zy_ward_code where code = d.ward)," +
         "       charge_name=f.name," +
         "       order_js=c.quantity," +
         "       order_je=c.order_je," +
         "       order_je_dj=c.order_je_dj," +
         "       order_js_dj=c.quantity_dj," +
         "       order_no=c.order_no," +
         "       page_no=c.page_no," +
         "       serial_no=c.serial_no," +
         "       c.cy_mtjs," +
         "       c.cy_jsl," +
         "       c.cy_zql," +
         "       c.cy_fj," +
         "       c.cy_ffcs," +
         "       c.cy_fysm," +
         "       c.cy_jssm," +
         "       doctor_code=(select name from a_employee_mi where code = c.doctor_code)," +
         "       e.name fy_name," +
         "       drug_unit=f.pack_unit," +
         "       unit_name=g.name" +
         " FROM yz_act_order_cy_detail a" +
         " INNER JOIN yp_base_yf b ON a.charge_code=b.charge_code" +
         "   AND a.serial=b.serial" +
         "   AND a.group_no=b.group_no" +
         " INNER JOIN yz_act_order_cy c ON c.order_no=a.order_no" +
         "   AND c.serial_no=a.serial_no" +
         " INNER JOIN zy_actpatient d ON c.inpatient_no=d.inpatient_no" +
         "   AND c.admiss_times=d.admiss_times" +
         " LEFT JOIN mzysh_zd_fysm e ON c.cy_fysm=e.code" +
         " INNER JOIN yp_zd_dict f ON b.charge_code=f.code" +
         "   AND b.serial=f.serial" +
         " LEFT JOIN yp_zd_unit g ON f.pack_unit=g.code" +
         " WHERE c.page_no=#{pageNo}" +
         "   AND d.ward=#{wardCode}" +
         "   AND a.group_no=#{groupNo}" +
         "   AND c.status_flag ${statusFlag}")
 List<CyDetailVo> selectCyDetail(DrugSubmitParam param);
 @Update({"<script>",
          "update yz_yp_zy_order " ,
                  " set yz_yp_zy_order.page_no =#{newPageNo} , yz_yp_zy_order.drawer=#{opCode}" ,
                  " from zy_actpatient c," ,
                  "     yp_zd_dict b" ,
                  " where yz_yp_zy_order.occ_time <![CDATA[>=]]> #{startDate}" ,
                  "  and yz_yp_zy_order.occ_time <![CDATA[<=]]> #{endDate}" ,
                  "  and yz_yp_zy_order.ward_code = #{wardCode}" ,
                  "  and yz_yp_zy_order.status_flag in ('1', '3', '4')" ,
                 "<when test='startBedNo!=null' >",
                 "  and      c.bed_no <![CDATA[>=]]> #{startBedNo} " ,
                 "</when>",
                 "<when test='endBedNo!=null' >",
                 "  and c.bed_no <![CDATA[<=]]> #{endBedNo}" ,
                 "</when>",
                  "  and yz_yp_zy_order.amount <![CDATA[>]]> 0" ,
                  "  and yz_yp_zy_order.inpatient_no = c.inpatient_no" ,
                  "  and yz_yp_zy_order.admiss_times = c.admiss_times" ,
                  "  and yz_yp_zy_order.page_no = #{pageNo}" ,
                  "  and yz_yp_zy_order.page_class = #{pageClass}" ,
                  "  and yz_yp_zy_order.group_no = #{groupNo}" ,
                  "<when test='alwaysFlag!=null' >",
                  "  and      yz_yp_zy_order.always_flag = #{alwaysFlag}" ,
                  "</when>",
                  "<when test='jsyFlag!=null' >",
                  "  and isnull(b.js_flag, '0')=#{jsyFlag}" ,
                  "</when>",
                  "  and yz_yp_zy_order.charge_code = b.code",
         "</script>"
 })
 int updateYzYpZyOrderPageNo(DrugSubmitParam param);

@Update({"<script>",
         "update yz_act_occ" ,
                 " set yz_act_occ.page_no =#{newPageNo}" ,
                 " from zy_actpatient c," ,
                 "     yp_zd_dict b" ,
                 " where yz_act_occ.occ_time <![CDATA[>=]]> #{startDate}" ,
                 "  and yz_act_occ.occ_time <![CDATA[<=]]> #{endDate}" ,
                 "  and yz_act_occ.ward_code = #{wardCode}" ,
                 "  and isnull(yz_act_occ.status_flag, '') in ('', '1', '3', '4')" ,
                 "<when test='startBedNo!=null' >",
                 "  and      c.bed_no <![CDATA[>=]]> #{startBedNo} " ,
                 "</when>",
                 "<when test='endBedNo!=null' >",
                 "  and c.bed_no <![CDATA[<=]]> #{endBedNo}" ,
                 "</when>",
                 "  and yz_act_occ.drug_quan2 <![CDATA[>]]> 0" ,
                 "  and yz_act_occ.inpatient_no = c.inpatient_no" ,
                 "  and yz_act_occ.admiss_times = c.admiss_times" ,
                 "  and yz_act_occ.page_no = #{pageNo}" ,
                 "  and yz_act_occ.page_class = #{pageClass}" ,
                 "  and yz_act_occ.group_no = #{groupNo}" ,
                 "<when test='jsyFlag!=null' >",
                 "  and isnull(b.js_flag, '0')=#{jsyFlag}" ,
                 "</when>",
                 "<when test='alwaysFlag!=null' >",
                 "  and      yz_act_occ.always_flag = #{alwaysFlag}" ,
                 "</when>",
                 "  and yz_act_occ.charge_code = b.code",
        "</script>"
})
 int updateYzActOccPageNo(DrugSubmitParam param);
@Update("<script>" +
        " update yz_act_order_cy set status_flag =#{statusFlag} , submit_id =#{submitId} , submit_date =#{submitDate}" +
        " <if test=\" chargeDate != null  \">"+
        " ,charge_date = #{chargeDate} " +
        " </if>"+
        " where order_no =#{orderNo} and serial_no =#{serialNo}" +
        "</script>")
 int updateYzActOrderCy(YzActOrderCy yzActOrderCy);
 @Select(" select min(occ_time) occ_time" +
         " from yz_yp_zy_order" +
         " where status_flag = '1'" +
         "  and page_no = 0" +
         "  and page_class = #{pageClass}" +
         "  and ward_code = #{wardCode}" +
         "  and inpatient_no in (select inpatient_no from zy_actpatient)" +
         "  and amount > 0")
 Date selectMinOccTime(DrugSubmitParam param);
@Select(" select status_flag from yz_act_order_cy where order_no =#{orderNo} and serial_no =#{serialNo} ")
 String selctCyStatusFlag(YzActOrderCy yzActOrderCy);
 @Update(" update yz_yp_zy_order set page_no =0 where page_no =#{pageNo} and status_flag in ( '1' , '3' , '4' ) ")
 int delYzYpZyOrderPageNo(DrugSubmitParam param);
 @Update(" update yz_act_occ set page_no =0 where page_no =#{pageNo} and isnull ( status_flag , '' ) in ( '' , '1' , '3' , '4' ) ")
 int delYzActOccPageNo(DrugSubmitParam param);
 /**
  * @description:查询退药汇总
  * @author: lihong
  * @date: 2023/10/8 10:28
  * @param: param
  * @return: java.util.List<thyyxxk.webserver.entity.medicaladvice.medicamanage.drugmanage.DrugTotalVo>
  **/
 @Select({"<script>",
          "SELECT charge_code=b.charge_code     ," ,
                  "       serial     =b.serial   ," ,
                  "       drug_name   =max(d.name)," ,
                  "       spec       =max(d.specification)," ,
                  "       amount     =sum(a.amount)  ," ,
                  "       fee = convert(decimal(18,2),sum(a.amount)*max(b.pack_retprice))," ,
                  "       retprice   =max(b.pack_retprice)," ,
                  "       page_no    =max(isnull(a.page_no_ty,0))," ,
                  "       confirm_flag='1'," ,
                  "       jy_flag     ='0'," ,
                  "       amount_jf   =0," ,
                  "       dept_code=max(a.dept_code)," ,
                  "       ward_code=max(a.ward_code)," ,
                  "       ward_name = (select name from zy_ward_code where code = max(a.ward_code) )," +
                  "       group_no =max(a.group_no)," ,
                  "       pay_self =max(pay_self)" ,
                  " FROM yz_yp_zy_order a,yp_base_yf b,zy_actpatient c,yp_zd_dict d" ,
                  " where    a.page_class not in ( '5','6') and isnull(a.page_no_ty,0) =#{pageNo} and" ,
                  "        a.group_no=#{groupNo} and" ,
                  "        (case when ${pageNo} <![CDATA[>]]> 0 then #{startDate} else occ_time end) <![CDATA[>=]]> #{startDate} and" ,
                  "        (case when ${pageNo} <![CDATA[>]]> 0 then #{endDate} else occ_time end) <![CDATA[<=]]> #{endDate} and" ,
                  "        a.status_flag='1' and" ,
                  "        a.amount <![CDATA[<]]> 0          and" ,
                  "        a.inpatient_no=c.inpatient_no and" ,
                  "        a.admiss_times=c.admiss_times and" ,
                  "<when test='startBedNo!=null' >",
                  "        c.bed_no <![CDATA[>=]]> #{startBedNo} and " ,
                  "</when>",
                  "<when test='endBedNo!=null' >",
                  "   c.bed_no <![CDATA[<=]]> #{endBedNo}  and " ,
                  "</when>",
                  "        a.ward_code=#{wardCode} and" ,
                  "        a.charge_code=b.charge_code  and" ,
                  "        a.group_no   =b.group_no  and" ,
                  "        a.serial     =b.serial and" ,
                  "        a.charge_code = d.code and" ,
                  "        a.serial = d.serial" ,
                  " group by b.charge_code,b.serial",
         "</script>"
 })
 List<DrugTotalVo> selectTcSumTotal(DrugSubmitParam param);
/**
 * @description:查询退药明细 按时间
 * @author: lihong
 * @date: 2023/10/8 10:28
 * @param: param
 * @return: java.util.List<thyyxxk.webserver.entity.medicaladvice.medicamanage.drugmanage.DrugDetailTimeVo>
 **/
@Select({
        "<script>",
         "SELECT charge_code=b.charge_code     ," ,
                 "       serial     =b.serial   ," ,
                 "       drug_name   =d.name     ," ,
                 "       spec       =d.specification," ,
                 "       amount     =a.amount  ," ,
                 "       fee = convert(decimal(18,2),a.amount*b.pack_retprice)," ,
                 "       retprice   =b.pack_retprice," ,
                 "       page_no    =a.page_no_ty," ,
                 "       confirm_flag='1'," ,
                 "       jy_flag     ='0'," ,
                 "       inpatient_no=a.inpatient_no," ,
                 "       admiss_times=a.admiss_times," ,
                 "       act_order_no=a.act_order_no," ,
                 "       occ_time    =a.occ_time," ,
                 "       name        =a.name," ,
                 "       bed_no      =c.bed_no," ,
                 "       ward_code   =a.ward_code," ,
                 "       dept_code   =a.dept_code," ,
                 "       infant_flag =a.infant_flag," ,
                 "       drawer      =a.drawer," ,
                 "       always_flag =a.always_flag," ,
                 "       self_flag   =a.self_flag," ,
                 "       separate_flag=a.separate_flag," ,
                 "       suprice_flag =a.suprice_flag," ,
                 "       drug_flag    =a.drug_flag," ,
                 "       pay_self     =a.pay_self," ,
                 "       doctor_name  =a.doctor_name," ,
                 "       zy_serial_no =a.zy_serial_no," ,
                 "       group_no=a.group_no," ,
                 "       status_flag=a.status_flag," ,
                 "       drug_class=a.drug_class" ,
                 " FROM yz_yp_zy_order a,yp_base_yf b,zy_actpatient c,yp_zd_dict d" ,
                 " where    a.page_class not in ( '5','6') and" ,
                 "        isnull(a.page_no_ty,0) =#{pageNo} and" ,
                 "        a.group_no=#{groupNo} and" ,
                 "        (case when ${pageNo} <![CDATA[>]]> 0 then #{startDate} else occ_time end)  <![CDATA[>=]]> #{startDate} and" ,
                 "        (case when ${pageNo} <![CDATA[>]]> 0 then #{endDate} else occ_time end)  <![CDATA[<=]]> #{endDate} and" ,
                 "        a.status_flag ='1' and" ,
                 "        a.amount <![CDATA[<]]> 0          and" ,
                 "        a.inpatient_no=c.inpatient_no and" ,
                 "        a.admiss_times=c.admiss_times and" ,
                 "<when test='startBedNo!=null' >",
                 "        c.bed_no <![CDATA[>=]]> #{startBedNo} and " ,
                 "</when>",
                 "<when test='endBedNo!=null' >",
                 "   c.bed_no <![CDATA[<=]]> #{endBedNo}  and " ,
                 "</when>",
                 "         a.ward_code=#{wardCode} and" ,
                 "        a.charge_code=b.charge_code  and" ,
                 "        a.group_no=b.group_no  and" ,
                 "        a.serial     =b.serial and" ,
                 "        a.charge_code = d.code and" ,
                 "        a.serial = d.serial" ,
                 " order by c.bed_no,a.inpatient_no,a.admiss_times,a.charge_code,a.occ_time",
        "</script>"
})
 List<DrugDetailTimeVo> selectTcTimeDetail(DrugSubmitParam param);
/**
 * @description:查询退药明细
 * @author: lihong
 * @date: 2023/10/8 10:28
 * @param: param
 * @return: java.util.List<thyyxxk.webserver.entity.medicaladvice.medicamanage.drugmanage.DrugDetailVo>
 **/
@Select({
        "<script>",
         " SELECT charge_code=b.charge_code     ," ,
                 "       serial     =b.serial   ," ,
                 "       drug_name   =max(e.name)     ," ,
                 "       spec       =max(e.specification)," ,
                 "       amount     =sum(a.amount)  ," ,
                 "       retprice   =max(b.pack_retprice)," ,
                 "       page_no    =max(a.page_no_ty)," ,
                 "       amount_b   =sum(a.amount)," ,
                 "       confirm_flag='1'," ,
                 "       jy_flag     ='0'," ,
                 "       inpatient_no=a.inpatient_no," ,
                 "       admiss_times=a.admiss_times," ,
                 "       name        =max(a.name)," ,
                 "       bed_no      =max(d.bed_no)," ,
                 "       ward_code   =max(a.ward_code)," ,
                 "       dept_code   =max(a.dept_code)" ,
                 "FROM yz_yp_zy_order a,yp_base_yf b,zy_actpatient d,yp_zd_dict e" ,
                 "where    a.page_class not in ( '5','6')  and isnull(a.page_no_ty,0) =#{pageNo}  and" ,
                 "        a.group_no=#{groupNo} and" ,
                 "        (case when ${pageNo} <![CDATA[>]]> 0 then #{startDate} else occ_time end) <![CDATA[>=]]> #{startDate} and" ,
                 "        (case when ${pageNo} <![CDATA[>]]> 0 then #{endDate} else occ_time end) <![CDATA[<=]]> #{endDate} and" ,
                 "        a.status_flag ='1' and" ,
                 "        a.amount <![CDATA[<]]> 0          and" ,
                 "        a.inpatient_no=d.inpatient_no and" ,
                 "        a.admiss_times=d.admiss_times and" ,
                "<when test='startBedNo!=null' >",
                "        d.bed_no <![CDATA[>=]]> #{startBedNo} and " ,
                "</when>",
                "<when test='endBedNo!=null' >",
                "   d.bed_no <![CDATA[<=]]> #{endBedNo}  and " ,
                "</when>",
                 "         a.ward_code=#{wardCode} and" ,
                 "        a.charge_code=b.charge_code  and" ,
                 "        a.serial     =b.serial and" ,
                 "        a.group_no     =b.group_no and" ,
                 "        a.charge_code = e.code and" ,
                 "        a.serial = e.serial" ,
                 " group by a.inpatient_no,a.admiss_times,b.charge_code,b.serial",
        "</script>"
})
 List<DrugDetailVo> selectTcDetail(DrugSubmitParam param);
 @Update({
         "<script>",
         "update yz_yp_zy_order" ,
                 "set page_no_ty =#{newPageNo}" ,
                 "from zy_actpatient c" ,
                 "where yz_yp_zy_order.occ_time <![CDATA[>=]]> #{startDate}" ,
                 "  and yz_yp_zy_order.occ_time <![CDATA[<=]]> #{endDate}" ,
                 "  and yz_yp_zy_order.ward_code = #{wardCode}" ,
                 "  and yz_yp_zy_order.status_flag = '1'" ,
                "<when test='startBedNo!=null' >",
                "    and    c.bed_no <![CDATA[>=]]> #{startBedNo}  " ,
                "</when>",
                "<when test='endBedNo!=null' >",
                "  and  c.bed_no <![CDATA[<=]]> #{endBedNo}   " ,
                "</when>",
                 "  and yz_yp_zy_order.amount <![CDATA[<]]> 0" ,
                 "  and yz_yp_zy_order.inpatient_no = c.inpatient_no" ,
                 "  and yz_yp_zy_order.admiss_times = c.admiss_times" ,
                 "  and isnull(yz_yp_zy_order.page_no_ty, 0) = 0" ,
                 "  and yz_yp_zy_order.page_class != '5'" ,
                 "  and yz_yp_zy_order.group_no =#{groupNo}",
         "</script>"
 })
 int updateYzYpZyOrderTcPageNo(DrugSubmitParam param);
 @Update(" update yz_yp_zy_order set page_no_ty =0  where page_no_ty = #{pageNo}")
 int delYzYpZyOrderTcPageNo(DrugSubmitParam param);

 /**
  * @description:查询药单类型
  * @author: lihong
  * @date: 2023/10/9 10:39
  * @param: param
  * @return: java.util.List<thyyxxk.webserver.entity.medicaladvice.medicamanage.common.YzYpPageNo>
  **/
 @Select({
         "<script>",
         "SELECT yp_zy_patient.page_no," ,
                 "       page_class=min(isnull(page_type,supply_code))," ,
                 "       group_no =max(group_no)," ,
                 "       group_name =(select group_name from yp_zd_group_name a where a.group_no= max(yp_zy_patient.group_no))," ,
                 "       confirm_time=max(confirm_time)," ,
                 "       act_order_no = max(act_order_no)" ,
                 " FROM yp_zy_patient" ,
                 " where    confirm_time  <![CDATA[>=]]> #{startDate}" ,
                 "     and   confirm_time  <![CDATA[<=]]> #{endDate}" ,
                 "     and   page_no <![CDATA[>]]> 0" ,
                 "     and (group_no='73' or group_no = '81' )" ,
                 "     and ward_code = #{wardCode}" ,
                 "<when test=\"pageClass!=null and pageClass!='' \">",
                 "    and (isnull(page_type,'') = #{pageClass} or supply_code=#{supplyCode} )  " ,
                 "</when>",
                 "     and isnull(section_code,'') = ''" ,
                 " group by page_no",
         "</script>"
 })
 List<YzYpPageNo> selectYpPageNo(DrugSubmitParam param);

 /**
  * @description:查询 长期或临时 发药汇总
  * @author: lihong
  * @date: 2023/10/9 11:01
  * @param: param
  * @return: java.util.List<thyyxxk.webserver.entity.medicaladvice.medicamanage.drugmanage.DispenseDrugTotalVo>
  **/
 @Select({
         "<script>",
         "SELECT" ,
                 "    name=a.name," ,
                 "    bed_no=a.bed_no," ,
                 "    always_flag=case a.always_flag when 'a' then '长期' else '临时' end," ,
                 "    drug_name   =max(b.name)    ," ,
                 "    spec       =max(b.specification)," ,
                 "    amount     =sum(a.amount)  ," ,
                 "    ward_code   =max(a.ward_code)," ,
                 "    dept_code   =max(a.dept_code)," ,
                 "    confirm_time=a.confirm_time," ,
                 "    cs=c.name" ,
                 "<if test=\"alwaysFlag=='a'.toString\">",
                 "      ,  order_date=a.order_date  " ,
                 "</if>",
                 " FROM yp_zy_patient a,yp_zd_dict b left join yp_zd_manufactory c " +
                         "on b.manu_code=c.code " ,
                 " where  a.confirm_time <![CDATA[>=]]> #{startDate} " ,
                 "                 and    a.confirm_time <![CDATA[<=]]> #{endDate} " ,
                 "                 and    a.ward_code = #{wardCode} " ,
                 "                 and    a.charge_code=b.code  " ,
                 "                 and    a.serial     =b.serial " ,
                 "                 and    a.always_flag=#{alwaysFlag} " ,
               "<when test=\"startBedNo!=null and startBedNo!='' \">",
               "      and  cast(a.bed_no as int ) <![CDATA[>=]]> #{startBedNo}   " ,
               "</when>",
               "<when test=\"endBedNo!=null and endBedNo!='' \" >",
               "  and  cast(a.bed_no as int ) <![CDATA[<=]]> #{endBedNo}   " ,
               "</when>",
                 " group by b.code,b.serial,a.always_flag,a.name,a.bed_no,a.confirm_time,c.name" ,
              "<if test=\"alwaysFlag=='a'.toString\">",
              "       ,a.order_date " ,
              "</if>",
         "</script>"
 })
 List<DispenseDrugTotalVo> selectDispenseDrugTotalVo(DrugSubmitParam param);
 /**
  * @description:查询汇总明细 按单号
  * @author: lihong
  * @date: 2023/10/9 11:34
  * @param: param
  * @return: java.util.List<java.lang.Object>
  **/
 @Select("SELECT charge_code=b.code  ," +
         "       serial     =b.serial       ," +
         "       drug_name   =max(b.name)    ," +
         "       spec       =max(b.specification)," +
         "       amount     =sum(a.amount)  ," +
         "       amount_jf  =sum(case when supply_code='w' then amount else 0 end)  ," +
         "       retprice   =max(a.retprice)," +
         "       fee         =sum(convert(decimal(18,2),(case when supply_code='w' then 0 else amount end) * a.retprice))," +
         "       ward_code   =max(a.ward_code)," +
         "       dept_code   =max(a.dept_code)" +
         " FROM yp_zy_patient a,yp_zd_dict b" +
         " where" +
         "        a.confirm_time >= #{startDate} and" +
         "        a.confirm_time <= #{endDate} and" +
         "        a.ward_code = #{wardCode} and" +
         "    ((case when ${pageNo} > 0 then #{pageClass} else isnull(a.page_type,'') end) = #{pageClass} or" +
         "     (case when ${pageNo} > 0 then #{supplyCode} else isnull(a.supply_code,'') end) = #{supplyCode} ) and" +
         "        a.page_no=#{pageNo} and" +
         "        a.charge_code=b.code  and" +
         "        a.serial     =b.serial" +
         " group by b.code,b.serial")
 List<DrugTotalVo> selectDispenseDrugTotalDetail(DrugSubmitParam param);
/**
 * @description:查询发药明细
 * @author: lihong
 * @date: 2023/10/9 14:22
 * @param: param
 * @return: java.util.List<thyyxxk.webserver.entity.medicaladvice.medicamanage.drugmanage.DrugDetailVo>
 **/
@Select("SELECT charge_code=a.charge_code     ," +
        "       serial     =a.serial  ," +
        "       drug_name   =b.name    ," +
        "       spec       =b.specification," +
        "       amount     =a.amount  ," +
        "       retprice   =a.retprice," +
        "       fee         =convert(decimal(18,2),a.amount*a.retprice)," +
        "       page_no    =a.page_no," +
        "       inpatient_no=a.inpatient_no," +
        "       admiss_times=a.admiss_times," +
        "       act_order_no=a.act_order_no," +
        "       occ_time    =a.order_date," +
        "       name        =a.name," +
        "       bed_no      =a.bed_no," +
        "       ward_code   =a.ward_code," +
        "       dept_code   =a.dept_code," +
        "       confirm_id  =a.keeper," +
        "  confirm_name = (select name  from a_employee_mi where a_employee_mi.code=a.keeper ),"+
        "       confirm_time=a.confirm_time," +
        "       confirm_flag=case when supply_code='w' then '2' else '0' end," +
        "       py_code=b.py_code" +
        " FROM yp_zy_patient a,yp_zd_dict b" +
        " where    a.page_no =#{pageNo}  and" +
        "        a.charge_code=b.code  and" +
        "        a.serial   =b.serial" +
        " order by inpatient_no,admiss_times,charge_code,serial,occ_time")
 List<DrugDetailVo> selectDispenseDrugDetail(DrugSubmitParam param);


@Select("SELECT charge_code=a.charge_code     ,   " +
        "         serial     =a.serial  ," +
        "         drugname   =b.name    ," +
        "         spec       =b.specification," +
        "         amount     =a.amount  ,  " +
        "         retprice   =a.retprice," +
        "       fee         =convert(decimal(18,2),a.amount*a.retprice)," +
        "         page_no    =a.page_no," +
        "         inpatient_no=a.inpatient_no," +
        "         admiss_times=a.admiss_times," +
        "         act_order_no=a.act_order_no," +
        "         occ_time    =a.order_date," +
        "         name        =a.name," +
        "         bed_no      =a.bed_no," +
        "         ward_code   =a.ward_code," +
        "         dept_code   =a.dept_code," +
        "         confirm_id  =a.keeper," +
        "  confirm_name = (select name  from a_employee_mi where a_employee_mi.code=a.keeper ),"+
        "         confirm_time=a.confirm_time," +
        "         confirm_flag=case when supply_code='w' then '2' else '0' end" +
        "    FROM yp_zy_patient a,yp_zd_dict b" +
        "  where    a.confirm_time >=#{startDate} and" +
        "         a.confirm_time <=#{endDate} and" +
        "         a.ward_code = #{wardCode} and" +
        "         a.amount < 0          and" +
        "         a.charge_code=b.code  and" +
        "         a.serial   =b.serial " +
        "  order by inpatient_no,admiss_times,charge_code,serial,occ_time")
 List<DrugDetailVo> selectDispenseTyDetail(DrugSubmitParam param);

/**
 * @description:查询结余明细
 * @author: lihong
 * @date: 2023/10/9 15:05
 * @param: param
 * @return: java.util.List<thyyxxk.webserver.entity.medicaladvice.medicamanage.drugmanage.DrugDetailVo>
 **/
@Select({
        "<script>",
        "SELECT charge_code=a.charge_code     ," ,
                "       serial     =a.serial  ," ,
                "       drug_name   =b.name    ," ,
                "       spec       =b.specification," ,
                "       amount     =a.amount  ," ,
                "       retprice   =a.price," ,
        "       fee = convert(decimal(18,2),a.amount*a.price)," ,
                "       page_no    =a.page_no," ,
                "       ward_code   =a.ward_code," ,
                "       dept_code   =a.dept_code," ,
                "       confirm_id  =a.confirm_id," ,
               "  confirm_name = (select name  from a_employee_mi where a_employee_mi.code=a.confirm_id ),",
                "       confirm_time=a.confirm_time," ,
                "       supply_code=''" ,
                " FROM yz_yp_jy a,yp_zd_dict b" ,
                " where    a.confirm_time <![CDATA[>=]]>#{startDate} and" ,
                "        a.confirm_time <![CDATA[<=]]>#{endDate} and" ,
                "        a.ward_code = #{wardCode} and" ,
               "<when test='pageNo!=null' >",
               "       a.page_no = #{pageNo} and  " ,
               "</when>",
                "        a.charge_code=b.code  and" ,
                "        a.serial   =b.serial" ,
                " order by charge_code,serial",
        "</script>"
})
 List<DrugDetailVo> selectJyDetail(DrugSubmitParam param);
/**
 * @description:大输液消耗统计
 * @author: lihong
 * @date: 2023/10/10 16:07
 * @param: param
 * @return: java.util.List<thyyxxk.webserver.entity.medicaladvice.medicamanage.drugmanage.DrugDetailVo>
 **/
 @Select("SELECT amount=sum(a.amount)," +
         "       fee    =sum(convert(decimal(18,2),a.amount * a.retprice))  ," +
         "       charge_code=b.code," +
         "       serial     =b.serial," +
         "       drugname   =max(b.name)," +
         "       spec       =max(b.specification)," +
         "       print_date =getdate()" +
         " FROM yz_yp_zy_order a ,yp_zd_dict b(nolock)" +
         " where    a.occ_time >= #{startDate} and" +
         "        a.occ_time <= #{endDate} and" +
         "        a.ward_code  = #{wardCode} and" +
         "        a.drug_class='i' and" +
         "        a.page_class ='4' and" +
         "        a.charge_code=b.code and" +
         "        isnull(a.serial,'01')=b.serial and" +
         "        a.page_no='0' and isnull(b.js_flag,'0')=1" +
         "  and a.status_flag in ('1','3','4')" +
         " group by b.code,b.serial")
List<DrugDetailVo> selectDsyTotal(DrugSubmitParam param);

}
