package cd.casic.ci.process.process.service.template.impl;

import cd.casic.ci.process.dto.req.task.TaskUpdateReq;
import cd.casic.ci.process.dto.req.template.TemplateTaskUpdateReq;
import cd.casic.ci.process.dto.resp.task.TasksResp;
import cd.casic.ci.process.dto.resp.template.TemplateTasksResp;
import cd.casic.ci.process.process.converter.TemplateTaskConverter;
import cd.casic.ci.process.process.dao.template.TemplateTaskDao;
import cd.casic.ci.process.process.dataObject.task.PipTask;
import cd.casic.ci.process.process.dataObject.template.TemplateTask;
import cd.casic.ci.process.process.service.template.TemplateTaskService;
import cd.casic.ci.process.util.WebFrameworkUtils;
import cd.casic.framework.commons.exception.ServiceException;
import cd.casic.framework.commons.exception.enums.GlobalErrorCodeConstants;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class TemplateTaskServiceImpl extends ServiceImpl<TemplateTaskDao, TemplateTask> implements TemplateTaskService {
    @Resource
    private TemplateTaskDao taskDao;
    @Resource
    private TemplateTaskConverter taskConverter;
    @Override
    public List<TemplateTask> getTask(TemplateTask task) {
        LambdaQueryWrapper<TemplateTask> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotEmpty(task.getId()),TemplateTask::getId,task.getId());
        wrapper.eq(StringUtils.isNotEmpty(task.getTemplateId()),TemplateTask::getTemplateId,task.getTemplateId());
        wrapper.eq(StringUtils.isNotEmpty(task.getStageId()),TemplateTask::getStageId,task.getStageId());
        return taskDao.selectList(wrapper);
    }
    @Override
    public List<TemplateTask> getTaskByStageIdList(List<String> stageIdList){
        LambdaQueryWrapper<TemplateTask> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(TemplateTask::getStageId,stageIdList);
        return taskDao.selectList(wrapper);
    }
    @Override
    public TemplateTasksResp getRespById(String taskId){
        TemplateTask pipTask = new TemplateTask();
        pipTask.setId(taskId);
        List<TemplateTask> taskList = getTask(pipTask);
        if (CollectionUtils.isEmpty(taskList)) {
            return null;
        }
        return taskConverter.doToResp(taskList.get(0));
    }

    @Override
    public Boolean updateTask(TemplateTaskUpdateReq req) {
        if (StringUtils.isEmpty(req.getId())) {
            throw new ServiceException(GlobalErrorCodeConstants.PIPELINE_ERROR.getCode(),"传入id有问题");
        }
        TemplateTask byId = getById(req.getId());
        if (byId == null) {
            throw new ServiceException(GlobalErrorCodeConstants.PIPELINE_ERROR.getCode(),"task不存在");
        }
        String taskName = req.getTaskName();
        Map<String, Object> taskProperties = req.getTaskProperties();
        String taskType = req.getTaskType();
        byId.setTaskProperties(taskProperties);
        byId.setTaskType(taskType);
        byId.setTaskName(taskName);
        byId.setUpdater(WebFrameworkUtils.getLoginUserIdStr());
        byId.setUpdateTime(LocalDateTime.now());
        return updateById(byId);
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void copyTask(String taskId) {
        TemplateTask sourceTask = getById(taskId);
        if (sourceTask == null) {
            return;
        }
        String stageId = sourceTask.getStageId();
        TemplateTask query = new TemplateTask();
        query.setStageId(stageId);
        List<TemplateTask> sameLevelTaskList = getTask(query);
        if (CollectionUtils.isEmpty(sameLevelTaskList)) {
            return;
        }
        List<TemplateTask> updateList = new ArrayList<>(sameLevelTaskList.size());
        for (TemplateTask pipTask : sameLevelTaskList) {
            if (!pipTask.getId().equals(sourceTask.getId())) {
                if (pipTask.getTaskSort()>sourceTask.getTaskSort()) {
                    pipTask.setTaskSort(pipTask.getTaskSort()+1);
                    updateList.add(pipTask);
                }
            }
        }
        sourceTask.setTaskSort(sourceTask.getTaskSort()+1);
        sourceTask.setId(null);
        save(sourceTask);
        if (!CollectionUtils.isEmpty(updateList)) {
            updateBatchById(updateList);
        }
    }
}
