package com.zcloud.safetyDutyList.command.tasklist;

import com.alibaba.cola.exception.BizException;
import com.zcloud.safetyDutyList.domain.enums.TaskStatusEnum;
import com.zcloud.safetyDutyList.domain.gateway.tasklist.TaskDetailGateway;
import com.zcloud.safetyDutyList.domain.model.tasklist.TaskDetailE;
import com.zcloud.safetyDutyList.dto.tasklist.TaskDetailCloseCmd;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 任务详情关闭执行器
 * <p>
 * 处理关闭任务详情的业务逻辑：
 *   1. 校验任务是否存在
 *   2. 将任务状态更新为已关闭
 */
@Component
@AllArgsConstructor
public class TaskDetailCloseExe {
    private final TaskDetailGateway taskDetailGateway;

    public void execute(TaskDetailCloseCmd cmd) {
        TaskDetailE detailE = taskDetailGateway.getByTaskDetailId(cmd.getTaskDetailId());
        if (detailE == null) {
            throw new BizException("任务不存在");
        }
        detailE.setTaskStatus(TaskStatusEnum.CLOSED.getCode());
        boolean res = taskDetailGateway.update(detailE);
        if (!res) {
            throw new BizException("关闭失败");
        }
    }
}
