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

import cn.hnthyy.thmz.pageDto.PageBean;
import cn.hnthyy.thmz.vo.YpZdBaseYfVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 药房库存清单数据接口
 */
public interface YfInventoryListMapper {

    /**
     * 分页查询药房库存清单
     * @param groupNo
     * @param classCode
     * @param pageBean
     * @return
     */
    @Select({"<script>" +
            "select top ${pageBean.pageSize} * from ",
            "(SELECT ROW_NUMBER() OVER (ORDER BY y.charge_code asc)AS RowNumber, a.code," +
                    "  a.name," +
                    "  a.serial," +
                    "  a.class_code," +
                    "  a.specification," +
                    "  pack_retprice = isnull(b.pack_retprice,a.pack_retprice)," +
                    "  a.drug_flag," +
                    "  a.py_code," +
                    "  a.d_code AS wb_code," +
                    "  a.visible_flag," +
                    "  a.manu_code," +
                    "  b.stock_amount," +
                    "  stock_value =isnull( b.pack_retprice,a.pack_retprice) * b.stock_amount  ," +
                    "  b.location," +
                    "  b.stock_amount_virtual," +
                    "  b.visible_flag_mz," +
                    "  b.visible_flag_zy" +
                    " FROM yp_zd_dict a,yp_base_yf b,yp_print_name y" +
                    " where a.code=b.charge_code " +
                    "  and a.serial=b.serial " +
                    "  and y.charge_code = a.code " +
                    "  and b.group_no=#{groupNo} " +
                    " and (y.py_code like '${searchText}%' or y.d_code like '${searchText}%' or y.charge_code like '${searchText}%' or y.print_name like '${searchText}%') " +
                    " <when test='classCode!=null'>" +
                    "  and a.class_code like #{classCode}" +
                    " </when>" +
                    "  and ((isnull(a.del_flag,'0') <![CDATA[<>]]> '1' and (isnull(visible_flag_zy,'0') <![CDATA[<>]]> '1' or" +
                    "  isnull(visible_flag_mz,'0') <![CDATA[<>]]> '1')) or ( b.stock_amount <![CDATA[<>]]> 0)) group by a.code, a.name, a.serial," +
                    "            a.class_code, a.specification,a.drug_flag, a.py_code, a.d_code,y.charge_code,a.pack_retprice,b.pack_retprice, " +
                    "           a.visible_flag, a.manu_code, b.stock_amount, b.location, b.stock_amount_virtual, b.visible_flag_mz, b.visible_flag_zy" +
                    ") as A WHERE RowNumber >#{pageBean.pageSize}*#{pageBean.pageIndex} order by RowNumber asc" ,
            " </script>"})
    List<YpZdBaseYfVo> selectYpZdBaseYfPage(@Param("groupNo")String groupNo,@Param("classCode") String classCode,@Param("searchText") String searchText,@Param("pageBean")PageBean pageBean);


    /**
     * 查询药房库存药品总条数
     * @param groupNo
     * @param classCode
     * @param searchText
     * @return
     */
    @Select({"<script>" +
            "SELECT count(1) from " +
            "(select a.code, a.serial" +
                    " FROM yp_zd_dict a,yp_base_yf b,yp_print_name y" +
                    " where a.code=b.charge_code " +
                    " and a.serial=b.serial " +
                    " and y.charge_code = a.code " +
                    "  and b.group_no=#{groupNo} " +
                    " and (y.py_code like '${searchText}%' or y.d_code like '${searchText}%' or y.charge_code like '${searchText}%' or y.print_name like '${searchText}%') " +
                    " <when test='classCode!=null'>" +
                    "  and a.class_code like #{classCode}" +
                    " </when>" +
                    "  and ((isnull(a.del_flag,'0') <![CDATA[<>]]> '1' and (isnull(visible_flag_zy,'0') <![CDATA[<>]]> '1' or" +
                    "  isnull(visible_flag_mz,'0') <![CDATA[<>]]> '1')) or ( b.stock_amount <![CDATA[<>]]> 0)) group by a.code, a.serial)aa" +
            " </script>"})
    int countYpZdBaseYf(@Param("groupNo")String groupNo,@Param("classCode") String classCode,@Param("searchText") String searchText);

    /**
     * 分页查询多药房库存清单
     * @param classCode
     * @param searchText
     * @param pageBean
     * @return
     */
    @Select("<script>" +
            "select top ${pageBean.pageSize} * " +
            " from (SELECT ROW_NUMBER() OVER (ORDER BY cc.charge_code asc)AS RowNumber,aa.code,aa.name,'01' as serial,aa.class_code,min(aa.specification) specification_min," +
            "       drug_flag,manu_code,max(aa.stock_amount_mz) stock_amount_mz,max(aa.stock_amount_mz/bb.pack_size) stock_amount_large_mz," +
            "       max(aa.stock_amount_zy) stock_amount_zy,max(stock_amount_zy/pack_size) stock_amount_large_zy,max(aa.stock_amount_dz) stock_amount_dz,max(stock_amount_dz/pack_size) stock_amount_large_dz," +
            "       max(aa.stock_amount_yk) stock_amount_yk,max(stock_amount_yk/pack_size) stock_amount_large_yk,max(aa.specification) specification_max," +
            "       max(visible_flag_mz) visible_flag_mz,max(visible_flag_zy) visible_flag_zy,isnull(max(visible_flag_dz),0) visible_flag_dz,isnull(max(visible_flag_yk),0) visible_flag_yk" +
            " from (" +
            "       SELECT  e.code," +
            "               a.name," +
            "               serial=e.serial_min," +
            "               a.class_code," +
            "               a.drug_flag," +
            "               specification=min(e.specification)," +
            "               a.manu_code," +
            "               stock_amount_mz= case when b.group_no=71 then sum(case when a.serial=e.serial_min then isnull(b.stock_amount,0) else isnull(b.stock_amount,0)*e.pack_size end) else NULL end," +
            "               stock_amount_zy= case when b.group_no=72 then sum(case when a.serial=e.serial_min then isnull(b.stock_amount,0) else isnull(b.stock_amount,0)*e.pack_size end) else NULL end," +
            "               stock_amount_dz= case when b.group_no=91 then sum(case when a.serial=e.serial_min then isnull(b.stock_amount,0) else isnull(b.stock_amount,0)*e.pack_size end) else NULL end," +
            "               stock_amount_yk= NULL," +
            "               visible_flag_mz = case when b.group_no=71 then max(case when b.visible_flag_zy='1' and b.visible_flag_mz='1' then 0 else 1 end) else NULL end," +
            "               visible_flag_zy = case when b.group_no=72 then max(case when b.visible_flag_zy='1' and b.visible_flag_mz='1' then 0 else 1 end) else NULL end," +
            "               visible_flag_dz = case when b.group_no=91 then max(case when b.visible_flag_zy='1' and b.visible_flag_mz='1' then 0 else 1 end) else NULL end," +
            "               visible_flag_yk = NULL" +
            "       FROM yp_zd_dict a,yp_base_yf b,(select code,name,serial,pack_size,'01' as serial_min ,specification from yp_zd_dict) e" +
            "       where    a.code=b.charge_code and" +
            "           a.serial=b.serial and" +
            "           a.serial=e.serial and" +
            "           a.code=e.code and" +
            "           b.group_no in (71,72,91)" +
            "       group by   e.code,   a.name,   e.serial_min,a.manu_code,a.class_code ,a.drug_flag,b.group_no" +
            "       union" +
            "       SELECT e.code," +
            "              a.name," +
            "              serial=e.serial_min," +
            "              a.class_code," +
            "              a.drug_flag," +
            "              specification=min(e.specification)," +
            "              a.manu_code," +
            "              stock_amount_mz=NULL," +
            "              stock_amount_zy=NULL," +
            "              stock_amount_dz=NULL," +
            "              stock_amount_yk= sum(case when a.serial=e.serial_min then isnull(b.stock_amount,0) else isnull(b.stock_amount,0)*e.pack_size end)," +
            "              visible_flag_mz = NULL," +
            "              visible_flag_zy = NULL," +
            "              visible_flag_dz = NULL," +
            "              visible_flag_yk = max(case when  b.visible_flag='1' then 0 else 1 end)" +
            "       FROM yp_zd_dict a,yp_base b,(select code,name,serial,pack_size,'01' as serial_min ,specification from yp_zd_dict) e" +
            "       where    a.code=b.charge_code and" +
            "           a.serial=b.serial and" +
            "           a.serial=e.serial and" +
            "           b.group_no='11' and" +
            "           a.code=e.code  and" +
            "         (( isnull(b.visible_flag,0) <![CDATA[<>]]> '1' or b.stock_amount <![CDATA[<>]]> 0))" +
            "       group by e.code,   a.name,   e.serial_min,a.manu_code,a.class_code ,a.drug_flag" +
            "     ) aa," +
            "     (select 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) bb," +
            "     ( select charge_code from yp_print_name f where" +
            "       (py_code like '${searchText}%' or d_code like '${searchText}%' or charge_code like '${searchText}%' or print_name like '${searchText}%')" +
            "       group by charge_code) cc" +
            " where aa.code=bb.code    and" +
            "    aa.code  =cc.charge_code " +
            " <when test='classCode!=null'>" +
            "  and aa.class_code like #{classCode}" +
            " </when>" +
            " group by aa.code,aa.name,cc.charge_code,class_code,drug_flag,manu_code) ddaw WHERE RowNumber >#{pageBean.pageSize}*#{pageBean.pageIndex} order by RowNumber asc" +
            "</script>")
    List<YpZdBaseYfVo> selectYpZdBaseYfMultiplePage(@Param("classCode") String classCode,@Param("searchText")String searchText,@Param("pageBean")PageBean pageBean);

    /**
     * 查询多药房库存药品总条数
     * @param classCode
     * @param searchText
     * @return
     */
    @Select("<script>" +
            "SELECT COUNT(1) FROM" +
            "(SELECT aa.code,aa.name,class_code,drug_flag,manu_code " +
            "from (" +
            "       SELECT  e.code," +
            "               a.name," +
            "               serial=e.serial_min," +
            "               a.class_code," +
            "               a.drug_flag," +
            "               specification=min(e.specification)," +
            "               a.manu_code" +
            "       FROM yp_zd_dict a,yp_base_yf b,(select code,name,serial,pack_size,'01' as serial_min ,specification from yp_zd_dict) e" +
            "       where    a.code=b.charge_code and" +
            "           a.serial=b.serial and" +
            "           a.serial=e.serial and" +
            "           a.code=e.code and" +
            "           b.group_no in (71,72,91)" +
            "       group by   e.code,   a.name,   e.serial_min,a.manu_code,a.class_code ,a.drug_flag,b.group_no" +
            "       union" +
            "       SELECT e.code," +
            "              a.name," +
            "              serial=e.serial_min," +
            "              a.class_code," +
            "              a.drug_flag," +
            "              specification=min(e.specification)," +
            "              a.manu_code" +
            "       FROM yp_zd_dict a,yp_base b,(select code,name,serial,pack_size,'01' as serial_min ,specification from yp_zd_dict) e" +
            "       where    a.code=b.charge_code and" +
            "           a.serial=b.serial and" +
            "           a.serial=e.serial and" +
            "           b.group_no='11' and" +
            "           a.code=e.code  and" +
            "         (( isnull(b.visible_flag,0) <![CDATA[<>]]> '1' or b.stock_amount <![CDATA[<>]]> 0))" +
            "       group by e.code,   a.name,   e.serial_min,a.manu_code,a.class_code ,a.drug_flag" +
            "     ) aa," +
            "     (select 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) bb," +
            "     ( select charge_code from yp_print_name f where" +
            "       (py_code like '${searchText}%' or d_code like '${searchText}%' or charge_code like '${searchText}%' or print_name like '${searchText}%')" +
            "       group by charge_code) cc" +
            " where aa.code=bb.code    and" +
            "    aa.code  =cc.charge_code " +
            " <when test='classCode!=null'>" +
            "  and aa.class_code like #{classCode}" +
            " </when>" +
            " group by aa.code,aa.name,class_code,drug_flag,manu_code)D" +
            "</script>")
    int countYpZdBaseYfMultiple(@Param("classCode") String classCode,@Param("searchText")String searchText);
}
