package cn.hnthyy.thmz.mapper.his;

import cn.hnthyy.thmz.entity.his.YpBaseYf;
import cn.hnthyy.thmz.vo.YpBaseYfVo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.util.List;

/**
 * 药品库存操作
 */
public interface YpBaseYfMapper {
    /**
     * 减虚拟库存
     * @return
     */
    @Update("update yp_base_yf set stock_amount_virtual =stock_amount_virtual - #{amountVirtual,jdbcType=DECIMAL} where charge_code =#{chargeCode,jdbcType=CHAR} and serial =#{serial,jdbcType=CHAR} and group_no =#{groupNo,jdbcType=CHAR} ")
    int updateStockAmountVirtual(@Param("chargeCode") String chargeCode, @Param("serial") String serial, @Param("groupNo") String groupNo, @Param("amountVirtual") BigDecimal amountVirtual);



    /**
     * 增加虚拟库存
     * @return
     */
    @Update("update yp_base_yf set stock_amount_virtual =stock_amount_virtual + #{amountVirtual,jdbcType=DECIMAL} where charge_code =#{chargeCode,jdbcType=CHAR} and serial =#{serial,jdbcType=CHAR} and group_no =#{groupNo,jdbcType=CHAR} ")
    int updateStockAmountVirtualAdd(@Param("chargeCode") String chargeCode, @Param("serial") String serial, @Param("groupNo") String groupNo, @Param("amountVirtual") BigDecimal amountVirtual);

    /**
     * 平虚库存
     * @return
     */
    @Update("update yp_base_yf set stock_amount_virtual =stock_amount where charge_code =#{chargeCode,jdbcType=CHAR} and serial =#{serial,jdbcType=CHAR} and group_no =#{groupNo,jdbcType=CHAR} ")
    int updateResetVirtual(@Param("chargeCode") String chargeCode, @Param("serial") String serial, @Param("groupNo") String groupNo);

    /**
     * 更新药房库存信息
     * @return
     */
    @Update({"<script>",
            "update yp_base_yf ",
            "<trim prefix='set' prefixOverrides=',' suffix=' where charge_code = #{chargeCode} and serial=#{serial} and group_no=#{groupNo} ' >",
            "<when test='maxStockAmount!=null'>",
            "max_stock_amount =#{maxStockAmount,jdbcType=FLOAT}",
            "</when>",
            "<when test='miniStockAmount!=null'>",
            ",mini_stock_amount =#{miniStockAmount,jdbcType=FLOAT}",
            "</when>",
            "<when test='stockAmount!=null'>",
            ",stock_amount =#{stockAmount,jdbcType=FLOAT}",
            "</when>",
            "<when test='stockAmountVirtual!=null'>",
            ",stock_amount_virtual =#{stockAmountVirtual,jdbcType=FLOAT}",
            "</when>",
            "<when test='stockValue!=null'>",
            ",stock_value =#{stockValue,jdbcType=FLOAT}",
            "</when>",
            "</trim>",
            "</script>"})
    int updateYpBaseYf(YpBaseYf ypBaseYf);

    /**
     * 查询药房库存信息
     * @param chargeCode
     * @param serial
     * @param groupNo
     * @return
     */
    @Select("select charge_code,serial,stock_amount,stock_value,group_no,location " +
            "from yp_base_yf where charge_code=#{chargeCode} and serial=#{serial} and group_no=#{groupNo}")
    YpBaseYf selectYpBaseYf(@Param("chargeCode") String chargeCode, @Param("serial") String serial,@Param("groupNo") String groupNo);

    /**
     * 查询药品药房最大拆零比例
     * @param chargeCode
     * @param groupNo
     * @return
     */
    @Select("select split_percent = isnull(max(split_percent),0) from  yp_base_yf where group_no = #{groupNo} and  charge_code =#{chargeCode}  group by charge_code,group_no")
    Double selectSplitPercent(@Param("chargeCode") String chargeCode,@Param("groupNo") String groupNo);

    /**
     * 新药品入库
     * @param ypBaseYf
     * @return
     */
    @Insert("insert into yp_base_yf ( charge_code , serial , stock_amount , stock_value , max_stock , mini_stock , visible_flag_mz , visible_flag_zy , group_no , out_with_limit , pack_retprice ," +
            " retprice , stock_amount_virtual , split_percent ) values ( #{chargeCode} , #{serial} , #{stockAmount} , #{stockValue} , #{maxStock} , 0 , '0' , '0' , #{groupNo} , 0 , #{packRetprice} , #{retprice} , #{stockAmountVirtual} , #{splitPercent} )")
    int insertYpBaseYf(YpBaseYf ypBaseYf);

    /**
     *  更新库存
     * @param chargeCode
     * @param serial
     * @param groupNo
     * @param stockAmount
     * @param stockValue
     * @return
     */
    @Update("update yp_base_yf set stock_amount =stock_amount + #{stockAmount,jdbcType=DECIMAL} , stock_value =stock_value + #{stockValue,jdbcType=DECIMAL} , stock_amount_virtual =stock_amount_virtual + #{stockAmount,jdbcType=DECIMAL} where charge_code =#{chargeCode} and serial =#{serial} and group_no =#{groupNo}")
    int updateStockAmount(@Param("chargeCode") String chargeCode, @Param("serial") String serial, @Param("groupNo") String groupNo, @Param("stockAmount") Double stockAmount, @Param("stockValue") BigDecimal stockValue);

    /**
     *  模糊查询药品信息
     * @param searchText
     * @param groupNo
     * @return
     */
    @Select("  SELECT top 10 a.charge_code,   " +
            "         a.serial,   " +
            "         a.stock_amount,   " +
            "         a.pack_retprice,   " +
            "         b.name,   " +
            "         b.specification,   " +
            "         b.pack_size,   " +
            "         b.py_code,   " +
            "         b.d_code  " +
            "    FROM yp_base_yf a,   " +
            "         yp_zd_dict b " +
            "   WHERE ( a.serial = b.serial )  and" +
            "          a.charge_code = b.code  and" +
            "          (isnull(b.py_code,'') like '${searchText}%' or" +
            "          isnull(b.d_code,'') like '${searchText}%' or" +
            "          a.charge_code like '${searchText}%' or" +
            "          b.name like '${searchText}%') and" +
            "          a.group_no = #{groupNo} and " +
            "          a.stock_amount > 0" )
    List<YpBaseYfVo> selectYpBaseYfLike(@Param("searchText") String searchText, @Param("groupNo") String groupNo);

    /**
     *  根据编码查询药房药品信息
     * @param chargeCode
     * @return
     */
    @Select("  SELECT a.charge_code,   " +
            "         a.serial,   " +
            "         a.stock_amount,   " +
            "         a.pack_retprice,   " +
            "         b.name,   " +
            "         b.specification,   " +
            "         b.pack_size,   " +
            "         b.py_code,   " +
            "         b.d_code  " +
            "    FROM yp_base_yf a,   " +
            "         yp_zd_dict b " +
            "   WHERE ( a.serial = b.serial )  and" +
            "          a.charge_code = b.code  and" +
            "          a.charge_code = #{chargeCode} and" +
            "          a.group_no = #{groupNo}" )
    List<YpBaseYfVo> selectYpByChargeCodeAndGroupNo(@Param("chargeCode") String chargeCode,@Param("groupNo") String groupNo);
}
