package com.dsk.system.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.dtos.UrbanInvestmentPlatformDto;
import com.dsk.common.dtos.UrbanInvestmentPlatformSubjectLevelDto;
import com.dsk.common.utils.DskOpenApiUtil;
import com.dsk.system.domain.customer.vo.CustomerStatusListVo;
import com.dsk.system.service.ICustomerService;
import com.dsk.system.service.UrbanInvestmentPlatformService;
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 EconomicServiceImpl
 * @Description 城投平台业务层实现
 * @Author Dgm
 * @Date 2023/5/18 10:23
 * @Version
 */
@Service
public class UrbanInvestmentPlatformServiceImpl implements UrbanInvestmentPlatformService {

    @Autowired
    private DskOpenApiUtil dskOpenApiUtil;

    @Autowired
    private ICustomerService iCustomerService;

    @Override
    public AjaxResult page(UrbanInvestmentPlatformDto pageDto) {
        Map<String, Object> map = dskOpenApiUtil.requestBody("/urbanInvestment/page", BeanUtil.beanToMap(pageDto, false, false));
        Integer code = MapUtils.getInteger(map, "code", 300);
        if (HttpStatus.OK.value() != code) {
            throw new RuntimeException();
        }
        Map data = MapUtils.getMap(map, "data", null);
        List<Object> list = CommonUtils.assertAsArray(MapUtils.getObject(data, "list", ""));
        if (CollectionUtils.isEmpty(list)) {
            return BeanUtil.toBean(map, AjaxResult.class);
        }

        ArrayList<String> companyNames = new ArrayList<>();
        ArrayList<Long> cIds = new ArrayList<>();
        for (Object dataMap : list) {
            String name = MapUtils.getString(CommonUtils.assertAsMap(dataMap), "companyName");
            if (ObjectUtil.isNotEmpty(name) && ObjectUtil.isNotEmpty(pageDto.getKeyword())) {
                name = name.replace("<font color='red'>","");
                name = name.replace("</font>","");
                companyNames.add(name);
            } else {
                companyNames.add(name);
            }
            cIds.add(MapUtils.getLong(CommonUtils.assertAsMap(dataMap), "companyId"));
        }
        List<CustomerStatusListVo> customerStatusListVos = iCustomerService.selectStatusListByCompanyName(companyNames);

        Map<String, Object> bondCreditRatingBatchMap = new HashMap<>(1);
        bondCreditRatingBatchMap.put("cIds", cIds);
        Map<String, Object> bondCreditRatingMap = dskOpenApiUtil.requestBody("/operate/enterprise/bondCreditRatingBatch", bondCreditRatingBatchMap);
        Integer codeRating = MapUtils.getInteger(bondCreditRatingMap, "code", 300);
        ArrayList<Map<String, Object>> objArrayList = new ArrayList(1);
        if (codeRating.equals(HttpStatus.OK.value())) {
            Object object = bondCreditRatingMap.get("data");
            if (ObjectUtil.isNotEmpty(object)) {
                objArrayList = (ArrayList)object;
            }
        }

        //按照城投企业id合并两个list
        //按照城投企业id合并两个list
        for (Object companyObj : list) {
            Map<String, Object> companyMap = CommonUtils.assertAsMap(companyObj);
            Long companyId = MapUtils.getLong(companyMap, "companyId");
            String uipId = MapUtils.getString(companyMap, "uipId");
            String companyName = MapUtils.getString(companyMap, "companyName", "");
            if (ObjectUtil.isNotEmpty(companyName) && ObjectUtil.isNotEmpty(pageDto.getKeyword())) {
                companyName = companyName.replace("<font color='red'>","");
                companyName = companyName.replace("</font>","");
            }
            companyMap.put("claimStatus", null);
            companyMap.put("bratingSubjectLevel", null);
            for (CustomerStatusListVo vo : customerStatusListVos) {
                if (companyName.equals(vo.getCompanyName())) {
                    companyMap.put("claimStatus", vo.getStatus());
                }
            }
            // 去除重复的数据
            Set<UrbanInvestmentPlatformSubjectLevelDto> subjectLevelDtoSet = new HashSet<>();
            for (Map<String, Object> m : objArrayList) {
                UrbanInvestmentPlatformSubjectLevelDto dto = new UrbanInvestmentPlatformSubjectLevelDto();
                Long companyIdCredit = MapUtils.getLong(m, "companyId", 0L);
                String rate = MapUtils.getString(m, "bratingSubjectLevel");
                dto.setCompanyId(companyIdCredit);
                dto.setBratingSubjectLevel(rate);
                subjectLevelDtoSet.add(dto);
            }
            for (UrbanInvestmentPlatformSubjectLevelDto dto : subjectLevelDtoSet) {
                if (dto.getCompanyId().equals(companyId)) {
                    companyMap.put("bratingSubjectLevel", dto.getBratingSubjectLevel());
                }
            }
        }
        return BeanUtil.toBean(map, AjaxResult.class);
    }

    @Override
    public AjaxResult details(String id) {
        Map<String, Object> bodyMap = new HashMap<>(1);
        bodyMap.put("id", id);
        Map<String, Object> map = dskOpenApiUtil.requestBody("/urbanInvestment/details", bodyMap);
        return BeanUtil.toBean(map, AjaxResult.class);
    }

    @Override
    public AjaxResult statistics(UrbanInvestmentPlatformDto dto) {
        Map<String, Object> map = dskOpenApiUtil.requestBody("/urbanInvestment/statistics", BeanUtil.beanToMap(dto, false, false));
        return BeanUtil.toBean(map, AjaxResult.class);
    }
}
