package com.fs.hisStore.mapper;

import java.util.List;

import com.fs.common.annotation.DataSource;
import com.fs.common.enums.DataSourceType;
import com.fs.hisStore.config.MedicalMallConfig;
import com.fs.hisStore.domain.FsStoreProductAttrValueScrm;
import com.fs.hisStore.param.FsProductAttrValueParam;
import com.fs.hisStore.param.FsStoreProductAttrValueQueryParam;
import com.fs.hisStore.param.FsStoreTuiProductAttrValueParam;
import com.fs.hisStore.vo.FsStoreProductAttrValueQueryVO;
import com.fs.hisStore.vo.FsStoreProductAttrValueVO;
import com.fs.hisStore.vo.FsStoreTuiProductAttrValueVO;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

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

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

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

    /**
     * 修改商品属性值
     *
     * @param fsStoreProductAttrValue 商品属性值
     * @return 结果
     */
    public int updateFsStoreProductAttrValue(FsStoreProductAttrValueScrm 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_scrm where product_id=#{productId}")
    int deleteFsStoreProductAttrValueByProductId(Long productId);
    @Select("select * from fs_store_product_attr_value_scrm where  product_id=#{productId}")
    @DataSource(DataSourceType.SLAVE)
    List<FsStoreProductAttrValueScrm> selectFsStoreProductAttrValueByProductId(Long productId);
    @Select("select ifnull(stock,0) from fs_store_product_attr_value_scrm where  id=#{productAttrValueId}")
    int selectFsStoreProductStockById(Long productAttrValueId);

    /**
     * 使用行锁查询规格库存
     */
    @Select("select ifnull(stock,0) from fs_store_product_attr_value_scrm where id=#{productAttrValueId} for update")
    @DataSource(DataSourceType.SLAVE)
    Integer selectProductAttrStockForUpdate(@Param("productAttrValueId") Long productAttrValueId);

    @Update("update fs_store_product_attr_value_scrm 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_scrm 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,c.cate_name  from fs_store_product_attr_value_scrm v inner join fs_store_product_scrm p on p.product_id=v.product_id left join fs_store_product_category_scrm 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>" +

            "<if test = 'maps.companyId != null    '> " +
            "and find_in_set(#{maps.companyId},p.company_ids)  " +
            "</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_scrm v " +
            "inner join fs_store_product_scrm p on p.product_id=v.product_id    " +
            "inner join fs_store_scrm fs on fs.store_id = p.store_id " +
            "<if test='config.isAudit == \"1\" '>" +
            "and fs.is_audit = 1 " +
            "</if>" +
            "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,@Param("config") MedicalMallConfig config);

    @Select({"<script> " +
            "select v.*,p.tui_cate_id,p.product_name from fs_store_product_attr_value_scrm v inner join fs_store_product_scrm 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);

    @Update({"<script> " +
            "UPDATE fs_store_product_attr_value_scrm 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>"})
    void updateFsStoreProductAttrValuePrice(List<Long> ids, double v);

    List<FsStoreProductAttrValueScrm> getFsStoreProductAttrValueListInProductId(List<Long> productIds);

    @Update({"<script> " +
            " UPDATE fs_store_product_attr_value_scrm" +
            " SET stock = stock + CAST(#{totalNum} AS SIGNED)" +
            " WHERE product_id = #{productId}" +
            " AND bar_code IN",
            "<foreach collection='barCodeList' item='barCode' open='(' separator=',' close=')'>" +
            "#{barCode}" +
            "</foreach>" +
            "</script>"
    })
    void incStock(@Param("productId") Long productId,@Param("barCodeList") List<String> barCodeList,@Param("totalNum") String totalNum);
}
