package com.dsk.search.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson2.JSONObject;
import com.dsk.acc.openapi.client.util.CommonUtils;
import com.dsk.biz.domain.vo.CustomerStatusListVo;
import com.dsk.biz.service.ICustomerService;
import com.dsk.common.core.domain.AjaxResult;
import com.dsk.common.core.page.TableDataInfo;
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 com.dsk.jsk.service.EnterpriseService;
import org.apache.commons.collections4.CollectionUtils;
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.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;

    @Autowired
    ICustomerService iCustomerService;

    @Autowired
    private EnterpriseService enterpriseService;

    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 bossCompany(JSONObject object) {
        Map<String, Object> map = dskOpenApiUtil.requestBody("/nationzj/enterprice/bossCompany", object);

        Integer code = MapUtils.getInteger(map, "code", 300);
        if (200 != code) throw new RuntimeException();

        Map data = MapUtils.getMap(map, "data", null);
        List<Object> list = CommonUtils.assertAsArray(MapUtils.getObject(data, "list", ""));

        if (CollectionUtils.isEmpty(list)) {
            return BeanUtil.toBean(map, AjaxResult.class);
        }

        ArrayList<String> companyNames = new ArrayList<>();
        for (Object dataMap : list) {
            String name = MapUtils.getString(CommonUtils.assertAsMap(dataMap), "name");
            if (ObjectUtil.isNotEmpty(name) && name.contains("font")) {
                name = name.replaceAll("<font color='#FF204E'>", "");
                name = name.replaceAll("</font>", "");
                companyNames.add(name);
            } else {
                companyNames.add(name);
            }
        }

        List<CustomerStatusListVo> claimStatusList = iCustomerService.selectStatusListByCompanyName(companyNames);

        for (Object enterprise : list) {
            Map<String, Object> companyMap = CommonUtils.assertAsMap(enterprise);
            String companyName = MapUtils.getString(companyMap, "name", "");
            if (ObjectUtil.isNotEmpty(companyName) && companyName.contains("font")) {
                companyName = companyName.replace("<font color='#FF204E'>", "");
                companyName = companyName.replace("</font>", "");
            }
            companyMap.put("claimStatus", null);
            for (CustomerStatusListVo vo : claimStatusList) {
                if (companyName.equals(vo.getCompanyName())) {
                    companyMap.put("claimStatus", vo.getStatus());
                }
            }

            Integer jskEid = MapUtils.getInteger(companyMap, "jskEid");
            companyMap.put("uipId", enterpriseService.getUipIdByCompanyNameOrCompanyId(companyName, jskEid));
        }


        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")){
                if (null!=list.get(i).get("projectLevel")){
                    vo.setProjectName(MapUtil.getStr(list.get(i),"projectLevel").concat("-")+StringUtils.removeRed(MapUtil.getStr(list.get(i),"projectName")));
                }else {
                    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);
    }
}
