TaskOperationServiceImpl.java 4.39 KB
package com.topdraw.business.process.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.topdraw.business.module.task.attribute.domain.TaskAttr;
import com.topdraw.business.module.task.attribute.service.TaskAttrService;
import com.topdraw.business.module.task.attribute.service.dto.TaskAttrDTO;
import com.topdraw.business.module.task.domain.Task;
import com.topdraw.business.module.task.service.TaskService;
import com.topdraw.business.module.task.service.dto.TaskDTO;
import com.topdraw.business.module.task.template.service.dto.TaskTemplateDTO;
import com.topdraw.business.process.service.TaskOperationService;
import com.topdraw.business.process.service.TaskTemplateOperationService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author :
 * @description:
 * @function :
 * @date :Created in 2022/4/25 15:48
 * @version: :
 * @modified By:
 * @since : modified in 2022/4/25 15:48
 */
@Service
@Slf4j
public class TaskOperationServiceImpl implements TaskOperationService {

    @Autowired
    private TaskService taskService;
    @Autowired
    private TaskAttrService taskAttrService;
    @Autowired
    private TaskTemplateOperationService taskTemplateOperationService;

    public void asyncCreateTask(Task task) {
        String taskTemplateCode = task.getTaskTemplateCode();
        TaskTemplateDTO taskTemplateDTO = this.taskTemplateOperationService.findByCode(taskTemplateCode);
        Long id = taskTemplateDTO.getId();
        task.setTaskTemplateId(id);
        this.create(task);
    }

    private void create(Task task) {
        String code = task.getCode();
        TaskDTO taskDTO = this.findByCode(code);
        if (Objects.isNull(taskDTO.getId())) {
            TaskDTO taskDTO_ = this.taskService.create(task);
            /*if (Objects.nonNull(taskDTO_.getId())) {
                task.setId(taskDTO_.getId());
                this.createTaskAttr(task);
            }*/
        }
    }

    /**
     *
     * @param task_
     */
    private void createTaskAttr(Task task_) {
        TaskAttr taskAttr = new TaskAttr();
        taskAttr.setAttrStr(task_.getAttr());
        taskAttr.setTaskId(task_.getId());
        this.taskAttrService.create(taskAttr);
    }

    public void asyncUpdateTask(Task task) {
        String code = task.getCode();
        TaskDTO taskDTO = this.findByCode(code);
        if (Objects.nonNull(taskDTO.getId())) {
            Task task1 = new Task();
            BeanUtils.copyProperties(taskDTO, task1);

            Long id = taskDTO.getId();
            task1.setId(id);
            String taskTemplateCode = task.getTaskTemplateCode();
            if (StringUtils.isNotBlank(taskTemplateCode)) {
                TaskTemplateDTO taskTemplateDTO = this.taskTemplateOperationService.findByCode(taskTemplateCode);
                Long templateId = taskTemplateDTO.getId();
                task1.setTaskTemplateId(templateId);
            }

            task1.copy(task);
            TaskDTO update = this.update(task1);
            /*if (Objects.nonNull(update)) {
                this.updateTaskAttr(task);
            }*/
        }

    }

    /**
     *
     * @param task_
     */
    private void updateTaskAttr(Task task_) {

        TaskAttrDTO taskAttrDTO = this.findTaskAttrByTaskId(task_.getId());
        if (Objects.nonNull(taskAttrDTO.getId())) {
            TaskAttr taskAttr = new TaskAttr();
            BeanUtils.copyProperties(taskAttrDTO, taskAttr);
            taskAttr.setAttrStr(task_.getAttr());
            this.taskAttrService.update(taskAttr);
        }

    }

    private TaskAttrDTO findTaskAttrByTaskId(Long taskId) {
        return this.taskAttrService.findByTaskId(taskId);
    }

    private TaskDTO update(Task task) {
        return this.taskService.update(task);
    }

    public void asyncDeleteTask(Task task) {
        String code = task.getCode();
        TaskDTO taskDTO = this.findByCode(code);
        if (Objects.nonNull(taskDTO.getId())) {

            Long id = taskDTO.getId();
            task.setId(id);
            this.delete(task);

        }

    }

    private void delete(Task task) {
        this.taskService.delete(task);
    }

    private TaskDTO findByCode(String code){
        return this.taskService.findByCode(code);
    }

}