package com.jeelowcode.module.biz.enhance;


import com.jeelowcode.core.framework.config.aspect.enhancereport.model.EnhanceReportContext;
import com.jeelowcode.core.framework.config.aspect.enhancereport.plugin.ReportAfterAdvicePlugin;
import com.jeelowcode.core.framework.utils.Func;
import com.jeelowcode.core.framework.utils.FuncWeb;
import com.jeelowcode.framework.exception.JeeLowCodeException;
import com.jeelowcode.framework.exception.JeeLowCodeMoreException;
import com.jeelowcode.framework.utils.component.redis.JeeLowCodeRedisUtils;
import com.jeelowcode.service.infra.entity.ConfigDO;
import com.jeelowcode.service.infra.service.IConfigService;
import com.jeelowcode.tool.framework.common.model.ContentModel;
import com.jeelowcode.tool.framework.common.model.PartModel;
import com.jeelowcode.tool.framework.common.util.http.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 取阿里平台数据
 */
@Slf4j
@Component("aliDataGetEnhance")
public class AliDataGetEnhance implements ReportAfterAdvicePlugin {

    @Autowired
    private JeeLowCodeRedisUtils redisUtils;

    @Autowired
    private IConfigService configService;

    @Override
    public void execute(EnhanceReportContext enhanceContext) {
        //传入取值代码及参数
        List<Map<String, Object>> resultList = new ArrayList<>();
        ConfigDO configUrl = configService.getConfigByKey("aliUrl");
        ConfigDO configKey = configService.getConfigByKey("aliAppkey");
        ConfigDO configSecret = configService.getConfigByKey("aliAppSecret");

        if(Objects.isNull(configUrl) || Objects.isNull(configKey) || Objects.isNull(configSecret)
                || StringUtils.isEmpty(configUrl.getValue()) || StringUtils.isEmpty(configKey.getValue()) || StringUtils.isEmpty(configSecret.getValue())){
            throw new JeeLowCodeException("缺少阿里平台必要参数配置");
        }
        // String token = HttpUtils.getToken("http://10.0.160.55","campusT0Yoh1U6tXD3ZVXy","3d4d89b5-8a14-4b83-a9aa-715bdc8264a1");
        String token = "";
        if(!redisUtils.hasKey("aliToken")){
            token = HttpUtils.getToken(configUrl.getValue(),configKey.getValue(),configSecret.getValue());
            redisUtils.set("aliToken",token,7000);
        }
        log.info("token:"+token);
        if(!StringUtils.isEmpty(token)){
            String bodyString = "{\n" +
                    "    \"api\": \"/space/campus/list\",\n" +
                    "    \"params\": [\n" +
                    "        {\n" +
                    "            \"pageSize\": \"10000\",\n" +
                    "            \"page\": 1\n" +
                    "        }\n" +
                    "    ]\n" +
                    "}";
            List<PartModel> contentModels = HttpUtils.getTokenDate(configUrl.getValue()+"/_campus/open/api/invoked.json?access_token="+token,bodyString);
            log.info("returnMsg：" + contentModels.toString());
            if (Func.isNotEmpty(contentModels)) {
                resultList = contentModels.stream()
                        .map(contentModel -> {
                            Map<String, Object> map = new HashMap<>();
                            map.put(contentModel.getCumpusId(), contentModel);
                            return map;
                        })
                        .collect(Collectors.toList());
                FuncWeb.setReportResult(enhanceContext, resultList);
            }
        }
    }
}
