package com.dsk.system.dskService;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.mail.MailUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSONObject;
import com.dsk.common.core.domain.AjaxResult;
import com.dsk.common.core.redis.RedisCache;
import com.dsk.common.utils.DateUtils;
import com.dsk.common.utils.DskOpenApiUtil;
import org.apache.commons.collections4.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @ClassName enterpriseService
 * @Description TODO
 * @Author Dgm
 * @Date 2023/5/15 12:00
 * @Version 1.0
 */
@Service
public class IndexService {

    private static final Logger log = LoggerFactory.getLogger(IndexService.class);

    @Autowired
    private DskOpenApiUtil dskOpenApiUtil;

    @Autowired
    private RedisCache redisCache;

    public AjaxResult bigWinningBidsPage(JSONObject object) {
        ArrayList recentlyBidQueryDtoList = object.getJSONArray("recentlyBidQueryDto");
        Map<String,Object> objectMap = new HashMap<>();
        Map<String,Object> pageMap = new HashMap<>(4);
        pageMap.put("page",  object.get("pageNum"));
        pageMap.put("limit", object.get("pageSize"));
        pageMap.put("field", "tenderDate");
        pageMap.put("order", "desc");
        objectMap.put("page", pageMap);
        if (ObjectUtil.isEmpty(recentlyBidQueryDtoList)) {
            Map<String,Object> recentlyBidQueryMap = new HashMap<>();
            recentlyBidQueryDtoList = new ArrayList();
            recentlyBidQueryMap.put("startTenderTime", DateUtils.dateTime(DateUtils.addDays(new Date(), -6)));
            recentlyBidQueryMap.put("endTenderTime", DateUtils.getDate());
            recentlyBidQueryMap.put("startMoney", 20000);
            recentlyBidQueryMap.put("tenderType","施工,工程总承包");
            recentlyBidQueryDtoList.add(recentlyBidQueryMap);
            objectMap.put("recentlyBidQueryDto", recentlyBidQueryDtoList);
        } else {
            Map<String,Object> recentlyBidQueryMap = (Map<String,Object>)recentlyBidQueryDtoList.get(0);
            String startMoney = MapUtils.getString(recentlyBidQueryMap, "startMoney", "");
            String endMoney = MapUtils.getString(recentlyBidQueryMap, "endMoney", "");
            if (ObjectUtil.isEmpty(startMoney) && ObjectUtil.isEmpty(endMoney)) {
                recentlyBidQueryMap.put("startMoney", 20000);
            }
            recentlyBidQueryMap.put("tenderType","施工,工程总承包");
            objectMap.put("recentlyBidQueryDto", recentlyBidQueryDtoList);
        }
        Map<String, Object> map = dskOpenApiUtil.requestBody("/nationzj/project/recently", objectMap);
        return BeanUtil.toBean(map, AjaxResult.class);
    }

    public AjaxResult bigBidPage(JSONObject body) throws Exception {
        Object jskBidQueryDto = body.get("jskBidQueryDto");
        Map<String,Object> objectMap = new HashMap<>(2);
        Map<String,Object> pageMap = new HashMap<>(4);
        pageMap.put("page",  body.get("pageNum"));
        pageMap.put("limit", body.get("pageSize"));
        pageMap.put("field", "punishDate");
        pageMap.put("order", "desc");
        objectMap.put("page", pageMap);
        if (ObjectUtil.isEmpty(jskBidQueryDto)) {
            Map<String,Object> jskBidQueryDtoMap = new HashMap<>(1);
//            jskBidQueryDtoMap.put("startTenderTime", DateUtils.dateTime(DateUtils.addDays(new Date(), -6)));
//            jskBidQueryDtoMap.put("endTenderTime", DateUtils.getDate());
            jskBidQueryDtoMap.put("startBidMoney", 20000);
            objectMap.put("jskBidQueryDto", jskBidQueryDtoMap);
        } else {
            Map<String,Object> map = JSONUtil.toBean(JSONUtil.toJsonStr(jskBidQueryDto), Map.class);
            String startBidMoney = MapUtils.getString(map, "startBidMoney", "");
            String endBidMoney = MapUtils.getString(map, "endBidMoney", "");
            if (ObjectUtil.isEmpty(startBidMoney) && ObjectUtil.isEmpty(endBidMoney)) {
                map.put("startBidMoney", 20000);
            }
            objectMap.put("jskBidQueryDto", map);
        }
        Map<String, Object> map = dskOpenApiUtil.requestBody("/nationzj/jskBid/page", objectMap);
        return BeanUtil.toBean(map, AjaxResult.class);
    }

    public AjaxResult countByCompany(JSONObject object) {
        Date startDate = object.getDate("startDate");
        Date endDate = object.getDate("endDate");
        long l = 0;
        long i = 0;
        if (startDate!=null&&endDate!=null){
            l = DateUtil.betweenDay(startDate, endDate, false);
            i = DateUtil.betweenDay(endDate, new Date(), false);
        }
        if (null!=object&&null==object.get("provinceId")&&null==object.get("type")&&l==3&&i==0){
            Map<String, Object> countByCompany = redisCache.getCacheMap("index_countByCompany");
            if (null==countByCompany|| MapUtil.isEmpty(countByCompany)){
                Map<String, Object> map = dskOpenApiUtil.requestBody("/nationzj/marketAnalysis/centralEnterprse/countByCompany", object);
                redisCache.setCacheMap("index_countByCompany",map);
                redisCache.expire("index_countByCompany",7200);
                return BeanUtil.toBean(countByCompany, AjaxResult.class);
            }else {
                log.info("首页集团中标业绩从缓存中取");
                return BeanUtil.toBean(countByCompany, AjaxResult.class);
            }
        }
        Map<String, Object> map = dskOpenApiUtil.requestBody("/nationzj/marketAnalysis/centralEnterprse/countByCompany", object);
        return BeanUtil.toBean(map, AjaxResult.class);
    }

    public AjaxResult bidRank(JSONObject object) {
        Map<String, Object> map = dskOpenApiUtil.requestBody("/nationzj/marketAnalysis/bidRank", object);
        return BeanUtil.toBean(map, AjaxResult.class);
    }

    public static void main(String[] args) {
        System.out.println(DateUtils.getDate());
    }
}
