package com.dsk.jsk.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson2.JSONObject;
import com.dsk.common.constant.CacheConstants;
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.JsonUtils;
import com.dsk.jsk.domain.JskCombineBidPageDto;
import com.dsk.jsk.domain.JskCombineCertificateDto;
import com.dsk.jsk.domain.JskCombineCountDto;
import com.dsk.jsk.domain.JskCombineSearchDto;
import com.dsk.jsk.util.RedisCache;
import com.dsk.system.utils.DskOpenApiUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.TimeUnit;

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

    @Autowired
    private DskOpenApiUtil dskOpenApiUtil;
    @Resource
    private RedisCache redisCache;

    public TableDataInfo memberList(JskCombineSearchDto dto) throws Exception {
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/combine/memberList", BeanUtil.beanToMap(dto, false, false));
        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));
        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));
        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(Map<String,Object> 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", new JSONObject());
        return BeanUtil.toBean(map, AjaxResult.class);
    }

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

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

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

    public AjaxResult centralEnterprseLocalPage(Map<String,Object> 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", new JSONObject());
        return BeanUtil.toBean(map, AjaxResult.class);
    }

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

    public TableDataInfo bidPage(JskCombineBidPageDto dto) throws Exception {
        Map<String, Object> map = dskOpenApiUtil.requestBody("/nationzj/project/combine/tenderList", BeanUtil.beanToMap(dto, false, false));
        return dskOpenApiUtil.responsePage(map);
    }

    /***
     *@Description: 获取集团logo
     *@Param:
     *@return: com.dsk.common.core.domain.R
     *@Author: Dgm
     *@date: 2023/9/12 16:05
     */
    public R combineMemberLogo(Map<String,Object> object) {
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/enterprise/combineMemberLogo", object);
        return BeanUtil.toBean(map, R.class);
    }

    /***
     *@Description: 集团统计展示调用允许
     *@Param:
     *@return: com.dsk.common.core.domain.R
     *@Author: Dgm
     *@date: 2023/9/12 16:05
     */
    public R memberCount(JskCombineCountDto dto) {
        String redisKey = CacheConstants.DATA_COMBINE + dto.getCombineId();
        if (ObjectUtil.isNotEmpty(redisKey)) {
            Map<String, Object> cacheMap = JsonUtils.parseObject(redisCache.getCacheObject(redisKey), Map.class);
            if (MapUtils.isNotEmpty(cacheMap)) {
                return R.ok(cacheMap);
            }
        }
        dto.setQualificationType(209);
        dto.setEndTime(DateUtil.formatDate(new Date()));
        Map<String, Object> paramsMap = BeanUtil.beanToMap(dto, false, false);
        Map<String, Object> map = dskOpenApiUtil.requestBody("/operate/combine/memberCount", paramsMap);
        if (ObjectUtil.isNotEmpty(map.get("data"))) {
            Map<String, Object> data = BeanUtil.beanToMap(map.get("data"));
            data.put("performance", businessCount(paramsMap));
            map.put("data", data);
            redisCache.setCacheObject(redisKey, JsonUtils.toJsonString(data), 24, TimeUnit.HOURS);
        }
        return BeanUtil.toBean(map, R.class);
    }

    public Integer businessCount(Map<String, Object> paramsMap) {
        Integer performance = 0;
        Map<String, Object> resMap = dskOpenApiUtil.requestBody("/nationzj/project/combine/projectList", paramsMap);
        Integer code = MapUtils.getInteger(resMap, "code", 300);
        Map data = MapUtils.getMap(resMap, "data", null);
        if (code.equals(HttpStatus.OK.value())) {
            performance = MapUtils.getInteger(data, "totalCount", 0);
        }
        return performance;
    }
}
