package cn.hnthyy.thmz.mapper.his;

import cn.hnthyy.thmz.entity.his.YpOutDetl;
import cn.hnthyy.thmz.vo.YpOutDetlVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * @description:药品出库
 * @author: WANGJIALIANG
 * @time: 2021/8/3 9:38
 */
public interface YpOutDetlMapper {

    /**
     * 查询药库出库单
     * @param ypOutDetl
     * @return
     */
    @Select("<script>" +
            "select top ${pageSize} out_date,draw_no,group_no,keeper,drawer,out_type,group_no_out,accept_sign " +
            "  from (select ROW_NUMBER() OVER (ORDER BY out_date DESC)AS RowNumber," +
            "  out_date=a.out_date," +
            "  draw_no=a.draw_no," +
            "  group_no = a.group_no," +
            "  keeper=a.keeper," +
            "  drawer=max(a.drawer)," +
            "  out_type=isnull(out_type,'0')," +
            "  group_no_out," +
            "  accept_sign" +
            " from" +
            "  yp_out_detl a" +
            " where a.group_no_out= #{groupNoOut} and isnull(a.confirm_flag,'') = '1'" +
            "<when test='beginTime != null'>" +
            " and a.out_date<![CDATA[>=]]>#{beginTime}" +
            "</when>" +
            "<when test='endTime != null'>" +
            " and a.out_date<![CDATA[<=]]>#{endTime}" +
            "</when>" +
            "<when test='acceptSign != null'>" +
            " and a.accept_sign=#{acceptSign}" +
            "</when>" +
            "<when test='outType != null'>" +
            " and isnull(out_type,'0')=#{outType}" +
            "</when>" +
            "<when test='deptCode != null'>" +
            " and a.dept_code=#{deptCode}" +
            "</when>" +
            " group by out_date,draw_no,group_no,keeper,out_type,group_no_out,accept_sign ) as A WHERE RowNumber >#{pageSize}*#{pageIndex} order by RowNumber asc" +
            "</script>")
    List<YpOutDetl> selectYpOutDetlPage(YpOutDetl ypOutDetl);

    /**
     * 查询药库出库单总条数
     * @param ypOutDetl
     * @return
     */
    @Select("<script>" +
            "select count(1) from (" +
            "select distinct" +
            "  out_date=a.out_date," +
            "  draw_no=a.draw_no," +
            "  group_no = a.group_no," +
            "  keeper=a.keeper," +
            "  drawer=a.drawer," +
            "  out_type=isnull(out_type,'0')" +
            " from" +
            "  yp_out_detl a" +
            " where a.group_no_out= #{groupNoOut} and isnull(a.confirm_flag,'') = '1'" +
            "<when test='beginTime != null'>" +
            " and a.out_date<![CDATA[>=]]>#{beginTime}" +
            "</when>" +
            "<when test='endTime != null'>" +
            " and a.out_date<![CDATA[<=]]>#{endTime}" +
            "</when>" +
            "<when test='acceptSign != null'>" +
            " and a.accept_sign=#{acceptSign}" +
            "</when>" +
            "<when test='deptCode != null'>" +
            " and a.dept_code=#{deptCode}" +
            "</when>) a" +
            "</script>")
    int countYpOutDetl(YpOutDetl ypOutDetl);

    /**
     * 查询药库出库单详细
     * @param drawNo
     * @return
     */
    @Select("<script>" +
            "select confirm_flag,out_date,draw_no,eff_date," +
            "       drawer,serial,out_amt,out_seri,pack_retprice," +
            "       fix_price,acct_sign,buy_price,charge_code,group_no_out,group_no,in_draw_no,in_seri,accept_sign" +
            " from yp_out_detl" +
            " where draw_no = #{drawNo} " +
            " ORDER BY out_seri ASC" +
            "</script>")
    List<YpOutDetlVo> selectYpOutDetlDetail(String drawNo);

    /**
     * 更新药房入库状态
     * @param drawNo
     * @param outSeri
     * @return
     */
    @Update("update yp_out_detl set accept_sign ='1' where draw_no =#{drawNo} and out_seri =#{outSeri}")
    int updateAcceptSign(@Param("drawNo") String drawNo,@Param("outSeri") Integer outSeri);
}
