
package com.tmerclub.cloud.product.feign;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import com.tmerclub.cloud.api.multishop.bo.EsShopDetailBO;
import com.tmerclub.cloud.api.multishop.constant.ShopStatus;
import com.tmerclub.cloud.api.multishop.feign.ShopDetailFeignClient;
import com.tmerclub.cloud.api.product.feign.ProductFeignClient;
import com.tmerclub.cloud.api.supplier.bo.SupplierDetailBO;
import com.tmerclub.cloud.api.supplier.feign.SupplierDetailFeignClient;
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.bo.EsProductBO;
import com.tmerclub.cloud.common.product.constant.SupplierSpuType;
import com.tmerclub.cloud.common.product.vo.SpuVO;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.product.service.*;
import com.tmerclub.cloud.product.service.SkuService;
import com.tmerclub.cloud.product.service.SpuEsSearchService;
import com.tmerclub.cloud.product.service.SpuService;
import com.tmerclub.cloud.product.service.SpuTagReferenceService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author tmerclub
 * @date 2020/11/27
 */
@DubboService
public class ProductFeignController implements ProductFeignClient {

    @Autowired
    private SpuService spuService;
    @Autowired
    private SkuService skuService;
    @Autowired
    private SpuEsSearchService spuEsSearchService;
    @Autowired
    private SpuTagReferenceService spuTagReferenceService;
    @DubboReference
    private ShopDetailFeignClient shopDetailFeignClient;
    @DubboReference
    private SupplierDetailFeignClient supplierDetailFeignClient;

    @Override
    public ServerResponseEntity<EsProductBO> loadEsProductBO(Long spuId) {
        EsProductBO esProductBO = spuEsSearchService.loadEsProductBO(spuId);
        List<Long> spuTagIds = spuTagReferenceService.tagListBySpuId(spuId);
        if (CollUtil.isNotEmpty(spuTagIds)) {
            esProductBO.setTagIds(spuTagIds);
        }
        //如果是供应商品 获取供应商名称
        if (Objects.equals(esProductBO.getSupplierSpuType(), SupplierSpuType.YES.value())) {
            ServerResponseEntity<SupplierDetailBO> supplierEntity = supplierDetailFeignClient.getSupplierBySupplierId(esProductBO.getSupplierId());
            if (!supplierEntity.isSuccess()) {
                throw new LuckException(supplierEntity.getMsg());
            }
            SupplierDetailBO supplier = supplierEntity.getData();
            esProductBO.setSupplierName(supplier.getSupplierName());
        } else {
            // 获取店铺信息
            if (!Objects.equals(esProductBO.getShopId(), Constant.ZERO_LONG)) {
                ServerResponseEntity<EsShopDetailBO> shopDetailResponse = shopDetailFeignClient.getShopByShopId(esProductBO.getShopId());
                if (!shopDetailResponse.isSuccess()) {
                    throw new LuckException(shopDetailResponse.getMsg());
                }
                EsShopDetailBO shopDetail = shopDetailResponse.getData();
                if (esProductBO.getAppDisplay() && !Objects.equals(shopDetail.getShopStatus(), ShopStatus.OPEN.value())) {
                    esProductBO.setAppDisplay(Boolean.FALSE);
                }
                esProductBO.setShopName(shopDetail.getShopName());
                esProductBO.setShopImg(shopDetail.getShopLogo());
                esProductBO.setShopType(shopDetail.getType());
            }
        }
        if (Objects.isNull(esProductBO.getCommentNum())) {
            esProductBO.setCommentNum(0);
        }
        if (Objects.isNull(esProductBO.getSaleNum())) {
            esProductBO.setSaleNum(0);
        }
        if (CollUtil.isEmpty(esProductBO.getPartyCodes())) {
            esProductBO.setPartyCodes(null);
        }
        if (CollUtil.isEmpty(esProductBO.getModelIds())) {
            esProductBO.setModelIds(null);
        }
        return ServerResponseEntity.success(esProductBO);
    }

    @Override
    public ServerResponseEntity<List<Long>> getSpuIdsByShopCategoryIds(List<Long> shopCategoryIds) {
        return getSpuIdsBySpuUpdateDTO(shopCategoryIds, null, null, null);
    }

    @Override
    public ServerResponseEntity<List<Long>> getSpuIdsByCategoryIds(List<Long> categoryIds) {
        return getSpuIdsBySpuUpdateDTO(null, categoryIds, null, null);
    }

    @Override
    public ServerResponseEntity<List<Long>> getSpuIdsByBrandId(Long brandId) {
        return getSpuIdsBySpuUpdateDTO(null, null, brandId, null);
    }


    @Override
    public ServerResponseEntity<List<SpuVO>> listSpuIdsBySupplierIdAndSupplierSpuType(Long supplierId, String createTime, Integer supplierSpuType) {
        Date spuCreateTime = Objects.nonNull(createTime) ? DateUtil.parse(createTime, "yyyy-MM-dd") : null;
        return ServerResponseEntity.success(spuService.getSpuIdsBySupplierIdAndSupplierSpuType(supplierId, spuCreateTime, supplierSpuType));
    }

    @Override
    public List<SpuVO> listMerchantSpuIdsBySupplierId(Long supplierId) {
        return spuService.listMerchantSpuIdsBySupplierId(supplierId);
    }

    @Override
    public ServerResponseEntity<List<SpuVO>> listSpuBySpuIds(List<Long> spuIds) {
        return ServerResponseEntity.success(spuService.listSpuDetailBySpuIds(spuIds));
    }

    @Override
    public ServerResponseEntity<List<Long>> getSpuTagBySpuId(Long spuId) {
        return ServerResponseEntity.success(spuTagReferenceService.tagListBySpuId(spuId));
    }

    @Override
    public void handleStatusChange(Long spuId) {
        SpuVO spuVO = spuService.getBySpuId(spuId);
        if (!StatusEnum.offlineStatus(spuVO.getStatus())) {
            spuService.handleStatusChange(Collections.singletonList(spuId));
        }
        skuService.changeSkuStockStatusBySpuId(spuId, spuVO.getStatus());
    }

    /**
     * 获取spuId列表
     *
     * @param shopCategoryIds 店铺分类id列表
     * @param categoryIds     平台分类Id列表
     * @param brandId         品牌id
     * @param shopId          店铺id
     * @return
     */
    public ServerResponseEntity<List<Long>> getSpuIdsBySpuUpdateDTO(List<Long> shopCategoryIds, List<Long> categoryIds, Long brandId, Long shopId) {
        List<Long> spuIds = spuService.getSpuIdsBySpuUpdateDTO(shopCategoryIds, categoryIds, brandId, shopId);
        return ServerResponseEntity.success(spuIds);
    }
}
