package cn.hnthyy.thmz.mapper.his;

import cn.hnthyy.thmz.entity.his.YpInPlan;
import cn.hnthyy.thmz.entity.his.zd.YpZdDictBase;
import cn.hnthyy.thmz.vo.YpInPlanDrugVo;
import cn.hnthyy.thmz.vo.YpInPlanVo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;


/**
 * @description:
 * @author: WANGJIALIANG
 * @time: 2021/7/21 10:12
 */
public interface YpInPlanMapper {

    /**
     * 分页查询药品请购记录
     * @param ypInPlanVo
     * @return
     */
    @Select("<script>" +
            "select top ${pageSize} plan_date,status_flag,confirm_date,input_id,draw_no,confirm_id,drawer,group_yk,group_no " +
            " from (select ROW_NUMBER() OVER (ORDER BY draw_no DESC)AS RowNumber," +
            " plan_date," +
            " status_flag," +
            " confirm_date," +
            " input_id," +
            " draw_no," +
            " confirm_id," +
            " drawer," +
            " group_yk,group_no" +
            " FROM yp_in_plan" +
            " where 1=1 " +
            "<when test='statusFlag != null'>" +
            " and status_flag = #{statusFlag}" +
            "</when>" +
            "<when test='drawNo != null'>" +
            " and draw_no = #{drawNo}" +
            "</when>" +
            "<when test='groupNo != null'>" +
            "  and group_no=#{groupNo}" +
            "</when>" +
            "<when test='groupYk != null'>" +
            "  and group_yk=#{groupYk}" +
            "</when>" +
            "<when test='beginTime != null'>" +
            "  and plan_date <![CDATA[>=]]> #{beginTime}" +
            "</when>" +
            "<when test='endTime != null'>" +
            " and plan_date <![CDATA[<=]]> #{endTime}" +
            "</when>" +
            " group by plan_date," +
            " status_flag," +
            " confirm_date," +
            " input_id," +
            " draw_no," +
            " confirm_id," +
            " drawer," +
            " group_yk,group_no) as A WHERE RowNumber >#{pageSize}*#{pageIndex} order by RowNumber asc" +
            "</script>")
    List<YpInPlanVo> selectYpInPlanPage(YpInPlanVo ypInPlanVo);

    /**
     * 查询药品请购记录总数
     * @param ypInPlanVo
     * @return
     */
    @Select("<script>" +
            " select count(1) from (select " +
            " distinct plan_date," +
            " status_flag," +
            " confirm_date," +
            " input_id," +
            " draw_no," +
            " confirm_id," +
            " group_yk" +
            " FROM yp_in_plan" +
            " where 1=1 " +
            "<when test='statusFlag != null'>" +
            " and status_flag = #{statusFlag}" +
            "</when>" +
            "<when test='drawNo != null'>" +
            " and draw_no = #{drawNo}" +
            "</when>" +
            "<when test='groupNo != null'>" +
            "  and group_no=#{groupNo}" +
            "</when>" +
            "<when test='groupYk != null'>" +
            "  and group_yk=#{groupYk}" +
            "</when>" +
            "<when test='beginTime != null'>" +
            "  and plan_date <![CDATA[>=]]> #{beginTime}" +
            "</when>" +
            "<when test='endTime != null'>" +
            " and plan_date <![CDATA[<=]]> #{endTime}" +
            "</when>" +
            " group by plan_date," +
            " status_flag," +
            " confirm_date," +
            " input_id," +
            " draw_no," +
            " confirm_id," +
            " group_yk) a" +
            "</script>")
    int countYpInPlan(YpInPlanVo ypInPlanVo);

    /**
     * 查询药库药品信息
     * @param searchText
     * @param groupYk
     * @return
     */
    @Select("<script>" +
            "SELECT TOP" +
            " 10 * " +
            " FROM" +
            " (" +
            " SELECT DISTINCT" +
            " code," +
            " serial," +
            " name," +
            " pack_size," +
            " pack_retprice," +
            " specification," +
            " drug_flag," +
            " fix_price," +
            " stock_amount," +
            " group_no," +
            " manu_code," +
            " n_code," +
            " buy_price " +
            " FROM" +
            " view_yp_zd_dict_base" +
            " WHERE 1=1 " +
            " <when test='searchText != null'>" +
            " AND ( py_code LIKE '${searchText}%' OR d_code LIKE '${searchText}%' OR name LIKE '${searchText}%' OR code LIKE '${searchText}%'  ) " +
            " </when>"+
            " AND group_no = #{groupYk} " +
            " ) A" +
            "</script>")
    List<YpZdDictBase> selectYpZdDictBase(@Param("searchText") String searchText, @Param("groupYk") String groupYk);

    /**
     * 保存请领记录
     * @param ypInPlan
     * @return
     */
    @Insert("insert into yp_in_plan ( plan_date , plan_seri , charge_code , serial , group_no , group_yk , amount , drawer , status_flag , draw_no , input_id , sys_date ) " +
            " values ( #{planDate} , #{planSeri} ,#{chargeCode} ,#{serial} ,#{groupNo} ,#{groupYk} ,#{amount} ,#{drawer} ,1,#{drawNo},#{inputId},#{sysDate} )")
    int insertYpInPlan(YpInPlan ypInPlan);

    /**
     * 查询药品请购记录
     * @param drawNo
     * @return
     */
    @Select("SELECT plan_date,plan_seri,charge_code,serial,group_no,group_yk,amount,amount_out,drawer,status_flag,draw_no,confirm_id,confirm_date,input_id,sys_date FROM yp_in_plan" +
            " WHERE draw_no = #{drawNo}")
    List<YpInPlanDrugVo> selectYpInPlanDrug(@Param("drawNo") String drawNo);
}
