package com.dsk.jsk.service.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.dsk.acc.openapi.client.util.CommonUtils;
import com.dsk.common.core.domain.AjaxResult;
import com.dsk.common.utils.DskOpenApiUtil;
import com.dsk.jsk.domain.bo.ComposeQueryDto;
import com.dsk.jsk.service.service.RegionalEnterprisesService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @ClassName RegionalEnterprisesServiceImpl
 * @Description 区域企业业务层实现
 * @Author Dgm
 * @Date 2023/5/18 10:23
 * @Version
 */
@Service
public class RegionalEnterprisesServiceImpl implements RegionalEnterprisesService {

    @Autowired
    private DskOpenApiUtil dskOpenApiUtil;

    @Override
    public AjaxResult enterprisePage(ComposeQueryDto pageDto) {
        Map<String, Object> map = dskOpenApiUtil.requestBody("/nationzj/enterprice/page",BeanUtil.beanToMap(pageDto, false, false));
        Integer code = MapUtils.getInteger(map, "code", 300);
        if (code.equals(HttpStatus.OK.value())) {
            Map data = MapUtils.getMap(map, "data", null);
            List<Object> list = CommonUtils.assertAsArray(MapUtils.getObject(data, "list", ""));
            // 常合作客户
            List<Integer> getTopCustomerIds = getTopCustomerIds(list);
            Map<String,Object> topCustomerIdsMap = new HashMap<>(1);
            topCustomerIdsMap.put("ids", getTopCustomerIds);
            Map<String, Object> topCustomerMap = dskOpenApiUtil.requestBody("/operate/enterprise/selectCompanyNameList", topCustomerIdsMap);
            // 常合作供应商
            List<Integer> getTopSupplierIds = getTopSupplierIds(list);
            Map<String,Object> topSupplierIdsMap = new HashMap<>(1);
            topSupplierIdsMap.put("ids", getTopSupplierIds);
            Map<String, Object> topSupplierMap = dskOpenApiUtil.requestBody("/operate/enterprise/selectCompanyNameList", topSupplierIdsMap);
            if (CollectionUtils.isNotEmpty(list)) {
                for (Object companyObj : list) {
                    Map<String, Object> companyMap = CommonUtils.assertAsMap(companyObj);
                    companyMap.put("topCustomer", null);
                    //常合作客户id
                    Integer topCustomerId = MapUtils.getInteger(companyMap, "topCustomerId");
                    if (ObjectUtil.isNotEmpty(topCustomerId)) {
                        Integer topCustomerCode = MapUtils.getInteger(topCustomerMap, "code", 300);
                        if (topCustomerCode.equals(HttpStatus.OK.value())) {
                            List<Map<String, Object>> companyNameList = (List<Map<String, Object>>)MapUtils.getObject(topCustomerMap, "data", null);
                            for (Map<String, Object> comMap : companyNameList) {
                                Integer id  = MapUtils.getInteger(comMap, "id", 0);
                                if (id.equals(topCustomerId)) {
                                    String companyName = MapUtils.getString(comMap, "companyName", null);
                                    companyMap.put("topCustomer", companyName);
                                }
                            }
                        }
                    }
                    //常合作供应商id
                    Integer topSupplierId = MapUtils.getInteger(companyMap, "topSupplierId");
                    companyMap.put("topSupplier", null);
                    if (ObjectUtil.isNotEmpty(topSupplierId)) {
                        Integer topSupplierCode = MapUtils.getInteger(topSupplierMap, "code", 300);
                        if (topSupplierCode.equals(HttpStatus.OK.value())) {
                            List<Map<String, Object>> companyNameList = (List<Map<String, Object>>)MapUtils.getObject(topSupplierMap, "data", null);
                            for (Map<String, Object> comMap : companyNameList) {
                                Integer id  = MapUtils.getInteger(comMap, "id", 0);
                                if (id.equals(topSupplierId)) {
                                    String companyName = MapUtils.getString(comMap, "companyName", null);
                                    companyMap.put("topSupplier", companyName);
                                }
                            }
                        }
                    }
                }
            }
        }
        return BeanUtil.toBean(map, AjaxResult.class);
    }

    /***
     *@Description: 获取企业Id-客户
     *@Param:
     *@return: java.util.List<java.lang.Integer>
     *@Author: Dgm
     *@date: 2023/7/11 16:04
     */
    public List<Integer> getTopCustomerIds(List<Object> list) {
        List<Integer> getIds = new ArrayList<>(20);
        for (Object companyObj : list) {
            Map<String, Object> companyMap = CommonUtils.assertAsMap(companyObj);
            //常合作客户id
            Integer topCustomerId = MapUtils.getInteger(companyMap, "topCustomerId");
            getIds.add(topCustomerId);
        }
        return getIds;
    }


    /***
     *@Description: 获取企业Id-供应商
     *@Param:
     *@return: java.util.List<java.lang.Integer>
     *@Author: Dgm
     *@date: 2023/7/11 16:04
     */
    public List<Integer> getTopSupplierIds(List<Object> list) {
        List<Integer> getIds = new ArrayList<>(20);
        for (Object companyObj : list) {
            Map<String, Object> companyMap = CommonUtils.assertAsMap(companyObj);
            //常合作供应商id
            Integer topSupplierId = MapUtils.getInteger(companyMap, "topSupplierId");
            getIds.add(topSupplierId);
        }
        return getIds;
    }
}
