package shop.alien.store.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import shop.alien.entity.store.OperationLog;
import shop.alien.mapper.OperationLogMapper;
import shop.alien.store.service.OperationLogService;

/**
 * 操作日志服务实现类
 *
 * @author ssk
 * @since 2025-12-09
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class OperationLogServiceImpl extends ServiceImpl<OperationLogMapper, OperationLog> implements OperationLogService {

    @Override
    public void saveOperationLog(OperationLog operationLog) {
        try {
            // 確保操作時間不為空
            if (operationLog.getOperationTime() == null) {
                operationLog.setOperationTime(new java.util.Date());
            }
            
            // 確保操作模組不為空
            if (operationLog.getOperationModule() == null || operationLog.getOperationModule().isEmpty()) {
                operationLog.setOperationModule("未知模組");
            }
            
            // 確保操作類型不為空
            if (operationLog.getOperationType() == null || operationLog.getOperationType().isEmpty()) {
                operationLog.setOperationType("其他");
            }
            
            this.save(operationLog);
            log.info("操作日志保存成功: 模組={}, 類型={}, 內容={}", 
                    operationLog.getOperationModule(), 
                    operationLog.getOperationType(), 
                    operationLog.getOperationContent());
        } catch (Exception e) {
            log.error("保存操作日志失败: 模組={}, 類型={}, 內容={}", 
                    operationLog.getOperationModule(), 
                    operationLog.getOperationType(), 
                    operationLog.getOperationContent(), e);
            // 这里可以选择是否抛出异常，为了不影响主业务流程，这里只记录日志
        }
    }
}

