package com.xy.xyaicpzs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xy.xyaicpzs.domain.entity.OperationHistory;
import com.xy.xyaicpzs.service.OperationHistoryService;
import com.xy.xyaicpzs.mapper.OperationHistoryMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

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

/**
* @author XY003
* @description 针对表【operation_history(操作历史记录表)】的数据库操作Service实现
* @createDate 2025-06-19 14:51:51
*/
@Service
@Slf4j
public class OperationHistoryServiceImpl extends ServiceImpl<OperationHistoryMapper, OperationHistory>
    implements OperationHistoryService{

    @Override
    public void recordOperation(Long userId, String operationType, Integer operationModule, 
                               String operationResult, String resultMessage) {
        try {
            OperationHistory operationHistory = new OperationHistory();
            operationHistory.setUserId(userId);
            operationHistory.setOperationType(operationType);
            operationHistory.setOperationModule(operationModule);
            operationHistory.setOperationResult(operationResult);
            operationHistory.setResultMessage(resultMessage);
            operationHistory.setOperationTime(new Date());
            operationHistory.setUpdateTime(new Date());
            
            save(operationHistory);
            
            log.info("操作历史记录成功 - 用户ID: {}, 操作类型: {}, 操作模块: {}, 操作结果: {}", 
                    userId, operationType, operationModule, operationResult);
        } catch (Exception e) {
            log.error("记录操作历史失败 - 用户ID: {}, 操作类型: {}, 错误信息: {}", 
                     userId, operationType, e.getMessage(), e);
        }
    }

    @Override
    public List<OperationHistory> getOperationHistoryByUserIdAndModule(Long userId, Integer operationModule) {
        QueryWrapper<OperationHistory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", userId)
                   .eq("operationModule", operationModule)
                   .orderByDesc("operationTime");
        return list(queryWrapper);
    }
    
    @Override
    public List<OperationHistory> getOperationHistoryByModule(Integer operationModule) {
        QueryWrapper<OperationHistory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("operationModule", operationModule)
                   .orderByDesc("operationTime");
        return list(queryWrapper);
    }
}




