package com.ag.web.api;

import cn.hutool.core.lang.Assert;
import com.ag.secp.platform.spring.BeanMange;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @description:
 * @author: fengjun
 * @time: 2022/7/21 17:00
 */
@Controller
@RequestMapping(value = "/api")
public class ApiController {
    /**
     * @Description: 非鉴权接口
     * @return: void
     * @Author: fengjun
     * @Date: 2022/7/21
     */
    @RequestMapping(value = "/apiOpen", method = RequestMethod.POST)
    public void apiOpen(@RequestBody JSONObject jsonParam, HttpServletRequest request,
                        HttpServletResponse response) throws Exception {
        run(jsonParam, request, response);
    }

    @RequestMapping(value = "/apiFileOpen", method = RequestMethod.POST)
    public void apiFileOpen(@RequestParam("file") MultipartFile[] multipartFiles,
                            HttpServletRequest request,
                            HttpServletResponse response) throws Exception {
        JSONObject jsonParam = new JSONObject();
        for (Map.Entry<String, String[]> paramMap : request.getParameterMap().entrySet()) {
            jsonParam.put( paramMap.getKey(), paramMap.getValue()[0]);
        }
        run(jsonParam, request, response);
    }


    /**
    * @Description: 鉴权接口
    * @return: void
    * @Author: fengjun
    * @Date: 2022/7/21
    */
    @RequestMapping(value = "/apiAuth", method = RequestMethod.POST)
    public void apiAuth(@RequestBody JSONObject jsonParam, HttpServletRequest request,
                             HttpServletResponse response) throws Exception {
        Assert.isTrue(request.getSession()!=null,"请登录后尝试!");
        run(jsonParam, request, response);
    }

    public void run(JSONObject jsonParam, HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map map = new HashMap();
        for (Map.Entry<String, String[]> paramMap : request.getParameterMap().entrySet()) {
            map.put(paramMap.getKey(), paramMap.getValue()[0]);
        }
        BeanMange beanMange = new BeanMange(request,response,jsonParam.getString("beanname"),jsonParam.getString("method"),map);
        Set<String> params = jsonParam.keySet();
        for (String param : params) {
            if ("beanname".equals(param)||"method".equals(param)){
                continue;
            }
            if ("username".equals(param)){
                request.getSession().setAttribute("sys.username", jsonParam.getString(param));
            }
            request.setAttribute(param, jsonParam.getString(param));
        }
        Object execute = beanMange.execute();
    }
}
