Lius
6 天以前 7d2ae4c2fd8d45f0896b9accf84ff4177638fbcd
lxzn-module-eam/src/main/java/org/jeecg/modules/eam/service/impl/EamTechnicalStatusDeactivateServiceImpl.java
@@ -1,11 +1,55 @@
package org.jeecg.modules.eam.service.impl;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
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.TranslateDictTextUtils;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.eam.constant.BusinessCodeConst;
import org.jeecg.modules.eam.constant.HfTemplateCategoryEnum;
import org.jeecg.modules.eam.constant.TechnicalStatusDeactivateStatusEnum;
import org.jeecg.modules.eam.constant.TechnicalStatusEvaluationOrderChangeStatusEnum;
import org.jeecg.modules.eam.entity.EamBaseHFCode;
import org.jeecg.modules.eam.entity.EamEquipment;
import org.jeecg.modules.eam.entity.EamTechnicalStatusDeactivate;
import org.jeecg.modules.eam.mapper.EamTechnicalStatusDeactivateMapper;
import org.jeecg.modules.eam.request.EamTechnicalStatusDeactivateQuery;
import org.jeecg.modules.eam.request.EamTechnicalStatusDeactivateRequest;
import org.jeecg.modules.eam.service.IEamBaseHFCodeService;
import org.jeecg.modules.eam.service.IEamEquipmentService;
import org.jeecg.modules.eam.service.IEamTechnicalStatusDeactivateDetailService;
import org.jeecg.modules.eam.service.IEamTechnicalStatusDeactivateService;
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.ISysBusinessCodeRuleService;
import org.jeecg.modules.system.service.ISysUserService;
import org.jeecg.modules.system.vo.UserSelector;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.*;
import java.util.stream.Collectors;
/**
 * @Description: 停用加工设备申请单
@@ -13,7 +57,340 @@
 * @Date:   2025-07-09
 * @Version: V1.0
 */
@Service
public class EamTechnicalStatusDeactivateServiceImpl extends ServiceImpl<EamTechnicalStatusDeactivateMapper, EamTechnicalStatusDeactivate> implements IEamTechnicalStatusDeactivateService {
@Service("IEamTechnicalStatusDeactivateService")
public class EamTechnicalStatusDeactivateServiceImpl extends ServiceImpl<EamTechnicalStatusDeactivateMapper, EamTechnicalStatusDeactivate> implements IEamTechnicalStatusDeactivateService, FlowCallBackServiceI {
    @Autowired
    private IBaseFactoryUserService baseFactoryUserService;
    @Autowired
    private IBaseFactoryService baseFactoryService;
    @Autowired
    private ISysBusinessCodeRuleService businessCodeRuleService;
    @Autowired
    private IEamBaseHFCodeService hfCodeService;
    @Autowired
    private IEamTechnicalStatusDeactivateDetailService deactivateDetailService;
    @Autowired
    private IEamEquipmentService equipmentService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private FlowCommonService flowCommonService;
    @Autowired
    private IFlowDefinitionService flowDefinitionService;
    @Autowired
    private IFlowMyBusinessService flowMyBusinessService;
    @Autowired
    private IFlowTaskService flowTaskService;
    @Autowired
    private TranslateDictTextUtils translateDictTextUtils;
    @Override
    public IPage<EamTechnicalStatusDeactivate> queryPageList(Page<EamTechnicalStatusDeactivate> page, EamTechnicalStatusDeactivateQuery query) {
        QueryWrapper<EamTechnicalStatusDeactivate> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("del_flag", CommonConstant.DEL_FLAG_0);
        //用户数据权限
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (sysUser == null) {
            return page;
        }
        //根据中心过滤数据权限
        List<BaseFactoryUser> baseFactoryUserList = baseFactoryUserService.
                list(new LambdaQueryWrapper<BaseFactoryUser>().eq(BaseFactoryUser::getUserId, sysUser.getId()));
        if (!CollectionUtils.isEmpty(baseFactoryUserList)) {
            Set<String> factoryIds = baseFactoryUserList.stream().map(BaseFactoryUser::getFactoryId).collect(Collectors.toSet());
            Set<String> factoryCodeList = baseFactoryService.listByIds(factoryIds).stream().map(BaseFactory::getOrgCode).collect(Collectors.toSet());
            queryWrapper.in("factory_org_code", factoryCodeList);
        } else {
            return page;
        }
        if (query != null) {
            //编码 模糊查询
            if (StringUtils.isNotBlank(query.getDeactivateOrderNum())) {
                queryWrapper.like("deactivate_order_num", query.getDeactivateOrderNum());
            }
            //保养分类
            if (StringUtils.isNotBlank(query.getApplicationStatus())) {
                queryWrapper.eq("application_status", query.getApplicationStatus());
            }
            if (query.getDateBegin() != null && query.getDateEnd() != null) {
                queryWrapper.between("apply_date", query.getDateBegin(), query.getDateEnd());
            }
            //申请单位
            if (StringUtils.isNotBlank(query.getFactoryOrgCode())) {
                queryWrapper.eq("factory_org_code", query.getFactoryOrgCode());
            }
            //排序
            if (StringUtils.isNotBlank(query.getColumn()) && StringUtils.isNotBlank(query.getOrder())) {
                String column = query.getColumn();
                if (column.endsWith(CommonConstant.DICT_TEXT_SUFFIX)) {
                    column = column.substring(0, column.lastIndexOf(CommonConstant.DICT_TEXT_SUFFIX));
                }
                if (DataBaseConstant.SQL_ASC.equalsIgnoreCase(query.getOrder())) {
                    queryWrapper.orderByAsc(oConvertUtils.camelToUnderline(column));
                } else {
                    queryWrapper.orderByDesc(oConvertUtils.camelToUnderline(column));
                }
            } else {
                queryWrapper.orderByDesc("create_time");
            }
        } else {
            queryWrapper.orderByDesc("create_time");
        }
        return this.getBaseMapper().selectPage(page, queryWrapper);
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addTechnicalStatusDeactivate(EamTechnicalStatusDeactivateRequest request) {
        //用户数据权限
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (sysUser == null) {
            throw new JeecgBootException("获取登录用户信息失败!");
        }
        EamTechnicalStatusDeactivate order = new EamTechnicalStatusDeactivate();
        order.setApplicant(sysUser.getUsername());
        order.setApplyDate(new Date());
        order.setRemark(request.getRemark());
        order.setApplicationStatus(TechnicalStatusDeactivateStatusEnum.WAIT_SUBMIT.name());
        order.setFactoryOrgCode(request.getFactoryOrgCode());
        String codeSeq = businessCodeRuleService.generateBusinessCodeSeq(BusinessCodeConst.TECHNICAL_STATUS_DEACTIVATE_CODE_RULE);
        order.setDeactivateOrderNum(codeSeq);
        //删除标记
        order.setDelFlag(CommonConstant.DEL_FLAG_0);
        //停用加工设备申请单
        EamBaseHFCode eamBaseHFCode = hfCodeService.selectByCategory(HfTemplateCategoryEnum.TECHNICAL_STATUS_DEACTIVATE.name());
        if (eamBaseHFCode == null) {
            throw new JeecgBootException("未配置停用加工设备申请单的HF编码,添加失败!");
        }
        order.setHfCode(eamBaseHFCode.getHfCode());
        this.getBaseMapper().insert(order);
        //处理明细数据
        request.getTableDetailList().forEach(detail -> {
            EamEquipment equipment = equipmentService.getById(detail.getEquipmentId());
            if (equipment == null) {
                throw new JeecgBootException("设备[" + detail.getEquipmentCode() + "]不存在!");
            }
            detail.setId(null);
            detail.setCreateBy(null);
            detail.setCreateTime(null);
            detail.setUpdateBy(null);
            detail.setUpdateTime(null);
        });
        deactivateDetailService.saveBatch(request.getTableDetailList());
        return true;
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean editTechnicalStatusDeactivate(EamTechnicalStatusDeactivateRequest request) {
        EamTechnicalStatusDeactivate entity = this.getBaseMapper().selectById(request.getId());
        if(entity == null) {
            throw new JeecgBootException("要编辑的数据不存在!");
        }
        if(!TechnicalStatusDeactivateStatusEnum.WAIT_SUBMIT.name().equals(entity.getApplicationStatus())) {
            throw new JeecgBootException("只有待提交的状态才支持编辑!");
        }
        //用户数据权限
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (sysUser == null) {
            throw new JeecgBootException("获取登录用户信息失败!");
        }
        entity.setRemark(request.getRemark());
        entity.setFactoryOrgCode(request.getFactoryOrgCode());
        this.getBaseMapper().updateById(entity);
        //删除原来的设备明细
        deactivateDetailService.removeByChangeId(entity.getId());
        //处理明细数据
        request.getTableDetailList().forEach(detail -> {
            EamEquipment equipment = equipmentService.getById(detail.getEquipmentId());
            if (equipment == null) {
                throw new JeecgBootException("设备[" + detail.getEquipmentCode() + "]不存在!");
            }
            detail.setId(null);
            detail.setCreateBy(null);
            detail.setCreateTime(null);
            detail.setUpdateBy(null);
            detail.setUpdateTime(null);
        });
        deactivateDetailService.saveBatch(request.getTableDetailList());
        return true;
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean submit(String id) {
        EamTechnicalStatusDeactivate entity = this.getBaseMapper().selectById(id);
        if (entity == null) {
            throw new JeecgBootException("要提交的工单不存在,请刷新重试!");
        }
        if (!TechnicalStatusDeactivateStatusEnum.WAIT_SUBMIT.name().equals(entity.getApplicationStatus())) {
            throw new JeecgBootException("该工单已进行过提交!");
        }
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if(sysUser == null || !sysUser.getUsername().equals(entity.getApplicant())) {
            throw new JeecgBootException("不是本人,不能提交此工单!");
        }
        entity.setApplicationStatus(TechnicalStatusDeactivateStatusEnum.DEPART_HEADER_SIGNING.name());
        //查询下级审批人员
        List<UserSelector> userSelectors = sysUserService.selectOperatorList(null, entity.getFactoryOrgCode(), BusinessCodeConst.PCR0011);
        if (CollectionUtil.isEmpty(userSelectors)) {
            throw new JeecgBootException("设备未分配给使用单位主管室级领导,无法提交!");
        }
        List<String> userApprovalList = userSelectors.stream().map(UserSelector::getUsername).collect(Collectors.toList());
        //更新实体
        this.getBaseMapper().updateById(entity);
        //使用单位翻译
        String factoryName = translateDictTextUtils.translateField("factory_org_code", entity.getFactoryOrgCode(), "eam_base_factory, factory_name, org_code");
        //启动流程
        flowCommonService.initActBusiness("工单号: " + entity.getDeactivateOrderNum() + ";申请单位: " + factoryName,
                entity.getId(), "IEamTechnicalStatusDeactivateService", "technical_status_deactivate_process", null);
        Map<String, Object> variables = new HashMap<>();
        variables.put("dataId", entity.getId());
        if (StrUtil.isEmpty(entity.getRemark())) {
            variables.put("organization", "新增停用加工设备申请默认启动流程");
            variables.put("comment", "新增停用加工设备申请默认启动流程");
        } else {
            variables.put("organization", entity.getRemark());
            variables.put("comment", entity.getRemark());
        }
        variables.put("proofreading", true);
        variables.put("NextAssignee", userApprovalList);
        Result<?> result = flowDefinitionService.startProcessInstanceByKey("technical_status_deactivate_process", variables);
        if(result == null || !result.isSuccess()) {
            throw new JeecgBootException("启动流程失败!");
        }
        return true;
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public EamTechnicalStatusDeactivate approval(EamTechnicalStatusDeactivateRequest request) {
        EamTechnicalStatusDeactivate entity = this.getBaseMapper().selectById(request.getId());
        if (entity == null) {
            throw new JeecgBootException("审批的数据已删除,请刷新重试!");
        }
        // 获取当前登录用户
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (user == null || StrUtil.isBlank(user.getId())) {
            throw new JeecgBootException("未获取到登录用户,请重新登录后再试!");
        }
        request.setAssignee(user.getUsername());
        // 获取流程业务记录
        FlowMyBusiness flowMyBusiness = flowMyBusinessService.getFlowMyBusiness(request.getInstanceId(), request.getTaskId());
        if (flowMyBusiness == null) {
            throw new JeecgBootException("流程实例不存在,请刷新后重试!");
        }
        boolean userAuthorized = flowMyBusinessService.isUserAuthorized(flowMyBusiness, user);
        if (!userAuthorized) {
            throw new JeecgBootException("用户无权操作此任务,请刷新后重试!");
        }
        // 认领任务
        if (!flowMyBusinessService.claimTask(flowMyBusiness.getTaskId(), user)) {
            throw new JeecgBootException("任务不存在、已完成或已被他人认领!");
        }
        TechnicalStatusDeactivateStatusEnum status = TechnicalStatusDeactivateStatusEnum.getInstance(entity.getApplicationStatus());
        if (status == null) {
            return null;
        }
        //流程变量
        Map<String, Object> values = new HashMap<>();
        List<String> userApprovalList;
        List<UserSelector> userSelectors;
        switch (status) {
            case DEPART_HEADER_SIGNING:
                userSelectors = sysUserService.selectOperatorList(null, entity.getFactoryOrgCode(), BusinessCodeConst.PCR0018);
                if (CollectionUtil.isEmpty(userSelectors)) {
                    throw new JeecgBootException("设备未分配给使用单位主管部级领导,无法进入下级审批!");
                }
                userApprovalList = userSelectors.stream().map(UserSelector::getUsername).collect(Collectors.toList());
                values.put("dataId", entity.getId());
                if(StringUtils.isBlank(request.getDepartHeaderComment())) {
                    values.put("organization", "停用加工设备申请使用单位主管室级领导");
                    values.put("comment", "停用加工设备申请使用单位主管室级领导");
                    request.setComment("停用加工设备申请使用单位主管室级领导");
                } else {
                    values.put("organization", request.getDepartHeaderComment());
                    values.put("comment", request.getDepartHeaderComment());
                    request.setComment(request.getDepartHeaderComment());
                }
                //设置entity
                entity.setDepartHeaderSignature(user.getUsername());
                entity.setDepartHeaderSignatureTime(new Date());
                entity.setDepartHeaderComment(request.getDepartHeaderComment());
                entity.setDepartHeaderSignatureResult(request.getDepartHeaderSignatureResult());
                values.put("departHeaderConfirmFlag", request.getDepartHeaderSignatureResult());
                if(BusinessCodeConst.APPROVED.equals(request.getDepartHeaderSignatureResult())) {
                    //通过
                    entity.setApplicationStatus(TechnicalStatusDeactivateStatusEnum.DEPART_LEADER_SIGNING.name());
                    values.put("NextAssignee", userApprovalList);
                }else {
                    //驳回
                    entity.setApplicationStatus(TechnicalStatusDeactivateStatusEnum.REJECTED.name());
                }
                break;
            case DEPART_LEADER_SIGNING:
                //部门领导审核
                values.put("dataId", entity.getId());
                if(StringUtils.isBlank(request.getDepartLeaderComment())) {
                    values.put("organization", "停用加工设备申请部级领导审核");
                    values.put("comment", "停用加工设备申请部级领导审核");
                    request.setComment("停用加工设备申请部级领导审核");
                } else {
                    values.put("organization", request.getDepartLeaderComment());
                    values.put("comment", request.getDepartLeaderComment());
                    request.setComment(request.getDepartLeaderComment());
                }
                //设置entity
                entity.setDepartLeaderSignature(user.getUsername());
                entity.setDepartLeaderSignatureTime(new Date());
                entity.setDepartLeaderSignatureResult(request.getDepartLeaderSignatureResult());
                entity.setDepartLeaderComment(request.getDepartLeaderComment());
                if(BusinessCodeConst.APPROVED.equals(request.getDepartLeaderSignatureResult())) {
                    //通过
                    entity.setApplicationStatus(TechnicalStatusEvaluationOrderChangeStatusEnum.COMPLETED.name());
                }else {
                    //驳回
                    entity.setApplicationStatus(TechnicalStatusEvaluationOrderChangeStatusEnum.REJECTED.name());
                }
                break;
        }
        request.setValues(values);
        // 完成流程任务
        Result<?> result = flowTaskService.complete(request);
        if (!result.isSuccess()) {
            throw new JeecgBootException("审批失败,请刷新查看!");
        }
        //保存工单
        this.getBaseMapper().updateById(entity);
        return entity;
    }
    @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) {
        //业务是否干预流程,业务干预,流程干预,指定人员进行处理
        //获取下一步处理人
        Object object = values.get("NextAssignee");
        return (List<String>) object;
    }
}