package com.dsk.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
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<>();
        for (Object dataMap : list) {
            uipIds.add(MapUtils.getString(CommonUtils.assertAsMap(dataMap), "uipId"));
        }
//        List<String> claimStatusList = iCustomerService.selectUipIdList(uipIds);
        List<CustomerStatusListVo> customerStatusListVos = iCustomerService.selectStatusList(uipIds);
        //按照城投企业id合并两个list
        //按照城投企业id合并两个list
        for (Object companyObj : list) {
            Map<String, Object> companyMap = CommonUtils.assertAsMap(companyObj);
            String uipId = MapUtils.getString(companyMap, "uipId");
            companyMap.put("claimStatus", 0);
            for (CustomerStatusListVo vo : customerStatusListVos) {
                if (uipId.equals(vo.getUipId())) {
                    companyMap.put("claimStatus", 1);
                }
            }
        }

//        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);
    }
}
