package com.aegis.emergency.dispatch.service.impl;

import com.aegis.emergency.dispatch.domain.DispatchTaskLog;
import com.aegis.emergency.dispatch.mapper.DispatchTaskLogMapper;
import com.aegis.emergency.dispatch.service.IDispatchTaskLogService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import org.springframework.stereotype.Service;

/**
 * 调度任务操作日志 Service 实现
 *
 * @author aegis
 */
@Service
public class DispatchTaskLogServiceImpl
        extends ServiceImpl<DispatchTaskLogMapper, DispatchTaskLog>
        implements IDispatchTaskLogService {

    @Override
    public List<DispatchTaskLog> listByTaskId(Long taskId) {
        if (taskId == null) {
            return Collections.emptyList();
        }
        return this.list(new LambdaQueryWrapper<DispatchTaskLog>()
            .eq(DispatchTaskLog::getTaskId, taskId)
            .orderByDesc(DispatchTaskLog::getCreateTime));
    }

    @Override
    public boolean recordLog(Long taskId, String action, String payload, Long operatorId, String operatorName) {
        DispatchTaskLog log = new DispatchTaskLog();
        log.setTaskId(taskId);
        log.setAction(action);
        log.setPayload(payload);
        log.setOperatorId(operatorId);
        log.setOperatorName(operatorName);
        log.setCreateTime(new Date());
        return this.save(log);
    }
}