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.system.utils.DskOpenApiUtil;
import com.dsk.jsk.domain.bo.SpecialBondInformationDetailsDto;
import com.dsk.jsk.domain.bo.SpecialBondInformationPageDto;
import com.dsk.jsk.domain.bo.SpecialPurposeBondsDto;
import com.dsk.jsk.domain.bo.SpecialPurposeBondsPageDto;
import com.dsk.jsk.service.service.SpecialPurposeBondsService;
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.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @ClassName EconomicServiceImpl
 * @Description 专项债项目业务层实现
 * @Author Dgm
 * @Date 2023/5/18 10:23
 * @Version
 */
@Service
public class SpecialPurposeBondsServiceImpl implements SpecialPurposeBondsService {

    @Autowired
    private DskOpenApiUtil dskOpenApiUtil;

    @Override
    public AjaxResult page(SpecialPurposeBondsPageDto dto) {
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/specialPurposeBonds/projects/page", BeanUtil.beanToMap(dto, 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", ""));
            if (CollectionUtils.isNotEmpty(list)) {
                for (Object projectObj : list) {
                    Map<String, Object> projectMap = CommonUtils.assertAsMap(projectObj);
                    // 项目总投资
                    Double projectTotalInvestment = MapUtils.getDouble(projectMap, "projectTotalInvestment",0.00);
                    projectMap.put("projectTotalInvestment", projectTotalInvestment / 10000);
                    // 专项债用作资本金
                    Double specialBondCapital = MapUtils.getDouble(projectMap, "specialBondCapital",0.00);
                    projectMap.put("specialBondCapital", specialBondCapital / 10000);
                    // 项目资本金
                    Double projectCapital = MapUtils.getDouble(projectMap, "projectCapital",0.00);
                    projectMap.put("projectCapital", projectCapital / 10000);
                }
            }
        }
        return BeanUtil.toBean(map, AjaxResult.class);
    }

    @Override
    public AjaxResult details(SpecialBondInformationDetailsDto detailsDto) {
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/specialPurposeBonds/details", BeanUtil.beanToMap(detailsDto, false, false));
        Integer code = MapUtils.getInteger(map, "code", 300);
        if (code.equals(HttpStatus.OK.value())) {
            Map projectMap = MapUtils.getMap(map, "data", null);
            if (ObjectUtil.isNotEmpty(projectMap)) {
                // 项目总投资
                Double projectTotalInvestment = MapUtils.getDouble(projectMap, "projectTotalInvestment",0.00);
                projectMap.put("projectTotalInvestment", projectTotalInvestment / 10000);
                // 专项债用作资本金
                Double specialBondCapital = MapUtils.getDouble(projectMap, "specialBondCapital",0.00);
                projectMap.put("specialBondCapital", specialBondCapital / 10000);
                // 项目资本金
                Double projectCapital = MapUtils.getDouble(projectMap, "projectCapital",0.00);
                projectMap.put("projectCapital", projectCapital / 10000);
                // 其他资金
                Double otherFunds = MapUtils.getDouble(projectMap, "otherFunds",0.00);
                projectMap.put("otherFunds", otherFunds / 10000);
                // 计划使用专项债融资额
                Double econData012 = MapUtils.getDouble(projectMap, "econData012",0.00);
                projectMap.put("econData012", econData012 / 10000);
                // 当前使用专项债融资额
                Double econData013 = MapUtils.getDouble(projectMap, "econData013",0.00);
                projectMap.put("econData013", econData013 / 10000);
                // 其他融资
                Double otherFinancing = MapUtils.getDouble(projectMap, "otherFinancing",0.00);
                projectMap.put("otherFinancing", otherFinancing / 10000);
                // 政府安排资金
                Double govSupportFunds = MapUtils.getDouble(projectMap, "govSupportFunds",0.00);
                projectMap.put("govSupportFunds", govSupportFunds / 10000);
                // 项目预测总收益
                Double projectForecastTotalRevenue = MapUtils.getDouble(projectMap, "projectForecastTotalRevenue",0.00);
                projectMap.put("projectForecastTotalRevenue", projectForecastTotalRevenue / 10000);
            }
        }
        return BeanUtil.toBean(map, AjaxResult.class);
    }

    @Override
    public AjaxResult bondStatistics(SpecialPurposeBondsDto dto) {
        // todo 临时处理 待还原
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/specialPurposeBonds/bond/statistics", BeanUtil.beanToMap(dto, false, false));
        Integer code = MapUtils.getInteger(map, "code", 300);
        if (code.equals(HttpStatus.OK.value())) {
            List<Object> list = CommonUtils.assertAsArray(MapUtils.getObject(map, "data", ""));
            Iterator<Object> it = list.iterator();
            while (it.hasNext()) {
                Map<String, Object> informationMap = (Map<String, Object>)it.next();
                // 专项债用于项目规模
                Integer count = MapUtils.getInteger(informationMap, "count",0);
                if (count.equals(0)) {
                    it.remove();
                }
            }
        }
        return BeanUtil.toBean(map, AjaxResult.class);
    }

    @Override
    public AjaxResult bondPage(SpecialBondInformationPageDto pageDto) {
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/specialPurposeBonds/bond/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", ""));
            if (CollectionUtils.isNotEmpty(list)) {
                for (Object informationObj : list) {
                    Map<String, Object> informationMap = CommonUtils.assertAsMap(informationObj);
                    // 专项债用于项目规模
                    Double isUsedProjectScale = MapUtils.getDouble(informationMap, "isUsedProjectScale",0.00);
                    informationMap.put("isUsedProjectScale", isUsedProjectScale / 10000);
                }
            }
        }
        return BeanUtil.toBean(map, AjaxResult.class);
    }
}
