package com.elephant.framework.galaxy.aroad.module.system.service.impl;

import com.elephant.framework.galaxy.aroad.module.system.entity.Log;
import com.elephant.framework.galaxy.aroad.module.system.mapper.LogMapper;
import com.elephant.framework.galaxy.aroad.module.system.service.LogService;
import com.elephant.framework.galaxy.aroad.module.system.vo.PreLogVO;
import com.elephant.framework.galaxy.aroad.constant.CommonConstants;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 日志表 服务实现类
 * </p>
 *
 * @author wangjian
 * @since 2019-02-21
 */
@Service
public class LogServiceImpl extends ServiceImpl<LogMapper, Log> implements LogService {
    /**
     * 批量插入前端错误日志
     *
     * @param preLogVoList 日志信息
     * @return true/false
     */
    @Override
    public Boolean saveBatchLogs(List<PreLogVO> preLogVoList) {
        List<Log> sysLogs = preLogVoList.stream()
                .map(pre -> {
                    Log log = new Log();
                    log.setType(CommonConstants.STATUS_LOCK);
                    log.setTitle(pre.getInfo());
                    log.setException(pre.getStack());
                    log.setParams(pre.getMessage());
                    log.setCreateTime(LocalDateTime.now());
                    log.setRequestUri(pre.getUrl());
                    log.setCreateBy(pre.getUser());
                    return log;
                })
                .collect(Collectors.toList());
        return this.saveBatch(sysLogs);
    }
}
