lyh
2025-06-25 e756af0f5bfd1addbd5d5c145441fb34aad91a28
lxzn-module-dnc/src/main/java/org/jeecg/modules/dnc/service/impl/GuideCardBatchServiceImpl.java
@@ -1,21 +1,39 @@
package org.jeecg.modules.dnc.service.impl;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.flowable.engine.TaskService;
import org.flowable.task.api.Task;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.dnc.constant.DocAttributionTypeEnum;
import org.jeecg.modules.dnc.entity.*;
import org.jeecg.modules.dnc.mapper.GuideCardBatchMapper;
import org.jeecg.modules.dnc.response.ActivitiCode;
import org.jeecg.modules.dnc.response.CommonCode;
import org.jeecg.modules.dnc.response.UcenterCode;
import org.jeecg.modules.dnc.service.*;
import org.jeecg.modules.dnc.utils.ValidateUtil;
import org.jeecg.modules.dncFlow.constant.GuideCardBatchEnum;
import org.jeecg.modules.dncFlow.entity.DispatchFile;
import org.jeecg.modules.dncFlow.vo.GuideCardBatchFlowTaskVo;
import org.jeecg.modules.flowable.apithird.business.entity.FlowMyBusiness;
import org.jeecg.modules.flowable.apithird.business.service.IFlowMyBusinessService;
import org.jeecg.modules.flowable.apithird.service.FlowCallBackServiceI;
import org.jeecg.modules.flowable.apithird.service.FlowCommonService;
import org.jeecg.modules.flowable.service.IFlowDefinitionService;
import org.jeecg.modules.flowable.service.IFlowTaskService;
import org.jeecg.modules.system.service.ISysDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import java.util.*;
/**
 * @Description: nc文件对应数控程序加工确认表
@@ -23,17 +41,14 @@
 * @Date:   2025-05-27
 * @Version: V1.0
 */
@Service
public class GuideCardBatchServiceImpl extends ServiceImpl<GuideCardBatchMapper, GuideCardBatch> implements IGuideCardBatchService {
@Service("IGuideCardBatchService")
public class GuideCardBatchServiceImpl extends ServiceImpl<GuideCardBatchMapper, GuideCardBatch> implements IGuideCardBatchService , FlowCallBackServiceI {
    @Autowired
    private ISysDictService sysDictService;
    @Autowired
    private IDocInfoService docInfoService;
    @Autowired
    private IDocRelativeService docRelativeService;
    @Autowired
    private IPartsInfoService partsInfoService;
@@ -46,6 +61,17 @@
    @Autowired
    private IDeviceTypeService deviceTypeService;
    @Resource
    private FlowCommonService flowCommonService;
    @Resource
    private IFlowDefinitionService flowDefinitionService;
    @Autowired
    private IFlowTaskService flowTaskService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private IFlowMyBusinessService flowMyBusinessService;
    /**
     * 生成流水号
@@ -143,4 +169,179 @@
        guideCardBatch.setCreateTime(new Date());
        return this.save(guideCardBatch);
    }
    /**
     * 发起确认流程
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<?> startGuideCardBatch(String id){
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        //获取程序加工确认表
        GuideCardBatch guideCardBatch=this.getById(id);
        if (guideCardBatch==null) {
            return Result.ok("未找到对应的程序加工确认表");
        }
        //填充数据
        guideCardBatch.setCompiler(user.getUsername());
        guideCardBatch.setCompilerTime(new Date());
        guideCardBatch.setFlowStatus(GuideCardBatchEnum.VERIFY.getCode());
        this.updateById(guideCardBatch);
        System.out.println("程序加工确认表 确认流程:" + guideCardBatch.getId());
        flowCommonService.initActBusiness("流水号:"+guideCardBatch.getSerialNumber()+" 程序加工确认表进行确认流程",
                guideCardBatch.getId(), "IGuideCardBatchService", "nc_guide_card_batch", null);
        Map<String, Object> variables = new HashMap<>();
        variables.put("dataId", guideCardBatch.getId());
        variables.put("organization", "用户"+user.getRealname()+"发起流程");
        variables.put("comment",  "用户"+user.getRealname()+"发起流程");
        variables.put("proofreading",true);
        Result result= flowDefinitionService.startProcessInstanceByKey("nc_guide_card_batch", variables);
        if (!result.isSuccess()) {
            guideCardBatch.setCompiler(null);
            guideCardBatch.setCompilerTime(null);
            this.updateById(guideCardBatch);
        }
        return result;
    }
    /**
     * 流程节点审核
     * @param guideCardBatchFlowTaskVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<?> auditGuideCardBatch(GuideCardBatchFlowTaskVo guideCardBatchFlowTaskVo){
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        try {
            // 参数校验
            if (!ValidateUtil.validateString(guideCardBatchFlowTaskVo.getTaskId()) || !ValidateUtil.validateString(guideCardBatchFlowTaskVo.getDataId())) {
                return Result.error(CommonCode.INVALID_PARAM.toString());
            }
            String userId = user.getId();
            guideCardBatchFlowTaskVo.setAssignee(user.getUsername());
            if (!ValidateUtil.validateString(userId)) {
                return Result.error(UcenterCode.UCENTER_ACCOUNT_NOT_EXIST.toString());
            }
            // 数据查询
            GuideCardBatch guideCardBatch = this.getById(guideCardBatchFlowTaskVo.getDataId());
            if (guideCardBatch == null) {
                return Result.error(CommonCode.INVALID_PARAM.toString());
            }
            DocInfo docInfo = docInfoService.getById(guideCardBatch.getDocId());
            if (docInfo == null) {
                return Result.error(ActivitiCode.ACT_DOC_ERROR.toString());
            }
            // 2. 查询流程业务记录(处理空结果)
            List<FlowMyBusiness> businessList = flowMyBusinessService.list(
                    new QueryWrapper<FlowMyBusiness>()
                            .eq("process_instance_id", guideCardBatchFlowTaskVo.getInstanceId())
            );
            if (businessList.isEmpty()) {
                return Result.error("流程记录不存在");
            }
            FlowMyBusiness flowMyBusiness = businessList.get(0);
            // 3. 校验用户是否为候选处理人
            List<String> todoUsers = JSON.parseArray(flowMyBusiness.getTodoUsers(), String.class);
            if (todoUsers == null || !todoUsers.contains(user.getUsername())) {
                return Result.error("用户无权操作此任务");
            }
            // 4. 认领任务(处理已被认领的情况)
            String taskId = flowMyBusiness.getTaskId();
            Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
            if (task == null) {
                return Result.error("任务不存在或已完成");
            }
            if (task.getAssignee() != null && !task.getAssignee().equals(user.getUsername())) {
                return Result.error("任务已被他人认领");
            }
            taskService.claim(taskId, user.getUsername());
            // 设置流程变量
            Map<String, Object> values = new HashMap<>();
            values.put("dataId", guideCardBatch.getId());
            values.put("assignee", userId);
            values.put("comment", guideCardBatchFlowTaskVo.getComment());
            values.put("organization", guideCardBatchFlowTaskVo.getComment());
            if (guideCardBatchFlowTaskVo.getCheckType() != null) {
                values.put("checkType", guideCardBatchFlowTaskVo.getCheckType());
            }
            if (guideCardBatchFlowTaskVo.getConfirmType() != null) {
                values.put("confirmType", guideCardBatchFlowTaskVo.getConfirmType());
            }
            if (guideCardBatchFlowTaskVo.getApproveType() != null) {
                values.put("approveType", guideCardBatchFlowTaskVo.getApproveType());
            }
            guideCardBatchFlowTaskVo.setValues(values);
            // 完成流程任务
            Result result = flowTaskService.complete(guideCardBatchFlowTaskVo);
            if (result.isSuccess()) {
                if (guideCardBatchFlowTaskVo.getCheckType() != null) {
                    if (guideCardBatchFlowTaskVo.getCheckType()){
                        guideCardBatch.setFlowStatus(GuideCardBatchEnum.CONFIRM.getCode());
                        guideCardBatch.setProofreader(user.getUsername());
                        guideCardBatch.setProofreaderTime(new Date());
                    }else {
                        guideCardBatch.setFlowStatus(GuideCardBatchEnum.PREPARE.getCode());
                    }
                }
                if (guideCardBatchFlowTaskVo.getConfirmType() != null) {
                    if (guideCardBatchFlowTaskVo.getConfirmType()){
                        guideCardBatch.setFlowStatus(GuideCardBatchEnum.APPROVE.getCode());
                        guideCardBatch.setOperator(user.getUsername());
                        guideCardBatch.setInspectionTime(new Date());
                        guideCardBatch.setInspectionOpinion(guideCardBatchFlowTaskVo.getInspectionOpinion());
                    }else {
                        guideCardBatch.setFlowStatus(GuideCardBatchEnum.PREPARE.getCode());
                    }
                }
                if (guideCardBatchFlowTaskVo.getApproveType() != null) {
                    if (guideCardBatchFlowTaskVo.getApproveType()){
                        guideCardBatch.setFlowStatus(GuideCardBatchEnum.COMPLETE.getCode());
                        guideCardBatch.setApprover(user.getUsername());
                        guideCardBatch.setApproverTime(new Date());
                    }else {
                        guideCardBatch.setFlowStatus(GuideCardBatchEnum.PREPARE.getCode());
                        }
                }
                this.updateById(guideCardBatch);
            } else {
                return result;
            }
            return Result.OK("操作成功");
        } catch (Exception e) {
            // 设置事务回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Result.error("操作失败:" + e.getMessage());
        }
    }
    @Override
    public void afterFlowHandle(FlowMyBusiness business) {
        business.getTaskNameId();//接下来审批的节点
        business.getValues();//前端传进来的参数
        business.getActStatus();
    }
    @Override
    public Object getBusinessDataById(String dataId) {
        return this.getById(dataId);
    }
    @Override
    public Map<String, Object> flowValuesOfTask(String taskNameId, Map<String, Object> values) {
        return null;
    }
    @Override
    public List<String> flowCandidateUsernamesOfTask(String taskNameId, Map<String, Object> values) {
        //业务是否干预流程,业务干预,流程干预,指定人员进行处理
        return null;
    }
}