package com.fs.hisStore.mapper;

import java.util.List;

import com.fs.hisStore.config.MedicalMallConfig;
import com.fs.hisStore.domain.FsStoreCartScrm;
import com.fs.hisStore.domain.FsStoreScrm;
import com.fs.hisStore.param.FsStoreCartCountParam;
import com.fs.hisStore.vo.FsStoreCartQueryVO;
import com.fs.hisStore.vo.FsStoreCartVO;
import com.fs.store.vo.FsStoreCartScrmQueryVO;
import com.fs.store.vo.FsStoreCartScrmVO;
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 FsStoreCartScrmMapper
{
    /**
     * 查询购物车
     *
     * @param id 购物车ID
     * @return 购物车
     */
    public FsStoreCartScrm selectFsStoreCartById(Long id);

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

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

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

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

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

    @Select({"<script>"+
            "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_scrm c " +
            "inner join fs_store_product_scrm p on p.product_id=c.product_id " +
            "<if test='config.isStores == \"1\"'>" +
            "inner join fs_store_scrm fs on fs.store_id = p.store_id " +
            "</if>" +
            "<if test='config.isAudit == \"1\"'>" +
            "and fs.is_audit = 1 " +
            "</if>" +
            "inner join fs_store_product_attr_value_scrm 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_del=0 and p.is_show=1 " +
            "<if test='config.isAudit == \"1\"'>" +
            "and p.is_audit = '1' " +
            "</if>" +
            "and c.user_id= #{uid} " +
            "</script>"})
    List<FsStoreCartVO> selectFsStoreCartListByUid(@Param("uid") long uid,@Param("config") MedicalMallConfig config);
    @Delete({"<script>"+
            "delete from fs_store_cart_scrm 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.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 from fs_store_cart_scrm c left join fs_store_product_scrm p on p.product_id=c.product_id left join fs_store_product_attr_value_scrm 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_scrm 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_scrm c inner join fs_store_product_scrm p on p.product_id=c.product_id inner join fs_store_product_attr_value_scrm 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_scrm c  " +
            "inner join fs_store_product_scrm p on p.product_id = c.product_id " +
            "inner join fs_store_product_attr_value_scrm v on v.id = c.product_attr_value_id " +
            "left join fs_store_scrm s on v.store_id = s.store_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 " +
            "<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);

    @Select({"<script> " +
            "select q.* FROM (" +
            "select c.*,p.cate_id,p.product_name,p.image as product_image,p.temp_id,p.product_type,v.price,v.ot_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,s.store_id,s.store_name from fs_store_cart_scrm c left join fs_store_product_scrm p " +
            "on p.product_id=c.product_id left join fs_store_product_attr_value_scrm v on v.id=c.product_attr_value_id " +
            "left join fs_store_scrm s on v.store_id=s.store_id " +
            "where find_in_set(c.id,#{ids})" +
            ") q where 1= 1" +
            "<if test = 'storeId != null    '> " +
//                "and q.store_id =#{storeId} " +
//                "</if>" +
//                "<if test = 'storeId == -1    '> " +
//                "and q.store_id IS NULL " +
            "<choose>" +
            "<when test='storeId == -1'>" +
            "and q.store_id IS NULL" +
            "</when>" +
            "<otherwise>" +
            "and q.store_id =#{storeId} " +
            "</otherwise>" +
            "</choose>" +
            "</if>" +
            "</script>"})
    List<FsStoreCartScrmQueryVO> selectFsStoreCartMultiStoreListByIds(@Param("ids") String ids, @Param("storeId")Long storeId);

    @Select(
//            "select s.*" +
//                    "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 " +
//                    "left join fs_store s on v.store_id = s.store_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} GROUP BY s.store_id"
            "SELECT sorted.store_id, sorted.store_name" +
                    " FROM (" +
                    "SELECT c.id, c.product_id, c.product_attr_value_id, c.user_id, c.create_time AS cart_create_time," +
                    "s.store_id, s.store_name " +
                    "FROM fs_store_cart_scrm c " +
                    "INNER JOIN fs_store_product_scrm p ON p.product_id = c.product_id " +
                    "INNER JOIN fs_store_product_attr_value_scrm v ON v.id = c.product_attr_value_id " +
                    "LEFT JOIN fs_store_scrm s ON v.store_id = s.store_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} " +
                    "ORDER BY c.create_time DESC " +
                    ") sorted " +
                    "GROUP BY sorted.store_id;"
    )
    List<FsStoreScrm> selectFsStoreByUid(long uid);

    @Select({"<script> " +
            "select q.* FROM (" +
            "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,s.store_id,s.store_name,( SELECT COUNT(*) FROM fs_store_verify_code_scrm vc WHERE vc.product_id = p.product_id AND vc.outbound_status = 0 AND is_del = 0) AS num " +
            "from fs_store_cart_scrm c " +
            "inner join fs_store_product_scrm p on p.product_id=c.product_id " +
            "inner join fs_store_product_attr_value_scrm v on v.id=c.product_attr_value_id " +
            "left join fs_store_scrm s on v.store_id = s.store_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}" +
            ") q where 1= 1" +
            "<if test = 'storeId != null    '> " +
            "<choose>" +
            "<when test='storeId == -1'>" +
            "and q.store_id IS NULL" +
            "</when>" +
            "<otherwise>" +
            "and q.store_id =#{storeId} " +
            "</otherwise>" +
            "</choose>" +
            "</if>" +
            "</script>"})
    List<FsStoreCartScrmVO> selectFsStoreCartListByUidMultiStore(@Param("uid") long uid, @Param("storeId") Long storeId);
}
