package com.haha.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.haha.entity.OperationLog;
import com.haha.mapper.OperationLogMapper;
import com.haha.service.OperationLogService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 操作日志服务实现类
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class OperationLogServiceImpl implements OperationLogService {
    
    private final OperationLogMapper operationLogMapper;
    
    @Override
    public Page<OperationLog> pageList(Page<OperationLog> page, String module, String username, 
                                        Integer status, LocalDateTime startTime, LocalDateTime endTime) {
        LambdaQueryWrapper<OperationLog> wrapper = new LambdaQueryWrapper<>();
        
        if (StringUtils.hasText(module)) {
            wrapper.like(OperationLog::getModule, module);
        }
        if (StringUtils.hasText(username)) {
            wrapper.like(OperationLog::getAdminName, username);
        }
        if (status != null) {
            wrapper.eq(OperationLog::getStatus, status);
        }
        if (startTime != null) {
            wrapper.ge(OperationLog::getCreateTime, startTime);
        }
        if (endTime != null) {
            wrapper.le(OperationLog::getCreateTime, endTime);
        }
        
        wrapper.orderByDesc(OperationLog::getCreateTime);
        
        return operationLogMapper.selectPage(page, wrapper);
    }
    
    @Override
    public OperationLog getById(Long id) {
        return operationLogMapper.selectById(id);
    }
    
    @Override
    public void save(OperationLog operationLog) {
        if (operationLog.getCreateTime() == null) {
            operationLog.setCreateTime(LocalDateTime.now());
        }
        operationLogMapper.insert(operationLog);
    }
    
    @Override
    @Transactional
    public void deleteBatch(List<Long> ids) {
        if (ids != null && !ids.isEmpty()) {
            operationLogMapper.deleteBatchIds(ids);
        }
    }
    
    @Override
    @Transactional
    public void clearAll() {
        LambdaQueryWrapper<OperationLog> wrapper = new LambdaQueryWrapper<>();
        wrapper.isNotNull(OperationLog::getId);
        operationLogMapper.delete(wrapper);
    }
    
    @Override
    @Transactional
    public void clearBeforeTime(LocalDateTime beforeTime) {
        LambdaQueryWrapper<OperationLog> wrapper = new LambdaQueryWrapper<>();
        wrapper.lt(OperationLog::getCreateTime, beforeTime);
        operationLogMapper.delete(wrapper);
    }
    
    @Override
    public List<Map<String, Object>> countByModule(LocalDateTime startTime) {
        return operationLogMapper.countByModule(startTime);
    }
    
    @Override
    public List<Map<String, Object>> countByStatus(LocalDateTime startTime) {
        return operationLogMapper.countByStatus(startTime);
    }
    
    @Override
    public List<Map<String, Object>> countByDate(LocalDateTime startTime) {
        return operationLogMapper.countByDate(startTime);
    }
    
    @Override
    public Map<String, Object> getStatistics() {
        Map<String, Object> result = new HashMap<>();
        
        // 今日开始时间
        LocalDateTime todayStart = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        // 近7天开始时间
        LocalDateTime weekStart = LocalDateTime.of(LocalDate.now().minusDays(7), LocalTime.MIN);
        
        // 今日操作数
        LambdaQueryWrapper<OperationLog> todayWrapper = new LambdaQueryWrapper<>();
        todayWrapper.ge(OperationLog::getCreateTime, todayStart);
        Long todayCount = operationLogMapper.selectCount(todayWrapper);
        result.put("todayCount", todayCount);
        
        // 近7天操作数
        LambdaQueryWrapper<OperationLog> weekWrapper = new LambdaQueryWrapper<>();
        weekWrapper.ge(OperationLog::getCreateTime, weekStart);
        Long weekCount = operationLogMapper.selectCount(weekWrapper);
        result.put("weekCount", weekCount);
        
        // 今日成功数
        LambdaQueryWrapper<OperationLog> successWrapper = new LambdaQueryWrapper<>();
        successWrapper.ge(OperationLog::getCreateTime, todayStart);
        successWrapper.eq(OperationLog::getStatus, OperationLog.STATUS_SUCCESS);
        Long todaySuccess = operationLogMapper.selectCount(successWrapper);
        result.put("todaySuccess", todaySuccess);
        
        // 今日失败数
        LambdaQueryWrapper<OperationLog> failWrapper = new LambdaQueryWrapper<>();
        failWrapper.ge(OperationLog::getCreateTime, todayStart);
        failWrapper.eq(OperationLog::getStatus, OperationLog.STATUS_FAIL);
        Long todayFail = operationLogMapper.selectCount(failWrapper);
        result.put("todayFail", todayFail);
        
        // 各模块操作统计
        List<Map<String, Object>> moduleStats = countByModule(weekStart);
        result.put("moduleStats", moduleStats);
        
        // 每日操作趋势
        List<Map<String, Object>> dailyStats = countByDate(weekStart);
        result.put("dailyStats", dailyStats);
        
        return result;
    }
}
