package com.fufan.mtsaas.modules.base.intflog.service;

import com.baomidou.mybatisplus.plugins.Page;
import com.fufan.mtsaas.modules.base.intflog.dao.TIntfLogDao;
import com.fufan.mtsaas.modules.base.intflog.model.TIntfLog;
import com.fufan.mtsaas.modules.common.bo.response.RequestPojo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * (IntfLogUtil)日志工具
 *
 * @author Liby
 * @since 2023-02-03 11:08:38
 */

@Component
public class IntfLogUtil {
    @Autowired
    private TIntfLogService logService;

    @Autowired
    private TIntfLogDao tIntfLogDao;

    private static TIntfLogService logServices;
    private static TIntfLogDao intfLogDao;

    @PostConstruct
    public void init() {
        logServices = logService;
        intfLogDao = tIntfLogDao;
    }

    public final static void processLog(String fromSys, String targSys, String processStatus,
        String requestMsg, String responseMsg, String exceptionMsg, String exceptionResponser,
        String intfType, String key, RequestPojo requestPojo, String orignalRequestMsg, String orignalResponseMsg, String url){

        Page page = new Page();
        HashMap<String, String> mapData = new HashMap();
        mapData.put("uniqueKey", key);
        mapData.put("interfaceType", intfType);
//        mapData.put("createTime", DateUtil.formatDate(new Date(), DateUtil.DATE_FORMAT));
        List<TIntfLog> list = intfLogDao.selectList(page, mapData);
        if(Objects.nonNull(list) && list.size() > 0){
            TIntfLog logData = list.get(0);
            logData.setFromSys(fromSys);
            logData.setTargSys(targSys);
            logData.setProcessStatus(processStatus);
            logData.setRequestMsg(requestMsg + "\n\n" + orignalRequestMsg);
            logData.setResponseMsg(responseMsg + "\n\n" + orignalResponseMsg);
            logData.setExceptionMsg(exceptionMsg);
            logData.setExceptionResponser(exceptionResponser);
            logData.setUpdateBy(requestPojo.getUserName());
            logData.setUpdateTime(new Date());
            logData.setInterfaceType(intfType);
            logData.setUniqueKey(key);
            logData.setErrorCount(logData.getErrorCount()+1);
            logData.setRequestUrl(url);
            logServices.update(logData);
        }else {
            TIntfLog logData = new TIntfLog();
            logData.setFromSys(fromSys);
            logData.setTargSys(targSys);
            logData.setProcessStatus(processStatus);
            logData.setRequestMsg(requestMsg + "\n\n" + orignalRequestMsg);
            logData.setResponseMsg(responseMsg + "\n\n" + orignalResponseMsg);
            logData.setExceptionMsg(exceptionMsg);
            logData.setExceptionResponser(exceptionResponser);
            logData.setCreateBy(requestPojo.getUserName());
            logData.setCreateTime(new Date());
            logData.setInterfaceType(intfType);
            logData.setUniqueKey(key);
            logData.setUpdateBy(requestPojo.getUserName());
            logData.setUpdateTime(new Date());
            logData.setCompanyId(Long.valueOf(requestPojo.getCustId()));
            logData.setIsEnabled(0);
            if(processStatus.equalsIgnoreCase("E")) {
                logData.setErrorCount(1);
            }else{
                logData.setErrorCount(0);
            }
            logData.setRequestUrl(url);
            logServices.insert(logData);
        }
    }
}
