package com.fs.store.mapper;

import java.util.List;
import com.fs.store.domain.FsStoreProduct;
import com.fs.store.param.FsStoreProductQueryParam;
import com.fs.store.vo.*;
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 FsStoreProductMapper
{
    /**
     * 查询商品
     *
     * @param productId 商品ID
     * @return 商品
     */
    public FsStoreProduct selectFsStoreProductById(Long productId);

    /**
     * 查询商品列表
     *
     * @param fsStoreProduct 商品
     * @return 商品集合
     */
    public List<FsStoreProduct> selectFsStoreProductList(FsStoreProduct fsStoreProduct);

    /**
     * 新增商品
     *
     * @param fsStoreProduct 商品
     * @return 结果
     */
    public int insertFsStoreProduct(FsStoreProduct fsStoreProduct);

    /**
     * 修改商品
     *
     * @param fsStoreProduct 商品
     * @return 结果
     */
    public int updateFsStoreProduct(FsStoreProduct fsStoreProduct);

    /**
     * 删除商品
     *
     * @param productId 商品ID
     * @return 结果
     */
    public int deleteFsStoreProductById(Long productId);

    /**
     * 批量删除商品
     *
     * @param productIds 需要删除的数据ID
     * @return 结果
     */
    public int deleteFsStoreProductByIds(Long[] productIds);
    @Select({"<script> " +
            "select p.*,pc.cate_name  from fs_store_product p left join fs_store_product_category pc on p.cate_id=pc.cate_id   " +
            "where 1=1 " +
            "<if test = 'maps.productName != null and  maps.productName !=\"\"    '> " +
            "and p.product_name like CONCAT('%',#{maps.productName},'%') " +
            "</if>" +
            "<if test = 'maps.cateId != null    '> " +
            "and (pc.cate_id =#{maps.cateId} or pc.pid=#{maps.cateId} )" +
            "</if>" +
            "<if test = 'maps.productType != null    '> " +
            "and p.product_type =#{maps.productType} " +
            "</if>" +
            "<if test = 'maps.isShow != null    '> " +
            "and p.is_show =#{maps.isShow} " +
            "</if>" +
            "<if test = 'maps.companyIds != null and maps.companyIds != \"\" '> " +
            "and find_in_set(#{maps.companyIds}, p.company_ids) " +
            "</if>" +
            " order by p.product_id desc "+
            "</script>"})
    List<FsStoreProductListVO> selectFsStoreProductListVO(@Param("maps")FsStoreProduct fsStoreProduct);


    @Select({"<script> " +
            "  select p.*,pc.cate_name from (SELECT ave.bar_code as bar_code,p.product_id, p.image, p.slider_image,p.product_name, p.product_info,p.keyword, p.cate_id, p.price, p.vip_price, " +
            " p.ot_price,p.agent_price, p.postage,p.unit_name,p.sort,p.sales,p.stock,p.is_show,p.is_hot,p.is_benefit,p.is_best,p.is_new,p.description,p.create_time,p.update_time,p.is_postage,p.is_del,p.give_integral," +
            " p.cost,p.is_good,p.browse,p.code_path,p.temp_id,p.spec_type,p.is_integral,p.integral,p.product_type,p.prescribe_code, p.prescribe_spec,p.prescribe_factory,p.prescribe_name,p.is_display,p.tui_cate_id " +
            " FROM fs_store_product p LEFT JOIN fs_store_product_attr_value ave on p.product_id=ave.product_id  WHERE ave.bar_code != '' and p.product_id is not null" +
            ") p left join fs_store_product_category pc on p.cate_id=pc.cate_id   " +
            " where 1=1 " +
            "<if test = 'maps.productName != null and  maps.productName !=\"\"  '> " +
            "and p.product_name like CONCAT('%',#{maps.productName},'%') " +
            "</if>" +
            "<if test = 'maps.barCode != null and  maps.barCode !=\"\"    '> " +
            "and p.bar_code like CONCAT('%',#{maps.barCode},'%') " +
            "</if>" +
            "<if test = 'maps.cateId != null    '> " +
            "and (pc.cate_id =#{maps.cateId} or pc.pid=#{maps.cateId} )" +
            "</if>" +
            "<if test = 'maps.productType != null    '> " +
            "and p.product_type =#{maps.productType} " +
            "</if>" +
            "<if test = 'maps.isShow != null    '> " +
            "and p.is_show =#{maps.isShow} " +
            "</if>" +
            " order by p.product_id desc "+
            "</script>"})
    List<FsStoreProductListVO> selectFsStoreProductBarCodeListVO(@Param("maps")FsStoreProduct fsStoreProduct);

    @Select("select * from fs_store_product where is_del=0 and is_show=1 and  is_best=1 and is_display=1 order by sort desc limit #{count}")
    List<FsStoreProductListQueryVO> selectFsStoreProductTuiQuery(int count);
    @Select("select * from fs_store_product where  is_del=0 and is_show=1 order by sort desc limit #{count}")
    List<FsStoreProductListQueryVO> selectFsStoreProductByCateIdQuery(Long cateId);
    @Select({"<script> " +
            "select p.* from fs_store_product p  " +
            "where is_del=0 and is_show=1  " +
            "<if test = 'maps.productName != null and  maps.productName !=\"\"    '> " +
            "and p.product_name like CONCAT('%',#{maps.productName},'%') " +
            "</if>" +
            "<if test = 'maps.isDisplay != null and maps.isDisplay == 1   '> " +
            "and p.is_display =#{maps.isDisplay}  " +
            "</if>" +
            "<if test = 'maps.companyId != null'> " +
            "and find_in_set(#{maps.companyId}, p.company_ids) " +
            "</if>" +
            "<if test = 'maps.cateId != null    '> " +
            "and p.cate_id =#{maps.cateId}  " +
            "</if>" +
            "<if test = 'maps.defaultOrder != null and maps.defaultOrder==\"desc\"  '> " +
            "order by p.sort desc " +
            "</if>" +
            "<if test = 'maps.priceOrder != null and maps.priceOrder==\"desc\"   '> " +
            "order by p.price desc " +
            "</if>" +
            "<if test = 'maps.priceOrder != null and maps.priceOrder==\"asc\"   '> " +
            "order by p.price asc " +
            "</if>" +
            "<if test = 'maps.salesOrder != null  and maps.salesOrder==\"desc\" '> " +
            "order by p.sales desc " +
            "</if>" +
            "<if test = 'maps.salesOrder != null  and maps.salesOrder==\"asc\" '> " +
            "order by p.sales asc " +
            "</if>" +
            "<if test = 'maps.newOrder != null and maps.newOrder==\"desc\" '> " +
            "and p.is_new =1 order by p.create_time desc  " +
            "</if>" +
            "</script>"})
    List<FsStoreProductListQueryVO> selectFsStoreProductListQuery(@Param("maps")FsStoreProductQueryParam param);
    @Select({"<script> " +
            "select p.* from fs_store_product p  " +
            "where is_del=0 and is_show=1 and product_id=#{productId} " +
            "</script>"})
    FsStoreProductQueryVO selectFsStoreProductByIdQuery(Long productId);
    @Update("update fs_store_product set stock=stock-#{num}, sales=sales+#{num}" +
            " where product_id=#{productId} and stock >= #{num}")
    int decProductAttrStock(@Param("productId")Long productId, @Param("num")Integer cartNum);
    @Update("update fs_store_product set stock=stock+#{num}, sales=sales-#{num}" +
            " where product_id=#{productId}")
    int incStockDecSales( @Param("num")Long num, @Param("productId")Long productId);
    @Select("select * from fs_store_product where is_del=0 and is_show=1 and  is_new=1 and is_display=1 order by sort desc limit #{count}")
    List<FsStoreProductListQueryVO> selectFsStoreProductNewQuery(int count);
    @Select("select * from fs_store_product where is_del=0 and is_show=1 and  is_hot=1 and is_display=1 order by sort desc limit #{count}")
    List<FsStoreProductListQueryVO> selectFsStoreProductHotQuery(int count);
    @Select("select * from fs_store_product where is_del=0 and is_show=1 and  is_good=1 and is_display=1 order by sort desc limit #{count}")
    List<FsStoreProductListQueryVO> selectFsStoreProductGoodQuery(int count);
    @Select("select * from fs_store_product where is_del=0 and is_show=1 and  is_best=1 and is_display=1 order by sort desc")
    List<FsStoreProductListQueryVO> selectFsStoreProductTuiListQuery();
    @Select("select * from fs_store_product where is_del=0 and is_show=1 and  is_good=1 and is_display=1 order by sort desc")
    List<FsStoreProductListQueryVO> selectFsStoreProductGoodListQuery();
    @Select({"<script> " +
            "select count(1) from fs_store_product  " +
            "where 1=1 " +
            "<if test = 'type != null and  type ==1  '> " +
            "and  DATE_FORMAT(create_time, '%Y-%m-%d')  = DATE_FORMAT(NOW(), '%Y-%m-%d') " +
            "</if>" +
            "</script>"})
    Long selectFsStoreProductCount(@Param("type") int type );
    @Select({"<script> " +
            "select * from fs_store_product where find_in_set(product_id,#{ids})  " +
            "</script>"})
    List<FsStoreProductActivityListVO> selectFsStoreProductByIds(@Param("ids")String productIds);

    @Update("update fs_store_product p set stock=(select sum(stock) from fs_store_product_attr_value where product_id=#{productId}) where product_id=#{productId}  ")
    Integer updateStock(Long productId);
    @Select("select * from fs_store_product where is_del=0 and find_in_set(product_id,#{productIds}) order by sort desc")
    List<FsStoreProductListQueryVO> selectFsStoreProductListByIds(@Param("productIds") String productIds);


    @Select({"<script> " +
            "  SELECT p.product_id, p.image, p.slider_image,p.product_name, p.product_info,p.keyword, p.cate_id," +
            " p.vip_price, p.ot_price,p.agent_price, p.postage,p.unit_name,p.sort,p.sales,p.is_show,p.is_hot,p.is_benefit,p.is_best,p.is_new,p.description," +
            " p.create_time,p.update_time,p.is_postage,p.is_del,p.give_integral, p.cost,p.is_good,p.browse,p.code_path,p.temp_id,p.spec_type,p.is_integral,p.integral," +
            " p.product_type,p.prescribe_code,p.prescribe_spec,p.prescribe_factory,p.prescribe_name,ave.sku,ave.stock,ave.price,ave.bar_code,ave.group_bar_code,ave.weight,ave.volume FROM fs_store_product p " +
            "  LEFT JOIN fs_store_product_attr_value ave on p.product_id=ave.product_id WHERE  p.product_id is not null " +
            "</script>"})
    List<FsStoreProductExportVO> selectFsStoreProductExportList(@Param("maps")FsStoreProduct fsStoreProduct);


}
