package com.dsk.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson2.JSONObject;
import com.dsk.acc.openapi.client.util.CommonUtils;
import com.dsk.common.core.domain.AjaxResult;
import com.dsk.common.core.domain.model.EnterpriseInfoHeaderBody;
import com.dsk.common.utils.DskOpenApiUtil;
import com.dsk.system.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.List;
import java.util.Map;

/**
 * @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(JSONObject object) {
        Map<String, Object> map = dskOpenApiUtil.requestBody("/nationzj/enterprice/page",object);
        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", ""));
            if (CollectionUtils.isNotEmpty(list)) {
                for (Object companyObj : list) {
                    Map<String, Object> companyMap = CommonUtils.assertAsMap(companyObj);
                    //常合作客户id
                    Integer topCustomerId = MapUtils.getInteger(companyMap, "topCustomerId");
                    EnterpriseInfoHeaderBody body = new EnterpriseInfoHeaderBody();
                    body.setCompanyId(topCustomerId);
                    if (ObjectUtil.isNotEmpty(topCustomerId)) {
                        Map<String, Object> infoHeaderMap = dskOpenApiUtil.requestBody("/api/jsk/enterprise/infoHeader", BeanUtil.beanToMap(body, false, false));
                        Integer infoHeaderCode = MapUtils.getInteger(infoHeaderMap, "code", 300);
                        if (infoHeaderCode.equals(HttpStatus.OK.value())) {
                            Map infoHeaderData = MapUtils.getMap(infoHeaderMap, "data", null);
                            String companyName = MapUtils.getString(infoHeaderData, "companyName", null);
                            companyMap.put("topCustomer", companyName);
                        } else {
                            companyMap.put("topCustomer", null);
                        }
                    } else {
                        companyMap.put("topCustomer", null);
                    }
                    //常合作供应商id
                    Integer topSupplierId = MapUtils.getInteger(companyMap, "topSupplierId");
                    body.setCompanyId(topSupplierId);
                    if (ObjectUtil.isNotEmpty(topSupplierId)) {
                        Map<String, Object> infoHeaderMap = dskOpenApiUtil.requestBody("/api/jsk/enterprise/infoHeader", BeanUtil.beanToMap(body, false, false));
                        Integer infoHeaderCode = MapUtils.getInteger(infoHeaderMap, "code", 300);
                        if (infoHeaderCode.equals(HttpStatus.OK.value())) {
                            Map infoHeaderData = MapUtils.getMap(infoHeaderMap, "data", null);
                            String companyName = MapUtils.getString(infoHeaderData, "companyName", null);
                            companyMap.put("topSupplier", companyName);
                        } else {
                            companyMap.put("topSupplier", null);
                        }
                    } else {
                        companyMap.put("topSupplier", null);
                    }
                }
            }
        }
        return BeanUtil.toBean(map, AjaxResult.class);
    }
}
