lyh
7 天以前 0bc74278ff8130936bc7aed763e8901fedb4f3b0
lxzn-module-eam-common/src/main/java/org/jeecg/modules/eam/service/impl/EamReportProductHazardsServiceImpl.java
@@ -1,5 +1,8 @@
package org.jeecg.modules.eam.service.impl;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
@@ -8,22 +11,39 @@
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
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.constant.CommonConstant;
import org.jeecg.common.constant.DataBaseConstant;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.eam.constant.BusinessCodeConst;
import org.jeecg.modules.eam.constant.EamReportAccidentsRegisterEnum;
import org.jeecg.modules.eam.constant.EamReportProductHazardsEnum;
import org.jeecg.modules.eam.dto.EamReportProductHazardsDto;
import org.jeecg.modules.eam.entity.EamEquipment;
import org.jeecg.modules.eam.entity.EamReportAccidentsRegister;
import org.jeecg.modules.eam.entity.EamReportProductHazards;
import org.jeecg.modules.eam.entity.EamReportRepair;
import org.jeecg.modules.eam.mapper.EamReportProductHazardsMapper;
import org.jeecg.modules.eam.request.EamReportProductHazardsQuery;
import org.jeecg.modules.eam.service.IEamEquipmentService;
import org.jeecg.modules.eam.service.IEamReportProductHazardsService;
import org.jeecg.modules.eam.service.IEamReportRepairService;
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.entity.BaseFactory;
import org.jeecg.modules.system.entity.BaseFactoryUser;
import org.jeecg.modules.system.service.IBaseFactoryService;
import org.jeecg.modules.system.service.IBaseFactoryUserService;
import org.jeecg.modules.system.service.ISysUserService;
import org.jeecg.modules.system.vo.UserSelector;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
@@ -31,9 +51,8 @@
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;
/**
@@ -43,7 +62,7 @@
 * @Version: V1.0
 */
@Service("IEamReportProductHazardsService")
public class EamReportProductHazardsServiceImpl extends ServiceImpl<EamReportProductHazardsMapper, EamReportProductHazards> implements IEamReportProductHazardsService {
public class EamReportProductHazardsServiceImpl extends ServiceImpl<EamReportProductHazardsMapper, EamReportProductHazards> implements IEamReportProductHazardsService, FlowCallBackServiceI {
    @Autowired
    private IBaseFactoryUserService baseFactoryUserService;
@@ -52,7 +71,25 @@
    @Autowired
    @Lazy
    private IEamReportRepairService iEamReportRepairService;
    @Resource
    private IFlowMyBusinessService flowMyBusinessService;
    @Resource
    private TaskService taskService;
    @Resource
    private IFlowTaskService flowTaskService;
    @Autowired
    private ISysUserService sysUserService;
    @Resource
    private FlowCommonService flowCommonService;
    @Resource
    private IFlowDefinitionService flowDefinitionService;
    @Resource
    private IEamEquipmentService eamEquipmentService;
    /**
     * 分页列表
     * @param page
@@ -94,6 +131,13 @@
            if (StringUtils.isNotBlank(eamReportProductHazardsQuery.getEquipmentName())) {
                queryWrapper.like("e.equipment_name", eamReportProductHazardsQuery.getEquipmentName());
            }
            if (StringUtils.isNotBlank(eamReportProductHazardsQuery.getConfirmStatus())) {
                queryWrapper.eq("erph.confirm_status", eamReportProductHazardsQuery.getConfirmStatus());
            }
            if (StringUtils.isNotBlank(eamReportProductHazardsQuery.getProductLoss())) {
                queryWrapper.eq("erph.product_loss", eamReportProductHazardsQuery.getProductLoss());
            }
            queryWrapper.eq("erph.del_flag", CommonConstant.DEL_FLAG_0);
            //排序
            if (StringUtils.isNotBlank(eamReportProductHazardsQuery.getColumn()) && StringUtils.isNotBlank(eamReportProductHazardsQuery.getOrder())) {
                String column = eamReportProductHazardsQuery.getColumn();
@@ -137,7 +181,164 @@
     */
    @Override
    public Result<?> submit(String id){
        return null;
        EamReportProductHazards eamReportProductHazards=this.getById(id);
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        EamEquipment eamEquipment = eamEquipmentService.getById(eamReportProductHazards.getEquipmentId());
        List<UserSelector> userSelectorList = sysUserService.selectOperatorList(eamEquipment.getEquipmentCode(), eamEquipment.getFactoryOrgCode(), BusinessCodeConst.PCR0024);
        // 启动设备事故登记流程
        flowCommonService.initActBusiness("设备编号:" + eamEquipment.getEquipmentCode() + ", 设备名称:" + eamEquipment.getEquipmentName()+"发起产品安全隐患确认流程",
                eamReportProductHazards.getId(),
                "IEamReportProductHazardsService",
                "eam_report_product_hazards",
                null);
        Map<String, Object> variables = new HashMap<>();
        variables.put("dataId", eamReportProductHazards.getId());
        variables.put("organization", "操作工提交启动流程");
        variables.put("comment", "操作工提交单启动流程");
        variables.put("proofreading", true);
        if (CollectionUtils.isEmpty(userSelectorList)) {
            throw new JeecgBootException("设备未存在主管工艺,无法进入下级审批!");
        }else {
            List<String> usernameList = userSelectorList.stream().map(UserSelector::getUsername).collect(Collectors.toList());
            variables.put("NextAssignee", usernameList);
        }
        Result result = flowDefinitionService.startProcessInstanceByKey("eam_report_product_hazards", variables);
        if (!result.isSuccess()) {
            return Result.error("流程启动失败");
        } else {
            eamReportProductHazards.setConfirmer(user.getUsername());
            eamReportProductHazards.setConfirmTime(new Date());
            eamReportProductHazards.setConfirmStatus(EamReportProductHazardsEnum.PENDING_SUPERIOR_TECHNICAL_CONFIRMATION.name());
            this.updateById(eamReportProductHazards);
            return result;
        }
    }
    /**
     * 流程审核
     * @param eamReportProductHazardsDto
     */
    @Override
    public Result<?> audit(EamReportProductHazardsDto eamReportProductHazardsDto){
        EamReportProductHazards eamReportProductHazards = this.baseMapper.selectById(eamReportProductHazardsDto.getId());
        if (eamReportProductHazards == null) {
            throw new JeecgBootException("工单不存在,请刷新重试!");
        }
        // 获取当前登录用户
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (user == null || StrUtil.isBlank(user.getId())) {
            throw new JeecgBootException("未获取到登录用户,请重新登录后再试!");
        }
        eamReportProductHazardsDto.setAssignee(user.getUsername());
        // 获取流程业务记录
        FlowMyBusiness flowMyBusiness = flowMyBusinessService.getFlowMyBusiness(eamReportProductHazardsDto.getInstanceId());
        if (flowMyBusiness == null) {
            throw new JeecgBootException("流程实例不存在,请刷新后重试!");
        }
        boolean userAuthorized = isUserAuthorized(flowMyBusiness, user);
        if (!userAuthorized) {
            throw new JeecgBootException("用户无权操作此任务,请刷新后重试!");
        }
        // 认领任务
        if (!claimTask(flowMyBusiness.getTaskId(), user)) {
            throw new JeecgBootException("任务不存在、已完成或已被他人认领!");
        }
        EamEquipment equipment = eamEquipmentService.getById(eamReportProductHazardsDto.getEquipmentId());
        if (equipment == null) {
            throw new JeecgBootException("设备不存在,请检查!");
        }
        EamReportProductHazardsEnum status = EamReportProductHazardsEnum.getInstance(eamReportProductHazards.getConfirmStatus());
        if (status == null) {
            return null;
        }
        //流程变量
        Map<String, Object> values = new HashMap<>();
        List<String> userApprovalList;
        List<UserSelector> userSelectors;
        switch (status) {
            //待主管工艺确认
            case PENDING_SUPERIOR_TECHNICAL_CONFIRMATION:
                userSelectors = sysUserService.selectOperatorList(equipment.getEquipmentCode(), equipment.getFactoryOrgCode(), BusinessCodeConst.PCR0025);
                if (CollectionUtil.isEmpty(userSelectors)) {
                    throw new JeecgBootException("设备未存在副部级领导,无法进入下级审批!");
                }else{
                    userApprovalList= userSelectors.stream().map(UserSelector::getUsername).collect(Collectors.toList());
                    values.put("NextAssignee", userApprovalList);
                    values.put("dataId", eamReportProductHazardsDto.getId());
                    if (StrUtil.isEmpty(eamReportProductHazardsDto.getTechnologistComment())){
                        eamReportProductHazardsDto.setTechnologistComment("");
                    }
                    values.put("organization",eamReportProductHazardsDto.getTechnologistComment());
                    values.put("comment", eamReportProductHazardsDto.getTechnologistComment());
                    values.put("technologistResult", eamReportProductHazardsDto.getTechnologistResult());
                    eamReportProductHazardsDto.setComment(eamReportProductHazardsDto.getTechnologistComment());
                    eamReportProductHazards.setTechnologist(user.getUsername());
                    eamReportProductHazards.setTechnologistTime(new Date());
                    eamReportProductHazards.setTechnologistResult(eamReportProductHazardsDto.getTechnologistResult());
                    eamReportProductHazards.setTechnologistComment(eamReportProductHazardsDto.getTechnologistComment());
                    if ("2".equals(eamReportProductHazardsDto.getTechnologistResult())){
                        //主管工艺拒绝
                        eamReportProductHazards.setConfirmStatus(EamReportProductHazardsEnum.REJECTED.name());
                    }else {
                        eamReportProductHazards.setConfirmStatus(EamReportProductHazardsEnum.PENDING_DEPARTMENT_LEADER_CONFIRMATION.name());
                    }
                }
                break;
            //待副部级领导确认
            case PENDING_DEPARTMENT_LEADER_CONFIRMATION:
                values.put("dataId", eamReportProductHazardsDto.getId());
                if (StrUtil.isEmpty(eamReportProductHazardsDto.getDeputyDepartmentComment())){
                    eamReportProductHazardsDto.setDeputyDepartmentComment("");
                }
                values.put("organization",eamReportProductHazardsDto.getDeputyDepartmentComment());
                values.put("comment", eamReportProductHazardsDto.getDeputyDepartmentComment());
                values.put("deputyDepartmentResult", eamReportProductHazardsDto.getDeputyDepartmentResult());
                eamReportProductHazardsDto.setComment(eamReportProductHazardsDto.getDeputyDepartmentComment());
                eamReportProductHazards.setDeputyDepartment(user.getUsername());
                eamReportProductHazards.setDeputyDepartmentTime(new Date());
                eamReportProductHazards.setDeputyDepartmentResult(eamReportProductHazardsDto.getDeputyDepartmentResult());
                eamReportProductHazards.setDeputyDepartmentComment(eamReportProductHazardsDto.getDeputyDepartmentComment());
                if ("2".equals(eamReportProductHazardsDto.getDeputyDepartmentResult())){
                    //副部级领导拒绝
                    eamReportProductHazards.setConfirmStatus(EamReportProductHazardsEnum.REJECTED.name());
                }else {
                    eamReportProductHazards.setConfirmStatus(EamReportProductHazardsEnum.REPAIR_COMPLETED.name());
                }
                break;
            default:
                throw  new JeecgBootException("审批失败");
        }
        eamReportProductHazardsDto.setValues(values);
        //保存工单
        this.updateById(eamReportProductHazards);
        Result result = flowTaskService.complete(eamReportProductHazardsDto);
        if (!result.isSuccess()) {
            throw new JeecgBootException("审批失败,请刷新查看!");
        }
        return Result.ok("审批成功");
    }
    private boolean claimTask(String taskId, LoginUser user) {
        Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
        if (task == null) {
            return false;
        }
        if (task.getAssignee() != null && !task.getAssignee().equals(user.getUsername())) {
            return false;
        }
        taskService.claim(taskId, user.getUsername());
        return true;
    }
    /**
     * 判断用户是否拥有此权限
     * @param flowMyBusiness
     * @param user
     * @return
     */
    private boolean isUserAuthorized(FlowMyBusiness flowMyBusiness, LoginUser user) {
        List<String> todoUsers = JSON.parseArray(flowMyBusiness.getTodoUsers(), String.class);
        return todoUsers != null && todoUsers.contains(user.getUsername());
    }
    /**
@@ -166,4 +367,26 @@
        this.removeById(id);
        return true;
    }
    @Override
    public void afterFlowHandle(FlowMyBusiness business) {
    }
    @Override
    public Object getBusinessDataById(String dataId) {
        return null;
    }
    @Override
    public Map<String, Object> flowValuesOfTask(String taskNameId, Map<String, Object> values) {
        return Collections.emptyMap();
    }
    @Override
    public List<String> flowCandidateUsernamesOfTask(String taskNameId, Map<String, Object> values) {
        //获取下一步处理人
        Object object = values.get("NextAssignee");
        return (List<String>) object;
    }
}