package com.dsk.jsk.service;

import cn.hutool.core.bean.BeanException;
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.page.TableDataInfo;
import com.dsk.common.utils.DskOpenApiUtil;
import com.dsk.jsk.domain.*;
import com.dsk.jsk.domain.bo.EnterpriseProjectImportantListDto;
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 EnterpriseProjectService {

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

    @Autowired
    private DskOpenApiUtil dskOpenApiUtil;


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


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


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


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


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


    public TableDataInfo approvalExaminePage(EnterpriseProjectApprovalExaminePageBody body) throws Exception {
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterpriseProject/approvalExaminePage", BeanUtil.beanToMap(body, false, false));
        return dskOpenApiUtil.responsePage(map);
    }


    public TableDataInfo approvalMarketingPage(EnterpriseProjectApprovalMarketingPageBody body) throws Exception {
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterpriseProject/approvalMarketingPage", BeanUtil.beanToMap(body, false, false));
        return dskOpenApiUtil.responsePage(map);
    }


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


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

    public TableDataInfo specialDebtPage(EnterpriseProjectSpecialDebtPageBody body) throws Exception {
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterpriseProject/specialDebtPage", BeanUtil.beanToMap(body, false, false));
        return dskOpenApiUtil.responsePage(map);
    }

    /***
     *@Description: 招标公告 分页列表
     *@Param:
     *@return: com.dsk.common.core.domain.R
     *@Author: Dgm
     *@date: 2023/10/9 14:41
     */
    public TableDataInfo bidNoticePage(EnterpriseProjectBidNoticePageBody body) throws Exception {
        if (body.isVaildCid()) {
            return new TableDataInfo(new ArrayList<>(), 0);
        }
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterpriseProject/bidNoticePage", BeanUtil.beanToMap(body, false, false));
        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);
        }

        ArrayList<Long> cIds = new ArrayList<>();
        for (Object dataMap : list) {
            cIds.add(MapUtils.getLong(CommonUtils.assertAsMap(dataMap), "agencyId"));
        }

        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("ids", cIds);
        Map<String, Object> uipIdMap = dskOpenApiUtil.requestBody("/operate/enterprise/getUipIdByCid", paramMap);
        Object uipIdData = uipIdMap.get("data");
        List<Map<String, Object>> mapList = new ArrayList<>();
        if (ObjectUtil.isNotEmpty(uipIdData)) {
            mapList = (List<Map<String, Object>>)uipIdData;
        }
        for (Object dataMap : list) {
            Map<String, Object> companyMap = CommonUtils.assertAsMap(dataMap);
            Long agencyId = MapUtils.getLong(companyMap, "agencyId", 0L);
            companyMap.put("uipId", null);
            for (Map<String, Object> m : mapList) {
                String uipId = MapUtils.getString(m, "uipId", null);
                Long companyId = MapUtils.getLong(m, "companyId", 0L);
                if (agencyId.equals(companyId)) {
                    companyMap.put("uipId", uipId);
                }
            }
        }
        return dskOpenApiUtil.responsePage(map);
    }

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


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

    /***
     *@Description: 招标项目类别
     *@Param:
     *@return: com.dsk.common.core.domain.R
     *@Author: Dgm
     *@date: 2023/10/9 14:41
     */
    public R bidNoticeProjectCategory(EnterpriseProjectBidNoticeTenderStageBody body) throws Exception {
        if (body.isVaildCid()) {
            return R.ok();
        }
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterpriseProject/bidNoticeProjectCategory", BeanUtil.beanToMap(body, false, false));
        return BeanUtil.toBean(map, R.class);
    }

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


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

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


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

        HashMap<String, Object> contentParam = new HashMap<>();
        contentParam.put("data_type", "zhaobiao_new");
        contentParam.put("filter_type", 2);
        contentParam.put("strategy_id", contentId);
        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 bidNoticeProPage(EnterpriseProjectBidNoticeProPageBody body) throws Exception {
        if (body.isVaildCid()) return new TableDataInfo(new ArrayList<>(), 0);
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterpriseProject/bidNoticeProPage", BeanUtil.beanToMap(body, false, false));
        return dskOpenApiUtil.responsePage(map);
    }

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

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

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


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


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

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

    /***
     *@Description: 公招市场-流程速览
     *@Param:
     *@return: com.dsk.common.core.domain.R
     *@Author: Dgm
     *@date: 2023/9/19 13:49
     */
    public R projectProcess(EnterpriseProjectProcessDetailBody body) throws Exception {
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/recruitmentMarket/projectProcess", BeanUtil.beanToMap(body, false, false));
        return BeanUtil.toBean(map, R.class);
    }

    public TableDataInfo importantList(EnterpriseProjectImportantListDto dto) throws Exception{
        if(ObjectUtil.isEmpty(dto.getCid())) throw new BeanException("企业id不能为空！");
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterpriseProject/importantList", BeanUtil.beanToMap(dto, false, false));
        return dskOpenApiUtil.responsePage(map);
    }
}
