package com.dsk.system.dskService;

import cn.hutool.core.bean.BeanException;
import cn.hutool.core.bean.BeanUtil;
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.utils.DskOpenApiUtil;
import com.dsk.system.domain.dsk.dto.JskCombineCertificateDto;
import com.dsk.system.domain.dsk.dto.JskCombineSearchDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.apache.ibatis.util.MapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Arrays;
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("/operate/combine/businessList", 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);
    }
}
