package com.qmth.distributed.print.api; import com.qmth.boot.api.annotation.Aac; import com.qmth.boot.api.annotation.BOOL; import com.qmth.boot.api.constant.ApiConstant; import com.qmth.boot.api.exception.ApiException; import com.qmth.distributed.print.business.bean.flow.CustomFlowSaveDto; import com.qmth.distributed.print.business.entity.TFCustomFlow; import com.qmth.distributed.print.business.service.ActivitiService; import com.qmth.distributed.print.business.service.TFCustomFlowService; import com.qmth.teachcloud.common.contant.SystemConstant; import com.qmth.teachcloud.common.entity.BasicSchool; import com.qmth.teachcloud.common.entity.SysUser; import com.qmth.teachcloud.common.enums.ExceptionResultEnum; import com.qmth.teachcloud.common.service.CommonCacheService; import com.qmth.teachcloud.common.util.*; import io.swagger.annotations.*; import org.activiti.engine.TaskService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.transaction.annotation.Transactional; import org.springframework.validation.BindingResult; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import javax.validation.Valid; import java.security.NoSuchAlgorithmException; import java.util.Map; import java.util.Objects; import java.util.concurrent.atomic.AtomicInteger; /** *

* 自定义流程表 前端控制器 *

* * @author wangliang * @since 2022-01-21 */ @Api(tags = "自定义流程Controller") @RestController @RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + "/${prefix.url.customFlow}") @Aac(auth = BOOL.FALSE, strict = BOOL.FALSE) @Validated public class TFCustomFlowController { private final static Logger log = LoggerFactory.getLogger(TFCustomFlowController.class); @Resource TFCustomFlowService tfCustomFlowService; @Resource RedisUtil redisUtil; @Resource ActivitiService activitiService; @Resource CommonCacheService commonCacheService; @ApiOperation(value = "保存和发布流程") @ApiResponses({@ApiResponse(code = 200, message = "常规信息", response = ResultUtil.class)}) @RequestMapping(value = "/save", method = RequestMethod.POST) @Transactional public Result save(@Valid @RequestBody CustomFlowSaveDto customFlowSaveDto, BindingResult bindingResult) throws NoSuchAlgorithmException { if (bindingResult.hasErrors()) { return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage()); } SysUser sysUser = (SysUser) ServletUtil.getRequestUser(); customFlowSaveDto.setSchoolAndOrgInfo(sysUser.getSchoolId(), sysUser.getOrgId()); String flowBpmnId = MD5Util.encoder(customFlowSaveDto.toString()); BasicSchool basicSchool = commonCacheService.schoolCache(customFlowSaveDto.getSchoolId()); flowBpmnId = basicSchool.getCode() + "_" + flowBpmnId; boolean lock = redisUtil.lock(SystemConstant.REDIS_LOCK_CUSTOM_FLOW_PREFIX + flowBpmnId, SystemConstant.REDIS_LOCK_CUSTOM_FLOW_TIME_OUT); if (!lock) { throw ExceptionResultEnum.ERROR.exception("正在发布中,请稍候再试!"); } try { Integer dbVersion = tfCustomFlowService.findMaxVersion(customFlowSaveDto.getSchoolId(), customFlowSaveDto.getOrgId(), customFlowSaveDto.getType()); TFCustomFlow tfCustomFlow = new TFCustomFlow(customFlowSaveDto.getSchoolId(), customFlowSaveDto.getOrgId(), customFlowSaveDto.getName(), customFlowSaveDto.getType(), customFlowSaveDto.getPublish(), JacksonUtil.parseJson(customFlowSaveDto.getCustomFlowLists()), sysUser.getId(), flowBpmnId); AtomicInteger atomicInteger = null; if (Objects.isNull(dbVersion)) {//新增 atomicInteger = new AtomicInteger(1); tfCustomFlow.setVersion(atomicInteger.get()); } else {//版本号递增 atomicInteger = new AtomicInteger(dbVersion); tfCustomFlow.setVersion(atomicInteger.incrementAndGet()); } //自定义流程处理开始 Map map = activitiService.dynamicBuildBpmn(customFlowSaveDto, flowBpmnId); tfCustomFlow.setFlowProcessVar(JacksonUtil.parseJson(map)); tfCustomFlow.setActFlowId((String) map.get(SystemConstant.PROCESS_DEFINITION_ID)); tfCustomFlowService.save(tfCustomFlow); } catch (Exception e) { log.error(SystemConstant.LOG_ERROR, e); if (e instanceof ApiException) { ResultUtil.error((ApiException) e, e.getMessage()); } else { ResultUtil.error(e.getMessage()); } } finally { redisUtil.releaseLock(SystemConstant.REDIS_LOCK_CUSTOM_FLOW_PREFIX + flowBpmnId); } return ResultUtil.ok(true); } @Resource TaskService taskService; @ApiOperation(value = "测试启动流程") @ApiResponses({@ApiResponse(code = 200, message = "常规信息", response = ResultUtil.class)}) @RequestMapping(value = "/testStart", method = RequestMethod.POST) @Transactional public Result testStart(@ApiParam(value = "流程id", required = true) @RequestParam String id) { SysUser sysUser = (SysUser) ServletUtil.getRequestUser(); activitiService.customFlowStartUpdateApproveId(SystemConstant.convertIdToLong(id), sysUser.getId()); return ResultUtil.ok(true); } }