package cn.hnthyy.thmz.mapper.his;

import cn.hnthyy.thmz.entity.his.YpInDetlYf;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 药房药品入库持久化类
 */
public interface YpInDetlYfMapper {

    /**
     * 查询药品最大入库序号
     * @param groupNo
     * @param inDocuNo
     * @return
     */
    @Select("select isnull(max ( in_seri ),0) from yp_in_detl_yf where group_no =#{groupNo} and in_docu_no =#{inDocuNo}")
    int selectMaxInSeri(@Param("groupNo") String groupNo,@Param("inDocuNo") String inDocuNo);

    /**
     * 保存药房药品入库记录
     * @param ypInDetlYf
     * @return
     */
    @Insert("insert into yp_in_detl_yf ( in_date , in_seri , charge_code , serial , buy_amt , buy_price , eff_date , in_docu_no  , manu_code , purchaser , checker , in_type , pack_retprice , acct_type , fix_price , group_no , current_stock , sys_date , input_id , stock_amount , group_out , out_draw_no , in_comment )" +
            "  values ( #{inDate} , #{inSeri} , #{chargeCode} , #{serial} , #{buyAmt} , #{buyPrice} , #{effDate} , #{inDocuNo}  , #{manuCode} , #{purchaser} , #{checker} , #{inType} , #{packRetprice} , #{acctType} , #{fixPrice} , #{groupNo} , #{currentStock} , #{sysDate} , #{inputId} , #{stockAmount} , #{groupOut} , #{outDrawNo} , #{inComment} )")
    int insertYpInDetlYf(YpInDetlYf ypInDetlYf);

    /**
     * 入库查询
     * @param ypInDetlYf
     * @return
     */
    @Select("<script>" +
            " select top ${rows} *" +
            " from (select ROW_NUMBER() over (order by in_date desc) as RowNumber," +
            " in_date,rept_no,supply_code,in_docu_no,purchaser,confirm_id,confirm_date,acct_id,acct_date,input_id," +
            "       sys_date,charge_code,serial,pack_retprice,buy_price,buy_amt,eff_date,manu_code,manu_no," +
            "       current_stock,fp_date,kl,in_comment,license_no,in_seri,in_type,buy_total_price = buy_price*buy_amt,plan_no" +
            " FROM yp_in_detl_yf a where 1=1 " +
            "<when test='ypInDetlYf.beginTime != null'>" +
            " and in_date <![CDATA[>=]]> #{ypInDetlYf.beginTime}" +
            "</when>" +
            "<when test='ypInDetlYf.endTime != null'>" +
            " and in_date <![CDATA[<=]]> #{ypInDetlYf.endTime}" +
            "</when>" +
            "<when test='ypInDetlYf.inDocuNo != null'>" +
            " and in_docu_no = #{ypInDetlYf.inDocuNo}" +
            "</when>" +
            "<when test='ypInDetlYf.planNo != null'>" +
            " and plan_no = #{ypInDetlYf.planNo}" +
            "</when>" +
            "<when test='ypInDetlYf.manuCode != null'>" +
            " and manu_code = #{ypInDetlYf.manuCode}" +
            "</when>" +
            "<when test='ypInDetlYf.inputId != null'>" +
            " and input_id = #{ypInDetlYf.inputId}" +
            "</when>" +
            "<when test='ypInDetlYf.chargeCode != null'>" +
            " and charge_code = #{ypInDetlYf.chargeCode}" +
            "</when>" +
            "<when test='ypInDetlYf.inType != null'>" +
            " and in_type = #{ypInDetlYf.inType}" +
            "</when>" +
            "<when test='ypInDetlYf.groupNo != null'>" +
            " and group_no = #{ypInDetlYf.groupNo}" +
            "</when>)AS A WHERE RowNumber >${rows}*${page-1} order by RowNumber asc" +
            "</script>")
    List<YpInDetlYf> selectYpInDetlYf(@Param("ypInDetlYf") YpInDetlYf ypInDetlYf,@Param("page") Integer page,@Param("rows") Integer rows);

    /**
     * 入库查询条数
     * @param ypInDetlYf
     * @return
     */
    @Select("<script>" +
            " select count(1)" +
            " FROM yp_in_detl_yf a where 1=1 " +
            "<when test='beginTime != null'>" +
            " and in_date <![CDATA[>=]]> #{beginTime}" +
            "</when>" +
            "<when test='endTime != null'>" +
            " and in_date <![CDATA[<=]]> #{endTime}" +
            "</when>" +
            "<when test='inDocuNo != null'>" +
            " and in_docu_no = #{inDocuNo}" +
            "</when>" +
            "<when test='planNo != null'>" +
            " and plan_no = #{planNo}" +
            "</when>" +
            "<when test='manuCode != null'>" +
            " and manu_code = #{manuCode}" +
            "</when>" +
            "<when test='inputId != null'>" +
            " and input_id = #{inputId}" +
            "</when>" +
            "<when test='chargeCode != null'>" +
            " and charge_code = #{chargeCode}" +
            "</when>" +
            "<when test='inType != null'>" +
            " and in_type = #{inType}" +
            "</when>" +
            "<when test='groupNo != null'>" +
            " and group_no = #{groupNo}" +
            "</when>" +
            "</script>")
    int countYpInDetlYf(YpInDetlYf ypInDetlYf);
}
