
package com.jeelowcode.module.biz.controller;

import com.jeelowcode.core.framework.utils.Func;
import com.jeelowcode.core.framework.utils.FuncWeb;
import com.jeelowcode.framework.exception.JeeLowCodeException;
import com.jeelowcode.framework.global.JeeLowCodeBaseConstant;
import com.jeelowcode.framework.tenant.annotation.JeeLowCodeTenantIgnore;
import com.jeelowcode.module.biz.dto.RequestDTO;
import com.jeelowcode.module.biz.service.IDemoService;
import com.jeelowcode.framework.utils.model.global.BaseWebResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
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.pojo.CommonResult;
import com.jeelowcode.tool.framework.common.util.http.HttpUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import static com.jeelowcode.tool.framework.common.pojo.CommonResult.error;
import static com.jeelowcode.tool.framework.common.pojo.CommonResult.success;

/**
 * @author JX
 * @create 2024-02-26 14:59
 * @dedescription:
 */
@JeeLowCodeTenantIgnore
@Tag(name = "低代码框架 - 个性化接口")
@RestController
@AllArgsConstructor
@RequestMapping(JeeLowCodeBaseConstant.REQUEST_URL_START +"/demo")
public class DemoController {

    private final IDemoService demoService;

    @Autowired
    private IConfigService configService;


    @GetMapping({"/getData"})
    @ApiOperationSupport(order = 2)
    @Operation(summary = "获取demo数据")
    public BaseWebResult getData() {
        List<Map<String, Object>> dataMapList = demoService.getDemoData();
        return BaseWebResult.success(dataMapList);
    }

    @GetMapping({"/getAliToken"})
    @ApiOperationSupport(order = 3)
    @Operation(summary = "取阿里平台Token")
    public CommonResult<String> getAliToken() {
        try{
            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(configUrl.getValue()+"/_campus/open/accessToken.json",configKey.getValue(),configSecret.getValue());
            return success(token);
        }catch (Exception e){
            return error(500,e.getMessage());
        }
    }

    @GetMapping({"/getAliData"})
    @ApiOperationSupport(order = 4)
    @Operation(summary = "取阿里平台园区数据")
    public CommonResult<List<PartModel>> getAliData(String token) {
        ConfigDO configUrl = configService.getConfigByKey("aliUrl");
        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);
        return success(contentModels);
    }


    @PostMapping({"/getCustomerInterface"})
    @ApiOperationSupport(order = 5)
    @Operation(summary = "取阿里平台通用接口取数")
    public CommonResult<String> getCustomerInterface(@RequestBody RequestDTO params) {
        try{
            String result = HttpUtils.getInterfaceData(params.getUrl(),params.getParams(),params.getBody(),params.getMethod(),params.getAuthorization());
            return success(result);
        }catch (Exception e){
            return error(500,e.getMessage());
        }
    }
}
