package com.yihu.hos.system.service; import com.fasterxml.jackson.databind.ObjectMapper; import com.yihu.hos.common.constants.Constants; import com.yihu.hos.core.file.FileUtil; import com.yihu.hos.core.http.HTTPResponse; import com.yihu.hos.core.http.HttpClientKit; import com.yihu.hos.services.ServiceFlowEventService; import com.yihu.hos.system.dao.FlowClassDao; import com.yihu.hos.system.dao.FlowTempDao; import com.yihu.hos.system.dao.intf.IFlowClassDao; import com.yihu.hos.system.dao.intf.IFlowDao; import com.yihu.hos.system.dao.intf.IFlowTempDao; import com.yihu.hos.system.model.SystemServiceFlow; import com.yihu.hos.system.model.SystemServiceFlowClass; import com.yihu.hos.system.model.SystemServiceFlowTemp; import com.yihu.hos.system.service.intf.IFlowManage; import com.yihu.hos.web.framework.model.ActionResult; import com.yihu.hos.web.framework.model.DictItem; import com.yihu.hos.web.framework.model.Result; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import java.io.File; import java.io.IOException; import java.util.*; /** * 系统流程管理业务类 * * @author HZY * @vsrsion 1.0 * Created at 2016/8/19. */ @Service("flowManager") public class FlowManager implements IFlowManage { public static final String BEAN_ID = "flowManager"; @Value("${esb.genCamelUrl}") private String genCamelUrl; @Resource(name = "flowDao") private IFlowDao flowDao; @Resource(name = FlowClassDao.BEAN_ID) private IFlowClassDao flowClassDao; @Resource(name = FlowTempDao.BEAN_ID) private IFlowTempDao flowTempDao; @Autowired ServiceFlowEventService serviceFlowEventService; @Override public Result getFlowList(Map params) throws Exception { return flowDao.getFlowList(params); } @Override public SystemServiceFlow getFlowById(Integer id) throws Exception { return flowDao.getEntity(SystemServiceFlow.class, id); } @Transactional public Result addFlow(SystemServiceFlow obj) throws Exception { obj.setCreateDate(new Date()); flowDao.saveEntity(obj); if (Constants.CLASS.equals(obj.getFileType())){ List flowClassList = obj.getFlowClassArray(); for (SystemServiceFlowClass flowClass:flowClassList){ flowClass.setFlowId(obj.getId()); flowDao.saveEntity(flowClass); //发送消息到MQ对列 sendUpdateMessage(obj.getCode(), flowClass, Constants.FLOW_OP_ADD); } }else if (Constants.JAVA.equals(obj.getFileType())){ List flowTempList = obj.getFlowTempArray(); for (SystemServiceFlowTemp flowTemp:flowTempList){ flowTemp.setFlowId(obj.getId()); flowDao.saveEntity(flowTemp); } } return Result.success("保存成功"); } @Transactional public Result updateFlow(SystemServiceFlow obj) throws Exception { SystemServiceFlow flow = flowDao.getEntity(SystemServiceFlow.class, obj.getId()); flow.setCode(obj.getCode()); flow.setName(obj.getName()); flow.setPath(obj.getPath()); flow.setChart(obj.getChart()); flow.setValid(obj.getValid()); flow.setFileType(obj.getFileType()); if (Constants.JAVA.equals(flow.getFileType())){ List flowTempList = obj.getFlowTempArray(); boolean succ = flowTempDao.deleteFlowTempByFlowId(obj.getId()); if (succ){ for (SystemServiceFlowTemp flowTemp:flowTempList){ flowTempDao.saveEntity(flowTemp); } } }else if (Constants.CLASS.equals(flow.getFileType())){ List classIds = flowClassDao.getFlowClassIds(obj.getId());//原flowclass集合 List flowClassList = obj.getFlowClassArray(); SystemServiceFlowClass flowClassRoute = null; String oper = ""; for (SystemServiceFlowClass flowClass:flowClassList){ if (flowClass.getId()!=null) { classIds.remove(flowClass.getId()); flowClassDao.updateEntity(flowClass); if (!flowClass.getType().equals(Constants.FLOW_TYPE_ROUTE)) { sendUpdateMessage(flow.getCode(), flowClass, Constants.FLOW_OP_UPDATE); } else { flowClassRoute = flowClass; oper = Constants.FLOW_OP_UPDATE; } }else { if (!flowClass.getType().equals(Constants.FLOW_TYPE_ROUTE)) { flowClassDao.saveEntity(flowClass); sendUpdateMessage(flow.getCode(), flowClass, Constants.FLOW_OP_ADD); } else { flowClassRoute = flowClass; oper = Constants.FLOW_OP_ADD; } } } //删除判断 if (classIds !=null && classIds.size()>0){ for (Integer id:classIds){ SystemServiceFlowClass flowClass = getFlowClassById(id); flowClassDao.deleteEntity(flowClass); if (!flowClass.getType().equals(Constants.FLOW_TYPE_ROUTE)) { sendUpdateMessage(flow.getCode(), flowClass, Constants.FLOW_OP_DELETE); } else { flowClassRoute = flowClass; flowClassRoute.setIsUpdate("1"); oper = Constants.FLOW_OP_DELETE; } } } if (flowClassRoute != null) { sendUpdateMessage(flow.getCode(), flowClassRoute, oper); } } flowDao.updateEntity(flow); return Result.success("更新成功"); } @Transactional public Result deleteFlow(Integer id) throws Exception { SystemServiceFlow flow = flowDao.getEntity(SystemServiceFlow.class, id); List flowClassList = flowClassDao.getFlowClassByFlowId(id); SystemServiceFlowClass flowClassRoute = null; String oper = ""; for (SystemServiceFlowClass flowClass:flowClassList){ flowClassDao.deleteEntity(flowClass); //发送消息到MQ对列 if (!flowClass.getType().equals(Constants.FLOW_TYPE_ROUTE)) { sendUpdateMessage(flow.getCode(), flowClass, Constants.FLOW_OP_DELETE); } else { flowClassRoute = flowClass; flowClassRoute.setIsUpdate("1"); oper = Constants.FLOW_OP_DELETE; } } if (flowClassRoute != null) { sendUpdateMessage(flow.getCode(), flowClassRoute, oper); } if (Constants.JAVA.equals(flow.getFileType())){ flowTempDao.deleteFlowTempByFlowId(id); } flowDao.deleteEntity(flow); return Result.success("删除成功"); } /* =================================== flowClass 部分================================================*/ @Override public Result getFlowClassList(Map params) throws Exception { return null; } @Override public List getFlowClassByFlowId(Integer flowId) throws Exception { return flowClassDao.getFlowClassByFlowId(flowId); } public SystemServiceFlowClass getFlowClassById(Integer id) throws Exception { return flowClassDao.getEntity(SystemServiceFlowClass.class, id); } @Transactional public Result addFlowClass(SystemServiceFlowClass obj) throws Exception { flowDao.saveEntity(obj); return Result.success("保存成功"); } @Transactional public Result updateFlowClass(SystemServiceFlowClass obj) throws Exception { return null; } @Transactional public boolean deleteFlowClassByFlowId(Integer flowId) { boolean succ =flowClassDao.deleteFlowClassByFlowId(flowId); return succ; } @Override public List getFlowTempByFlowId(Integer id) throws Exception { return flowTempDao.getFlowTempByFlowId(id); } //TODO @Override public boolean genFlewByflowTempId(Integer flowTempId) throws Exception { SystemServiceFlow flow = getFlowById(flowTempId); //生成新的route类 //添加新processor记录 return false; } @Override public String uploadFile(MultipartFile file, String baseSavePath) { String fileName = file.getOriginalFilename(); boolean succ = false; try { succ = FileUtil.writeFile(baseSavePath + File.separator + fileName, file.getBytes(), "utf-8"); if (succ){ return fileName; } } catch (IOException e) { e.printStackTrace(); } return null; } /** * 发送MQ消息-更新路由 * @param flowCode 服务流程Code标识 * @param flowClass * @param operate */ public void sendUpdateMessage(String flowCode,SystemServiceFlowClass flowClass,String operate){ //发送消息到MQ对列 if ("1".equals(flowClass.getIsUpdate()) && Constants.FLOW_TYPE_ROUTE.equals(flowClass.getType())) { //route switch (operate){ case "add" : serviceFlowEventService.routeDefineAdded(flowCode, flowClass.getPackageName(), flowClass.getClassName(), flowClass.getClassPath()); break; case "delete" : serviceFlowEventService.routeDefineDelete(flowCode, flowClass.getPackageName(), flowClass.getClassName()); break; case "update" : serviceFlowEventService.routeDefineChanged(flowCode, flowClass.getPackageName(), flowClass.getClassName(), flowClass.getClassPath()); break; default : break; } } else if ("1".equals(flowClass.getIsUpdate()) && Constants.FLOW_TYPE_PROCESSOR.equals(flowClass.getType())) { //processor switch (operate){ case "add" : serviceFlowEventService.processorAdded(flowCode, flowClass.getPackageName(), flowClass.getClassName(), flowClass.getClassPath()); break; case "delete" : serviceFlowEventService.processorDataDeleted(flowCode, flowClass.getPackageName(), flowClass.getClassName(), flowClass.getClassPath()); break; case "update" : serviceFlowEventService.processorDataChanged(flowCode, flowClass.getPackageName(), flowClass.getClassName(), flowClass.getClassPath()); break; default : break; } } } /** * 获取流程列表 * @param type 流程的文件类型 * @return * @throws Exception */ @Override public ActionResult getFlowList(String type) throws Exception { List flowList = flowDao.getFlowList(type); ActionResult re = new ActionResult(); if(flowList!=null&&flowList.size()>0) { List dictList = new ArrayList<>(); for(SystemServiceFlow item:flowList){ DictItem dict = new DictItem(); dict.setCode(item.getId().toString()); dict.setValue(item.getName()); dict.setExtend(""); dictList.add(dict); } re.setData(dictList); } return re; } /** * TODO 调用broker接口生成camel相关文件 * @param flowId * @param newCron * @throws Exception */ public Integer genCamelFile(Integer flowId, String newCron) throws Exception { Long timestamp = System.currentTimeMillis(); ObjectMapper objectMapper = new ObjectMapper(); List flowClassRouters = flowTempDao.getFlowTemps(flowId, Constants.FLOW_TYPE_ROUTE); List flowClassProces = flowTempDao.getFlowTemps(flowId, Constants.FLOW_TYPE_PROCESSOR); SystemServiceFlow oldFlow = getFlowById(flowId); //route模板文件记录是否存在。不存在就返回。 if (!flowClassRouters.isEmpty()){ Map params = null; SystemServiceFlowTemp flowTemp =flowClassRouters.get(0); StringBuilder basePath = new StringBuilder();; if (flowTemp.getPackageName()!=null){ String packagePath[] = flowTemp.getPackageName().split("\\."); for (int i=0;i(); params.put("type",Constants.FLOW_TYPE_PROCESSOR); params.put("filePath", process.getClassPath()); params.put("packageName", proPath.toString()); params.put("newClassName",process.getClassName());//原文件名加当前时间戳 params.put("oldClassName", process.getClassName()); params.put("newCron",newCron); HTTPResponse response = HttpClientKit.post(genCamelUrl, params); if (response.getStatusCode()==200 ){ Map body = objectMapper.readValue(response.getBody(),Map.class); boolean succ = (boolean) body.get("successFlg"); if (succ){ newProcessPath = body.get("message").toString(); }else { return null; } } System.out.println(response.getBody()); SystemServiceFlowClass processClass = new SystemServiceFlowClass(); processClass.setPackageName(process.getPackageName()); processClass.setClassName(process.getClassName()); processClass.setClassPath(newProcessPath); processClass.setFlowId(newFlow.getId()); processClass.setType(Constants.FLOW_TYPE_PROCESSOR); flowClassDao.saveEntity(processClass); processClass.setIsUpdate("1"); sendUpdateMessage(newFlow.getCode(), processClass, Constants.FLOW_OP_ADD); // copyProcessor(process.getClassPath(),proPath.toString(),process.getClassName()); } String newClassName = flowTemp.getClassName()+timestamp; String newRoutePath =null; params = new HashMap<>(); params.put("type",Constants.FLOW_TYPE_ROUTE); params.put("filePath", flowTemp.getClassPath()); params.put("packageName", basePath.toString()); params.put("oldClassName", flowTemp.getClassName()); params.put("newClassName",newClassName);//原文件名加当前时间戳 params.put("newCron",newCron); HTTPResponse response = HttpClientKit.post(genCamelUrl, params); if (response.getStatusCode()==200 ){ Map body = objectMapper.readValue(response.getBody(),Map.class); boolean succ = (boolean) body.get("successFlg"); if (succ){ newRoutePath = body.get("message").toString(); }else { return null; } } System.out.println(response.getBody()); SystemServiceFlowClass newFlowClass = new SystemServiceFlowClass(); newFlowClass.setPackageName(flowTemp.getPackageName()); newFlowClass.setClassName(newClassName); newFlowClass.setClassPath(newRoutePath); newFlowClass.setFlowId(newFlow.getId()); newFlowClass.setType(Constants.FLOW_TYPE_ROUTE); flowClassDao.saveEntity(newFlowClass); newFlowClass.setIsUpdate("1"); sendUpdateMessage(newFlow.getCode(), newFlowClass, Constants.FLOW_OP_ADD); // genNewRoutefile(flowTemp.getClassPath(),basePath.toString(),flowTemp.getClassName(),newClassName,newCron); return newFlow.getId(); } return null; } /** * 修改camel相关文件 * @param flowId 流程ID * @param newCron 新cron * @return * @throws Exception */ @Override public Integer updateCamelFile(Integer flowTempId,Integer flowId, String newCron) throws Exception { Long timestamp = System.currentTimeMillis(); ObjectMapper objectMapper = new ObjectMapper(); List flowTempRouters = flowTempDao.getFlowTemps(flowTempId, Constants.FLOW_TYPE_ROUTE); List flowClassRouters = flowClassDao.getFlowClass(flowId, Constants.FLOW_TYPE_ROUTE); // SystemServiceFlow oldFlow = getFlowById(flowId); SystemServiceFlow flow = flowDao.getEntity(SystemServiceFlow.class,flowId); //route模板文件记录是否存在。不存在就返回。 if (!flowTempRouters.isEmpty()){ SystemServiceFlowTemp flowTemp =flowTempRouters.get(0); SystemServiceFlowClass flowClass =flowClassRouters.get(0); StringBuilder basePath = new StringBuilder();; if (flowTemp.getPackageName()!=null){ String packagePath[] = flowTemp.getPackageName().split("\\."); for (int i=0;i params = new HashMap<>(); params.put("type",Constants.FLOW_TYPE_ROUTE); params.put("filePath", flowTemp.getClassPath()); params.put("packageName", basePath.toString()); params.put("oldClassName", flowTemp.getClassName()); params.put("newClassName", flowClass.getClassName());//原文件名加当前时间戳 params.put("newCron",newCron); HTTPResponse response = HttpClientKit.post(genCamelUrl, params); if (response.getStatusCode()==200 ){ Map body = objectMapper.readValue(response.getBody(),Map.class); boolean succ = (boolean) body.get("successFlg"); if (succ){ //route文件生成成功,发送消息 flowClass.setIsUpdate("1"); sendUpdateMessage(flow.getCode(), flowClass, Constants.FLOW_OP_UPDATE); return flowId; }else { return null; } }else { return null; } // genNewRoutefile(flowTemp.getClassPath(),basePath.toString(),flowTemp.getClassName(),newClassName,newCron); } return null; } }