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

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.tmerclub.cloud.api.distribution.feign.DistributionFeignClient;
import com.tmerclub.cloud.api.group.feign.GroupFeignClient;
import com.tmerclub.cloud.api.seckill.feign.SeckillFeignClient;
import com.tmerclub.cloud.common.constant.Constant;
import com.tmerclub.cloud.common.constant.StatusEnum;
import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.product.constant.SpuStatus;
import com.tmerclub.cloud.common.product.constant.SpuType;
import com.tmerclub.cloud.common.product.vo.SpuVO;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.security.AuthUserContext;
import com.tmerclub.cloud.product.mapper.BrandMapper;
import com.tmerclub.cloud.product.mapper.SkuComboMapper;
import com.tmerclub.cloud.product.mapper.SpuMapper;
import com.tmerclub.cloud.product.service.SpuOfflineService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * spu信息
 *
 * @author lhd
 * @date 2021-07-03 15:27:24
 */
@Service
public class SpuOfflineServiceImpl implements SpuOfflineService {

    @Autowired
    private SpuMapper spuMapper;
    @Autowired
    private SkuComboMapper skuComboMapper;
    @Autowired
    private BrandMapper brandMapper;
    @DubboReference
    private GroupFeignClient groupFeignClient;
    @DubboReference
    private SeckillFeignClient seckillFeignClient;
    @DubboReference
    private DistributionFeignClient distributionFeignClient;

    @Override
    public void offlineSpuStatusAndActivity(Integer type, List<Long> spuIds, Long shopId, List<Long> categoryIds, Integer status, List<Long> shopIds) {
        // 查询出所有的启用商品进行修改
        List<SpuVO> spuList = spuMapper.getActivitySpuInfoByCategoryIdsAndShopId(type, spuIds, shopId, categoryIds, status, shopIds);
        if (CollectionUtil.isEmpty(spuList)) {
            return;
        }
        List<Long> seckillSpuIds = new ArrayList<>(Constant.INITIAL_CAPACITY);
        List<Long> groupSpuIds = new ArrayList<>(Constant.INITIAL_CAPACITY);
        for (SpuVO spuVO : spuList) {
            if (Objects.equals(spuVO.getSpuType(), SpuType.SECKILL.value())) {
                spuVO.setSpuType(0);
                spuVO.setActivityId(0L);
                seckillSpuIds.add(spuVO.getSpuId());
            }
            if (Objects.equals(spuVO.getSpuType(), SpuType.GROUP.value())) {
                spuVO.setSpuType(0);
                spuVO.setActivityId(0L);
                groupSpuIds.add(spuVO.getSpuId());
            }

            // 商品那边可能有平台下架和商家下架，其余都为0，商家下架
            if (type != 1) {
                spuVO.setStatus(0);
            }
        }
        spuMapper.updateBatch(spuList);
        // 下线掉关联当前商品的组合商品
        offlineComboSpuBySpuIds(spuIds);
        // 下线秒杀、团购活动
        offlineActivityBySpuIds(seckillSpuIds, groupSpuIds);
        // 更新品牌商品数量
        spuIds = updateSpuCount(spuList);
        // 下线分销商品
        ServerResponseEntity<Void> responseEntity = distributionFeignClient.handleSpuState(spuIds, new ArrayList<>(Constant.INITIAL_CAPACITY), StatusEnum.DISABLE.value());
        if (!responseEntity.isSuccess()) {
            throw new LuckException(responseEntity.getMsg());
        }
    }

    private void offlineComboSpuBySpuIds(List<Long> spuIds) {
        //spuc
    }

    private List<Long> updateSpuCount(List<SpuVO> spuList) {
        List<Long> spuIds = spuList.stream().map(SpuVO::getSpuId).collect(Collectors.toList());
        if (CollUtil.isNotEmpty(spuIds)) {
            List<Long> brandIds = brandMapper.listBrandIdBySpuIds(spuIds);
            if (CollUtil.isNotEmpty(brandIds)) {
                brandMapper.batchUpdateSpuCount(brandIds);
            }
        }
        return spuIds;
    }

    @Override
    public void offlineSpuStatusAndActivityByBrandId(List<Long> spuIds, Long brandId) {
        Integer sysType = AuthUserContext.get().getSysType();
        // 查询出所有的启用商品进行修改
        List<SpuVO> spuList = spuMapper.getActivitySpuInfoByCategoryIdsAndShopId(1, spuIds, null, null, null, null);
        List<Long> seckillSpuIds = new ArrayList<>(Constant.INITIAL_CAPACITY);
        List<Long> groupSpuIds = new ArrayList<>(Constant.INITIAL_CAPACITY);
        for (SpuVO spuVO : spuList) {
            spuVO.setBrandId(null);
            spuVO.setStatus(SpuStatus.OFF_SHELF.value());
            if (Objects.equals(spuVO.getSpuType(), SpuType.GROUP.value())) {
                spuVO.setSpuType(0);
                spuVO.setActivityId(0L);
                groupSpuIds.add(spuVO.getSpuId());
            }
            if (Objects.equals(spuVO.getSpuType(), SpuType.SECKILL.value())) {
                spuVO.setSpuType(0);
                spuVO.setActivityId(0L);
                seckillSpuIds.add(spuVO.getSpuId());
            }
        }
        if (CollectionUtil.isEmpty(spuList)) {
            return;
        }
        spuMapper.updateBatchFieldSetNull(spuList);
        // 下线对应的活动
        offlineActivityBySpuIds(seckillSpuIds, groupSpuIds);
        // dev
        if (Objects.nonNull(brandId) && !Objects.equals(brandId, Constant.ZERO_LONG)) {
            brandMapper.batchUpdateSpuCount(Collections.singleton(brandId));
        }
        // 下线分销商品
        ServerResponseEntity<Void> responseEntity = distributionFeignClient.handleSpuState(spuIds, new ArrayList<>(Constant.INITIAL_CAPACITY), StatusEnum.DISABLE.value());
        if (!responseEntity.isSuccess()) {
            throw new LuckException(responseEntity.getMsg());
        }
    }


    private void offlineActivityBySpuIds(List<Long> seckillSpuIds, List<Long> groupSpuIds) {
        // 下线对应的活动
        if (CollectionUtil.isNotEmpty(seckillSpuIds)) {
            ServerResponseEntity<Void> responseEntity = seckillFeignClient.offlineSeckillBySpuIds(seckillSpuIds, false);
            if (!responseEntity.isSuccess()) {
                throw new LuckException(responseEntity.getMsg());
            }
        }
        // 下线对应的团购活动
        if (CollectionUtil.isNotEmpty(groupSpuIds)) {
            ServerResponseEntity<Void> responseEntity = groupFeignClient.offlineGroupBySpuIds(groupSpuIds);
            if (!responseEntity.isSuccess()) {
                throw new LuckException(responseEntity.getMsg());
            }
        }
    }


    @Override
    public void offlineCommissionSpuStatusAndActivity(Integer type, List<Long> spuIds, Long shopId, List<Long> categoryIds, Integer status, Integer sysType) {
        // 查询出所有的启用商品进行修改
        List<SpuVO> spuList = spuMapper.getActivitySpuInfoByCategoryIdsAndShopId(type, spuIds, shopId, categoryIds, status, null);
        List<Long> seckillSpuIds = new ArrayList<>(Constant.INITIAL_CAPACITY);
        List<Long> groupSpuIds = new ArrayList<>(Constant.INITIAL_CAPACITY);
        for (SpuVO spuVO : spuList) {
            if (Objects.equals(spuVO.getSpuType(), SpuType.SECKILL.value())) {
                spuVO.setSpuType(SpuType.NORMAL.value());
                spuVO.setActivityId(0L);
                seckillSpuIds.add(spuVO.getSpuId());
            }
            if (Objects.equals(spuVO.getSpuType(), SpuType.GROUP.value())) {
                spuVO.setSpuType(SpuType.NORMAL.value());
                spuVO.setActivityId(0L);
                groupSpuIds.add(spuVO.getSpuId());
            }
            // 商品那边可能有平台下架和商家下架，其余都为0，商家下架
            if (type != 1) {
                spuVO.setStatus(0);
            }
        }
        if (CollectionUtil.isEmpty(spuList)) {
            return;
        }
        spuMapper.updateBatch(spuList);
        // 下线秒杀、团购活动
        offlineActivityBySpuIds(seckillSpuIds, groupSpuIds);
        // 更新品牌商品数量
        spuIds = updateSpuCount(spuList);
        // 下线分销商品
        ServerResponseEntity<Void> responseEntity = distributionFeignClient.handleSpuState(spuIds, Collections.singletonList(shopId), StatusEnum.DISABLE.value());
        if (!responseEntity.isSuccess()) {
            throw new LuckException(responseEntity.getMsg());
        }
    }
}
