package com.sooka.system.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sooka.common.core.controller.BaseController;
import com.sooka.common.core.domain.AjaxResult;
import com.sooka.system.domain.BaseBusinessEntity;
import com.sooka.system.service.impl.Guiji_Base_Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.HashMap;
import java.util.Map;

/**
 * 远程接口调用日志信息Controller
 * 
 * @author zhe
 * @date 2022-08-20
 */
@Controller
@RequestMapping("/system/interfaceLog")
public class Guiji_Base_Controller extends BaseController
{

    @Autowired
    private Guiji_Base_Service guiji_Base_Service;

    public static Map<String, String> mapStringToMap(String str){
        str = str.substring(1,str.length()-1);
        String[] strs = str.split(",");
        Map map = new HashMap();
        for (String string : strs){
            String key = string.split("=")[0];
            String value = string.split("=")[1];
            map.put(key, value);
        }
        return map;
    }

    /**
     * 新增日志信息
     */
    @ResponseBody
    @PostMapping("/addLog")
    public AjaxResult addLog(@RequestBody String log)
    {
        log = log.replace("\\r\\n", "");
        log = log.replace("\\\"", "\"");
        log = log.replace("\"{", "{");
        log = log.replace("}\"", "}");
        log = log.replace("@type", "_@type");
        JSONObject jsonObject = JSON.parseObject(log);
        BaseBusinessEntity baseBusinessEntity = new BaseBusinessEntity();
        baseBusinessEntity.setInterfaceinfoId(jsonObject.getString("interfaceinfoId"));
        baseBusinessEntity.setInterfaceinfoName(jsonObject.getString("interfaceinfoName"));
        baseBusinessEntity.setParam(jsonObject.getString("param"));
        baseBusinessEntity.setOperationStatus(Long.valueOf(jsonObject.getString("operationStatus")));
        baseBusinessEntity.setResults(jsonObject.getString("results"));
        baseBusinessEntity.setExceptionLog(jsonObject.getString("exceptionLog"));
        try {
            guiji_Base_Service.addLog(baseBusinessEntity);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error();
        }
        return AjaxResult.success();
    }

}
