package com.lutao.cloud.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lutao.cloud.admin.api.dto.SysLogDTO;
import com.lutao.cloud.admin.api.dto.SysLogsDTO;
import com.lutao.cloud.admin.api.entity.SysLog;
import com.lutao.cloud.admin.api.vo.PreLogVO;
import com.lutao.cloud.admin.mapper.SysLogMapper;
import com.lutao.cloud.admin.service.SysLogService;
import com.lutao.cloud.common.core.constant.CommonConstants;
import com.lutao.cloud.common.core.util.StringUtil;
import com.lutao.cloud.common.data.tenant.TenantBroker;
import com.lutao.cloud.common.data.tenant.TenantContextHolder;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * <p>
 * 日志表 服务实现类
 * </p>
 *

 * @since 2017-11-20
 */
@Service
public class SysLogServiceImpl extends ServiceImpl<SysLogMapper, SysLog> implements SysLogService{

	/**
	 * 批量插入前端错误日志
	 * @param preLogVoList 日志信息
	 * @return true/false
	 */
	@Override
	public Boolean saveBatchLogs(List<PreLogVO> preLogVoList){
		List<SysLog> sysLogs = preLogVoList.stream().map(pre ->{
			SysLog log = new SysLog();
			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);
	}

	/**
	 * 根据条件分页查询日志
	 * @param page
	 * @param sysLog
	 * @return
	 */
	@Override
	public Page getLogByPage(Page page, SysLogsDTO sysLog){

		LambdaQueryWrapper<SysLog> wrapper = Wrappers.lambdaQuery();
//		if (StrUtil.isNotBlank(sysLog.getType())){
//			wrapper.eq(SysLog::getType, sysLog.getType());
//		}

		// 操作人
		if (StringUtil.isNotBlank(sysLog.getCreateBy())) {
			wrapper.eq(SysLog::getCreateBy, sysLog.getCreateBy());
		}

		// 开始时间，结束时间
		if (sysLog.getStartTime() != null) {
			LocalDateTime start = sysLog.getStartTime().atTime(0,0,0);
			LocalDateTime end = sysLog.getEndTime().atTime(23,59,59,999);
			wrapper.between(SysLog::getCreateTime,start,end);
		}

		// 搜索关键字
		if (StringUtil.isNotBlank(sysLog.getSearchKey())) {
			wrapper.and(wr -> wr.like(SysLog::getTitle,sysLog.getSearchKey())
					.or().like(SysLog::getParams,sysLog.getSearchKey()));
		}
		wrapper.orderByDesc(SysLog::getCreateTime);

		return baseMapper.selectPage(page, wrapper);
	}

	/**
	 * 插入日志
	 * @param sysLog 日志对象
	 * @return true/false
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public Boolean saveLog(SysLogDTO sysLog){
		TenantBroker.applyAs(sysLog::getTenantId, tenantId ->{
			TenantContextHolder.setTenantId(tenantId);
			SysLog log = new SysLog();
			BeanUtils.copyProperties(sysLog, log, "createTime");
			return baseMapper.insert(log);
		});
		return Boolean.TRUE;
	}

}
