package com.xdap.self_development.service.impl;

import com.alibaba.fastjson2.JSON;
import com.definesys.mpaas.query.db.PageQueryResult;
import com.xdap.api.moudle.user.pojo.XdapUsers;
import com.xdap.self_development.common.BusinessDatabase;
import com.xdap.self_development.controller.request.RejectTodoRequest;
import com.xdap.self_development.controller.request.TodoTaskRequest;
import com.xdap.self_development.domain.dto.EngineParamDetailDTO;
import com.xdap.self_development.domain.dto.PageResultDTO;
import com.xdap.self_development.domain.enums.CurrentNodeEnum;
import com.xdap.self_development.domain.enums.DistributeStatusEnum;
import com.xdap.self_development.domain.enums.ParamValueVersionEnum;
import com.xdap.self_development.domain.pojo.*;
import com.xdap.self_development.exception.CommonException;
import com.xdap.self_development.service.TodoTaskService;
import com.xdap.self_development.service.XdapDeptUsersService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.*;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 待办任务服务实现类
 *
 * @version 1.0
 * @since 2025
 */
@Service
public class TodoTaskServiceImpl implements TodoTaskService {

    @Resource
    private XdapDeptUsersService xdapDeptUsersService;
    @Resource
    private BusinessDatabase bd;

    /**
     * 分页查询发起任务
     *
     * @param taskRequest 待办任务请求对象
     * @return 发起任务分页结果DTO
     */
    @Override
    public PageResultDTO<InitiatedTaskPojo> selectByPage(TodoTaskRequest taskRequest) {
        if (ObjectUtils.isEmpty(taskRequest.getCreatedById())) {
            throw new CommonException("请传入发起人");
        }
        List<InitiatedTaskPojo> tods = new ArrayList<>();
        PageResultDTO<InitiatedTaskPojo> taskPojoPageResultDTO = new PageResultDTO<>();
        PageQueryResult<InitiatedTaskPojo> queryResult = bd.getBusinessDatabase()
                .eq("created_by_id", taskRequest.getCreatedById())
                .eq("is_delete", 0)
                .orderBy("creation_date", "desc")
                .doPageQuery(taskRequest.getPage(), taskRequest.getSize(), InitiatedTaskPojo.class);

        for (InitiatedTaskPojo taskPojo : queryResult.getResult()) {
            InitiatedTaskPojo taskPojo1 = new InitiatedTaskPojo();
            String currentProcessor = taskPojo.getCurrentProcessor();
            BeanUtils.copyProperties(taskPojo, taskPojo1);

            List<String> strings = new ArrayList<>();
            if (ObjectUtils.isNotEmpty(currentProcessor)) {
                try {
                    strings = JSON.parseArray(currentProcessor).toList(String.class).stream()
                            .map(x -> {
                                XdapUsers xdapUsers = xdapDeptUsersService.selectUserByID(x);
                                if (ObjectUtils.isNotEmpty(xdapUsers)) {
                                    return xdapUsers.getUsername();
                                } else {
                                    return "未知用户名:" + x;
                                }
                            }).collect(Collectors.toList());
                } catch (Exception e) {
                    strings = Collections.singletonList(currentProcessor);
                }
            }
            taskPojo1.setCurrentProcessor(String.join(",", strings));
            tods.add(taskPojo1);
        }
        taskPojoPageResultDTO.setList(tods);
        taskPojoPageResultDTO.setTotalCount(queryResult.getCount());
        return taskPojoPageResultDTO;
    }

    /**
     * 分页查询责任人待办任务
     *
     * @param todoTaskRequest 待办任务请求对象
     * @return 待办任务分页结果DTO
     */
    @Override
    public PageResultDTO<TodoTaskPojo> selectByresPersonTodoPage(TodoTaskRequest todoTaskRequest) {

        PageResultDTO<TodoTaskPojo> pageResultDTO = new PageResultDTO<>();
        PageQueryResult<TodoTaskPojo> queryResult = bd.getBusinessDatabase()
                .eq("owner_id", todoTaskRequest.getPersonId())
                .eq("is_delete", 0)
                .orderBy("creation_date", "desc")
                .doPageQuery(todoTaskRequest.getPage(), todoTaskRequest.getSize(), TodoTaskPojo.class);

        List<TodoTaskPojo> tods = new ArrayList<>();
        for (TodoTaskPojo todoTaskPojo : queryResult.getResult()) {
            TodoTaskPojo todoTaskPojo1 = new TodoTaskPojo();
            String currentProcessor = todoTaskPojo.getCurrentProcessor();
            BeanUtils.copyProperties(todoTaskPojo, todoTaskPojo1);

            List<String> strings = new ArrayList<>();
            if (ObjectUtils.isNotEmpty(currentProcessor)) {
                try {
                    strings = JSON.parseArray(currentProcessor).toList(String.class).stream()
                            .map(x -> {
                                XdapUsers xdapUsers = xdapDeptUsersService.selectUserByID(x);
                                if (ObjectUtils.isNotEmpty(xdapUsers)) {
                                    return xdapUsers.getUsername();
                                } else {
                                    return "未知用户名:" + x;
                                }
                            }).collect(Collectors.toList());
                } catch (Exception e) {
                    strings = Collections.singletonList(currentProcessor);
                }
            }
            todoTaskPojo1.setCurrentProcessor(String.join(",", strings));
            tods.add(todoTaskPojo1);
        }
        pageResultDTO.setList(tods);
        pageResultDTO.setTotalCount(queryResult.getCount());
        return pageResultDTO;
    }

    /**
     * 分页查询责任人已办任务
     *
     * @param taskRequest 待办任务请求对象
     * @return 已办任务分页结果DTO
     */
    @Override
    public PageResultDTO<CompletedTaskPojo> selectByresPersonCompletedPage(TodoTaskRequest taskRequest) {

        PageResultDTO<CompletedTaskPojo> pageResultDTO = new PageResultDTO<>();
        List<CompletedTaskPojo> completedTaskPojos = bd.getBusinessDatabase()
                .eq("owner_id", taskRequest.getPersonId())
                .eq("is_delete", 0)
                .orderBy("creation_date", "desc")
                .doQuery(CompletedTaskPojo.class);

        List<CompletedTaskPojo> tods = new ArrayList<>();
        for (CompletedTaskPojo taskPojo : completedTaskPojos) {
            CompletedTaskPojo taskPojo1 = new CompletedTaskPojo();
            String currentProcessor = taskPojo.getCurrentProcessor();
            BeanUtils.copyProperties(taskPojo, taskPojo1);

            List<String> strings = new ArrayList<>();
            if (ObjectUtils.isNotEmpty(currentProcessor)) {
                try {
                    strings = JSON.parseArray(currentProcessor).toList(String.class).stream()
                            .map(x -> {
                                XdapUsers xdapUsers = xdapDeptUsersService.selectUserByID(x);
                                if (ObjectUtils.isNotEmpty(xdapUsers)) {
                                    return xdapUsers.getUsername();
                                } else {
                                    return "未知用户名:" + x;
                                }
                            }).collect(Collectors.toList());
                } catch (Exception e) {
                    strings = Collections.singletonList(currentProcessor);
                }
            }
            taskPojo1.setCurrentProcessor(String.join(",", strings));
            tods.add(taskPojo1);
        }
        List<CompletedTaskPojo> collect = new ArrayList<>(tods.stream()
                .collect(Collectors
                        .toMap(CompletedTaskPojo::getDocumentNo, Function.identity(),
                                BinaryOperator
                                        .maxBy(Comparator.comparing(CompletedTaskPojo::getCreationDate))))
                .values());
        collect.sort(Comparator.comparing(CompletedTaskPojo::getCreationDate).reversed());

        // 分页
        int fromIndex = (taskRequest.getPage() - 1) * taskRequest.getSize();
        int toIndex = Math.min(fromIndex + taskRequest.getSize(), collect.size());
        List<CompletedTaskPojo> paginatedList = collect.subList(fromIndex, toIndex);

        pageResultDTO.setList(paginatedList);
        pageResultDTO.setTotalCount(collect.size());
        return pageResultDTO;

    }

    /**
     * 创建责任人任务
     *
     * @param paramDetailPojosAll 引擎参数详情DTO列表
     * @param userID 用户ID
     */
    @Override
    public void createResPerson(List<EngineParamDetailDTO> paramDetailPojosAll, String userID) {
        String modelId = paramDetailPojosAll.get(0).getEngineModelId();
        //为参数更新责任人
        for (EngineParamDetailDTO engineParamDetailDTO : paramDetailPojosAll) {
            bd.getBusinessDatabase()
                    .update("is_distributed_res", 1)
                    .update("responsible_person_id", engineParamDetailDTO.getResPersonId())
                    .eq("id", engineParamDetailDTO.getId())
                    .doUpdate(EngineParamDetailPojo.class);
        }

        ArrayList<TodoTaskPojo> todoTaskPojos = new ArrayList<>();
        XdapUsers createdUser = xdapDeptUsersService.selectUserByID(userID);
        DataEntryEngineModelPojo model = bd.getBusinessDatabase()
                .eq("id", modelId)
                .doQueryFirst(DataEntryEngineModelPojo.class);

        // 获取所有责任人ID组装待办
        Set<String> resIds = paramDetailPojosAll.stream()
                .map(EngineParamDetailDTO::getResPersonId)
                .collect(Collectors.toSet());
        for (String resId : resIds) {
            // 检查是否有当前责任人的待办，有就不增加代办
            List<TodoTaskPojo> resTodos = bd.getBusinessDatabase()
                    .eq("model_id", modelId)
                    .eq("data_type", CurrentNodeEnum.JOBSPLIT.getDesc())
                    .eq("is_delete", 0)
                    .eq("owner_id", resId)
                    .eq("process_title", model.getModelName() + "-" + "分发参数填写人任务")
                    .doQuery(TodoTaskPojo.class);
            if (!resTodos.isEmpty()) {
                continue;
            }
            // 没有就新增待办
            TodoTaskPojo todoTaskPojo = new TodoTaskPojo();
            todoTaskPojo.setCreatedById(userID);
            todoTaskPojo.setDocumentNo(UUID.randomUUID().toString());
            todoTaskPojo.setProcessTitle(model.getModelName() + "-" + "分发参数填写人任务");
            todoTaskPojo.setCurrentNode("待分解");
            todoTaskPojo.setCurrentProcessor(JSON.toJSONString(Collections.singletonList(resId)));
            todoTaskPojo.setStatusCode(null/*String.valueOf(ParamValueVersionEnum.DRAFT.getCode())*/);
            todoTaskPojo.setModelName(ObjectUtils.isNotEmpty(model) ? model.getModelName() : "");
            todoTaskPojo.setModelId(modelId);
            todoTaskPojo.setCreationDate(Timestamp.valueOf(LocalDateTime.now()));
            todoTaskPojo.setDataType(CurrentNodeEnum.JOBSPLIT.getDesc());
            todoTaskPojo.setVersionNumber(ObjectUtils.isNotEmpty(model) ? model.getVersionNumber() : 0);
            todoTaskPojo.setIsDelete(0);
            todoTaskPojo.setOwnerId(resId);
            todoTaskPojo.setCreatedBy(ObjectUtils.isNotEmpty(createdUser) ? createdUser.getUsername() : "");
            todoTaskPojos.add(todoTaskPojo);
        }
        if (!todoTaskPojos.isEmpty()) {
            bd.getBusinessDatabase().doBatchInsert(todoTaskPojos);
        }

        // 检查分发状态
        List<EngineParamDetailPojo> toBeDistributedParams = bd.getBusinessDatabase()
                .eq("engine_model_id", modelId)
                .eq("major_version", model.getVersionNumber())
                .eq("is_distributed_res", 0)
                .doQuery(EngineParamDetailPojo.class);

        // 机型的分发状态改为已分发
        if (ObjectUtils.isEmpty(toBeDistributedParams)) {
            DistributeStatusEnum byDesc = DistributeStatusEnum.getByDesc("已分发");
            Integer status = byDesc != null ? byDesc.getCode() : null;
            bd.getBusinessDatabase()
                    .eq("id", modelId)
                    .update("distribute_status", status)
                    .doUpdate(DataEntryEngineModelPojo.class);
        } else {
            DistributeStatusEnum byDesc = DistributeStatusEnum.getByDesc("部分分发");
            Integer status = byDesc != null ? byDesc.getCode() : null;
            bd.getBusinessDatabase()
                    .eq("id", modelId)
                    .update("distribute_status", status)
                    .doUpdate(DataEntryEngineModelPojo.class);
        }

        InitiatedTaskPojo initiatedTask = bd.getBusinessDatabase()
                .eq("model_id", model.getId())
                .eq("data_type", CurrentNodeEnum.JOBSPLIT.getDesc())
                .eq("created_by_id", userID)
                .eq("process_title", model.getModelName() + "-" + "分发参数责任人任务")
                .doQueryFirst(InitiatedTaskPojo.class);

        String disStatus = ObjectUtils.isEmpty(toBeDistributedParams) ? "已分解" : "待分解";
        if (ObjectUtils.isEmpty(initiatedTask)) {
            InitiatedTaskPojo initiatedTaskPojo = new InitiatedTaskPojo();
            initiatedTaskPojo.setCreatedById(userID);
            initiatedTaskPojo.setDocumentNo(UUID.randomUUID().toString());
            initiatedTaskPojo.setProcessTitle(model.getModelName() + "-" + "分发参数责任人任务");
            initiatedTaskPojo.setCurrentNode(disStatus);
            initiatedTaskPojo.setCurrentProcessor(ObjectUtils.isEmpty(toBeDistributedParams) ? null : JSON.toJSONString(resIds));
            initiatedTaskPojo.setStatusCode(null/*String.valueOf(ParamValueVersionEnum.DRAFT.getCode())*/);
            initiatedTaskPojo.setModelName(ObjectUtils.isNotEmpty(model) ? model.getModelName() : "");
            initiatedTaskPojo.setModelId(modelId);
            initiatedTaskPojo.setCreationDate(Timestamp.valueOf(LocalDateTime.now()));
            initiatedTaskPojo.setDataType(CurrentNodeEnum.JOBSPLIT.getDesc());
            initiatedTaskPojo.setVersionNumber(ObjectUtils.isNotEmpty(model) ? model.getVersionNumber() : 0);
            initiatedTaskPojo.setIsDelete(0);
            initiatedTaskPojo.setCreatedBy(ObjectUtils.isNotEmpty(createdUser) ? createdUser.getUsername() : "");
            bd.getBusinessDatabase().doInsert(initiatedTaskPojo);
        } else {
            bd.getBusinessDatabase()
                    .eq("id", initiatedTask.getId())
                    .update("current_node", disStatus)
                    .update("current_processor", ObjectUtils.isEmpty(toBeDistributedParams) ? null : JSON.toJSONString(resIds))
                    .doUpdate(InitiatedTaskPojo.class);
        }

        CompletedTaskPojo completedTask = bd.getBusinessDatabase()
                .eq("model_id", model.getId())
                .eq("data_type", CurrentNodeEnum.JOBSPLIT.getDesc())
                .eq("created_by_id", userID)
                .eq("process_title", model.getModelName() + "-" + "分发参数责任人任务")
                .doQueryFirst(CompletedTaskPojo.class);

        String distStatus = ObjectUtils.isEmpty(toBeDistributedParams) ? "已分解" : "待分解";
        if (ObjectUtils.isEmpty(completedTask)) {
            CompletedTaskPojo completedTaskPojo = new CompletedTaskPojo();
            completedTaskPojo.setCreatedById(userID);
            completedTaskPojo.setDocumentNo(UUID.randomUUID().toString());
            completedTaskPojo.setProcessTitle(model.getModelName() + "-" + "分发参数责任人任务");
            completedTaskPojo.setCurrentNode(distStatus);
            completedTaskPojo.setCurrentProcessor(ObjectUtils.isEmpty(toBeDistributedParams) ? null : JSON.toJSONString(resIds));
            completedTaskPojo.setStatusCode(null/*String.valueOf(ParamValueVersionEnum.DRAFT.getCode())*/);
            completedTaskPojo.setModelName(ObjectUtils.isNotEmpty(model) ? model.getModelName() : "");
            completedTaskPojo.setModelId(modelId);
            completedTaskPojo.setCreationDate(Timestamp.valueOf(LocalDateTime.now()));
            completedTaskPojo.setDataType(CurrentNodeEnum.JOBSPLIT.getDesc());
            completedTaskPojo.setVersionNumber(ObjectUtils.isNotEmpty(model) ? model.getVersionNumber() : 0);
            completedTaskPojo.setOwnerId(userID);
            completedTaskPojo.setIsDelete(0);
            completedTaskPojo.setCreatedBy(ObjectUtils.isNotEmpty(createdUser) ? createdUser.getUsername() : "");
            bd.getBusinessDatabase().doInsert(completedTaskPojo);
        } else {
            bd.getBusinessDatabase()
                    .eq("id", completedTask.getId())
                    .update("current_node", distStatus)
                    .update("current_processor", ObjectUtils.isEmpty(toBeDistributedParams) ? null : JSON.toJSONString(resIds))
                    .doUpdate(CompletedTaskPojo.class);
        }
    }

    @Override
    public void rejectTodo(RejectTodoRequest request) {
        TodoTaskPojo todoTaskPojo = bd.getBusinessDatabase()
                .eq("id", request.getTodoId())
                .doQueryFirst(TodoTaskPojo.class);
        if (ObjectUtils.isEmpty(todoTaskPojo)) {
            throw new CommonException("待办任务不存在");
        }
        DataEntryEngineModelPojo model = bd.getBusinessDatabase()
                .eq("id", todoTaskPojo.getModelId())
                .doQueryFirst(DataEntryEngineModelPojo.class);
        if (ObjectUtils.isEmpty(model)) {
            throw new CommonException("机型不存在，请检查");
        }
        // 填写人拒绝
        if ((ObjectUtils.isNotEmpty(request.getFillerId()))
                && (request.getResponsiblePersonId() == null || ObjectUtils.isEmpty(request.getResponsiblePersonId()))) {
            // 填写人拒绝待办
            bd.getBusinessDatabase()
                    .eq("engine_model_id", model.getId())
                    .eq("major_version", model.getVersionNumber())
                    .eq("filled_by", request.getFillerId())
                    .update("is_distributed_filled", 0)
                    .update("filled_by", null)
                    .doUpdate(EngineParamDetailPojo.class);
            // 检查责任人是否有责任人待办
            TodoTaskPojo resTodo = bd.getBusinessDatabase()
                    .eq("model_id", todoTaskPojo.getModelId())
                    .eq("data_type", CurrentNodeEnum.JOBSPLIT.getDesc())
                    .eq("owner_id", todoTaskPojo.getCreatedById())
                    .doQueryFirst(TodoTaskPojo.class);
            if (resTodo != null && resTodo.getIsDelete() == 1) {
                bd.getBusinessDatabase()
                        .eq("id", resTodo.getId())
                        .update("is_delete", 0)
                        .doUpdate(TodoTaskPojo.class);
                // 更新发起
                bd.getBusinessDatabase()
                        .eq("model_id", model.getId())
                        .eq("data_type", CurrentNodeEnum.JOBSPLIT.getDesc())
                        .eq("created_by_id", todoTaskPojo.getCreatedById())
                        .eq("process_title", model.getModelName() + "-" + "分发参数填写人任务")
                        .update("current_node", "待分解")
                        .update("current_processor", null)
                        .doUpdate(InitiatedTaskPojo.class);
                // 更新完成
                bd.getBusinessDatabase()
                        .eq("model_id", model.getId())
                        .eq("data_type", CurrentNodeEnum.JOBSPLIT.getDesc())
                        .eq("owner_id", todoTaskPojo.getCreatedById())
                        .eq("process_title", model.getModelName() + "-" + "分发参数填写人任务")
                        .update("current_node", "待分解")
                        .update("current_processor", null)
                        .doUpdate(CompletedTaskPojo.class);
            }
        }
        // 责任人拒绝
        else if ((request.getFillerId() == null || ObjectUtils.isEmpty(request.getFillerId()))
                && ObjectUtils.isNotEmpty(request.getResponsiblePersonId())) {
            List<EngineParamDetailPojo> pojoList = bd.getBusinessDatabase()
                    .eq("engine_model_id", model.getId())
                    .eq("major_version", model.getVersionNumber())
                    .ne("responsible_person_id", request.getResponsiblePersonId())
                    .doQuery(EngineParamDetailPojo.class);

            bd.getBusinessDatabase()
                    .eq("engine_model_id", model.getId())
                    .eq("major_version", model.getVersionNumber())
                    .eq("responsible_person_id", request.getResponsiblePersonId())
                    .update("is_distributed_res", 0)
                    .update("responsible_person_id", null)
                    .update("is_distributed_filled", 0)
                    .update("filled_by", null)
                    .doUpdate(EngineParamDetailPojo.class);

            Integer status = pojoList.isEmpty() ? 2 : 3;
            bd.getBusinessDatabase()
                    .eq("id", model.getId())
                    .update("distribute_status", status)
                    .doUpdate(DataEntryEngineModelPojo.class);
            // 更新发起
            bd.getBusinessDatabase()
                    .eq("model_id", model.getId())
                    .eq("data_type", CurrentNodeEnum.JOBSPLIT.getDesc())
                    .eq("created_by_id", todoTaskPojo.getCreatedById())
                    .eq("process_title", model.getModelName() + "-" + "分发参数责任人任务")
                    .update("current_node", "待分解")
                    .update("current_processor", null)
                    .doUpdate(InitiatedTaskPojo.class);

            // 更新完成
            bd.getBusinessDatabase()
                    .eq("model_id", model.getId())
                    .eq("data_type", CurrentNodeEnum.JOBSPLIT.getDesc())
                    .eq("owner_id", todoTaskPojo.getCreatedById())
                    .eq("process_title", model.getModelName() + "-" + "分发参数责任人任务")
                    .update("current_node", "待分解")
                    .update("current_processor", null)
                    .doUpdate(CompletedTaskPojo.class);
        } else {
            throw new CommonException("参数错误");
        }
        // 删除待办
        bd.getBusinessDatabase()
                .eq("id", todoTaskPojo.getId())
                .update("is_delete", 1)
                .doUpdate(TodoTaskPojo.class);
    }

    @Override
    public void skipApproval(RejectTodoRequest request) {
        TodoTaskPojo todoTaskPojo = bd.getBusinessDatabase()
                .eq("id", request.getTodoId())
                .doQueryFirst(TodoTaskPojo.class);
        if (ObjectUtils.isEmpty(todoTaskPojo)) {
            throw new CommonException("待办任务不存在");
        }
        DataEntryEngineModelPojo model = bd.getBusinessDatabase()
                .eq("id", todoTaskPojo.getModelId())
                .doQueryFirst(DataEntryEngineModelPojo.class);
        if (ObjectUtils.isEmpty(model)) {
            throw new CommonException("机型不存在，请检查");
        }
        List<EngineParamDetailPojo> list = new ArrayList<>();
        if ((ObjectUtils.isNotEmpty(request.getFillerId()))
                && (request.getResponsiblePersonId() == null || ObjectUtils.isEmpty(request.getResponsiblePersonId()))) {
            bd.getBusinessDatabase()
                    .eq("engine_model_id", model.getId())
                    .eq("major_version", model.getVersionNumber())
                    .eq("filled_by", request.getFillerId())
                    .update("version_status", ParamValueVersionEnum.PASSED.getCode())
                    .doUpdate(EngineParamDetailPojo.class);
            // 更新发起
            bd.getBusinessDatabase()
                    .eq("model_id", model.getId())
                    .eq("data_type", CurrentNodeEnum.JOBSPLIT.getDesc())
                    .eq("created_by_id", todoTaskPojo.getCreatedById())
                    .eq("process_title", model.getModelName() + "-" + "分发参数填写人任务")
                    .update("current_node", "已完成")
                    .update("current_processor", null)
                    .doUpdate(InitiatedTaskPojo.class);
            // 更新完成
            bd.getBusinessDatabase()
                    .eq("model_id", model.getId())
                    .eq("data_type", CurrentNodeEnum.JOBSPLIT.getDesc())
                    .eq("owner_id", todoTaskPojo.getCreatedById())
                    .eq("process_title", model.getModelName() + "-" + "分发参数填写人任务")
                    .update("current_node", "已完成")
                    .update("current_processor", null)
                    .doUpdate(CompletedTaskPojo.class);
        } else if ((request.getFillerId() == null || ObjectUtils.isEmpty(request.getFillerId()))
                && ObjectUtils.isNotEmpty(request.getResponsiblePersonId())) {
            bd.getBusinessDatabase()
                    .eq("engine_model_id", model.getId())
                    .eq("major_version", model.getVersionNumber())
                    .eq("responsible_person_id", request.getResponsiblePersonId())
                    .update("version_status", ParamValueVersionEnum.PASSED.getCode())
                    .doUpdate(EngineParamDetailPojo.class);

            // 更新发起
            bd.getBusinessDatabase()
                    .eq("model_id", model.getId())
                    .eq("data_type", CurrentNodeEnum.JOBSPLIT.getDesc())
                    .eq("created_by_id", todoTaskPojo.getCreatedById())
                    .eq("process_title", model.getModelName() + "-" + "分发参数责任人任务")
                    .update("current_node", "已完成")
                    .update("current_processor", null)
                    .doUpdate(InitiatedTaskPojo.class);

            // 更新完成
            bd.getBusinessDatabase()
                    .eq("model_id", model.getId())
                    .eq("data_type", CurrentNodeEnum.JOBSPLIT.getDesc())
                    .eq("owner_id", todoTaskPojo.getCreatedById())
                    .eq("process_title", model.getModelName() + "-" + "分发参数责任人任务")
                    .update("current_node", "已完成")
                    .update("current_processor", null)
                    .doUpdate(CompletedTaskPojo.class);
        } else {
            throw new CommonException("参数错误");
        }
        // 更新待办
        bd.getBusinessDatabase()
                .eq("id", request.getTodoId())
                .update("is_delete", 1)
                .doUpdate(TodoTaskPojo.class);
    }
}
