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.constant.CacheConstants;
import com.dsk.common.core.domain.AjaxResult;
import com.dsk.common.helper.LoginHelper;
import com.dsk.common.utils.DateUtils;
import com.dsk.common.utils.DskOpenApiUtil;
import com.dsk.jsk.domain.*;
import com.dsk.jsk.domain.bo.*;
import com.dsk.jsk.service.service.EconomicService;
import com.dsk.jsk.util.RedisCache;
import org.apache.commons.collections4.MapUtils;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
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;

    /**
     * 全国宏观经济：1 / 辖区经济：2 / 地区对比：3
     */
    private static final Integer TYPE = 1;

    /**
     * 默认排序
     */
    private static final String FIELD = "gdp";
    private static final String ORDER = "desc";




    @Override
    public AjaxResult nationalPage(OpRegionalEconomicDataV1PageDto dto) {
        if (ObjectUtil.isEmpty(dto.getYear())) {
            dto.setYear(DateUtils.getYear() - 1);
        }
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/economic/national/nationalPage",  BeanUtil.beanToMap(dto, false, false));
        Integer code = MapUtils.getInteger(map, "code", 300);
        if (!code.equals(HttpStatus.OK.value())) {
            throw new RuntimeException();
        }
        return BeanUtil.toBean(map, AjaxResult.class);
    }

    @Override
    public AjaxResult yearsList(OpRegionalEconomicDataYearsListDto dto) {
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/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("/operate/economic/details", BeanUtil.beanToMap(detailsDto, false, false));
        return BeanUtil.toBean(map, AjaxResult.class);
    }

    @Override
    public AjaxResult location(OpRegionalLocalDto detailsDto) {
        Long userId = LoginHelper.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("/operate/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("/operate/economic/statistics/regional",  BeanUtil.beanToMap(dto, false, false));
        return BeanUtil.toBean(map, AjaxResult.class);
    }

    @Override
    public AjaxResult regionalList(OpRegionalEconomicDataRegionalListDto dto) {
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/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("/operate/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("/operate/economic/regional/multipleCompare",  BeanUtil.beanToMap(dto, false, false));
        return BeanUtil.toBean(map, AjaxResult.class);
    }

}
