package com.glxp.mipsdl.service.system;

import cn.hutool.core.thread.ThreadUtil;
import com.glxp.mipsdl.constant.ConstantType;
import com.glxp.mipsdl.dao.system.SysErpLogDao;
import com.glxp.mipsdl.entity.system.SysErpLogEntity;
import com.glxp.mipsdl.util.ExecutorUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 日志操作Service
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class SysErpLogService {

    @Resource
    private ExecutorUtil executorUtil;
    @Resource
    private SysErpLogDao sysErpLogDao;

    /**
     * 添加接口日志
     *
     * @param createTime
     * @param uri
     * @param method
     * @param params
     * @param result
     * @param time
     */
    public void addInterfaceErpLog(Date createTime, String uri, String method, String params, String result, long time) {
        executorUtil.getLogExecutor().submit(ThreadUtil.newThread(() -> {
            SysErpLogEntity logEntity = new SysErpLogEntity();
            logEntity.setUrl(uri);
            logEntity.setName(method);
            logEntity.setParams(params);
            logEntity.setResult(result);
            logEntity.setCreateTime(createTime);
            logEntity.setType(ConstantType.LOG_TYPE_INTERFACE);
            logEntity.setStatus(ConstantType.LOG_STATUS_SUCCESS);
            logEntity.setTime(time);
            sysErpLogDao.insert(logEntity);
        }, "addInterfaceErpLog"));
    }

    /**
     * 添加请求第三方接口日志
     *
     * @param createTime
     * @param uri
     * @param method
     * @param params
     * @param result
     * @param time
     */
    public void addThirdErpLog(Date createTime, String uri, String method, String params, String result, long time, Integer status) {
        executorUtil.getLogExecutor().submit(ThreadUtil.newThread(() -> {
            SysErpLogEntity logEntity = new SysErpLogEntity();
            logEntity.setUrl(uri);
            logEntity.setName(method);
            logEntity.setParams(params);
            logEntity.setResult(result);
            logEntity.setCreateTime(createTime);
            logEntity.setType(ConstantType.LOG_TYPE_THIRD);
            logEntity.setStatus(status);
            logEntity.setTime(time);
            sysErpLogDao.insert(logEntity);
        }, "addThirdErpLog"));
    }

}
