package com.dsk.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.dsk.common.constant.CacheConstants;
import com.dsk.common.core.domain.AjaxResult;
import com.dsk.common.core.redis.RedisCache;
import com.dsk.common.dtos.*;
import com.dsk.common.utils.DateUtils;
import com.dsk.common.utils.DskOpenApiUtil;
import com.dsk.common.utils.SecurityUtils;
import com.dsk.system.service.EconomicService;
import org.apache.commons.collections4.MapUtils;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName EconomicServiceImpl
 * @Description 经济大全实现
 * @Author Dgm
 * @Date 2023/5/18 10:23
 * @Version
 */
@Service
public class EconomicServiceImpl implements EconomicService {

    @Resource
    private DskOpenApiUtil dskOpenApiUtil;

    @Resource
    private RedisCache redisCache;

    @Override
    public AjaxResult nationalPage(OpRegionalEconomicDataV1PageDto dto) {
        if (ObjectUtil.isEmpty(dto.getYear())) {
            dto.setYear(DateUtils.getYear() - 1);
        }
        Map<String, Object> map = dskOpenApiUtil.requestBody("/economic/national/nationalPage",  BeanUtil.beanToMap(dto, false, false));
        return BeanUtil.toBean(map, AjaxResult.class);
    }

    @Override
    public AjaxResult yearsList(OpRegionalEconomicDataYearsListDto dto) {
        Map<String, Object> map = dskOpenApiUtil.requestBody("/economic/years/list",  BeanUtil.beanToMap(dto, false, false));
        return BeanUtil.toBean(map, AjaxResult.class);
    }

    @Override
    public AjaxResult details(OpRegionalEconomicDataDetailsDto detailsDto) {
        Map<String, Object> map = dskOpenApiUtil.requestBody("/economic/details", BeanUtil.beanToMap(detailsDto, false, false));
        return BeanUtil.toBean(map, AjaxResult.class);
    }

    @Override
    public AjaxResult location(OpRegionalLocalDto detailsDto) {
        Long userId = SecurityUtils.getLoginUser().getUserId();
        String redisKey = CacheConstants.PERSONAL_LOCATION + userId;
        if (ObjectUtil.isEmpty(detailsDto.getProvinceId()) && ObjectUtil.isEmpty(detailsDto.getCityId()) && ObjectUtil.isEmpty(detailsDto.getAreaId())) {
            if (ObjectUtil.isNotEmpty(redisKey)) {
                Map<String, Object> cacheMap = redisCache.getCacheMap(redisKey);
                if (MapUtils.isNotEmpty(cacheMap)) {
                    return AjaxResult.success(cacheMap);
                }
            }
        }

        Map<String, Object> map = dskOpenApiUtil.requestBody("/economic/location", BeanUtil.beanToMap(detailsDto, false, false));
        Integer code = MapUtils.getInteger(map, "code", 300);
        if (!code.equals(HttpStatus.OK.value())) {
            throw new RuntimeException();
        }

        Map data = MapUtils.getMap(map, "data", null);
        if (ObjectUtil.isNotEmpty(detailsDto.getProvinceId()) || ObjectUtil.isNotEmpty(detailsDto.getCityId()) || ObjectUtil.isNotEmpty(detailsDto.getAreaId())) {
            redisCache.setCacheMap(redisKey, data);
        }
        return BeanUtil.toBean(map, AjaxResult.class);
    }

    @Override
    public AjaxResult statisticsRegional(OpRegionalEconomicDataStatisticsRegionalDto dto) {
        Map<String, Object> map = dskOpenApiUtil.requestBody("/economic/statistics/regional",  BeanUtil.beanToMap(dto, false, false));
        //20230721 TODO:临时代码 待删除
        Integer code = MapUtils.getInteger(map, "code", 300);
        if (!code.equals(HttpStatus.OK.value())) {
            throw new RuntimeException();
        }
        Object data = map.get("data");
        if (ObjectUtil.isNotEmpty(data)) {
            List<Map<String, Object>> mapList = (ArrayList)data;
            for (Map<String, Object> recentlyYearMap : mapList) {
                Map<String, Object> recently = (Map<String, Object>)recentlyYearMap.get("recentlyYear");
                recently.put("gdpGrowthCompare", "-10");
                recently.put("govDebtBalanceCompare", "-10");
                recently.put("gdpCompare", "-10");
                recently.put("populationCompare", "-10");
                recently.put("gbrCompare", "-10");
            }
        }
        return BeanUtil.toBean(map, AjaxResult.class);
    }

    @Override
    public AjaxResult regionalList(OpRegionalEconomicDataRegionalListDto dto) {
        Map<String, Object> map = dskOpenApiUtil.requestBody("/economic/regional/list",  BeanUtil.beanToMap(dto, false, false));
        return BeanUtil.toBean(map, AjaxResult.class);
    }

    @Override
    public AjaxResult regionalCompare(OpRegionalEconomicDataStatisticsRegionalDto dto) {
        Map<String, Object> map = dskOpenApiUtil.requestBody("/economic/regional/compare",  BeanUtil.beanToMap(dto, false, false));
        return BeanUtil.toBean(map, AjaxResult.class);
    }

    @Override
    public AjaxResult regionalMultipleCompare(OpRegionalEconomicRegionalCompareDto dto) {
        Map<String, Object> map = dskOpenApiUtil.requestBody("/economic/regional/multipleCompare",  BeanUtil.beanToMap(dto, false, false));
        return BeanUtil.toBean(map, AjaxResult.class);
    }

}
