package com.wms_main.dao.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wms_main.dao.ITSysApiService;
import com.wms_main.mapper.SysApiMapper;
import com.wms_main.model.po.TSysApi;
import com.wms_main.repository.http.entity.HttpRequest;
import com.wms_main.repository.http.entity.HttpResponse;
import com.wms_main.repository.utils.StringUtils;
import com.wms_main.repository.utils.UUIDUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * api表服务实现
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class TSysApiServiceImpl extends ServiceImpl<SysApiMapper, TSysApi> implements ITSysApiService {
    /**
     *
     * @param request 请求参数
     * @param response 响应参数
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertApiLog(HttpRequest request, HttpResponse response) {
        TSysApi apiLog = new TSysApi();
        apiLog.setLogId(UUIDUtils.getNewUUID());
        apiLog.setLogTime(LocalDateTime.now());
        // 设置请求相关信息
        if (request != null) {
            apiLog.setUrl(request.getUrl());
            apiLog.setTimeout(request.getTimeout());
            if (request.getMethod() != null) {
                apiLog.setMethod(request.getMethod().name());
            }
            apiLog.setContentType(request.getContentType());
            apiLog.setData(StringUtils.formatStringWithMaxLength(request.getData(), 2048));
            apiLog.setToken(StringUtils.formatStringWithMaxLength(request.getToken(), 2048));
        }
        // 设置响应相关信息
        if (response != null) {
            apiLog.setSuccess(response.isSuccess() ? "成功" : "失败");
            apiLog.setResponseCode(response.getResponseCode());
            apiLog.setRequestMessage(StringUtils.formatStringWithMaxLength(response.getRequestMessage(), 2048));
            apiLog.setResponseMessage(StringUtils.formatStringWithMaxLength(response.getResponseMessage(), 2048));
            apiLog.setRequestTime(response.getRequestTime());
            apiLog.setResponseTime(response.getResponseTime());
            apiLog.setRequestUrl(response.getRequestUrl());
            apiLog.setUseTime(response.getUseTime());
            if (response.getException() != null) {
                apiLog.setExceptionMessage(StringUtils.formatStringWithMaxLength(response.getException().getMessage(), 2048));
            }
        }
        // 保存api日志
        save(apiLog);
        // 记录文本日志
        log.info(logText(apiLog));
    }

    /**
     * 生成日志文本
     * @param apiLog 日志对象
     * @return 字符串
     */
    private String logText(TSysApi apiLog) {
        return "\n请求方式：" + apiLog.getMethod()
                + "\n请求地址：" + apiLog.getUrl()
                + "\n请求数据：" + apiLog.getData()
                + "\n响应状态：" + apiLog.getSuccess()
                + "\n响应数据：" + apiLog.getRequestMessage();
    }
}
