
package com.tmerclub.cloud.multishop.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.tmerclub.cloud.api.feign.SearchSpuFeignClient;
import com.tmerclub.cloud.common.constant.Constant;
import com.tmerclub.cloud.common.database.dto.PageDTO;
import com.tmerclub.cloud.common.database.util.PageUtil;
import com.tmerclub.cloud.common.database.vo.PageVO;
import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.product.dto.ProductSearchLimitDTO;
import com.tmerclub.cloud.common.product.vo.search.SpuSearchVO;
import com.tmerclub.cloud.multishop.mapper.ShopCollectionMapper;
import com.tmerclub.cloud.multishop.mapper.ShopExtensionMapper;
import com.tmerclub.cloud.multishop.model.ShopCollection;
import com.tmerclub.cloud.multishop.service.ShopCollectionService;
import com.tmerclub.cloud.multishop.service.ShopDetailService;
import com.tmerclub.cloud.multishop.vo.ShopCollectionVO;
import com.tmerclub.cloud.multishop.vo.ShopExtensionVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 用户店铺收藏记录
 *
 * @author lhd
 * @date 2021-02-23 14:41:42
 */
@Service
public class UserCollectionShopServiceImpl implements ShopCollectionService {

    @Autowired
    private ShopCollectionMapper shopCollectionMapper;

    @Autowired
    private ShopExtensionMapper shopExtensionMapper;

    @Autowired
    private ShopDetailService shopDetailService;

    @DubboReference
    private SearchSpuFeignClient searchSpuFeignClient;


    @Override
    public ShopCollectionVO getByCollectionId(Long collectionId) {
        return shopCollectionMapper.getByCollectionId(collectionId);
    }

    @Override
    public void save(ShopCollection userCollectionShop) {
        shopCollectionMapper.save(userCollectionShop);
    }

    @Override
    public void update(ShopCollection userCollectionShop) {
        shopCollectionMapper.update(userCollectionShop);
    }

    @Override
    public PageVO<ShopCollectionVO> getUserCollectionShopPageByUserIdAndShopName(PageDTO pageDTO, Long userId, String shopName) {
        PageVO<ShopCollectionVO> shopCollectionPage = PageUtil.doPage(pageDTO, () -> shopCollectionMapper.listUserCollectionShopPageByUserIdAndShopName(userId, shopName));
        if (CollectionUtil.isEmpty(shopCollectionPage.getList())) {
            return shopCollectionPage;
        }
        List<Long> shopIds = shopCollectionPage.getList().stream().map(ShopCollectionVO::getShopId).collect(Collectors.toList());
        ProductSearchLimitDTO productSearchLimitDTO = new ProductSearchLimitDTO(Constant.SPU_SIZE_FIVE);
        productSearchLimitDTO.setShopIds(shopIds);
        List<SpuSearchVO> shopSpuList = searchSpuFeignClient.limitSpuList(productSearchLimitDTO).getData();
        Map<Long, List<SpuSearchVO>> spuMap = shopSpuList.stream().collect(Collectors.groupingBy(SpuSearchVO::getShopId));
        for (ShopCollectionVO shopCollectionVO : shopCollectionPage.getList()) {
            if (!spuMap.containsKey(shopCollectionVO.getShopId()) || CollectionUtil.isEmpty(spuMap.get(shopCollectionVO.getShopId()))) {
                continue;
            }
            shopCollectionVO.setSpuSearchList(spuMap.get(shopCollectionVO.getShopId()));
        }
        return shopCollectionPage;
    }

    @Override
    public int countByShopIdAndUserId(Long shopId, Long userId) {
        return shopCollectionMapper.countByShopIdAndUserId(shopId, userId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addOrCancel(Long shopId, Long userId) {
        if (Objects.isNull(shopDetailService.getByShopId(shopId))) {
            // 该店铺不存在
            throw new LuckException("该店铺不存在");
        }
        boolean isAdd = false;
        if (countByShopIdAndUserId(shopId, userId) > 0) {
            shopCollectionMapper.deleteByShopIdAndUserId(shopId, userId);
        } else {
            ShopCollection userCollectionShop = new ShopCollection();
            userCollectionShop.setUserId(userId);
            userCollectionShop.setShopId(shopId);
            shopCollectionMapper.save(userCollectionShop);
            isAdd = true;
        }
        // 修改店铺的收藏数量
        long changeNum = isAdd ? 1L : -1L;
        ShopExtensionVO shopExtensionVO = shopExtensionMapper.getByShopId(shopId);
        shopExtensionVO.setCollectionNum(shopExtensionVO.getCollectionNum() + changeNum);
        shopExtensionMapper.update(shopExtensionVO);
        return isAdd;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchCancel(List<Long> shopIds, Long userId) {
        boolean remove = false;
        for (Long shopId : shopIds) {
            // 修改店铺的收藏数量
            ShopExtensionVO shopExtensionVO = shopExtensionMapper.getByShopId(shopId);
            if (Objects.isNull(shopExtensionVO)) {
                continue;
            }
            if (countByShopIdAndUserId(shopId, userId) > 0) {
                remove = shopCollectionMapper.deleteByShopIdAndUserId(shopId, userId);
                shopExtensionVO.setCollectionNum(shopExtensionVO.getCollectionNum() - 1);
                shopExtensionMapper.update(shopExtensionVO);
            }
        }
        return remove;
    }
}
