package com.dsk.search.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson2.JSONObject;
import com.dsk.common.core.domain.AjaxResult;
import com.dsk.common.exception.ServiceException;
import com.dsk.common.utils.DskOpenApiUtil;
import com.dsk.common.utils.StringUtils;
import com.dsk.jsk.domain.vo.ImportantProjectExportVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

    @Autowired
    private DskOpenApiUtil dskOpenApiUtil;

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

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

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

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

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

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

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

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

    public AjaxResult enterpriseByName(String keyword) {
        Map<String, Object> params = new HashMap<>();
        params.put("keyword", keyword);
        Map<String, Object> map = dskOpenApiUtil.requestBody("/nationzj/enterprice/findByName", params);
        return BeanUtil.toBean(map, AjaxResult.class);
    }

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

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

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

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

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


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

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

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

    public List<ImportantProjectExportVo> exportImportant(JSONObject object) {
        if (ObjectUtil.isEmpty(object.get("page"))) {
            throw new ServiceException("导出条数不能为空！");
        }
        Map<String, Object> page = (Map<String,Object>)object.get("page");
        if (null==page.get("limit")|| MapUtil.getInt(page,"limit")>2000){
            page.put("limit",2000);
        }
        page.put("page",1);
        object.put("page",page);

        Map<String, Object> map = dskOpenApiUtil.requestBody("/nationzj/bondProject/importantPage",object);
        if (ObjectUtil.isEmpty(map.get("data"))) throw new ServiceException("导出失败，系统错误！");
        Map<String, Object> data = BeanUtil.beanToMap(map.get("data"));
        if (ObjectUtil.isEmpty(data.get("list"))) throw new ServiceException("导出失败，系统错误！");

        List<Map<String, Object>> list = (List<Map<String, Object>>) data.get("list");
        if (ObjectUtil.isEmpty(list)) throw new ServiceException("导出失败，无数据导出！");
        List<ImportantProjectExportVo> result = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            ImportantProjectExportVo vo = new ImportantProjectExportVo();
            vo.setId(i + 1);
            vo.setBuildingProjectType(MapUtil.getStr(list.get(i),"buildingProjectType"));
            if (null!=list.get(i).get("projectName")){
                vo.setProjectName(StringUtils.removeRed(MapUtil.getStr(list.get(i),"projectName")));
            }
            if (null!=list.get(i).get("chargeDepartment")){
                vo.setChargeDepartment(StringUtils.removeRed(MapUtil.getStr(list.get(i),"chargeDepartment")));
            }
            if (null!=list.get(i).get("constructUnit")){
                vo.setConstructUnit(StringUtils.removeRed(MapUtil.getStr(list.get(i),"constructUnit")));
            }
            if (null!=list.get(i).get("projectScale")){
                vo.setProjectScale(StringUtils.removeRed(MapUtil.getStr(list.get(i),"projectScale")));
            }
            vo.setProjectTotalInvestment(MapUtil.getDouble(list.get(i),"projectTotalInvestment"));
            vo.setDomicile(MapUtil.getStr(list.get(i),"domicile"));
            vo.setFileTitle(MapUtil.getStr(list.get(i),"fileTitle"));
            vo.setFileUrl(MapUtil.getStr(list.get(i),"fileUrl"));
            result.add(vo);
        }
        return result;

    }

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