package com.fs.his.mapper;


import com.fs.his.domain.FsStoreProductAttrValue;
import com.fs.his.param.FsStoreProductAttrValueListDParam;
import com.fs.his.vo.FsStoreProductAttrValueListDVO;
import org.apache.ibatis.annotations.Delete;
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;

/**
 * 商品属性值Mapper接口
 *
 * @author fs
 * @date 2022-03-15
 */
public interface FsStoreProductAttrValueMapper
{
    /**
     * 查询商品属性值
     *
     * @param id 商品属性值ID
     * @return 商品属性值
     */
        public FsStoreProductAttrValue selectFsStoreProductAttrValueById(Long id);

    /**
     * 查询商品属性值列表
     *
     * @param fsStoreProductAttrValue 商品属性值
     * @return 商品属性值集合
     */
    public List<FsStoreProductAttrValue> selectFsStoreProductAttrValueList(FsStoreProductAttrValue fsStoreProductAttrValue);

    /**
     * 新增商品属性值
     *
     * @param fsStoreProductAttrValue 商品属性值
     * @return 结果
     */
    public int insertFsStoreProductAttrValue(FsStoreProductAttrValue fsStoreProductAttrValue);

    /**
     * 修改商品属性值
     *
     * @param fsStoreProductAttrValue 商品属性值
     * @return 结果
     */
    public int updateFsStoreProductAttrValue(FsStoreProductAttrValue fsStoreProductAttrValue);

    /**
     * 删除商品属性值
     *
     * @param id 商品属性值ID
     * @return 结果
     */
    public int deleteFsStoreProductAttrValueById(Long id);

    /**
     * 批量删除商品属性值
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    public int deleteFsStoreProductAttrValueByIds(Long[] ids);
    @Delete("delete from fs_store_product_attr_value where product_id=#{productId}")
    int deleteFsStoreProductAttrValueByProductId(Long productId);
    @Select("select *,cost_price cost from fs_store_product_attr_value where  product_id=#{productId}")
    List<FsStoreProductAttrValue> selectFsStoreProductAttrValueByProductId(Long productId);
    @Select("select ifnull(stock,0) from fs_store_product_attr_value where  id=#{productAttrValueId}")
    int selectFsStoreProductStockById(Long productAttrValueId);

    @Update("update fs_store_product_attr_value set stock=stock-#{num},sales=sales+#{num}" +
            " where id=#{productAttrValueId} and stock >= #{num} ")
    int decProductAttrStock(@Param("productAttrValueId") Long productAttrValueId, @Param("num") Integer cartNum);
    @Update("update fs_store_product_attr_value set stock=stock+#{num}, sales=sales-#{num}" +
            " where product_id=#{productId} and id=#{productAttrValueId}")
    int incProductAttrStock(@Param("num") Long num, @Param("productId") Long productId, @Param("productAttrValueId") Long productAttrValueId);
    @Select({"<script> " +
            "select v.*,p.product_name,p.product_type  from fs_store_product_attr_value v inner join fs_store_product p on p.product_id=v.product_id   " +
            "where p.is_show=1 " +
            "<if test = ' maps.keyword!=null and maps.keyword!=\"\"  '> " +
            "and p.product_name  LIKE CONCAT('%',#{maps.keyword},'%')   " +
            "</if>" +
            "<if test = ' maps.storeId!=null  '> " +
            "and p.store_id = #{maps.storeId} " +
            "</if>" +
            "<if test = ' maps.productType!=null  '> " +
            "and p.product_type = #{maps.productType} " +
            "</if>" +
            " order by v.id desc "+
            "</script>"})
    List<FsStoreProductAttrValueListDVO> selectFsStoreProductAttrValueListDVO(@Param("maps")FsStoreProductAttrValueListDParam param);

    @Update({"<script> " +
            "UPDATE fs_store_product_attr_value SET price=price*#{price} , ot_price=ot_price*#{price} WHERE product_id in"+
            "<foreach item=\"id\" collection=\"ids\" open=\"(\" separator=\",\" close=\")\">\n" +
            "            #{id}\n" +
            "        </foreach>"+
            "</script>"})
    int updateFsStoreProductAttrValuePrice(@Param("ids")List<Long> ids, @Param("price")double price);
//    @Select({"<script> " +
//            "select v.*,p.product_name,p.product_type,c.cate_name  from fs_store_product_attr_value v inner join fs_store_product p on p.product_id=v.product_id left join fs_store_product_category c on c.cate_id=p.cate_id   " +
//            "where 1=1 and v.bar_code is not null " +
//            "<if test = 'maps.productName != null and  maps.productName !=\"\"    '> " +
//            "and p.product_name like CONCAT('%',#{maps.productName},'%') " +
//            "</if>" +
//            "<if test = 'maps.productType != null    '> " +
//            "and p.product_type = #{maps.productType}  " +
//            "</if>" +
//            "<if test = 'maps.cateId != null    '> " +
//            "and (p.cate_id =#{maps.cateId} or c.pid=#{maps.cateId} )" +
//            "</if>" +
//            "<if test = 'maps.isShow != null    '> " +
//            "and p.is_show = #{maps.isShow}  " +
//            "</if>" +
//            " order by v.id desc "+
//            "</script>"})
//    List<FsStoreProductAttrValueVO> selectFsStoreProductAttrValueListVO(@Param("maps") FsProductAttrValueParam param);
//    @Select({"<script> " +
//            "select v.*,p.product_name from fs_store_product_attr_value v inner join fs_store_product p on p.product_id=v.product_id    " +
//            "where p.is_show=1 and p.is_display=1 and (v.brokerage &gt;0 || v.brokerage_two &gt;0) " +
//            "<if test = 'maps.productName != null and  maps.productName !=\"\"    '> " +
//            "and p.product_name like CONCAT('%',#{maps.productName},'%') " +
//            "</if>" +
//            "<if test = 'maps.tuiCateId != null and maps.tuiCateId !=0    '> " +
//            "and p.tui_cate_id =#{maps.tuiCateId} " +
//            "</if>" +
//            " order by v.id desc "+
//            "</script>"})
//    List<FsStoreProductAttrValueQueryVO> selectStoreProductAttrValueListQuery(@Param("maps") FsStoreProductAttrValueQueryParam param);
//
//    @Select({"<script> " +
//            "select v.*,p.tui_cate_id,p.product_name from fs_store_product_attr_value v inner join fs_store_product p on p.product_id=v.product_id    " +
//            "where p.is_show=1 and p.is_display=1 and (v.brokerage &gt;0 || v.brokerage_two &gt;0) " +
//            "<if test = 'maps.productName != null and  maps.productName !=\"\"    '> " +
//            "and p.product_name like CONCAT('%',#{maps.productName},'%') " +
//            "</if>" +
//            "<if test = 'maps.tuiCateId != null and maps.tuiCateId !=0    '> " +
//            "and p.tui_cate_id =#{maps.tuiCateId} " +
//            "</if>" +
//            " order by v.id desc "+
//            "</script>"})
//    List<FsStoreTuiProductAttrValueVO> selectStoreTuiProductAttrValueVOList(@Param("maps") FsStoreTuiProductAttrValueParam param);
    @Select("select av.id,av.cost_price as cost from fs_store_product_attr_value av left join fs_store_product p on p.product_id = av.product_id  where p.store_id = 34 ")
    List<FsStoreProductAttrValue> selectByStoreId();

    @Update("update fs_store_product_attr_value set cost_price = #{cost} where id = #{id}")
    void updateCostPrice(@Param("id")Long id, @Param("cost")BigDecimal  cost);
    @Select("    select id,doctor_brokerage, product_id, sku, stock, sales, price, image, cost_price cost, bar_code, ot_price, weight, volume, brokerage, brokerage_two,brokerage_three,give_integral  from fs_store_product_attr_value\n" +
            "    where  product_id=#{productId}")
    List<FsStoreProductAttrValue> selectFsStoreProductAttrValueVOByProductId(Long productId);

    @Select(" select * from fs_store_product_attr_value where id=#{id} and product_id=#{productId} ")
    FsStoreProductAttrValue  selectFsStoreProductAttrValueByIdAndPruductId(@Param("id") Long id,@Param("productId") Long productId);

    @Update(" update fs_store_product_attr_value set stock = #{stock} where id = #{id} ")
    int updateStoreProductAttrValueStock(Long id,Integer stock);

    @Select("select * from fs_store_product_attr_value where product_id = #{productId}")
    List<FsStoreProductAttrValue> selectFsStoreProductAttrValueListByProductId(@Param("productId") Long productId);

    /**
     * 批量获取商品规格
     * @param productIds
     * @return productIds
     * **/
    List<FsStoreProductAttrValue> getFsStoreProductAttrValueListInProductId(@Param("productIds") List<Long> productIds);
}
