
package com.jeelowcode.core.framework.service.impl;


import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.jeelowcode.core.framework.entity.LogRequestApiEntity;
import com.jeelowcode.core.framework.entity.LogRequestErrorApiEntity;
import com.jeelowcode.core.framework.mapper.LogApiErrorMapper;
import com.jeelowcode.core.framework.mapper.LogApiMapper;
import com.jeelowcode.core.framework.params.LogRequestApiParam;
import com.jeelowcode.core.framework.service.IApiLogService;
import com.jeelowcode.core.framework.utils.Func;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 *  日志相关日志相关日志相关
 */
@Service
public class ApiLogServiceImpl implements IApiLogService {

	@Autowired
	private LogApiMapper logApiMapper;

	@Autowired
	private LogApiErrorMapper logApiErrorMapper;

	//请求某一个日期前的api请求日志
	@Override
	public Integer clearApiLog(Date clearDate){
		return logApiMapper.clearApiLog(clearDate);
	}

	//清除某一个日期前的api错误请求日志
	@Override
	public Integer clearApiErrorLog(Date clearDate){
		return logApiErrorMapper.clearApiLog(clearDate);
	}

	//获取info日志列表
	@Override
	public IPage<LogRequestApiEntity> getInfoApiLogPage(LogRequestApiParam param, Page page){
		LambdaQueryWrapper<LogRequestApiEntity> wrapper=new LambdaQueryWrapper<>();
		wrapper.eq(Func.isNotEmpty(param.getTenantId()),LogRequestApiEntity::getTenantId,param.getTenantId());
		wrapper.eq(Func.isNotEmpty(param.getCreateUserName()),LogRequestApiEntity::getCreateUserName,param.getCreateUserName());
		wrapper.eq(Func.isNotEmpty(param.getRequestUri()),LogRequestApiEntity::getRequestUri,param.getRequestUri());
		//处理时间
		if(Func.isNotEmpty(param.getCreateTime())){
			String createTimeStr = param.getCreateTime();
			String[] timeStrs = createTimeStr.split(",");
			DateTime startTime = DateUtil.parse(timeStrs[0],"yyyy-MM-dd HH:mm:ss");
			DateTime endTime = DateUtil.parse(timeStrs[1],"yyyy-MM-dd HH:mm:ss");
			wrapper.between(LogRequestApiEntity::getCreateTime,startTime,endTime);
		}
		wrapper.gt(Func.isNotEmpty(param.getTime()),LogRequestApiEntity::getTime,param.getTime());

		wrapper.select(LogRequestApiEntity::getId,
				LogRequestApiEntity::getTenantId,
				LogRequestApiEntity::getCreateUserName,
				LogRequestApiEntity::getCreateTime,
				LogRequestApiEntity::getRequestUri,
				LogRequestApiEntity::getIp,
				LogRequestApiEntity::getTime,
				LogRequestApiEntity::getTitle,
				LogRequestApiEntity::getModelTitle,
				LogRequestApiEntity::getRequestMethod,
				LogRequestApiEntity::getRequestParams);
		wrapper.orderByDesc(LogRequestApiEntity::getId);
		Page pages = logApiMapper.selectPage(page, wrapper);
		return pages;
	}

	//获取error日志列表
	@Override
	public IPage<LogRequestErrorApiEntity> getErrorApiLogPage(LogRequestApiParam param, Page page){
		LambdaQueryWrapper<LogRequestErrorApiEntity> wrapper=new LambdaQueryWrapper<>();
		wrapper.eq(Func.isNotEmpty(param.getTenantId()),LogRequestErrorApiEntity::getTenantId,param.getTenantId());
		wrapper.eq(Func.isNotEmpty(param.getCreateUserName()),LogRequestErrorApiEntity::getCreateUserName,param.getCreateUserName());
		wrapper.eq(Func.isNotEmpty(param.getRequestUri()),LogRequestErrorApiEntity::getRequestUri,param.getRequestUri());
		//处理时间
		if(Func.isNotEmpty(param.getCreateTime())){
			String createTimeStr = param.getCreateTime();
			String[] timeStrs = createTimeStr.split(",");
			DateTime startTime = DateUtil.parse(timeStrs[0],"yyyy-MM-dd HH:mm:ss");
			DateTime endTime = DateUtil.parse(timeStrs[1],"yyyy-MM-dd HH:mm:ss");
			wrapper.between(LogRequestErrorApiEntity::getCreateTime,startTime,endTime);
		}
		wrapper.gt(Func.isNotEmpty(param.getTime()),LogRequestErrorApiEntity::getTime,param.getTime());


		wrapper.select(LogRequestErrorApiEntity::getId,
				LogRequestErrorApiEntity::getTenantId,
				LogRequestErrorApiEntity::getCreateUserName,
				LogRequestErrorApiEntity::getCreateTime,
				LogRequestErrorApiEntity::getRequestUri,
				LogRequestErrorApiEntity::getIp,
				LogRequestErrorApiEntity::getTime,
				LogRequestErrorApiEntity::getTitle,
				LogRequestErrorApiEntity::getModelTitle,
				LogRequestErrorApiEntity::getRequestMethod,
				LogRequestErrorApiEntity::getRequestParams);
		wrapper.orderByDesc(LogRequestErrorApiEntity::getId);
		Page pages = logApiErrorMapper.selectPage(page, wrapper);
		return pages;
	}

	//获取info日志详情
	@Override
	public LogRequestApiEntity getInfoApiLogDetail(Long id){
		return logApiMapper.selectById(id);
	}
	//获取error日志详情
	@Override
	public LogRequestErrorApiEntity getErrorApiLogDetail(Long id){
		return logApiErrorMapper.selectById(id);
	}

	// 统计模块使用记录
	@Override
	public List<Map<String, Object>> countModelUsageRecords(Date begin, Date end) {
		return logApiMapper.countModelUsageRecords(begin, end);
	}

}
