package com.dsk.search.service;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson2.JSONObject;
import com.dsk.common.core.domain.AjaxResult;
import com.dsk.system.utils.DskOpenApiUtil;
import com.dsk.jsk.service.EnterpriseService;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Map;

/**
 * @Author liujie
 * @ClassName MarketAnalysisServiceImpl
 * @Date 2023/5/15 16:40
 **/
@Service
public class MarketAnalysisService {

    @Autowired
    private DskOpenApiUtil dskOpenApiUtil;

    @Autowired
    private EnterpriseService enterpriseService;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    public AjaxResult combineRecentlyBid(JSONObject object) {
        Map<String, Object> map = dskOpenApiUtil.requestBody("/nationzj/marketAnalysis/combine/recentlyBid", object);
//        if (!ObjectUtils.isEmpty(map.get("data"))) {
//            List<Map<String, Object>> list = (List<Map<String, Object>>) map.get("data");
//            list.parallelStream().forEach(res -> {
//                Integer companyId = MapUtils.getInteger(res, "tendereeId");
//                String companyName = MapUtils.getString(res, "tenderee");
//                res.put("uipId", enterpriseService.getUipIdByCompanyNameOrCompanyId(companyName, companyId));
//            });
//        }
        return BeanUtil.toBean(map, AjaxResult.class);
    }

    public AjaxResult combineBidByYear(JSONObject object) {
        Map<String, Object> map = dskOpenApiUtil.requestBody("/nationzj/marketAnalysis/combine/bidByYear", object);
        if (!ObjectUtils.isEmpty(map.get("data"))) {
            List<Map<String, Object>> list = (List<Map<String, Object>>) map.get("data");
            list.parallelStream().forEach(res -> {
                Integer companyId = MapUtils.getInteger(res, "companyId");
                String companyName = MapUtils.getString(res, "companyName");
                res.put("uipId", enterpriseService.getUipIdByCompanyNameOrCompanyId(companyName, companyId));
            });
        }
        return BeanUtil.toBean(map, AjaxResult.class);
    }

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

    public AjaxResult combineGroupByType(JSONObject object) {
        Map<String, Object> map = dskOpenApiUtil.requestBody("/nationzj/marketAnalysis/combine/groupByType", object);
        if (!ObjectUtils.isEmpty(map.get("data"))) {
            List<Map<String, Object>> list = (List<Map<String, Object>>) map.get("data");
            list.parallelStream().forEach(res -> {
                Integer companyId = MapUtils.getInteger(res, "companyId");
                String companyName = MapUtils.getString(res, "companyName");
                res.put("uipId", enterpriseService.getUipIdByCompanyNameOrCompanyId(companyName, companyId));
            });
        }
        return BeanUtil.toBean(map, AjaxResult.class);
    }

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

    public AjaxResult combinePeojectTop(JSONObject object) {
        Map<String, Object> map = dskOpenApiUtil.requestBody("/nationzj/marketAnalysis/combine/peojectTop", object);
        if (!ObjectUtils.isEmpty(map.get("data"))) {
            List<Map<String, Object>> list = (List<Map<String, Object>>) map.get("data");
            list.parallelStream().forEach(res -> {
                Integer companyId = MapUtils.getInteger(res, "projectUnitId");
                String companyName = MapUtils.getString(res, "projectUnit");
                res.put("uipId", enterpriseService.getUipIdByCompanyNameOrCompanyId(companyName, companyId));
            });
        }
        return BeanUtil.toBean(map, AjaxResult.class);
    }
}
