package com.dsk.jsk.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
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.jsk.domain.*;
import com.dsk.system.utils.DskOpenApiUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
 * @ClassName CompanyRelationTableV1Controller
 * @Description
 * @Author Dgm
 * @Date 2023/12/13 11:28
 * @Version
 */
@Slf4j
@Service
public class CompanyRelationTableV1Service {

    @Autowired
    private DskOpenApiUtil dskOpenApiUtil;

    public TableDataInfo page(JskCompanyRelationTableV1Dto dto) throws Exception {
        Map<String, Object> map = null;
        if (dto.getCompanyType().equals(3)) {
            map = dskOpenApiUtil.requestBody("/operate/consultancy/combinePage", BeanUtil.beanToMap(dto, false, false));
        } else {
            map = dskOpenApiUtil.requestBody("/operate/consultancy/page", BeanUtil.beanToMap(dto, false, false));
        }
        return dskOpenApiUtil.responsePage(map);
    }

    public TableDataInfo detailPage(JskCompanyRelationTableV1DetailPageDto dto) throws Exception {
        Map<String, Object> map = null;
        if (dto.getCompanyType().equals(3) || dto.getCompanyType().equals(4)) {
            map = dskOpenApiUtil.requestBody("/operate/consultancy/combineDetailPage", BeanUtil.beanToMap(dto, false, false));
        } else {
            map = dskOpenApiUtil.requestBody("/operate/consultancy/detailPage", BeanUtil.beanToMap(dto, 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);
        }
        for (Object dataMap : list) {
            Map<String, Object> companyMap = CommonUtils.assertAsMap(dataMap);
            String agencyBusinessType = MapUtils.getString(companyMap, "agencyBusinessType", "");
            companyMap.put("agencyBusinessType", agencyBusinessType(agencyBusinessType, dto.getBusinessTypes()));
        }
        return dskOpenApiUtil.responsePage(map);
    }
    
    /***
     *@Description: 取其一
     *@Param: 
     *@return: java.lang.String
     *@Author: Dgm
     *@date: 2024/1/10 15:48
     */
    private String agencyBusinessType (String name, List<String> businessTypes) {
        List<String> businessType = new ArrayList<>();
        if (ObjectUtil.isNotEmpty(name)) {
            name = name.replaceAll("\\['","");
            name = name.replaceAll("']", "");
            name = name.replaceAll("'", "");
            name = name.replaceAll(" ", "");
            if (name.contains(",")) {
                String[] split = name.split(",");
                for (String s : split) {
                    businessType.add(s);
                }
            } else {
                businessType.add(name);
            }
        }
        String bTypes = null;
        if (ObjectUtil.isNotEmpty(businessType)) {
            bTypes = businessType.get(0);
        }
        return ObjectUtil.isNotEmpty(businessTypes) ?businessTypes.get(0): bTypes;
    }


    /***
     *@Description: -分页列表-下拉
     *@Param:
     *@return: com.dsk.common.core.domain.AjaxResult
     *@Author: Dgm
     *@date: 2023/5/18 10:29
     */
    public R pageSelect(JskCompanyRelationTableV1Dto dto) throws Exception{
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/consultancy/pageSelect", BeanUtil.beanToMap(dto, false, false));
        return BeanUtil.toBean(map, R.class);
    }

    /***
     *@Description: -分页列表-下拉
     *@Param:
     *@return: com.dsk.common.core.domain.AjaxResult
     *@Author: Dgm
     *@date: 2023/5/18 10:29
     */
    public R statistics(JskCompanyRelationTableV1DetailPageDto dto) throws Exception{
        Map<String, Object> map = null;
        if (dto.getCompanyType().equals(3) || dto.getCompanyType().equals(4)) {
            map = dskOpenApiUtil.requestBody("/operate/consultancy/combineDetail/statistics", BeanUtil.beanToMap(dto, false, false));
        } else {
            map = dskOpenApiUtil.requestBody("/operate/consultancy/statistics", BeanUtil.beanToMap(dto, false, false));
        }
        return BeanUtil.toBean(map, R.class);
    }

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