
package com.tmerclub.cloud.product.feign;

import cn.hutool.core.collection.CollectionUtil;
import com.tmerclub.cloud.api.auth.constant.SysTypeEnum;
import com.tmerclub.cloud.api.product.bo.PlatformCommissionItemBO;
import com.tmerclub.cloud.api.product.bo.PlatformCommissionOrderItemBO;
import com.tmerclub.cloud.api.product.dto.CategoryShopDTO;
import com.tmerclub.cloud.api.product.feign.CategoryShopFeignClient;
import com.tmerclub.cloud.common.product.vo.CategoryVO;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.product.bo.CategoryRateBO;
import com.tmerclub.cloud.product.mapper.CategoryMapper;
import com.tmerclub.cloud.product.service.CategoryService;
import com.tmerclub.cloud.product.service.CategoryShopService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

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

/**
 * @author tmerclub
 * @date 2021/5/24
 */
@DubboService
public class CategoryShopFeignController implements CategoryShopFeignClient {

    @Autowired
    private CategoryShopService categoryShopService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public ServerResponseEntity<List<PlatformCommissionOrderItemBO>> calculatePlatformCommission(List<PlatformCommissionOrderItemBO> platformCommissionOrderItems) {

        // 如果为空表示为从店铺改价过来，需要先获取分类ids
        if (Objects.isNull(platformCommissionOrderItems.get(0).getCategoryId())) {
            List<PlatformCommissionOrderItemBO> rateOrderItemList = categoryService.listBySkuIds(platformCommissionOrderItems);
            Map<Long, Long> rateMap = rateOrderItemList.stream().collect(Collectors.toMap(PlatformCommissionOrderItemBO::getSkuId, PlatformCommissionOrderItemBO::getCategoryId));
            for (PlatformCommissionOrderItemBO platformCommissionOrderItem : platformCommissionOrderItems) {
                platformCommissionOrderItem.setCategoryId(rateMap.get(platformCommissionOrderItem.getSkuId()));
            }
        }

        for (PlatformCommissionOrderItemBO platformCommissionOrderItem : platformCommissionOrderItems) {

            // 处理商家端的分类关联,获取店铺各个分类的费率
            PlatformCommissionItemBO shopItem = platformCommissionOrderItem.getShopItem();
            List<CategoryRateBO> shopCategoryRates = categoryShopService.listRateByShopId(shopItem.getShopId(), SysTypeEnum.MULTISHOP.value());
            if (CollectionUtil.isEmpty(shopCategoryRates)) {
                continue;
            }
            for (CategoryRateBO shopCategoryRate : shopCategoryRates) {
                if (Objects.equals(platformCommissionOrderItem.getCategoryId(), shopCategoryRate.getCategoryId()) && shopCategoryRate.getRate() != null) {
                    platformCommissionOrderItem.setRate(shopCategoryRate.getRate());
                }
            }

            if (Objects.isNull(platformCommissionOrderItem.getSupplierItem())) {
                continue;
            }

            // 处理供应商端的分类关联
            // 获取供应商各个分类的费率
            PlatformCommissionItemBO supplierItem = platformCommissionOrderItem.getSupplierItem();
            List<CategoryRateBO> supplierCategoryRates = categoryShopService.listRateByShopId(supplierItem.getShopId(), SysTypeEnum.SUPPLIER.value());
            if (CollectionUtil.isEmpty(supplierCategoryRates)) {
                continue;
            }
            for (CategoryRateBO supplierCategoryRate : supplierCategoryRates) {
                // 分类id相同，用平台的佣金
                if (Objects.equals(platformCommissionOrderItem.getCategoryId(), supplierCategoryRate.getCategoryId()) && supplierCategoryRate.getRate() != null) {
                    platformCommissionOrderItem.setSupplierRate(supplierCategoryRate.getRate());
                }
            }
        }
        return ServerResponseEntity.success(platformCommissionOrderItems);
    }

    @Override
    public ServerResponseEntity<List<Long>> getSupplierIdsByShopId(Long shopId) {
        return ServerResponseEntity.success(categoryShopService.getSupplierIdsByShopId(shopId));
    }

    @Override
    public ServerResponseEntity<List<Long>> getCategoryIdsByShopId(Long shopId, Integer sysType) {
        return ServerResponseEntity.success(categoryShopService.getCategoryIdsByShopId(shopId, sysType));
    }

    @Override
    public ServerResponseEntity<Void> insertBatchByShopId(List<CategoryShopDTO> categoryShopDTOList, Long shopId, Integer sysType) {
        categoryShopService.insertBatchByShopId(categoryShopDTOList, shopId, sysType);
        return ServerResponseEntity.success();
    }

    @Override
    public ServerResponseEntity<List<Long>> getOneCategoryIdByShopId(Long shopId, Integer sysType, Integer lang) {
        List<CategoryVO> threeCategoryList = categoryMapper.listSigningCategoryByShopIdAndStatus(shopId, sysType, null);
        if (Objects.isNull(threeCategoryList) || threeCategoryList.size() == 0) {
            // 签约分类为空时
            return ServerResponseEntity.success(null);
        }
        return ServerResponseEntity.success(threeCategoryList.stream().map(CategoryVO::getCategoryId).collect(Collectors.toList()));
    }

    @Override
    public ServerResponseEntity<List<Long>> getSupplyIdByCategoryIds(List<Long> categoryIds) {
        return ServerResponseEntity.success(categoryShopService.getSupplyIdByCategoryIds(categoryIds));
    }
}
