package com.zcloud.domain.command;

import com.alibaba.cola.exception.BizException;
import com.zcloud.domain.domain.enums.ProjectTaskExecuteStatusEnum;
import com.zcloud.domain.domain.gateway.ProjectTaskGateway;
import com.zcloud.domain.persistence.dataobject.ProjectTaskDO;
import com.zcloud.domain.persistence.repository.ProjectTaskRepository;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;


/**
 * web-app
 *
 * @Author zhaokai
 * @Date 2026-01-29 11:28:41
 */
@Component
@AllArgsConstructor
public class ProjectTaskRemoveExe {
    private final ProjectTaskGateway projectTaskGateway;
    private final ProjectTaskRepository projectTaskRepository;

    @Transactional(rollbackFor = Exception.class)
    public boolean execute(Long id) {
        ProjectTaskDO info = projectTaskRepository.getById(id);
        if (info == null) {
            throw new BizException("数据不存在");
        }
        if(ProjectTaskExecuteStatusEnum.FINISHED.getCode().equals(info.getExecuteStatus())){
            throw new BizException("已结束的课题不能删除");
        }

        boolean res = projectTaskGateway.deletedProjectTaskById(id);
        if (!res) {
            throw new BizException("删除失败");
        }
        return true;
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean execute(Long[] ids) {
        boolean res = projectTaskGateway.deletedProjectTaskByIds(ids);
        if (!res) {
            throw new BizException("删除失败");
        }
        return true;
    }
}

