package com.dsk.system.dskService;

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.core.domain.R;
import com.dsk.common.core.page.TableDataInfo;
import com.dsk.common.exception.ServiceException;
import com.dsk.common.utils.DskOpenApiUtil;
import com.dsk.system.domain.dsk.dto.JskCombineBidPageDto;
import com.dsk.system.domain.dsk.dto.JskCombineCertificateDto;
import com.dsk.system.domain.dsk.dto.JskCombineSearchDto;
import com.dsk.system.domain.dsk.vo.JskCombineBidProjectExportVo;
import com.dsk.system.domain.dsk.vo.JskCombineWinBidProjectExportVo;
import lombok.extern.slf4j.Slf4j;
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.List;
import java.util.Map;

/**
 * @author lcl
 * @create 2023/8/7
 */
@Slf4j
@Service
public class JskCombineInfoService {

    @Autowired
    private DskOpenApiUtil dskOpenApiUtil;
    @Autowired
    private EnterpriseService enterpriseService;

    public TableDataInfo memberList(JskCombineSearchDto dto) throws Exception {
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/combine/memberList", BeanUtil.beanToMap(dto, false, false));
//        if (!ObjectUtils.isEmpty(map.get("data"))) {
//            Map<String, Object> data = BeanUtil.beanToMap(map.get("data"));
//            if (!ObjectUtils.isEmpty(data.get("list"))) {
//                List<Map<String, Object>> list = (List<Map<String, Object>>) data.get("list");
//                list.parallelStream().forEach(res -> {
//                    Integer companyId = MapUtils.getInteger(res, "companyId");
//                    String companyName = MapUtils.getString(res, "companyName");
//                    res.put("uipId", enterpriseService.getUipIdByCompanyNameOrCompanyId(companyName, companyId));
//                });
//            }
//        }
        return dskOpenApiUtil.responsePage(map);
    }


    public R groupMemberCount(JskCombineSearchDto dto) {
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/combine/group/memberCount", BeanUtil.beanToMap(dto, false, false));
        return BeanUtil.toBean(map, R.class);
    }

    public TableDataInfo businessList(JskCombineSearchDto dto) throws Exception {
        Map<String, Object> map = dskOpenApiUtil.requestBody("/nationzj/project/combine/projectList", BeanUtil.beanToMap(dto, false, false));
        if (!ObjectUtils.isEmpty(map.get("data"))) {
            Map<String, Object> data = BeanUtil.beanToMap(map.get("data"));
            if (!ObjectUtils.isEmpty(data.get("list"))) {
                List<Map<String, Object>> list = (List<Map<String, Object>>) data.get("list");
                list.parallelStream().forEach(res -> {
                    Integer companyId = MapUtils.getInteger(res, "companyId");
                    String companyName = MapUtils.getString(res, "companyName");
                    res.put("uipId", enterpriseService.getUipIdByCompanyNameOrCompanyId(companyName, companyId));
                    Integer projectUnitId = MapUtils.getInteger(res, "projectUnitId");
                    String projectUnit = MapUtils.getString(res, "projectUnit");
                    res.put("projectUnitUipId", enterpriseService.getUipIdByCompanyNameOrCompanyId(projectUnit, projectUnitId));
                });
            }
        }
        return dskOpenApiUtil.responsePage(map);
    }

    public TableDataInfo certificateList(JskCombineSearchDto dto) throws Exception {
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/combine/certificateList", BeanUtil.beanToMap(dto, false, false));
        return dskOpenApiUtil.responsePage(map);
    }

    public TableDataInfo menberCertificateList(JskCombineCertificateDto dto) throws Exception {
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/combine/member/certificateList", BeanUtil.beanToMap(dto, false, false));
//        if (!ObjectUtils.isEmpty(map.get("data"))) {
//            Map<String, Object> data = BeanUtil.beanToMap(map.get("data"));
//            if (!ObjectUtils.isEmpty(data.get("list"))) {
//                List<Map<String, Object>> list = (List<Map<String, Object>>) data.get("list");
//                list.parallelStream().forEach(res -> {
//                    Integer companyId = MapUtils.getInteger(res, "companyId");
//                    String companyName = MapUtils.getString(res, "companyName");
//                    res.put("uipId", enterpriseService.getUipIdByCompanyNameOrCompanyId(companyName, companyId));
//                });
//            }
//        }
        return dskOpenApiUtil.responsePage(map);
    }

    public R groupCertificateCount(JskCombineSearchDto dto) {
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/combine/group/certificateCount", BeanUtil.beanToMap(dto, false, false));
        return BeanUtil.toBean(map, R.class);
    }

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

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

    public AjaxResult centralEnterprseChild() {
        Map<String, Object> map = dskOpenApiUtil.requestBody("/nationzj/enterprice/centralEnterprse/child", null);
        return BeanUtil.toBean(map, AjaxResult.class);
    }

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

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

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

    public AjaxResult centralEnterprseSocial() {
        Map<String, Object> map = dskOpenApiUtil.requestBody("/nationzj/enterprice/centralEnterprse/social", null);
        return BeanUtil.toBean(map, AjaxResult.class);
    }

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

    public List<JskCombineWinBidProjectExportVo> exportWinBid(JskCombineSearchDto dto) {
        if (ObjectUtils.isEmpty(dto.getPageSize())) throw new ServiceException("导出条数不能为空！");
        if (dto.getPageSize() > 2000) dto.setPageSize(2000);

        Map<String, Object> map = dskOpenApiUtil.requestBody("/nationzj/project/combine/projectList", BeanUtil.beanToMap(dto, false, false));
        if (ObjectUtils.isEmpty(map.get("data"))) throw new ServiceException("导出失败，系统错误！");
        Map<String, Object> data = BeanUtil.beanToMap(map.get("data"));
        if (ObjectUtils.isEmpty(data.get("list"))) throw new ServiceException("导出失败，系统错误！");

        List<Map<String, Object>> list = (List<Map<String, Object>>) data.get("list");
        if (ObjectUtils.isEmpty(list)) throw new ServiceException("导出失败，无数据导出！");
        List<JskCombineWinBidProjectExportVo> result = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            JskCombineWinBidProjectExportVo vo = new JskCombineWinBidProjectExportVo();
            vo.setId(i + 1);
            vo.setBidTime(MapUtil.getDate(list.get(i),"bidTime"));
            vo.setCompanyName(MapUtil.getStr(list.get(i),"companyName"));
            vo.setStockPercent(MapUtil.getDouble(list.get(i),"stockPercent"));
            vo.setMemberLevel(MapUtil.getStr(list.get(i),"memberLevel"));
            vo.setProjectName(MapUtil.getStr(list.get(i),"projectName"));
            vo.setBidAmount(MapUtil.getDouble(list.get(i),"bidAmount"));
            vo.setAddress(MapUtil.getStr(list.get(i),"address"));
            vo.setBoundType(MapUtil.getStr(list.get(i),"boundType"));
            vo.setProjectType(MapUtil.getStr(list.get(i),"projectType"));
            vo.setProjectUnit(MapUtil.getStr(list.get(i),"projectUnit"));
            result.add(vo);
        }
        return result;
    }


    public List<JskCombineBidProjectExportVo> exportBid(JskCombineBidPageDto dto) {
        if (ObjectUtils.isEmpty(dto.getPageSize())) throw new ServiceException("导出条数不能为空！");
        if (dto.getPageSize() > 2000) dto.setPageSize(2000);

        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/combine/bidPage", BeanUtil.beanToMap(dto, false, false));
        if (ObjectUtils.isEmpty(map.get("data"))) throw new ServiceException("导出失败，系统错误！");
        Map<String, Object> data = BeanUtil.beanToMap(map.get("data"));
        if (ObjectUtils.isEmpty(data.get("list"))) throw new ServiceException("导出失败，系统错误！");

        List<Map<String, Object>> list = (List<Map<String, Object>>) data.get("list");
        if (ObjectUtils.isEmpty(list)) throw new ServiceException("导出失败，无数据导出！");
        List<JskCombineBidProjectExportVo> result = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            JskCombineBidProjectExportVo vo = new JskCombineBidProjectExportVo();
            vo.setId(i + 1);
            vo.setIssueTime(MapUtil.getDate(list.get(i),"issueTime"));
            vo.setTenderee(MapUtil.getStr(list.get(i),"tenderee"));
            vo.setStockPercent(MapUtil.getDouble(list.get(i),"stockPercent"));
            vo.setMemberLevel(MapUtil.getStr(list.get(i),"memberLevel"));
            vo.setProjectName(MapUtil.getStr(list.get(i),"projectName"));
            vo.setBidAmount(MapUtil.getDouble(list.get(i),"bidAmount"));
            vo.setAddress(MapUtil.getStr(list.get(i),"address"));
            vo.setSubjectMatter(MapUtil.getStr(list.get(i),"subjectMatter"));
            vo.setProjectType(MapUtil.getStr(list.get(i),"projectType"));
            vo.setAgency(MapUtil.getStr(list.get(i),"agency"));
            result.add(vo);
        }
        return result;
    }

    public TableDataInfo bidPage(JskCombineBidPageDto dto) throws Exception {
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/combine/bidPage", BeanUtil.beanToMap(dto, false, false));
        if (ObjectUtil.isNotEmpty(map.get("data"))) {
            Map<String, Object> data = BeanUtil.beanToMap(map.get("data"));
            if (!ObjectUtils.isEmpty(data.get("list"))) {
                List<Map<String, Object>> list = (List<Map<String, Object>>) data.get("list");
                list.parallelStream().forEach(res -> {
                    //招标企业
                    Integer tendereeId = MapUtils.getInteger(res, "tendereeId");
                    String tenderee = MapUtils.getString(res, "tenderee");
                    // 代理机构名称
                    Integer agencyId = MapUtils.getInteger(res, "agencyId");
                    String agency = MapUtils.getString(res, "agency");
                    res.put("tendereeUipId", enterpriseService.getUipIdByCompanyNameOrCompanyId(tenderee, tendereeId));
                    res.put("agencyUipId", enterpriseService.getUipIdByCompanyNameOrCompanyId(agency, agencyId));
                });
            }
        }
        return dskOpenApiUtil.responsePage(map);
    }
}
