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.jsk.domain.bo.UrbanInvestmentPlatformDto;
import com.dsk.jsk.service.service.UrbanInvestmentPlatformService;
import com.dsk.system.utils.DskOpenApiUtil;
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;

    @Override
    public AjaxResult page(UrbanInvestmentPlatformDto pageDto) {
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/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.replaceAll("<font color='red'>","");
                name = name.replaceAll("</font>","");
                companyNames.add(name);
            } else {
                companyNames.add(name);
            }
            cIds.add(MapUtils.getLong(CommonUtils.assertAsMap(dataMap), "companyId"));
        }

        //按照城投企业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.replaceAll("<font color='red'>","");
                companyName = companyName.replaceAll("</font>","");
            }
            companyMap.put("claimStatus", null);
        }
        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("/operate/urbanInvestment/details", bodyMap);
        return BeanUtil.toBean(map, AjaxResult.class);
    }

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

    @Override
    public AjaxResult esportPre(UrbanInvestmentPlatformDto pageDto) {
        pageDto.setPageNum(1);
        pageDto.setPageSize(0);
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/urbanInvestment/page", BeanUtil.beanToMap(pageDto, false, false));
        Integer code = MapUtils.getInteger(map, "code", 300);
        if (HttpStatus.OK.value() != code) {
            throw new RuntimeException();
        }
        Integer totalCount = MapUtils.getInteger(map, "totalCount");
        HashMap<String, Object> resMap = new HashMap<>();
        resMap.put("totalCount",totalCount);
        return BeanUtil.toBean(resMap, AjaxResult.class);
    }

}
