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.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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @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> uipIds = new ArrayList<>();
        ArrayList<Long> cIds = new ArrayList<>();
        for (Object dataMap : list) {
            uipIds.add(MapUtils.getString(CommonUtils.assertAsMap(dataMap), "uipId"));
            cIds.add(MapUtils.getLong(CommonUtils.assertAsMap(dataMap), "companyId"));
        }
//        List<String> claimStatusList = iCustomerService.selectUipIdList(uipIds);
        List<CustomerStatusListVo> customerStatusListVos = iCustomerService.selectStatusList(uipIds);

        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");
            companyMap.put("claimStatus", 0);
            companyMap.put("bratingSubjectLevel", null);
            for (CustomerStatusListVo vo : customerStatusListVos) {
                if (uipId.equals(vo.getUipId())) {
                    companyMap.put("claimStatus", 1);
                }
            }

            for (Map<String, Object> m : objArrayList) {
                Long companyIdCredit = MapUtils.getLong(m, "companyId", 0L);
                if (companyIdCredit.equals(companyId)) {
                    companyMap.put("bratingSubjectLevel", m.get("bratingSubjectLevel"));
                }
            }
        }

//        for (Object companyObj : list) {
//            Map<String, Object> companyMap = CommonUtils.assertAsMap(companyObj);
//            String uipId = MapUtils.getString(companyMap, "uipId","uipId");
//            if (CollectionUtils.isEmpty(claimStatusList)) {
//                companyMap.put("claimStatus", 0);
//            } else if (claimStatusList.contains(uipId)) {
//                companyMap.put("claimStatus", 1);
//            } else {
//                companyMap.put("claimStatus", 0);
//            }
//        }
        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);
    }
}
