From f67410ddf300c6685187454810b11ae12b02c886 Mon Sep 17 00:00:00 2001 From: wangliwen Date: Thu, 3 Nov 2022 15:09:31 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8A=95=E6=A0=87=E6=BC=94=E7=A4=BA=E4=B8=8A?= =?UTF-8?q?=E7=BA=BF=E8=83=BD=E5=8A=9B=E6=B5=81=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ResourceMountShowController.java | 244 ++++++++++++++++++ .../show/ResourceOwnerShowListener.java | 194 ++++++++++++++ 2 files changed, 438 insertions(+) create mode 100644 renren-admin/src/main/java/io/renren/common/controller/ResourceMountShowController.java create mode 100644 renren-admin/src/main/java/io/renren/modules/resourceMountApply/listener/show/ResourceOwnerShowListener.java diff --git a/renren-admin/src/main/java/io/renren/common/controller/ResourceMountShowController.java b/renren-admin/src/main/java/io/renren/common/controller/ResourceMountShowController.java new file mode 100644 index 00000000..a6cc22cf --- /dev/null +++ b/renren-admin/src/main/java/io/renren/common/controller/ResourceMountShowController.java @@ -0,0 +1,244 @@ +package io.renren.common.controller; + +import cn.hutool.core.util.ObjectUtil; +import cn.hutool.crypto.SecureUtil; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import io.renren.common.annotation.LogOperation; +import io.renren.common.page.PageData; +import io.renren.common.utils.CodeGenerationUtils; +import io.renren.common.utils.Result; +import io.renren.common.validator.ValidatorUtils; +import io.renren.common.validator.group.AddGroup; +import io.renren.common.validator.group.DefaultGroup; +import io.renren.modules.activiti.dto.ProcessInstanceDTO; +import io.renren.modules.activiti.dto.ProcessStartDTO; +import io.renren.modules.activiti.service.ActProcessService; +import io.renren.modules.activiti.service.ActRunningService; +import io.renren.modules.resource.dto.ResourceDTO; +import io.renren.modules.resource.entity.ResourceEntityDelFlag; +import io.renren.modules.resource.service.ResourceService; +import io.renren.modules.resourceMountApply.dto.TResourceBatchMountApplyDTO; +import io.renren.modules.resourceMountApply.dto.TResourceMountApplyDTO; +import io.renren.modules.resourceMountApply.dto.TResourceUndercarriageApplyDTO; +import io.renren.modules.resourceMountApply.service.TResourceMountApplyService; +import io.renren.modules.security.user.SecurityUser; +import io.renren.modules.sys.dto.SysDeptDTO; +import io.renren.modules.sys.dto.SysUserDTO; +import io.renren.modules.sys.service.SysDeptService; +import io.renren.modules.sys.service.SysUserService; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import org.activiti.engine.TaskService; +import org.activiti.engine.task.Task; +import org.activiti.engine.task.TaskQuery; +import org.apache.commons.lang3.StringUtils; +import org.codehaus.jackson.map.ObjectMapper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.*; +import java.util.stream.Collectors; + +@Api(tags = "资源上架") +@RestController +@RequestMapping("/resource/center") +public class ResourceMountShowController { + private static final Logger logger = LoggerFactory.getLogger(ResourceMountShowController.class); + private static final ObjectMapper oMapper = new ObjectMapper(); + @Autowired + private ActProcessService actProcessService; + @Autowired + private ActRunningService actRunningService; + @Autowired + private TResourceMountApplyService tResourceMountApplyService; + @Autowired + private ResourceService resourceService; + @Autowired + private SysUserService sysUserService; + private final CodeGenerationUtils codeGenerationUtils = CodeGenerationUtils.getInstance(); + + @Autowired + protected TaskService taskService; + @Autowired + private JdbcTemplate jdbcTemplate; + @Autowired + private SysDeptService sysDeptService; + + @Value("${big_date.name}") + private String bigDateDeptName; // 大数据局名称 + + private static final String APPLY_KEY = "resourcemountapply_show"; // 资源上架 + + private static final String UNDERCARRIAGE_KEY = "resourcundercarriageapply"; // 资源下架 + + private static final Map apply_params = new HashMap() { + { + put("isLatestVersion", true); // 取最新版本 + put("key", APPLY_KEY); // 限定 能力资源上架 + } + }; + + private static final Map undercarriage_params = new HashMap() { + { + put("isLatestVersion", true); // 取最新版本 + put("key", UNDERCARRIAGE_KEY); // 限定 资源下架 + } + }; + + + @PostMapping(value = "/apply_show") + @ApiOperation("批量进行能力上架申请") + @LogOperation("批量进行能力上架申请") + public Result> apply(@RequestBody TResourceBatchMountApplyDTO tResourceBatchMountApplyDTO) { + // 仿照请求接口 /act/process/lastestPage + PageData> page = actProcessService.page(apply_params); + if (page.getTotal() <= 0) { // + return new Result().error("联系管理员添加流程"); + } + logger.info("---------------------------------------------------"); + logger.info(JSONObject.toJSONString(tResourceBatchMountApplyDTO)); + logger.info("####################################################"); + return new Result().ok(tResourceBatchMountApplyDTO.getResourceDTO().stream().map(index -> { + TResourceMountApplyDTO tResourceMountApplyDTO = new TResourceMountApplyDTO(); + tResourceMountApplyDTO.setPhone(tResourceBatchMountApplyDTO.getPhone()); + tResourceMountApplyDTO.setDeptId(tResourceBatchMountApplyDTO.getDeptId()); + tResourceMountApplyDTO.setUserId(tResourceBatchMountApplyDTO.getUserId()); + tResourceMountApplyDTO.setUserName(tResourceBatchMountApplyDTO.getUserName()); + tResourceMountApplyDTO.setParameterContent(JSON.toJSONString(index)); + tResourceMountApplyDTO.setParameterContentMd5(SecureUtil.md5(JSON.toJSONString(index))); + tResourceMountApplyDTO.setResourceDTO(index); + tResourceMountApplyDTO.setEnclosure(index.getEnclosure()); + tResourceMountApplyDTO.setFlowType("资源上架"); + try { + tResourceMountApplyDTO.setResourceId(tResourceMountApplyDTO.getResourceDTO().getId()); + } catch (Exception e) { + logger.error("资源上架异常", e); + } + + if (StringUtils.isEmpty(tResourceBatchMountApplyDTO.getDeptId())) { + Optional userDTO = Optional.ofNullable(sysUserService.get(Long.valueOf(tResourceBatchMountApplyDTO.getUserId()))); + userDTO.ifPresent(user -> { + if (user.getSuperAdmin() == 1) { + SysDeptDTO deptDTO = sysDeptService.getByName(bigDateDeptName); + tResourceMountApplyDTO.setDeptId(deptDTO.getId().toString()); + } + }); + } + + ValidatorUtils.validateEntity(tResourceMountApplyDTO, AddGroup.class, DefaultGroup.class); + tResourceMountApplyService.save(tResourceMountApplyDTO); // 保存单条资源申请记录 + if (tResourceMountApplyDTO.getId() == null) { + return null; + } + codeGenerationUtils.setApplyNumber("NLSJ", Arrays.asList(tResourceMountApplyDTO.getId()), jdbcTemplate); + TaskQuery taskQuery = taskService.createTaskQuery(); + Task task = taskQuery.active().processInstanceBusinessKey(tResourceMountApplyDTO.getId().toString()).singleResult(); + if (task != null) { + logger.error("该资源已发起上架"); + throw new RuntimeException("该资源已发起上架"); + } + + tResourceMountApplyDTO.setCompleteEntry(Boolean.TRUE); + logger.info("-------------------1.保存申请表单成功--------------------------"); + // 仿照请求接口 /act/running/startOfBusinessKey + ProcessStartDTO processStartDTO = new ProcessStartDTO(); + processStartDTO.setBusinessKey(tResourceMountApplyDTO.getId().toString()); + processStartDTO.setProcessDefinitionKey(APPLY_KEY); // 限定资源上架 + + Map variables = oMapper.convertValue(tResourceMountApplyDTO, Map.class); + processStartDTO.setVariables(variables); + ProcessInstanceDTO dto = actRunningService.startOfBusinessKey(processStartDTO); + logger.info("-------------------2.启动流程成功--------------------------"); + logger.info("ProcessInstanceDTO.getBusinessKey:{}", dto.getBusinessKey()); + + if (Long.valueOf(dto.getBusinessKey()) != null) { + // 仿照请求接口 /processForm/tabilityapplication/updateInstanceId + tResourceMountApplyService.updateInstanceId(dto.getProcessInstanceId(), Long.valueOf(dto.getBusinessKey())); + logger.info("-------------------更新updateInstanceId.成功--------------------------"); + logger.info("ProcessInstanceDTO.getProcessInstanceId:{}", dto.getProcessInstanceId()); + } + return dto; + }).filter(ObjectUtil::isNotNull).collect(Collectors.toList())); + } + + @PostMapping(value = "/undercarriage") + @ApiOperation("批量进行能力下架申请") + @LogOperation("批量进行能力下架申请") + public Result undercarriage(@RequestBody TResourceUndercarriageApplyDTO tResourceUndercarriageApplyDTO) { + // 仿照请求接口 /act/process/lastestPage + PageData> page = actProcessService.page(undercarriage_params); + if (page.getTotal() <= 0) { // + return new Result().error("联系管理员添加流程"); + } + logger.info("---------------------------------------------------"); + logger.info(JSONObject.toJSONString(tResourceUndercarriageApplyDTO)); + logger.info("####################################################"); + Optional undercarriage = + tResourceUndercarriageApplyDTO.getResource().stream().map(index -> { + Long resourceId = Long.valueOf(index.get("resourceId")); + Optional resourceDTO = Optional.ofNullable(resourceService.get(resourceId)); + if (resourceDTO.isPresent()) { + TaskQuery taskQuery = taskService.createTaskQuery(); + long task = taskQuery.active().processDefinitionKey(UNDERCARRIAGE_KEY).processInstanceBusinessKey(resourceId.toString()).count(); + return task > 0; + } else { + return true; + } + }).filter(index -> index).findAny(); + if (undercarriage.isPresent()) { + return new Result<>().ok("该资源已发起下架"); + } + return new Result().ok(tResourceUndercarriageApplyDTO.getResource().stream().map(index -> { + List dtoList = new ArrayList<>(); + Long resourceId = Long.valueOf(index.get("resourceId")); + Optional resourceDTO = Optional.ofNullable(resourceService.get(resourceId)); + resourceDTO.ifPresent(dto -> { + TaskQuery taskQuery = taskService.createTaskQuery(); + long task = taskQuery.active().processDefinitionKey(UNDERCARRIAGE_KEY).processInstanceBusinessKey(resourceId.toString()).count(); + if (task > 0) { + logger.error("该资源已发起下架"); + throw new RuntimeException("该资源已发起下架"); + } + dto.setUndercarriageReason(tResourceUndercarriageApplyDTO.getReason()); + dto.setUndercarriageEnclosure(tResourceUndercarriageApplyDTO.getEnclosure()); + dto.setUndercarriageEnclosureName(tResourceUndercarriageApplyDTO.getEnclosureName()); + dto.setUndercarriageTitle(tResourceUndercarriageApplyDTO.getTitle()); + dto.setUndercarriageUserPhone(tResourceUndercarriageApplyDTO.getPhone()); + dto.setDelFlag(ResourceEntityDelFlag.NORMAL.getFlag()); // 设置为正常 + dto.setFlowType("资源下架"); + String userId = SecurityUser.getUserId().toString(); + Optional userDTO = Optional.ofNullable(sysUserService.get(Long.valueOf(userId))); + userDTO.ifPresent(user -> { + dto.setUndercarriageUserName(user.getRealName()); + }); + codeGenerationUtils.setApplyNumber("NLXJ", Arrays.asList(dto.getId()), jdbcTemplate); + ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class); + resourceService.update(dto); + logger.error(resourceDTO.get().toString()); + dto.setCompleteEntry(Boolean.TRUE); + + logger.info("-------------------1.保存申请表单成功--------------------------"); + // 仿照请求接口 /act/running/startOfBusinessKey + ProcessStartDTO processStartDTO = new ProcessStartDTO(); + processStartDTO.setBusinessKey(resourceId.toString()); + processStartDTO.setProcessDefinitionKey(UNDERCARRIAGE_KEY); // 限定资源下架 + + Map variables = oMapper.convertValue(dto, Map.class); + processStartDTO.setVariables(variables); + ProcessInstanceDTO index_ = actRunningService.startOfBusinessKey(processStartDTO); + logger.info("-------------------2.启动流程成功--------------------------"); + logger.info("ProcessInstanceDTO.getBusinessKey:{}", index_.getBusinessKey()); + dtoList.add(index_); + }); + return dtoList.stream().findAny().orElse(null); + }).filter(ObjectUtil::isNotNull).collect(Collectors.toList())); + } +} diff --git a/renren-admin/src/main/java/io/renren/modules/resourceMountApply/listener/show/ResourceOwnerShowListener.java b/renren-admin/src/main/java/io/renren/modules/resourceMountApply/listener/show/ResourceOwnerShowListener.java new file mode 100644 index 00000000..76638970 --- /dev/null +++ b/renren-admin/src/main/java/io/renren/modules/resourceMountApply/listener/show/ResourceOwnerShowListener.java @@ -0,0 +1,194 @@ +package io.renren.modules.resourceMountApply.listener.show; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import io.renren.modules.processForm.service.ApiGateway; +import io.renren.modules.resource.dto.ResourceDTO; +import io.renren.modules.resource.entity.ResourceEntityDelFlag; +import io.renren.modules.resource.service.ResourceService; +import io.renren.modules.resourceMountApply.dto.TResourceMountApplyDTO; +import io.renren.modules.sys.dto.SysRoleDTO; +import io.renren.modules.sys.service.SysDeptService; +import io.renren.modules.sys.service.SysRoleService; +import io.renren.modules.sys.service.SysUserService; +import org.activiti.engine.TaskService; +import org.activiti.engine.delegate.*; +import org.activiti.engine.delegate.event.ActivitiEvent; +import org.activiti.engine.delegate.event.ActivitiEventListener; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; +import java.util.Date; +import java.util.Map; + +/** + * 资源所有者节点审批 + */ +@Component +public class ResourceOwnerShowListener implements TaskListener, ExecutionListener, ActivitiEventListener, JavaDelegate { + private static Logger logger = LoggerFactory.getLogger(ResourceOwnerShowListener.class); + + @Value("${big_date.name}") + private String bigDateDeptName; // 大数据局名称 + @Value("${big_date.default_assignee_role_id}") + private String defaultAssigneeRoleId; // 当某部门未设置部门审批人时,将使用该用户审批 + @Value("${big_date.assignee_role_name}") + private String roleName; // 具备审批的角色名称 + + @Autowired + private SysRoleService sysRoleService; + @Autowired + private TaskService taskService; + @Autowired + private SysUserService sysUserService; + @Resource(name = "${hisense.gateway.name}") + private ApiGateway apiGateway; + @Autowired + private SysDeptService sysDeptService; + @Autowired + private ResourceService resourceService; + + @Override + public void notify(DelegateExecution execution) throws Exception { + logger.error("----------------------进入审批通过节点---------------------------"); + logger.error("事件类型:" + execution.getEventName()); + final String eventName = execution.getEventName(); + + switch (eventName) { + case EVENTNAME_END: { + endTake(execution.getVariables()); + } + break; + } + } + + @Override + public void execute(DelegateExecution execution) throws Exception { + + } + + @Override + public void notify(DelegateTask delegateTask) { + logger.error("----------------------进入资源所有者节点---------------------------"); + logger.error("事件类型:" + delegateTask.getEventName()); + SysRoleDTO roleDTO = sysRoleService.getByName(roleName); + logger.error("roleDTOId:" + roleDTO.getId()); + final String eventName = delegateTask.getEventName(); + switch (eventName) { + case EVENTNAME_CREATE: // 创建当前审批节点事件 + create(delegateTask); + break; + case EVENTNAME_COMPLETE: + complete(delegateTask); + break; + default: + logger.error("未处理该事件:" + eventName); + } + logger.error("-----------------------结束资源所有者节点---------------------------"); + } + + /** + * Called when an event has been fired + * + * @param event the event + */ + @Override + public void onEvent(ActivitiEvent event) { + + } + + /** + * @return whether or not the current operation should fail when this listeners execution + * throws an exception. + */ + @Override + public boolean isFailOnException() { + return false; + } + + /** + * 进入最后结束节点 + * + * @param kv + */ + private void endTake(Map kv) { // 进入最后结束节点 + GsonBuilder builder = new GsonBuilder(); + builder.registerTypeAdapter(Date.class, (JsonDeserializer) (json, typeOfT, context) -> new Date(json.getAsJsonPrimitive().getAsLong())); + + Gson gson = builder.create(); + JsonElement jsonElement = gson.toJsonTree(kv); + TResourceMountApplyDTO resourceMountApplyDTO = gson.fromJson(jsonElement, TResourceMountApplyDTO.class); + ResourceDTO re = resourceService.get(resourceMountApplyDTO.getResourceId()); + if (resourceMountApplyDTO != null) { + if (resourceMountApplyDTO.getReject() != null && resourceMountApplyDTO.getReject() == Boolean.TRUE) { // 存在被拒绝的节点 + if (re != null) { + re.setDelFlag(ResourceEntityDelFlag.REJECT_REVIEW.getFlag()); + resourceService.update(re); + logger.error("审批未通过!资源id:" + re.getId()); + } + return; + } + + if (resourceMountApplyDTO.getReject() == null || resourceMountApplyDTO.getReject() != Boolean.TRUE) { // 都是同意 + if (re != null) { + re.setDelFlag(ResourceEntityDelFlag.NORMAL.getFlag()); + resourceService.update(re); + logger.error("审批通过!资源id:" + re.getId()); + try { + apiGateway.registerApi2Gateway(String.valueOf(re.getId())); // 发布到应用网关 + } catch (Exception exception) { + logger.error("发布到应用网关", exception); + } + } + } + } else { + logger.error("能力上架资源信息故障-->" + JSON.toJSONString(kv)); + } + } + + /** + * 流程结束,推送 + * + * @param delegateTask + */ + private void complete(DelegateTask delegateTask) { + Map kv = delegateTask.getVariables(); + GsonBuilder builder = new GsonBuilder(); + builder.registerTypeAdapter(Date.class, (JsonDeserializer) (json, typeOfT, context) -> new Date(json.getAsJsonPrimitive().getAsLong())); + + Gson gson = builder.create(); + JsonElement jsonElement = gson.toJsonTree(kv); + TResourceMountApplyDTO resourceMountApplyDTO = gson.fromJson(jsonElement, TResourceMountApplyDTO.class); + ResourceDTO re = resourceMountApplyDTO.getResourceDTO(); + if (re != null) { + re.setDelFlag(ResourceEntityDelFlag.NORMAL.getFlag()); + resourceService.update(re); + } + } + + + /** + * 节点创建时分配发起人为审核人 + * + * @param delegateTask + */ + private void create(DelegateTask delegateTask) { + Map kv = delegateTask.getVariables(); + GsonBuilder builder = new GsonBuilder(); + builder.registerTypeAdapter(Date.class, (JsonDeserializer) (json, typeOfT, context) -> new Date(json.getAsJsonPrimitive().getAsLong())); + + Gson gson = builder.create(); + JsonElement jsonElement = gson.toJsonTree(kv); + TResourceMountApplyDTO resourceMountApplyDTO = gson.fromJson(jsonElement, TResourceMountApplyDTO.class); + logger.error(JSONObject.toJSONString(resourceMountApplyDTO)); + taskService.setAssignee(delegateTask.getId(), resourceMountApplyDTO.getUserId()); + } +}