package com.gic.haoban.manage.web.controller.goods;

import com.alibaba.fastjson.JSON;
import com.gic.api.base.commons.Page;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.haoban.app.aggregation.api.dto.GoodsSettingDTO;
import com.gic.haoban.app.aggregation.api.service.SettingApiService;
import com.gic.store.goods.dto.goods.GoodsSpuInfoDTO;
import com.gic.store.goods.dto.qdto.GoodsListQDTO;
import com.gic.store.goods.service.GoodsCenterApiService;
import com.gic.store.goods.service.GoodsInfoOutApiService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @Author MUSI
 * @Date 2023/3/16 10:19 PM
 * @Description
 * @Version
 **/

@Component
public class GoodsInfoAdaptor {

    private static final Logger log = LoggerFactory.getLogger(GoodsInfoAdaptor.class);

    @Autowired
    private SettingApiService settingApiService;
    @Autowired
    private GoodsInfoOutApiService goodsInfoOutApiService;
    @Autowired
    private GoodsCenterApiService goodsCenterApiService;

    public List<String> queryGoodsIdWithSearch(String enterpriseId, String search) {
        // 根据关键字查询1000个商品
        GoodsListQDTO searchQdto = new GoodsListQDTO();
        searchQdto.setEnterpriseId(enterpriseId);
        searchQdto.setGoodsNameText(search);
        searchQdto.setPageNum(1);
        searchQdto.setPageSize(1000);
        ServiceResponse<Page<GoodsSpuInfoDTO>> serviceResponse = goodsInfoOutApiService.queryGoodsInfoList(searchQdto);
        if (!serviceResponse.isSuccess()) {
            log.info("查询商品异常 {}", JSON.toJSONString(serviceResponse));
            return Collections.emptyList();
        }
        if (serviceResponse.getResult() == null ||
                CollectionUtils.isEmpty(serviceResponse.getResult().getResult())) {
            log.info("查询商品异常 {}", JSON.toJSONString(serviceResponse));
            return Collections.emptyList();
        }
        return serviceResponse
                .getResult()
                .getResult()
                .stream()
                .map(GoodsSpuInfoDTO::getGoodsId)
                .collect(Collectors.toList());
    }

    /**
     * 查询好办商品展示的商品状态
     *
     * @param enterpriseId
     * @return
     */
    public List<Integer> getGoodsShowStatus(String enterpriseId) {
        if (StringUtils.isBlank(enterpriseId)) {
            return Collections.emptyList();
        }
        GoodsSettingDTO goodsSettingDTO = settingApiService.getSetting(enterpriseId);
        List<Integer> showStatus = new ArrayList<>();
        if (goodsSettingDTO != null) {
            int salesGoodsFlag = goodsSettingDTO.getSalesGoodsFlag();
            int notSalesGoodsFlag = goodsSettingDTO.getNotSalesGoodsFlag();
            if (salesGoodsFlag == 1 && notSalesGoodsFlag == 1) {
                //已上架和未上架
                showStatus.add(1);
                showStatus.add(2);
            } else if (salesGoodsFlag == 1) {
                //已上架
                showStatus.add(1);
            } else if (notSalesGoodsFlag == 1) {
                //未上架
                showStatus.add(2);
            } else {
                //啥都未打开
                showStatus.add(-99);
            }
        } else {
            // 默认展示已上架和未上架的数据
            showStatus.add(1);
            showStatus.add(2);
        }
        return showStatus;
    }

    /**
     * 查询商品最小价格
     * @param enterpriseId
     * @param goodsIds
     * @return
     */
    public Map<String, GoodsSpuInfoDTO> queryGoodsMinPrice(String enterpriseId, List<String> goodsIds) {

        if (org.springframework.util.CollectionUtils.isEmpty(goodsIds)) {
            return Collections.emptyMap();
        }
        ServiceResponse<List<GoodsSpuInfoDTO>> serviceResponse = goodsCenterApiService.queryGoodsMinPrice(enterpriseId, goodsIds);
        if (!serviceResponse.isSuccess() || com.alibaba.dubbo.common.utils.CollectionUtils.isEmpty(serviceResponse.getResult())) {
            log.info("查询商品最小价格异常 {}", JSON.toJSONString(serviceResponse));
            return Collections.emptyMap();
        }
        return serviceResponse
                .getResult()
                .stream()
                .collect(Collectors.toMap(GoodsSpuInfoDTO::getGoodsId, Function.identity()));
    }
}
