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

import co.yixiang.yshop.framework.common.enums.DeletedEnum;
import co.yixiang.yshop.framework.common.util.collection.CollectionUtils;
import co.yixiang.yshop.module.product.controller.app.relation.vo.AppStoreProductRelationQueryVo;
import co.yixiang.yshop.module.product.dal.dataobject.storeproduct.StoreProductDO;
import co.yixiang.yshop.module.product.dal.dataobject.storeproductrelation.StoreProductRelationDO;
import co.yixiang.yshop.module.product.dal.mysql.storeproduct.StoreProductMapper;
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.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

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

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

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

    @Resource
    private StoreProductRelationMapper storeProductRelationMapper;

    @Resource
    private StoreProductMapper storeProductMapper;


    /**
     * 是否收藏
     *
     * @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)
                        .eq(StoreProductRelationDO::getDeleted, DeletedEnum.NO.getStatus()));
        return count > 0;
    }

    /**
     * 添加收藏
     *
     * @param productId 商品id
     * @param uid       用户id
     */

    @Override
    @Transactional
    public void addProductRelation(Long productId, Long uid, String category) {
        StoreProductDO storeProductDO = storeProductMapper.selectById(productId);
        if (storeProductDO == null) throw exception(STORE_PRODUCT_NOT_EXISTS);
        if (isProductRelation(productId, uid, category)) {
            throw exception(STORE_PRODUCT_RELATION_EXISTS);
        }
        StoreProductRelationDO storeProductRelationDO = StoreProductRelationDO.builder()
                .productId(productId)
                .uid(uid)
                .category(category)
                .type(RelationEnum.COLLECT.getValue())
                .build();
        storeProductRelationMapper.insert(storeProductRelationDO);
    }

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

    /**
     * 批量取消收藏
     *
     * @param idList 收藏商品id列表
     * @param uid    用户id
     */
    @Override
    public void batchDelProductRelation(List<Long> idList, Long uid, String category, String type) {
        List<StoreProductRelationDO> productRelationList = this.lambdaQuery()
                .in(StoreProductRelationDO::getProductId, idList)
                .eq(StoreProductRelationDO::getUid, uid)
                .eq(StoreProductRelationDO::getType, type)
                .eq(StoreProductRelationDO::getCategory, category)
                .eq(StoreProductRelationDO::getDeleted, DeletedEnum.NO.getStatus())
                .list();
        if (CollectionUtils.isAnyEmpty(productRelationList) && RelationEnum.COLLECT.getValue().equals(type)) {
            throw exception(STORE_PRODUCT_RELATION_NOT_EXISTS);
        } else if (CollectionUtils.isAnyEmpty(productRelationList) && RelationEnum.FOOT.getValue().equals(type)) {
            throw exception(STORE_PRODUCT_FOOT_NOT_EXISTS);
        }
        storeProductRelationMapper.deleteBatchIds(productRelationList.stream().map(StoreProductRelationDO::getId).collect(Collectors.toList()));
    }

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

}
