package com.dsk.system.dskService;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapUtil;
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 com.dsk.common.utils.EncodeIdUtil;
import com.dsk.system.domain.customer.vo.CustomerStatusListVo;
import com.dsk.system.service.ICustomerService;
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;

/**
 * @ClassName enterpriseService
 * @Description TODO
 * @Author zhangyi
 * @Date 2023/5/15 12:00
 * @Version 1.0
 */
@Service
public class EnterpriseService {

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

    @Autowired
    private DskOpenApiUtil dskOpenApiUtil;

    @Autowired
    ICustomerService iCustomerService;

    public R infoHeader(EnterpriseInfoHeaderBody body) throws Exception {
        Map<String, Object> companyMap = dskOpenApiUtil.requestBody("/api/jsk/enterprise/infoHeader", BeanUtil.beanToMap(body, false, false));
        Integer companyCode = MapUtils.getInteger(companyMap, "code", 300);
        if (200 != companyCode) throw new RuntimeException();
        Map companyData = MapUtils.getMap(companyMap, "data", null);
        companyData.put("claimStatus", 0);

        Map<String, Object> uipMap = dskOpenApiUtil.requestBody("/operate/enterprise/financial", BeanUtil.beanToMap(body, false, false));
        Integer uipCode = MapUtils.getInteger(uipMap, "code", 300);
        if (200 != uipCode) return R.ok(companyData);

        Map uipData = MapUtils.getMap(uipMap, "data", new HashMap<>());
        String uipId = MapUtils.getString(uipData, "uipId", null);
        if (ObjectUtil.isEmpty(uipId)) {
            return R.ok(companyData);
        }

        ArrayList<String> uipIds = new ArrayList<>();
        uipIds.add(uipId);
        List<CustomerStatusListVo> claimStatusList = iCustomerService.selectStatusList(uipIds);

        if (CollectionUtils.isNotEmpty(claimStatusList)) {
            companyMap.put("claimStatus", 1);
        }

        return R.ok(companyData);
    }

    public R statistic(EnterpriseStatisticBody body) throws Exception {
        Map<String, Object> map = dskOpenApiUtil.requestBody("/api/jsk/enterprise/statistic", BeanUtil.beanToMap(body, false, false));
        return BeanUtil.toBean(map, R.class);
    }

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

    public R supplierPage(EnterpriseSupplierPageBody body) throws Exception {
        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("Api-Version", "1.0");
        Map<String, Object> map = dskOpenApiUtil.requestBodyWithHeader("/api/jsk/enterpriseBusiness/selectPageSupplierInfo", BeanUtil.beanToMap(body, false, false), headerMap);
        return BeanUtil.toBean(map, R.class);
    }

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

    //todo 新数据，待入库
    public TableDataInfo partners(EnterprisePartnersBody body) throws Exception {
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterprise/partners", BeanUtil.beanToMap(body, false, false));
        return dskOpenApiUtil.responsePage(map);
    }

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

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

    public R bidNoticePage(EnterpriseBidNoticePageBody body) throws Exception {
        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("Api-Version", "1.0");
        Map<String, Object> map = dskOpenApiUtil.requestBodyWithHeader("/api/jsk/enterpriseBusiness/selectPageBidNoticeInfo", BeanUtil.beanToMap(body, false, false), headerMap);
        return BeanUtil.toBean(map, R.class);
    }

    public R bidNoticeProvince(EnterpriseBidNoticePageBody body) throws Exception {
        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("Api-Version", "1.0");
        Map<String, Object> map = dskOpenApiUtil.requestBodyWithHeader("/api/jsk/enterpriseBusiness/selectBidProvinceByCompanyId", BeanUtil.beanToMap(body, false, false), headerMap);
        return BeanUtil.toBean(map, R.class);
    }

    public R bidNoticeTenderStage(EnterpriseBidNoticePageBody body) throws Exception {
        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("Api-Version", "1.0");
        Map<String, Object> map = dskOpenApiUtil.requestBodyWithHeader("/api/jsk/enterpriseBusiness/selectTenderStageByCompanyId", BeanUtil.beanToMap(body, false, false), headerMap);
        return BeanUtil.toBean(map, R.class);
    }

    public R bidNoticeDeatil(EnterpriseBidNoticeDetailBody body) throws Exception {
        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("Api-Version", "1.0");
        Map<String, Object> map = dskOpenApiUtil.requestBodyWithHeader("/api/jsk/enterpriseBusiness/bid/detail", BeanUtil.beanToMap(body, false, false), headerMap);
        return BeanUtil.toBean(map, R.class);
    }

    public TableDataInfo dynamicPage(EnterpriseDynamicPageBody body) throws Exception {
        Map<String, Object> map = dskOpenApiUtil.requestBody("/api/jsk/enterprise/dynamic", BeanUtil.beanToMap(body, false, false));
        return dskOpenApiUtil.responsePage(map);
    }

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

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

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

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

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

    public R getUipId(String companyName) throws Exception {
        Map<String, Object> params = new HashMap<>();
        params.put("companyName", companyName);
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterprise/getUipId", params);
        return BeanUtil.toBean(map, R.class);
    }

    public TableDataInfo uipSerach(EnterpriseUipSearchBody body) throws Exception {
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterprise/uipSerach", 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);
        }

        ArrayList<String> uipIds = new ArrayList<>();
        for (Object dataMap : list) {
            uipIds.add(MapUtils.getString(CommonUtils.assertAsMap(dataMap), "uipId"));
        }

        List<CustomerStatusListVo> claimStatusList = iCustomerService.selectStatusList(uipIds);

        //按照城投企业id合并两个list
        for (Object companyObj : list) {
            Map<String, Object> companyMap = CommonUtils.assertAsMap(companyObj);
            String uipId = MapUtils.getString(companyMap, "uipId");
            if (CollectionUtils.isEmpty(claimStatusList)) {
                companyMap.put("claimStatus", 0);
            }
            for (CustomerStatusListVo vo : claimStatusList) {
                if (uipId.equals(vo.getUipId())) {
                    companyMap.put("claimStatus", 1);
                }
            }
        }

        return new TableDataInfo(list, MapUtils.getInteger(data, "totalCount", 0));
    }

    public R uipGroupData() throws Exception {
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterprise/uipGroupData", new HashMap<>());
        return BeanUtil.toBean(map, R.class);
    }

    public R remark(EnterpriseRemarkBody vo) throws Exception {
        return R.ok(EncodeIdUtil.avDecode(EncodeIdUtil.binaryToUnicode(vo.mark)));
    }

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