package com.dsk.jsk.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.dsk.common.core.domain.AjaxResult;
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.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @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;

    public AjaxResult bigWinningBidsPage(JSONObject object) {
        JSONArray 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 JSONArray();
            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) {
        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());
    }
}
