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

import co.yixiang.yshop.framework.common.pojo.PageResult;
import co.yixiang.yshop.framework.mybatis.core.mapper.BaseMapperX;
import co.yixiang.yshop.framework.mybatis.core.query.LambdaQueryWrapperX;
import co.yixiang.yshop.module.product.controller.admin.storeproductrelation.vo.StoreProductRelationPageReqVO;
import co.yixiang.yshop.module.product.controller.admin.storeproductrelation.vo.StoreProductRelationRespVO;
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 org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 商品点赞和收藏 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 C.avatar,C.id as uid,A.type as type,A.category as category ,A.create_time as createTime,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,B.id as productId" +
            " from yshop_store_product_relation A left join yshop_store_product B " +
            "on A.product_id = B.id left join yshop_user C  on A.uid = C.id" +
            " where A.type=#{type} and A.uid=#{uid} and A.deleted = 0 and B.deleted = 0 group by productId order by A.create_time desc")
    List<AppStoreProductRelationQueryVo> userCollectRootProductList(Page page, @Param("uid") Long uid, @Param("type") String type);


    @Select("<script>select A.id,A.type, C.store_name as storeName,B.nickname," +
            "A.create_Time as createTime," +
            "B.avatar,B.id as uid,A.product_id as productId,A.category" +
            " 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 nickname != null \">and B.nickname LIKE CONCAT('%',#{nickname},'%')</if>" +
            "<if test =\"type !='' and type != null \">and A.type = #{type}</if>" +
            " order by A.create_Time DESC</script>")
    List<StoreProductRelationRespVO> allRelationList(Page page, @Param("nickname") String nickname, @Param("type") String type);

    @Select("<script>select count(*)" +
            " 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 nickname != null\">and B.nickname LIKE CONCAT('%',#{nickname},'%')</if>" +
            "<if test =\"type !='' and type != null \">and A.type = #{type}</if>" +
            " order by A.create_Time DESC</script>")
    Long allRelationListCount(@Param("nickname") String nickname, @Param("type") String type);


}
