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

import cn.hutool.core.collection.CollectionUtil;
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.i18n.I18nMessage;
import com.tmerclub.cloud.common.product.constant.SpuType;
import com.tmerclub.cloud.common.product.vo.SpuVO;
import com.tmerclub.cloud.common.product.vo.app.SpuAppVO;
import com.tmerclub.cloud.common.security.AuthUserContext;
import com.tmerclub.cloud.product.mapper.SpuCollectionMapper;
import com.tmerclub.cloud.product.model.SpuCollection;
import com.tmerclub.cloud.product.service.SpuCollectionService;
import com.tmerclub.cloud.product.service.SpuService;
import com.tmerclub.cloud.product.vo.SpuCollectionVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.Objects;

/**
 * 商品收藏信息
 *
 * @author tmerclub
 * @date 2020-11-21 14:43:16
 */
@Service
public class SpuCollectionServiceImpl implements SpuCollectionService {

    @Autowired
    private SpuCollectionMapper spuCollectionMapper;

    @Autowired
    private SpuService spuService;

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public PageVO<SpuCollectionVO> page(PageDTO pageDTO) {
        return PageUtil.doPage(pageDTO, () -> spuCollectionMapper.list());
    }

    @Override
    public SpuCollectionVO getById(Long id) {
        return spuCollectionMapper.getById(id);
    }

    @Override
    public void save(SpuCollection spuCollection) {
        spuCollectionMapper.save(spuCollection);
    }

    @Override
    public void update(SpuCollection spuCollection) {
        spuCollectionMapper.update(spuCollection);
    }

    @Override
    public void deleteById(Long id) {
        spuCollectionMapper.deleteById(id);
    }

    @Override
    public int userCollectionCount(Long spuId, Long userId) {
        return spuCollectionMapper.userCollectionCount(spuId, userId);
    }

    @Override
    public PageVO<SpuAppVO> getUserCollectionDtoPageByUserId(PageDTO pageDTO, Long userId, String spuName, Integer prodType) {
        return PageUtil.doPage(pageDTO, () -> spuCollectionMapper.getUserCollectionDtoPageByUserId(userId, spuName, I18nMessage.getLang(), prodType));
    }

    @Override
    public void deleteBySpuIdAndUserId(Long spuId, Long userId) {
        spuCollectionMapper.deleteUserCollection(spuId, null, userId);
    }

    @Override
    public void spuBatchCollection(List<Long> spuIdList) {
        Long userId = AuthUserContext.get().getUserId();
        List<Long> spuIds = spuCollectionMapper.hasCollection(spuIdList, userId);
        spuIdList.removeAll(spuIds);
        if (CollectionUtil.isEmpty(spuIdList)) {
            return;
        }
        // 过滤活动商品
        List<SpuVO> spuVOList = spuService.getBySpuIds(spuIdList);
        for (SpuVO spuVO : spuVOList) {
            if (Objects.equals(spuVO.getSpuType(), SpuType.ACTIVE.value())) {
                logger.info("活动商品不可收藏，spuId:{}", spuVO.getSpuId());
                spuIdList.remove(spuVO.getSpuId());
            }
        }
        spuCollectionMapper.saveBatch(spuIdList, userId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatchBySpuIdsAndUserId(List<Long> spuIds, Long userId) {
        boolean remove = false;
        for (Long spuId : spuIds) {
            if (Objects.isNull(spuService.getBySpuId(spuId))) {
                continue;
            }
            if (userCollectionCount(spuId, userId) > 0) {
                deleteBySpuIdAndUserId(spuId, userId);
            }
        }
    }

    @Override
    public void deleteBySpuId(Long spuId) {
        spuCollectionMapper.deleteBySpuId(spuId);
    }

}
