package co.yixiang.yshop.module.product.dal.mysql.storeproductrelation;

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.product.controller.app.product.vo.AppStoreProductReplyQueryVo;
import co.yixiang.yshop.module.product.controller.app.relation.vo.AppStoreProductRelationQueryVo;
import co.yixiang.yshop.module.product.dal.dataobject.storeproductrelation.StoreProductRelationDO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Mapper;
import co.yixiang.yshop.module.product.controller.admin.storeproductrelation.vo.*;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 商品点赞和收藏 Mapper
 *
 * @author yshop
 */
@Mapper
public interface StoreProductRelationMapper extends BaseMapperX<StoreProductRelationDO> {

    default PageResult<StoreProductRelationDO> selectPage(StoreProductRelationPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<StoreProductRelationDO>()
                .eqIfPresent(StoreProductRelationDO::getUid, reqVO.getUid())
                .eqIfPresent(StoreProductRelationDO::getProductId, reqVO.getProductId())
                .eqIfPresent(StoreProductRelationDO::getType, reqVO.getType())
                .eqIfPresent(StoreProductRelationDO::getCategory, reqVO.getCategory())
                .betweenIfPresent(StoreProductRelationDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(StoreProductRelationDO::getId));
    }


    @Select("select B.id pid,A.type as category,B.store_name as storeName,B.price,B.is_integral as isIntegral,A.id id," +
            "B.ot_price as otPrice,B.sales,B.image,B.is_show as isShow" +
            " from yshop_store_product_relation A left join yshop_store_product B " +
            "on A.product_id = B.id where A.type=#{type} and A.uid=#{uid} and A.deleted = 0 and B.deleted = 0 order by A.create_time desc")
    List<AppStoreProductRelationQueryVo> selectRelationList(Page page, @Param("uid") Long uid, @Param("type") String type);


    @Select("<script>select A.id, C.store_name as storeName,B.nickname," +
            "A.create_Time as createTime," +
            "B.avatar" +
            " from yshop_store_product_relation A left join yshop_user B " +
            "on A.uid = B.id left join yshop_store_product C on A.product_id = C.id" +
            " where A.deleted=0 " +
            "<if test =\"nickname !=''\">and B.nickname = #{nickname}</if>" +
            " order by A.create_Time DESC</script>")
    List<AppStoreProductReplyQueryVo> allRelationList(Page page, @Param("nickname") String nickname);

    @Select("<script>select count(*)" +
            " from yshop_store_product_relation A left join yshop_user B " +
            "on A.uid = B.id" +
            " where A.deleted=0 " +
            "<if test =\"nickname !=''\">and B.nickname = #{nickname}</if>" +
            " order by A.create_Time DESC</script>")
    Long allRelationListCount(@Param("nickname") String nickname);


}
