package top.tqx.demo_1.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.tqx.demo_1.common.PageResult;
import top.tqx.demo_1.entity.OperationLog;
import top.tqx.demo_1.mapper.OperationLogMapper;
import top.tqx.demo_1.service.LogService;
import top.tqx.demo_1.vo.OperationLogVO;

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

@Service
public class LogServiceImpl implements LogService {
    
    @Autowired
    private OperationLogMapper operationLogMapper;
    
    /**
     * 记录操作日志
     */
    @Override
    public void recordLog(Long userId, String operation, String content) {
        OperationLog log = new OperationLog();
        log.setUserId(userId);
        log.setOperationType(operation);
        log.setOperationDesc(content);
        log.setCreateTime(LocalDateTime.now());
        
        operationLogMapper.insert(log);
    }
    
    /**
     * 分页查询操作日志
     */
    @Override
    public PageResult<OperationLogVO> pageLogs(Integer pageNum, Integer pageSize, String username, String operationType) {
        Page<OperationLog> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<OperationLog> wrapper = new LambdaQueryWrapper<>();
        
        if (operationType != null && !operationType.isEmpty()) {
            wrapper.eq(OperationLog::getOperationType, operationType);
        }
        
        if (username != null && !username.isEmpty()) {
            wrapper.like(OperationLog::getUsername, username);
        }
        
        wrapper.orderByDesc(OperationLog::getCreateTime);
        
        Page<OperationLog> logPage = operationLogMapper.selectPage(page, wrapper);
        
        List<OperationLogVO> voList = logPage.getRecords().stream()
                .map(this::convertToVO)
                .collect(Collectors.toList());
        
        return PageResult.of(voList, logPage.getTotal(), logPage.getSize(), logPage.getCurrent());
    }
    
    /**
     * 获取用户操作日志
     */
    @Override
    public List<OperationLogVO> getUserLogs(Long userId) {
        List<OperationLog> logs = operationLogMapper.selectByUserId(userId);
        return logs.stream()
                .map(this::convertToVO)
                .collect(Collectors.toList());
    }
    
    /**
     * 获取文件操作日志
     */
    @Override
    public List<OperationLogVO> getFileLogs(Long fileId) {
        List<OperationLog> logs = operationLogMapper.selectByTargetId(fileId, "FILE");
        return logs.stream()
                .map(this::convertToVO)
                .collect(Collectors.toList());
    }
    
    private OperationLogVO convertToVO(OperationLog log) {
        OperationLogVO vo = new OperationLogVO();
        BeanUtils.copyProperties(log, vo);
        vo.setUserName(log.getUsername());
        vo.setOperation(log.getOperationType());
        vo.setContent(log.getOperationDesc());
        vo.setIp(log.getIpAddress());
        return vo;
    }
}