package com.fs.store.mapper;

import java.util.List;
import com.fs.store.domain.FsStoreCart;
import com.fs.store.param.FsStoreCartCountParam;
import com.fs.store.vo.FsStoreCartQueryVO;
import com.fs.store.vo.FsStoreCartVO;
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-21
 */
public interface FsStoreCartMapper
{
    /**
     * 查询购物车
     *
     * @param id 购物车ID
     * @return 购物车
     */
    public FsStoreCart selectFsStoreCartById(Long id);

    /**
     * 查询购物车列表
     *
     * @param fsStoreCart 购物车
     * @return 购物车集合
     */
    public List<FsStoreCart> selectFsStoreCartList(FsStoreCart fsStoreCart);

    /**
     * 新增购物车
     *
     * @param fsStoreCart 购物车
     * @return 结果
     */
    public int insertFsStoreCart(FsStoreCart fsStoreCart);

    /**
     * 修改购物车
     *
     * @param fsStoreCart 购物车
     * @return 结果
     */
    public int updateFsStoreCart(FsStoreCart fsStoreCart);

    /**
     * 删除购物车
     *
     * @param id 购物车ID
     * @return 结果
     */
    public int deleteFsStoreCartById(Long id);

    /**
     * 批量删除购物车
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    public int deleteFsStoreCartByIds(Long[] ids);

    @Select("select c.*,p.product_type,p.product_name,p.image as product_image,v.price,v.sku as product_attr_name,v.image as product_attr_image,v.stock from fs_store_cart c inner join fs_store_product p on p.product_id=c.product_id inner join fs_store_product_attr_value v on v.id=c.product_attr_value_id where c.is_pay=0 and c.is_del=0 and c.is_buy=0 and p.is_show=1 and p.is_del=0 and c.user_id= #{uid}")
    List<FsStoreCartVO> selectFsStoreCartListByUid(long uid);
    @Delete({"<script>"+
            "delete from fs_store_cart where id in"+
           "<foreach collection='array' item='id' open='(' separator=',' close=')'>#{id}</foreach>"+
            "</script>"})
    int delCart(Long[] ids);
    @Select("select c.*,p.cate_id,p.product_name,p.warehouse_id as warehouse_id,p.warehouse_code as warehouse_code,p.image as product_image,p.temp_id,p.product_type,v.price,v.sku as product_attr_name,v.image as product_attr_image,v.stock,v.cost,v.integral,v.weight,v.volume,v.bar_code,v.group_bar_code,v.brokerage,v.brokerage_two,v.brokerage_three," +
            "  v.usage_method,\n" +
            "  v.frequency,\n" +
            "  v.dosage"+
            " from fs_store_cart c left join fs_store_product p on p.product_id=c.product_id left join fs_store_product_attr_value v on v.id=c.product_attr_value_id where find_in_set(c.id,#{ids})")
    List<FsStoreCartQueryVO> selectFsStoreCartListByIds(String ids);
    @Update("update  fs_store_cart set is_pay=1 where find_in_set(id,#{cartIds})")
    void updateIsPay(String cartIds);


    @Select("select ifnull(sum(c.cart_num),0) from fs_store_cart c inner join fs_store_product p on p.product_id=c.product_id inner join fs_store_product_attr_value v on v.id=c.product_attr_value_id where c.is_pay=0 and c.is_del=0 and c.is_buy=0 and p.is_show=1 and p.is_del=0 and c.user_id= #{userId}")
    Integer selectFsStoreCartCountByUserId(long userId);
    @Select({"<script> " +
            "select ifnull(sum(c.cart_num),0) from fs_store_cart c  " +
            "where c.is_pay=0 and c.is_del=0 and c.is_buy=0 " +
            "<if test = 'maps.userId != null     '> " +
            "and c.user_id =#{maps.userId} " +
            "</if>" +
            "<if test = 'maps.productId != null    '> " +
            "and c.product_id =#{maps.productId} " +
            "</if>" +
            "</script>"})
    Integer selectFsStoreCartCount(@Param("maps") FsStoreCartCountParam param);
}
