package com.ruoyi.sim.service.impl;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.sim.mapper.TaskFaultMapper;
import com.ruoyi.sim.domain.TaskFault;
import org.springframework.transaction.annotation.Transactional;

/**
 * 任务故障关联Service业务层处理
 *
 * @author tom
 * @date 2024-12-13
 */
@Service
public class TaskFaultService {
    @Autowired
    private TaskFaultMapper taskFaultMapper;

    /**
     * 查询任务故障关联
     *
     * @param relId 任务故障关联主键
     * @return 任务故障关联
     */
    public TaskFault selectTaskFaultByRelId(Long relId) {
        return taskFaultMapper.selectTaskFaultByRelId(relId);
    }

    /**
     * 查询任务故障关联列表
     *
     * @param taskFault 任务故障关联
     * @return 任务故障关联
     */
    public List<TaskFault> selectTaskFaultList(TaskFault taskFault) {
        return taskFaultMapper.selectTaskFaultList(taskFault);
    }

    /**
     * 新增任务故障关联
     *
     * @param taskFault 任务故障关联
     * @return 结果
     */
    public int insertTaskFault(TaskFault taskFault) {
        return taskFaultMapper.insertTaskFault(taskFault);
    }

    /**
     * 修改任务故障关联
     *
     * @param taskFault 任务故障关联
     * @return 结果
     */
    public int updateTaskFault(TaskFault taskFault) {
        taskFault.setUpdateTime(DateUtils.getNowDate());
        return taskFaultMapper.updateTaskFault(taskFault);
    }

    /**
     * 批量删除任务故障关联
     *
     * @param relIds 需要删除的任务故障关联主键
     * @return 结果
     */
    public int deleteTaskFaultByRelIds(Long[] relIds) {
        return taskFaultMapper.deleteTaskFaultByRelIds(relIds);
    }

    /**
     * 删除任务故障关联信息
     *
     * @param relId 任务故障关联主键
     * @return 结果
     */
    public int deleteTaskFaultByRelId(Long relId) {
        return taskFaultMapper.deleteTaskFaultByRelId(relId);
    }

    // -------------------------------- tom add  --------------------------------
    public TaskFault selectUniqueTaskFault(Long taskId, String faultId) {
        TaskFault q = new TaskFault();
        q.setTaskId(taskId);
        q.setFaultId(faultId);
        List<TaskFault> list = selectTaskFaultList(q);
        if (list != null && !list.isEmpty()) {
            return list.get(0);
        } else {
            return null;
        }
    }


    public int insertTaskFault(Long taskId, String faultId, String flag) {
        TaskFault n = new TaskFault(0L, taskId, faultId, flag);
        return insertTaskFault(n);
    }


    public int updateTaskFault(Long relId, Long taskId, String faultId, String flag) {
        TaskFault f = selectTaskFaultByRelId(relId);
        f.setTaskId(taskId);
        f.setFaultId(faultId);
        f.setFlag(flag);
        return updateTaskFault(f);
    }


    public int insertOrUpdateTaskFault(TaskFault tf) {
        TaskFault f = selectUniqueTaskFault(tf.getTaskId(), tf.getFaultId());
        if (f == null) {
            return insertTaskFault(tf);
        } else {
            return updateTaskFault(tf);
        }
    }

    public int deleteTaskFaultByTaskId(Long taskId) {
        TaskFault q = new TaskFault();
        q.setTaskId(taskId);
        List<TaskFault> list = selectTaskFaultList(q);
        if (list == null || list.isEmpty()) {
            return 0;
        }
        for (TaskFault o : list) {
            deleteTaskFaultByRelId(o.getRelId());
        }
        return list.size();
    }
}
