package com.fs.his.mapper;

import com.fs.his.domain.FsStoreProduct;
import com.fs.his.domain.FsStoreProductAttrValue;
import com.fs.his.domain.FsStoreProductRule;
import com.fs.his.param.FsProductAttrValueParam;
import com.fs.his.param.FsStoreProductListSParam;
import com.fs.his.vo.*;
import com.fs.hisStore.vo.FsStoreProductListVO;
import com.fs.live.domain.LiveGoods;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 商品Mapper接口
 *
 * @author fs
 * @date 2023-06-08
 */
public interface FsStoreProductMapper {
    /**
     * 查询商品
     *
     * @param productId 商品主键
     * @return 商品
     */
    public FsStoreProduct selectFsStoreProductByProductId(Long productId);

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


    @Select({"<script> " +
            "select p.*,pc.cate_name,st.store_name  from fs_store_product p left join fs_store_product_category pc on p.cate_id=pc.cate_id LEFT JOIN fs_store st ON st.store_id=p.store_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.storeId != null    '> " +
            "and p.store_id =#{maps.storeId} " +
            "</if>" +
            "<if test = 'maps.barCode != null    '> " +
            "and p.product_id in(SELECT product_id FROM fs_store_product_attr_value WHERE bar_code =#{maps.barCode}) " +
            "</if>" +
            " order by p.product_id desc " +
            "</script>"})
    public List<FsStoreProductVO> selectFsStoreProductListVO(@Param("maps") FsStoreProduct fsStoreProduct);

    @Select({"<script> " +
            "select p.*,pc.cate_name,st.store_name  from fs_store_product p left join fs_store_product_category pc on p.cate_id=pc.cate_id LEFT JOIN fs_store st ON st.store_id=p.store_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.storeId != null    '> " +
            "and p.store_id =#{maps.storeId} " +
            "</if>" +
            "<if test = 'maps.barCode != null    '> " +
            "and p.product_id in(SELECT product_id FROM fs_store_product_attr_value WHERE bar_code =#{maps.barCode}) " +
            "</if>" +
            " order by p.product_id desc " +
            "</script>"})
    public List<FsStoreProductListSVO> selectFsStoreProductListSVO(@Param("maps") FsStoreProductListSParam fsStoreProduct);

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

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

    /**
     * 删除商品
     *
     * @param productId 商品主键
     * @return 结果
     */
    public int deleteFsStoreProductByProductId(Long productId);

    /**
     * 批量删除商品
     *
     * @param productIds 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteFsStoreProductByProductIds(Long[] productIds);

    @Select("select store_id dictValue,store_name dictLabel from fs_store ")
    List<OptionsVO> selectFsStoreProductStoreList();

    @Select("select * FROM fs_store_product_rule WHERE is_del=0")
    List<FsStoreProductRule> selectFsStoreProductRuleList();

    @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);

    @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({"<script> " +
            "select p.product_id  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.storeId != null    '> " +
            "and p.store_id =#{maps.storeId} " +
            "</if>" +
            "<if test = 'maps.barCode != null    '> " +
            "and p.product_id in(SELECT product_id FROM fs_store_product_attr_value WHERE bar_code =#{maps.barCode}) " +
            "</if>" +
            " order by p.product_id desc " +
            "</script>"})
    public List<Long> selectFsStoreProductIdList(@Param("maps") FsStoreProduct fsStoreProduct);

    @Update({"<script> " +
            "UPDATE fs_store_product 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 updateFsStoreProductPrice(@Param("ids") List<Long> ids, @Param("price") double price);

    @Select({"<script> " +
            "select p.*,pc.cate_name,st.store_name  from fs_store_product p left join fs_store_product_category pc on p.cate_id=pc.cate_id LEFT JOIN fs_store st ON st.store_id=p.store_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.storeId != null    '> " +
            "and p.store_id =#{maps.storeId} " +
            "</if>" +
            "<if test = 'maps.barCode != null    '> " +
            "and p.product_id in(SELECT product_id FROM fs_store_product_attr_value WHERE bar_code =#{maps.barCode}) " +
            "</if>" +
            " order by p.product_id desc " +
            "</script>"})
    List<FsStoreProductExcelVO> selectFsStoreProductExcelVO(@Param("maps") FsStoreProduct fsStoreProduct);

    @Select({"<script> " +
            "select v.*,p.product_name,p.product_type,p.is_drug,c.cate_name,store.store_name,p.prescribe_spec  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 LEFT JOIN fs_store store ON store.store_id=p.store_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.storeId != null and  maps.storeId !=\"\"    '> " +
            "and p.store_id =#{maps.storeId} " +
            "</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>" +
            " order by v.id desc " +
            "</script>"})
    List<FsStoreProductAttrValueVO> selectFsStoreProductAttrValueListVO(@Param("maps") FsProductAttrValueParam param);

    @Select("SELECT * FROM fs_store_product\n" +
            "WHERE images LIKE '%https://htj-1258038825.cos.ap-beijing.myqcloud.com%'; ")
    List<FsStoreProduct> selectFsStoreProductListByUpdate();

    @Select("SELECT * FROM fs_store_product_attr_value\n" +
            "WHERE image LIKE '%https://htj-1258038825.cos.ap-beijing.myqcloud.com%'; ")
    List<FsStoreProductAttrValue> selectFsStoreProductAttrValueListByUpdate();

    @Select("select * FROM fs_store_product where bar_code =#{barCode} and (store_id = 30 or store_id = 33)")
    List<FsStoreProduct> selectCode(String barCode);

    @Select("select v.*,p.product_name,p.product_type,p.is_drug,c.cate_name,store.store_name,p.prescribe_spec  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 LEFT JOIN fs_store store ON store.store_id=p.store_id\n" +
            "where 1=1 and v.product_id=#{productId} limit 1")
    FsStoreProductAttrValueVO selectFsStoreProductAttrValueVOByProdId(Long productId);

    @Select("select v.*,p.product_name,p.product_type,p.is_drug,p.unit_name,p.prescribe_spec,p.prescribe_code  from fs_store_product_attr_value v inner join fs_store_product p on p.product_id=v.product_id " +
            "where 1=1 and v.product_id=#{productId} limit 1")
    FsStoreProductAttrVO selectFsStoreProductAttrVOByProdId(Long productId);

    FsStoreProduct selectFsStoreProductById(Long productId);

    @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>" +
            "<if test = 'companyIds != null and companyIds != \"\" '> " +
            "and find_in_set(#{companyIds}, company_ids) " +
            "</if>" +
            "</script>"})
    Long selectFsStoreProductCompanyCount(@Param("type") int type, @Param("companyIds") Long companyIds);

    @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(int type);

    @Select(" select * from fs_store_product where product_id = #{productId} and store_id = #{storeId}")
    FsStoreProduct getStoreProductByProductIdAndStoreId(@Param("productId") Long productId, @Param("storeId") Long storeId);

    @Update({"UPDATE fs_store_product SET stock=#{stock}  WHERE product_id = #{productId} "})
    int updateStoreProductStock(@Param("productId") Long productId, @Param("stock") Integer stock);


    /**
     * 批量获取商品信息
     * @param productIds
     * @return List<FsStoreProduct>
     * **/
    List<FsStoreProduct> getStoreProductInProductIds(@Param("productIds") List<Long> productIds);

    @Select({"<script> " +
            "SELECT fsp.* FROM fs_store_product fsp " +
            " left join fs_store_product_attr_value fspav on fsp.product_id = fspav.product_id  " +
            "WHERE fsp.is_show = 1 and (fspav.bar_code is not null or fspav.group_bar_code is not null)  and " +
            " fsp.product_id NOT IN (" +
            "   SELECT product_id FROM live_goods " +
            "   WHERE live_id = #{maps.liveId} " +
            ")  " +
            "<if test = 'maps.keywords != null'> " +
            " and  fsp.product_name like CONCAT('%',#{maps.keywords},'%')  " +
            "</if>" +
            "</script>"})
    List<FsStoreProductListVO> liveList(@Param("maps") LiveGoods maps);

    FsStoreProduct selectFsStoreProductByBarCode(@Param("barCode") String barCode);
}
