package co.yixiang.yshop.module.cart.dal.mysql.storecart;

import java.util.*;

import co.yixiang.yshop.framework.common.pojo.PageResult;
import co.yixiang.yshop.framework.mybatis.core.query.LambdaQueryWrapperX;
import co.yixiang.yshop.framework.mybatis.core.mapper.BaseMapperX;
import co.yixiang.yshop.module.cart.dal.dataobject.storecart.StoreCartDO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Mapper;
import co.yixiang.yshop.module.cart.controller.admin.storecart.vo.*;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 购物车 Mapper
 *
 * @author yshop
 */
@Mapper
public interface StoreCartMapper extends BaseMapperX<StoreCartDO> {

    default PageResult<StoreCartDO> selectPage(StoreCartPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<StoreCartDO>()
                .eqIfPresent(StoreCartDO::getType, reqVO.getType())
                .eqIfPresent(StoreCartDO::getProductId, reqVO.getProductId())
                .orderByDesc(StoreCartDO::getId));
    }

    @Select("<script>select sc.id,sc.type,su.id as userId,su.nickname,sc.cart_num,sc.is_pay," +
            "sc.create_time,sp.store_name,sc.product_id as productId\n" +
            "from yshop_store_cart sc  \n" +
            "left join yshop_user su on sc.uid = su.id\n" +
            "left join yshop_store_product sp on sc.product_id = sp.id " +
            "where sc.deleted = 0 " +
            "<if test =\"nickname !='' and nickname != null \">and su.nickname like CONCAT('%', #{nickname}, '%')</if>" +
            "order by sc.create_time desc</script>")
    List<StoreCartRespVO> pageList(Page page, @Param("nickname") String nickname);


    @Select("<script>select count(*)" +
            "from yshop_store_cart sc  \n" +
            "left join yshop_user su on sc.uid = su.id\n" +
            "left join yshop_store_product sp on sc.product_id = sp.id " +
            "where sc.deleted = 0 " +
            "<if test =\"nickname !='' and nickname != null \">and su.nickname like CONCAT('%', #{nickname}, '%')</if>" +
            "order by sc.create_time desc</script>")
    Long pageListCount(@Param("nickname") String nickname);

    @Select("select IFNULL(sum(ysc.cart_num),0) from yshop_store_cart as ysc " +
            "inner join yshop_store_product_attr_value as yspav on ysc.product_attr_unique = yspav.`unique` " +
            "where ysc.is_pay = 0 and ysc.deleted = 0 and ysc.is_new = 0 and ysc.uid= #{uid} " +
            "and yspav.stock > 0")
    int cartSum(@Param("uid") Long uid);

}
