package cn.exploring.engine.server.db.util;

import com.alibaba.fastjson.JSON;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 对于log记录的一些通用方法
 */
public class LogRecordUtil {
    /**
     * 通用记录log的格式数据
     * @param serviceInfo
     * @param userId
     * @param objects
     * @return
     */
    public static String commonBuildLog(String serviceInfo, Integer userId, String detail, Object... objects) {
        List<String> objectMapList = Arrays.stream(objects)
                .map(ob -> JSON.toJSONString(ob))
                .collect(Collectors.toList());
        DateTimeFormatter commonDateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-mm-dd HH:MM:SS");
        List<String> resultStringList = new ArrayList();
        resultStringList.add(serviceInfo);
        resultStringList.add(userId.toString());
        resultStringList.add(detail);
        resultStringList.add(LocalDateTime.now().format(commonDateTimeFormatter));
        resultStringList.addAll(objectMapList);
        String logInfo = String.join("||", resultStringList);
        return logInfo;
    }
}
