package com.zcloud.safetyDutyList.command.tasklist;

import com.alibaba.cola.exception.BizException;
import com.zcloud.safetyDutyList.domain.gateway.tasklist.TaskListGateway;
import com.zcloud.safetyDutyList.domain.gateway.tasklist.TaskListIssueGateway;
import com.zcloud.safetyDutyList.domain.model.tasklist.TaskListE;
import com.zcloud.safetyDutyList.domain.model.tasklist.TaskListIssueE;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 清单删除执行器
 * <p>
 * 根据主键ID删除安全责任清单
 */
@Component
@AllArgsConstructor
public class TaskListRemoveExe {
    private final TaskListGateway taskListGateway;

    private final TaskListIssueGateway taskListIssueGateway;

    public void execute(Long id) {

        TaskListE taskListE = taskListGateway.getById(id);
        if (taskListE == null) {
            return;
        }

        List <TaskListIssueE> taskListIssueEList = taskListIssueGateway.listByTaskListId(taskListE.getTaskListId());
        if (taskListIssueEList != null && !taskListIssueEList.isEmpty()) {
            throw new BizException("该清单已下发，请先取消下发再删除");
        }

        boolean res = taskListGateway.deleteById(id);
        if (!res) {
            throw new BizException("删除失败");
        }
    }
}
