package com.dsk.system.dskService;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.dsk.acc.openapi.client.util.CommonUtils;
import com.dsk.common.core.domain.R;
import com.dsk.common.core.domain.model.*;
import com.dsk.common.core.page.TableDataInfo;
import com.dsk.common.utils.DskOpenApiUtil;
import org.apache.commons.collections4.CollectionUtils;
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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EnterpriseBussinessService {

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

    @Autowired
    private DskOpenApiUtil dskOpenApiUtil;

    public R customSelect() throws Exception {
        Map<String, Object> objectMap = new HashMap<>(2);
        List<String> sourceList = new ArrayList<>();
        sourceList.add("四库业绩");
        sourceList.add("中标业绩");
        sourceList.add("水利业绩");
        sourceList.add("中标公示");
        sourceList.add("荣誉奖项");
        objectMap.put("sourceList", sourceList);
        List<String> projectTypeList = new ArrayList<>();

        projectTypeList.add("房建工程");
        projectTypeList.add("市政工程");
        projectTypeList.add("公路工程");
        projectTypeList.add("水利工程");
        projectTypeList.add("铁路工程");
        projectTypeList.add("通信工程");
        projectTypeList.add("电力工程");
        projectTypeList.add("机电工程");
        projectTypeList.add("冶金工程");
        projectTypeList.add("矿山工程");
        projectTypeList.add("石油化工工程");
        projectTypeList.add("港口与航道工程");
        projectTypeList.add("其他");
        objectMap.put("projectTypeList", projectTypeList);
        return BeanUtil.toBean(objectMap, R.class);
    }

    public TableDataInfo clientPage(EnterpriseBussinessClientPageBody body) throws Exception {
        if (body.isVaildCid()) return new TableDataInfo(new ArrayList<>(), 0);
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterpriseBussiness/clientPage2", BeanUtil.beanToMap(body, false, false));
        return dskOpenApiUtil.responsePage(map);
    }


    public TableDataInfo clientProjectPage(EnterpriseBussinessClientProjectPageBody body) throws Exception {
        if (body.isVaildCid()) return null;
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterpriseBussiness/clientProjectPage2", BeanUtil.beanToMap(body, false, false));
        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 new TableDataInfo(list, 0);
        }

        for (Object supplierProject : list) {
            Map<String, Object> dataMap = CommonUtils.assertAsMap(supplierProject);
            dataMap.put("sourceId", null);
            dataMap.put("sourceType", null);
            dataMap.put("sourceList", null);
            String source = MapUtils.getString(dataMap, "source");
            if (ObjectUtil.isNotEmpty(source)) {
                List<EnterpriseBussinessVo> enterpriseBussinessVoList = JSONUtil.toList(source, EnterpriseBussinessVo.class);
                if (ObjectUtil.isNotEmpty(enterpriseBussinessVoList.get(0))) {
                    EnterpriseBussinessVo vo = enterpriseBussinessVoList.get(0);
                    dataMap.put("sourceId", vo.getSource_id());
                    dataMap.put("sourceType", vo.getSource_type());
                    dataMap.put("sourceList", enterpriseBussinessVoList);
                }
            }
        }
        return new TableDataInfo(list, MapUtils.getInteger(data, "totalCount", 0));
    }


    public TableDataInfo tenderPage(EnterpriseBussinessTenderPageBody body) throws Exception {
        if (body.isVaildCid()) return new TableDataInfo(new ArrayList<>(), 0);;
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterpriseBussiness/tenderPage", BeanUtil.beanToMap(body, false, false));
        return dskOpenApiUtil.responsePage(map);
    }


    public R tenderDetail(EnterpriseBussinessTenderDetailBody body) throws Exception {
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterpriseBussiness/tenderDetail", BeanUtil.beanToMap(body, false, false));
        Map data = MapUtils.getMap(map, "data", null);
        String mongoContentId = MapUtils.getString(data, "mongoContentId");
        if (ObjectUtil.isEmpty(mongoContentId)) {
            return BeanUtil.toBean(map, R.class);
        }

        HashMap<String, Object> contentParam = new HashMap<>();
        contentParam.put("data_type", "kaibiao");
        contentParam.put("filter_type", 2);
        contentParam.put("strategy_id", mongoContentId);
        Map<String, Object> contentMap = null;
        try {
           contentMap = dskOpenApiUtil.requestBody("/mongocontent/v1/cjb/mongo_content", contentParam);
            log.info("contentData:{}", JSONUtil.toJsonStr(contentMap));
        } catch (Exception e) {
            data.put("content", e.getMessage());
            map.put("data", data);
            return BeanUtil.toBean(map, R.class);
        }

        Map contentData = MapUtils.getMap(contentMap, "data", null);

        if (200 != MapUtils.getInteger(contentMap, "code", 300)) {
            data.put("content", JSONUtil.toJsonStr(contentMap));
            map.put("data", data);
            return BeanUtil.toBean(map, R.class);
        }

        data.put("content", MapUtils.getString(contentData, "htmlContent"));
        map.put("data", data);

        return BeanUtil.toBean(map, R.class);
    }


    public TableDataInfo supplierPage(EnterpriseBussinessSupplierPageBody body) throws Exception {
        if (body.isVaildCid()) {
            return new TableDataInfo(new ArrayList<>(), 0);
        }
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterpriseBussiness/supplierPage2", BeanUtil.beanToMap(body, false, false));
        return dskOpenApiUtil.responsePage(map);
    }


    public TableDataInfo supplierProjectPage(EnterpriseBussinessSupplierProjectPageBody body) throws Exception {
        if (body.isVaildCid()) {
            return new TableDataInfo(new ArrayList<>(), 0);
        }
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterpriseBussiness/supplierProjectPage2", BeanUtil.beanToMap(body, false, false));
        log.info(JSONUtil.toJsonStr(map));

        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 new TableDataInfo(list, 0);
        }

        for (Object supplierProject : list) {
            Map<String, Object> dataMap = CommonUtils.assertAsMap(supplierProject);
            dataMap.put("sourceId", null);
            dataMap.put("sourceType", null);
            String source = MapUtils.getString(dataMap, "source");
            if (ObjectUtil.isNotEmpty(source)) {
                List<EnterpriseBussinessVo> enterpriseBussinessVoList = JSONUtil.toList(source, EnterpriseBussinessVo.class);
                if (ObjectUtil.isNotEmpty(enterpriseBussinessVoList.get(0))) {
                    EnterpriseBussinessVo vo = enterpriseBussinessVoList.get(0);
                    dataMap.put("sourceId", vo.getSource_id());
                    dataMap.put("sourceType", vo.getSource_type());
                    dataMap.put("sourceList", enterpriseBussinessVoList);
                }
            }
        }
        return new TableDataInfo(list, MapUtils.getInteger(data, "totalCount", 0));
    }


    public R projectDetail(EnterpriseBussinessProjectDetailBody body) throws Exception {
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterpriseBussiness/projectDetail", BeanUtil.beanToMap(body, false, false));
        return BeanUtil.toBean(map, R.class);
    }


    public TableDataInfo oftenAgencyPage(EnterpriseBussinessOftenAgencyPageBody body) throws Exception {
        if (body.isVaildCid()) return new TableDataInfo(new ArrayList<>(), 0);
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterpriseBussiness/oftenAgencyPage", BeanUtil.beanToMap(body, false, false));
        return dskOpenApiUtil.responsePage(map);
    }


    public TableDataInfo bidCooperatePage(EnterpriseBussinessBidCooperatePageBody body) throws Exception {
        if (body.isVaildCid()) return new TableDataInfo(new ArrayList<>(), 0);
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterpriseBussiness/bidCooperatePage", BeanUtil.beanToMap(body, false, false));
        return dskOpenApiUtil.responsePage(map);
    }


    public TableDataInfo historySendPage(EnterpriseBussinessHistorySendPageBody body) throws Exception {
        if (body.isVaildCid()) return new TableDataInfo(new ArrayList<>(), 0);
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterpriseBussiness/historySendPage", BeanUtil.beanToMap(body, false, false));
        return dskOpenApiUtil.responsePage(map);
    }


    public R historySendProvince(EnterpriseBussinessHistorySendProvinceBody body) throws Exception {
        if (body.isVaildCid()) return R.ok();
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterpriseBussiness/historySendProvince", BeanUtil.beanToMap(body, false, false));
        return BeanUtil.toBean(map, R.class);
    }

    public R bidWinMergeDetail(EnterpriseBusinessBidWinMergeDetailBody body) throws Exception {
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterpriseBussiness/bidWinMergeDetail", BeanUtil.beanToMap(body, false, false));
        return BeanUtil.toBean(map, R.class);
    }

    public R skyProjectDetail(EnterpriseBusinessSkyProjectDetailBody body) throws Exception {
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterpriseBussiness/skyProjectDetail", BeanUtil.beanToMap(body, false, false));
        return BeanUtil.toBean(map, R.class);
    }

    public R awardDetail(EnterpriseBusinessAwardDetailBody body) throws Exception {
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterpriseBussiness/awardDetail", BeanUtil.beanToMap(body, false, false));
        Integer code = MapUtils.getInteger(map, "code", 300);
        if (200 != code) {
            throw new RuntimeException();
        }
        Map data = MapUtils.getMap(map, "data", null);
        if (ObjectUtil.isNotEmpty(data)) {
            String source = MapUtils.getString(data, "source");
            if (ObjectUtil.isNotEmpty(source)) {
                List<EnterpriseBusinessAwardDetailVo> enterpriseBussinessVoList = JSONUtil.toList(source, EnterpriseBusinessAwardDetailVo.class);
                if (ObjectUtil.isNotEmpty(enterpriseBussinessVoList.get(0))) {
                    EnterpriseBusinessAwardDetailVo vo = enterpriseBussinessVoList.get(0);
                    return R.ok(vo);
                }
            }
        }
        return BeanUtil.toBean(map, R.class);
    }


    public R select(EnterpriseBusinessAwardDetailBody body) throws Exception {
        Map<String, Object> map =  new HashMap<>();
        List<String> sourcesList = new ArrayList<>(5);
        sourcesList.add("中标公示");
        sourcesList.add("四库业绩");
        sourcesList.add("中标业绩");
        sourcesList.add("水利业绩");
        sourcesList.add("荣誉奖项");
        map.put("sources", sourcesList);

        List<String> projectsList = new ArrayList<>(2);
        projectsList.add("矿山工程");
        projectsList.add("铁路工程");
        projectsList.add("港口与航道工程");
        projectsList.add("冶金工程");
        projectsList.add("水利工程");
        projectsList.add("机电工程");
        projectsList.add("市政工程");
        projectsList.add("石油化工工程");
        projectsList.add("电力工程");
        projectsList.add("通信工程");
        projectsList.add("公路工程");
        projectsList.add("房建工程");
        projectsList.add("其他");
        map.put("projects", projectsList);
        return R.ok(map);
    }
}
