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

import cn.hnthyy.thmz.entity.his.YpZdDict;
import cn.hnthyy.thmz.pageDto.PageBean;
import cn.hnthyy.thmz.vo.YpBaseYfBoundVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @description: 药品请领、上下限管理
 * @author: WANGJIALIANG
 * @time: 2021/8/25 9:51
 */
public interface YfDrugApplyMapper {

    /**
     * 查询药房药品上下限信息
     * @param searchText
     * @param groupNo
     * @return
     * @return
     */
    @Select("<script>" +
            "SELECT  top ${pageBean.pageSize} * from " +
            "(SELECT  row_number() over (order by (case when b.mini_stock_amount>=max(isnull(b.stock_amount,0)) then 0 else 1 end)," +
            "(case when dd.stock_amount>0 then 0 else 1 end),(case when isnull(round(sum(case when datediff(day,confirm_time,getdate()) <![CDATA[>]]> 60 then 0 else quantity end),2),0)=0 then 1 else 0 end)," +
            "isnull( round(max(isnull(b.stock_amount,0)+isnull(bb.stock_amount,0)) /case when (sum(case when datediff(day,confirm_time,getdate()) <![CDATA[>]]> 14 then 0 else quantity end)/(14))<![CDATA[<=]]>0 then 999999999 else (sum(case when datediff(day,confirm_time,getdate()) <![CDATA[>]]> 14 then 0 else quantity end)/(14)) end,1),0)) RowNumber," +
            "  b.charge_code," +
            "  dd.stock_amount as stock_amount_yk," +
            "  a.name," +
            "  b.serial," +
            "  b.group_no," +
            "  a.specification," +
            "  a.drug_flag," +
            "  a.visible_flag," +
            "  a.manu_code," +
            "  stock_amount=max(isnull(b.stock_amount,0))," +
            "  location=b.location," +
            "  b.max_stock_amount," +
            "  b.mini_stock_amount," +
            "  seven_day_dosage=isnull(round(sum(case when datediff(day,confirm_time,getdate()) <![CDATA[>]]> 7 then 0 else quantity end),2),0)," +
            "  fourteen_day_dosage=isnull(round(sum(case when datediff(day,confirm_time,getdate()) <![CDATA[>]]> 14 then 0 else quantity end),2),0)," +
            "  thirty_day_dosage=isnull(round(sum(case when datediff(day,confirm_time,getdate()) <![CDATA[>]]> 30 then 0 else quantity end),2),0)," +
            "  sixty_day_dosage=isnull(round(sum(case when datediff(day,confirm_time,getdate()) <![CDATA[>]]> 60 then 0 else quantity end),2),0)," +
            "  available_day=isnull( round(max(isnull(b.stock_amount,0)+isnull(bb.stock_amount,0)) /case when (sum(case when datediff(day,confirm_time,getdate()) <![CDATA[>]]> 14 then 0 else quantity end)/(14))<![CDATA[<=]]>0 then 999999999 else (sum(case when datediff(day,confirm_time,getdate()) <![CDATA[>]]> 14 then 0 else quantity end)/(14)) end,1),0)" +
            " FROM yp_zd_dict a  ,yp_base_yf b,yp_base dd," +
            "     (select c.name,a.charge_item_code code,c.serial,a.confirm_time," +
            "        round(sum(case when a.serial <![CDATA[<]]> c.serial then quantity*cy_fy/pack_size else quantity*cy_fy end),2) quantity" +
            "      from yp_mz_fytj a," +
            "           (select a.name,a.code,a.serial,a.specification,a.pack_size" +
            "            from yp_zd_dict a,(select code,max(serial) serial" +
            "                               from yp_zd_dict group by code) b" +
            "            where a.code=b.code and a.serial=b.serial) c" +
            "      where a.charge_item_code=c.code and datediff(day,confirm_time,getdate()) <![CDATA[<]]> 61 and group_no =#{groupNo}" +
            "      group by c.name,a.charge_item_code,c.serial,a.confirm_time" +
            "      union all" +
            "      select" +
            "        c.name,a.charge_code code,c.serial,a.confirm_time," +
            "        round(sum(case when a.serial <![CDATA[<]]> c.serial then amount/pack_size else amount end),2) quantity" +
            "      from yp_zy_patient a," +
            "           (select a.name,a.code,a.serial,a.specification,a.pack_size" +
            "            from yp_zd_dict a,(select code,max(serial) serial" +
            "                               from yp_zd_dict group by code) b" +
            "            where a.code=b.code and a.serial=b.serial) c" +
            "      where a.charge_code=c.code and datediff(day,confirm_time,getdate()) <![CDATA[<]]> 61 and acct_sign=2 and group_no =#{groupNo}" +
            "      group by c.name,a.charge_code,c.serial,a.confirm_time) aa," +
            "     (select c.name,a.charge_code,c.serial," +
            "             case when a.group_no='81' then '22' else '11' end group_no," +
            "             round(sum(case when a.serial <![CDATA[<]]> c.serial then stock_amount/pack_size else stock_amount end),2) stock_amount" +
            "      from yp_base_yf a," +
            "           (select a.name,a.code,a.serial,a.specification,a.pack_size" +
            "            from yp_zd_dict a,(select code,max(serial) serial" +
            "                               from yp_zd_dict group by code) b" +
            "            where a.code=b.code and a.serial=b.serial) c" +
            "      where a.charge_code=c.code and a.group_no <![CDATA[<>]]> '91' and a.group_no =#{groupNo}" +
            "      group by c.name,a.charge_code,c.serial,(case when a.group_no='81' then '22' else'11' end)) bb" +
            " where   a.code=b.charge_code and" +
            "        a.serial=b.serial and" +
            "        b.group_no=#{groupNo} and" +
            "        a.code*=aa.code and" +
            "        a.serial*=aa.serial and" +
            "        a.code*=dd.charge_code and" +
            "        a.serial*=dd.serial and" +
            "        dd.group_no = case when b.group_no='81' then '22' else '11' end  and" +
            "        a.code*=bb.charge_code and" +
            "        a.serial*=bb.serial and" +
            "         (a.py_code like '${searchText}%' or" +
            "         a.d_code like '${searchText}%' or" +
            "         a.code like '${searchText}%' or" +
            "         a.name like '${searchText}%') and" +
            "        isnull(a.visible_flag,'0') <![CDATA[<>]]> '1' and" +
            "        isnull(a.del_flag,'0') <![CDATA[<>]]> '1'" +
            " group by  b.charge_code," +
            "          a.name," +
            "          b.serial," +
            "          b.group_no," +
            "          a.specification," +
            "          a.drug_flag," +
            "          a.visible_flag," +
            "          a.manu_code," +
            "          b.max_stock_amount," +
            "          b.mini_stock_amount," +
            "          a.pack_retprice   ," +
            "          dd.stock_amount   ," +
            "          b.location" +
            ") as A WHERE RowNumber >#{pageBean.pageSize}*#{pageBean.pageIndex} order by RowNumber asc" +
            "</script>")
    List<YpBaseYfBoundVo> selectYpBaseYfBoundPage(@Param("searchText")String searchText, @Param("groupNo") String groupNo, @Param("pageBean")PageBean pageBean);

    /**
     * 查询药房药品总条数
     * @param searchText
     * @param groupNo
     * @return
     */
    @Select("<script>" +
            "select count(1) " +
            "from (SELECT b.charge_code" +
            " FROM yp_zd_dict a  ,yp_base_yf b," +
            "     (select c.name,a.charge_item_code code,c.serial,a.confirm_time," +
            "        round(sum(case when a.serial <![CDATA[<]]> c.serial then quantity*cy_fy/pack_size else quantity*cy_fy end),2) quantity" +
            "      from yp_mz_fytj a," +
            "           (select a.name,a.code,a.serial,a.specification,a.pack_size" +
            "            from yp_zd_dict a,(select code,max(serial) serial" +
            "                               from yp_zd_dict group by code) b" +
            "            where a.code=b.code and a.serial=b.serial) c" +
            "      where a.charge_item_code=c.code and datediff(day,confirm_time,getdate()) <![CDATA[<]]> 61 and group_no =#{groupNo}" +
            "      group by c.name,a.charge_item_code,c.serial,a.confirm_time" +
            "      union all" +
            "      select" +
            "        c.name,a.charge_code code,c.serial,a.confirm_time," +
            "        round(sum(case when a.serial <![CDATA[<]]> c.serial then amount/pack_size else amount end),2) quantity" +
            "      from yp_zy_patient a," +
            "           (select a.name,a.code,a.serial,a.specification,a.pack_size" +
            "            from yp_zd_dict a,(select code,max(serial) serial" +
            "                               from yp_zd_dict group by code) b" +
            "            where a.code=b.code and a.serial=b.serial) c" +
            "      where a.charge_code=c.code and datediff(day,confirm_time,getdate()) <![CDATA[<]]> 61 and acct_sign=2 and group_no =#{groupNo}" +
            "      group by c.name,a.charge_code,c.serial,a.confirm_time) aa," +
            "     (select c.name,a.charge_code,c.serial," +
            "             case when a.group_no='81' then '22' else '11' end group_no," +
            "             round(sum(case when a.serial <![CDATA[<]]> c.serial then stock_amount/pack_size else stock_amount end),2) stock_amount" +
            "      from yp_base_yf a," +
            "           (select a.name,a.code,a.serial,a.specification,a.pack_size" +
            "            from yp_zd_dict a,(select code,max(serial) serial" +
            "                               from yp_zd_dict group by code) b" +
            "            where a.code=b.code and a.serial=b.serial) c" +
            "      where a.charge_code=c.code and a.group_no <![CDATA[<>]]> '91' and a.group_no =#{groupNo}" +
            "      group by c.name,a.charge_code,c.serial,(case when a.group_no='81' then '22' else'11' end)) bb" +
            " where   a.code=b.charge_code and" +
            "        a.serial=b.serial and" +
            "        b.group_no=#{groupNo} and" +
            "        a.code*=aa.code and" +
            "        a.serial*=aa.serial and" +
            "        a.code*=bb.charge_code and" +
            "        a.serial*=bb.serial and" +
            "         (a.py_code like '${searchText}%' or" +
            "         a.d_code like '${searchText}%' or" +
            "         a.code like '${searchText}%' or" +
            "         a.name like '${searchText}%') and" +
            "        isnull(a.visible_flag,'0') <![CDATA[<>]]> '1' and" +
            "        isnull(a.del_flag,'0') <![CDATA[<>]]> '1'" +
            " group by  b.charge_code," +
            "          a.name," +
            "          b.serial," +
            "          b.group_no," +
            "          a.specification," +
            "          a.drug_flag," +
            "          a.visible_flag," +
            "          a.manu_code," +
            "          max_stock_amount," +
            "          mini_stock_amount," +
            "          a.pack_retprice   ," +
            "          b.location) as gg" +
            "</script>")
    int countYpBaseYfBound(@Param("searchText")String searchText, @Param("groupNo") String groupNo);
}
