package co.yixiang.yshop.module.product.service.storeproductrelation;

import co.yixiang.yshop.module.product.controller.app.relation.vo.AppStoreProductRelationQueryVo;
import co.yixiang.yshop.module.product.dal.dataobject.storeproductrelation.StoreProductRelationDO;
import co.yixiang.yshop.module.product.dal.mysql.storeproductrelation.StoreProductRelationMapper;
import co.yixiang.yshop.module.product.enums.product.RelationEnum;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static co.yixiang.yshop.framework.common.exception.util.ServiceExceptionUtil.exception;
import static co.yixiang.yshop.module.product.enums.ErrorCodeConstants.STORE_PRODUCT_RELATION_EXISTS;
import static co.yixiang.yshop.module.product.enums.ErrorCodeConstants.STORE_PRODUCT_RELATION_NOT_EXISTS;

/**
 * 商品点赞和收藏 Service 实现类
 *
 * @author yshop
 */
@Service
@Validated
public class AppStoreProductRelationServiceImpl extends ServiceImpl<StoreProductRelationMapper, StoreProductRelationDO> implements AppStoreProductRelationService {

    @Resource
    private StoreProductRelationMapper storeProductRelationMapper;


    /**
     * 是否收藏
     * @param productId 商品ID
     * @param uid 用户ID
     * @return Boolean
     */
    @Override
    public Boolean isProductRelation(long productId, long uid,String category) {
        Long count = storeProductRelationMapper
                .selectCount(Wrappers.<StoreProductRelationDO>lambdaQuery()
                        .eq(StoreProductRelationDO::getUid,uid)
                        .eq(StoreProductRelationDO::getCategory,category)
                        .eq(StoreProductRelationDO::getType, RelationEnum.COLLECT.getValue())
                        .eq(StoreProductRelationDO::getProductId,productId));
        if(count > 0) {
            return true;
        }

        return false;
    }

    /**
     * 添加收藏
     * @param productId 商品id
     * @param uid 用户id
     */
    @Override
    public void addRroductRelation(long productId,long uid,String category) {
        if(isProductRelation(productId,uid,category)) {
            throw exception(STORE_PRODUCT_RELATION_EXISTS);
        }
        StoreProductRelationDO storeProductRelation = StoreProductRelationDO.builder()
                .productId(productId)
                .uid(uid)
                .category(category)
                .type(RelationEnum.COLLECT.getValue())
                .build();
        storeProductRelationMapper.insert(storeProductRelation);
    }

    /**
     * 取消收藏
     * @param productId 商品id
     * @param uid 用户id
     */
    @Override
    public void delRroductRelation(long productId,long uid,String category) {
        StoreProductRelationDO productRelation = this.lambdaQuery()
                .eq(StoreProductRelationDO::getProductId,productId)
                .eq(StoreProductRelationDO::getUid,uid)
                .eq(StoreProductRelationDO::getType,RelationEnum.COLLECT.getValue())
                .eq(StoreProductRelationDO::getCategory,category)
                .one();
        if(productRelation == null) {
            throw exception(STORE_PRODUCT_RELATION_NOT_EXISTS);
        }
        this.removeById(productRelation.getId());
    }

    /**
     * 获取用户收藏列表
     * @param page page
     * @param limit limit
     * @param uid 用户id
     * @return list
     */
    @Override
    public List<AppStoreProductRelationQueryVo> userCollectProduct(int page, int limit, Long uid, String type) {
        Page<StoreProductRelationDO> pageModel = new Page<>(page, limit);
        List<AppStoreProductRelationQueryVo> list = storeProductRelationMapper.selectRelationList(pageModel,uid,type);
        return list;
    }

}
