FlowManager.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. package com.yihu.hos.system.service;
  2. import com.yihu.hos.common.constants.Constants;
  3. import com.yihu.hos.core.file.FileUtil;
  4. import com.yihu.hos.core.http.HttpClientKit;
  5. import com.yihu.hos.services.ServiceFlowEventService;
  6. import com.yihu.hos.system.dao.FlowClassDao;
  7. import com.yihu.hos.system.dao.FlowTempDao;
  8. import com.yihu.hos.system.dao.intf.IFlowClassDao;
  9. import com.yihu.hos.system.dao.intf.IFlowDao;
  10. import com.yihu.hos.system.dao.intf.IFlowTempDao;
  11. import com.yihu.hos.system.model.SystemServiceFlow;
  12. import com.yihu.hos.system.model.SystemServiceFlowClass;
  13. import com.yihu.hos.system.model.SystemServiceFlowTemp;
  14. import com.yihu.hos.system.service.intf.IFlowManage;
  15. import com.yihu.hos.web.framework.model.ActionResult;
  16. import com.yihu.hos.web.framework.model.DictItem;
  17. import com.yihu.hos.web.framework.model.Result;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.beans.factory.annotation.Value;
  20. import org.springframework.stereotype.Service;
  21. import org.springframework.transaction.annotation.Transactional;
  22. import org.springframework.web.multipart.MultipartFile;
  23. import javax.annotation.Resource;
  24. import java.io.File;
  25. import java.io.IOException;
  26. import java.util.*;
  27. /**
  28. * 系统流程管理业务类
  29. *
  30. * @author HZY
  31. * @vsrsion 1.0
  32. * Created at 2016/8/19.
  33. */
  34. @Service("flowManager")
  35. public class FlowManager implements IFlowManage {
  36. public static final String BEAN_ID = "flowManager";
  37. @Value("${esb.genCamelUrl}")
  38. private String genCamelUrl;
  39. @Resource(name = "flowDao")
  40. private IFlowDao flowDao;
  41. @Resource(name = FlowClassDao.BEAN_ID)
  42. private IFlowClassDao flowClassDao;
  43. @Resource(name = FlowTempDao.BEAN_ID)
  44. private IFlowTempDao flowTempDao;
  45. @Autowired
  46. ServiceFlowEventService serviceFlowEventService;
  47. @Override
  48. public Result getFlowList(Map<String, Object> params) throws Exception {
  49. return flowDao.getFlowList(params);
  50. }
  51. @Override
  52. public SystemServiceFlow getFlowById(Integer id) throws Exception {
  53. return flowDao.getEntity(SystemServiceFlow.class, id);
  54. }
  55. @Transactional
  56. public Result addFlow(SystemServiceFlow obj) throws Exception {
  57. obj.setCreateDate(new Date());
  58. flowDao.saveEntity(obj);
  59. if (Constants.CLASS.equals(obj.getFileType())){
  60. List<SystemServiceFlowClass> flowClassList = obj.getFlowClassArray();
  61. for (SystemServiceFlowClass flowClass:flowClassList){
  62. flowClass.setFlowId(obj.getId());
  63. flowDao.saveEntity(flowClass);
  64. //发送消息到MQ对列
  65. sendUpdateMessage(obj.getCode(), flowClass, Constants.FLOW_OP_ADD);
  66. }
  67. }else if (Constants.JAVA.equals(obj.getFileType())){
  68. List<SystemServiceFlowTemp> flowTempList = obj.getFlowTempArray();
  69. for (SystemServiceFlowTemp flowTemp:flowTempList){
  70. flowTemp.setFlowId(obj.getId());
  71. flowDao.saveEntity(flowTemp);
  72. }
  73. }
  74. return Result.success("保存成功");
  75. }
  76. @Transactional
  77. public Result updateFlow(SystemServiceFlow obj) throws Exception {
  78. SystemServiceFlow flow = flowDao.getEntity(SystemServiceFlow.class, obj.getId());
  79. flow.setCode(obj.getCode());
  80. flow.setName(obj.getName());
  81. flow.setPath(obj.getPath());
  82. flow.setChart(obj.getChart());
  83. flow.setValid(obj.getValid());
  84. flow.setFileType(obj.getFileType());
  85. if (Constants.JAVA.equals(flow.getFileType())){
  86. List<SystemServiceFlowTemp> flowTempList = obj.getFlowTempArray();
  87. boolean succ = flowTempDao.deleteFlowTempByFlowId(obj.getId());
  88. if (succ){
  89. for (SystemServiceFlowTemp flowTemp:flowTempList){
  90. flowTempDao.saveEntity(flowTemp);
  91. }
  92. }
  93. }else if (Constants.CLASS.equals(flow.getFileType())){
  94. List<Integer> classIds = flowClassDao.getFlowClassIds(obj.getId());//原flowclass集合
  95. List<SystemServiceFlowClass> flowClassList = obj.getFlowClassArray();
  96. for (SystemServiceFlowClass flowClass:flowClassList){
  97. if (flowClass.getId()!=null) {
  98. classIds.remove(flowClass.getId());
  99. flowClassDao.updateEntity(flowClass);
  100. sendUpdateMessage(flow.getCode(), flowClass, Constants.FLOW_OP_UPDATE);
  101. }else {
  102. flowClassDao.saveEntity(flowClass);
  103. sendUpdateMessage(flow.getCode(), flowClass, Constants.FLOW_OP_ADD);
  104. }
  105. }
  106. //删除判断
  107. if (classIds !=null && classIds.size()>0){
  108. for (Integer id:classIds){
  109. SystemServiceFlowClass flowClass = getFlowClassById(id);
  110. flowClassDao.deleteEntity(flowClass);
  111. sendDeleteMessage(flow.getCode(), flowClass);
  112. }
  113. }
  114. }
  115. flowDao.updateEntity(flow);
  116. return Result.success("更新成功");
  117. }
  118. @Transactional
  119. public Result deleteFlow(Integer id) throws Exception {
  120. SystemServiceFlow flow = flowDao.getEntity(SystemServiceFlow.class, id);
  121. List<SystemServiceFlowClass> flowClassList = flowClassDao.getFlowClassByFlowId(id);
  122. for (SystemServiceFlowClass flowClass:flowClassList){
  123. flowClassDao.deleteEntity(flowClass);
  124. //发送消息到MQ对列
  125. sendDeleteMessage(flow.getCode(), flowClass);
  126. }
  127. boolean succ = flowTempDao.deleteFlowTempByFlowId(id);
  128. flowDao.deleteEntity(flow);
  129. return Result.success("删除成功");
  130. }
  131. /* =================================== flowClass 部分================================================*/
  132. @Override
  133. public Result getFlowClassList(Map<String, Object> params) throws Exception {
  134. return null;
  135. }
  136. @Override
  137. public List<SystemServiceFlowClass> getFlowClassByFlowId(Integer flowId) throws Exception {
  138. return flowClassDao.getFlowClassByFlowId(flowId);
  139. }
  140. public SystemServiceFlowClass getFlowClassById(Integer id) throws Exception {
  141. return flowClassDao.getEntity(SystemServiceFlowClass.class,id);
  142. }
  143. @Transactional
  144. public Result addFlowClass(SystemServiceFlowClass obj) throws Exception {
  145. flowDao.saveEntity(obj);
  146. return Result.success("保存成功");
  147. }
  148. @Transactional
  149. public Result updateFlowClass(SystemServiceFlowClass obj) throws Exception {
  150. return null;
  151. }
  152. @Transactional
  153. public boolean deleteFlowClassByFlowId(Integer flowId) {
  154. boolean succ =flowClassDao.deleteFlowClassByFlowId(flowId);
  155. return succ;
  156. }
  157. @Override
  158. public List<SystemServiceFlowTemp> getFlowTempByFlowId(Integer id) throws Exception {
  159. return flowTempDao.getFlowTempByFlowId(id);
  160. }
  161. //TODO
  162. @Override
  163. public boolean genFlewByflowTempId(Integer flowTempId) throws Exception {
  164. SystemServiceFlow flow = getFlowById(flowTempId);
  165. //生成新的route类
  166. //添加新processor记录
  167. return false;
  168. }
  169. @Override
  170. public String uploadFile(MultipartFile file, String baseSavePath) {
  171. String fileName = file.getOriginalFilename();
  172. boolean succ = false;
  173. try {
  174. succ = FileUtil.writeFile(baseSavePath + File.separator + fileName, file.getBytes(), "utf-8");
  175. if (succ){
  176. return fileName;
  177. }
  178. } catch (IOException e) {
  179. e.printStackTrace();
  180. }
  181. return null;
  182. }
  183. /**
  184. * 发送MQ消息-更新路由
  185. * @param flowCode 服务流程Code标识
  186. * @param flowClass
  187. * @param operate
  188. */
  189. public void sendUpdateMessage(String flowCode,SystemServiceFlowClass flowClass,String operate){
  190. //发送消息到MQ对列
  191. if ("1".equals(flowClass.getIsUpdate()) && Constants.FLOW_TYPE_ROUTE.equals(flowClass.getType())) {
  192. //route
  193. switch (operate){
  194. case "add" : serviceFlowEventService.routeDefineAdded(flowCode, flowClass.getPackageName(), flowClass.getClassName(), flowClass.getClassPath()); break;
  195. case "delete" : serviceFlowEventService.routeDefineDelete(flowCode, flowClass.getPackageName(), flowClass.getClassName()); break;
  196. case "update" : serviceFlowEventService.routeDefineChanged(flowCode, flowClass.getPackageName(), flowClass.getClassName(), flowClass.getClassPath()); break;
  197. default : break;
  198. }
  199. } else if ( "1".equals(flowClass.getIsUpdate()) && Constants.FLOW_TYPE_PROCESSOR.equals(flowClass.getType())) {
  200. //processor
  201. switch (operate){
  202. case "add" : serviceFlowEventService.processorAdded(flowCode, flowClass.getPackageName(), flowClass.getClassName(), flowClass.getClassPath()); break;
  203. case "delete" : serviceFlowEventService.processorDataDeleted(flowCode, flowClass.getPackageName(), flowClass.getClassName(), flowClass.getClassPath()); break;
  204. case "update" : serviceFlowEventService.processorDataChanged(flowCode, flowClass.getPackageName(), flowClass.getClassName(), flowClass.getClassPath()); break;
  205. default : break;
  206. }
  207. }
  208. }
  209. /**
  210. * 发送MQ消息-删除路由
  211. * @param flowCode 服务流程Code标识
  212. * @param flowClass
  213. */
  214. public void sendDeleteMessage(String flowCode,SystemServiceFlowClass flowClass){
  215. //发送消息到MQ对列
  216. if ( Constants.FLOW_TYPE_ROUTE.equals(flowClass.getType())) {
  217. //route
  218. serviceFlowEventService.routeDefineDelete(flowCode, flowClass.getPackageName(), flowClass.getClassName());
  219. } else if (Constants.FLOW_TYPE_PROCESSOR.equals(flowClass.getType())) {
  220. //processor
  221. serviceFlowEventService.processorDataDeleted(flowCode, flowClass.getPackageName(), flowClass.getClassName(), flowClass.getClassPath());
  222. }
  223. }
  224. /**
  225. * 获取流程列表
  226. * @param type 流程的文件类型
  227. * @return
  228. * @throws Exception
  229. */
  230. @Override
  231. public ActionResult getFlowList(String type) throws Exception {
  232. List<SystemServiceFlow> flowList = flowDao.getFlowList(type);
  233. ActionResult re = new ActionResult();
  234. if(flowList!=null&&flowList.size()>0)
  235. {
  236. List<DictItem> dictList = new ArrayList<>();
  237. for(SystemServiceFlow item:flowList){
  238. DictItem dict = new DictItem();
  239. dict.setCode(item.getId().toString());
  240. dict.setValue(item.getName());
  241. dict.setExtend("");
  242. dictList.add(dict);
  243. }
  244. re.setData(dictList);
  245. }
  246. return re;
  247. }
  248. /**
  249. * TODO 调用broker接口生成camel相关文件
  250. * @param flowId
  251. * @param newCron
  252. * @throws Exception
  253. */
  254. public boolean genCamelFile(Integer flowId, String newCron) throws Exception {
  255. Long timestamp = System.currentTimeMillis();
  256. List<SystemServiceFlowTemp> flowClassRouters = flowTempDao.getFlowTemps(flowId, Constants.FLOW_TYPE_ROUTE);
  257. List<SystemServiceFlowTemp> flowClassProces = flowTempDao.getFlowTemps(flowId, Constants.FLOW_TYPE_PROCESSOR);
  258. SystemServiceFlow oldFlow = getFlowById(flowId);
  259. if (!flowClassRouters.isEmpty()){
  260. Map<String,String> params = new HashMap<>();
  261. SystemServiceFlowTemp flowTemp =flowClassRouters.get(0);
  262. String newClassName = flowTemp.getClassName()+timestamp;
  263. String newClassPath = flowTemp.getClassPath().replaceAll(flowTemp.getClassName()+".java",newClassName+".class");
  264. params.put("pageName", flowTemp.getPackageName());
  265. params.put("oldClassName", flowTemp.getClassName());
  266. params.put("newClassName",newClassName);//原文件名加当前时间戳
  267. params.put("newCron",newCron);
  268. String result = HttpClientKit.post(genCamelUrl, params).getBody();
  269. //成功生成文件后,添加flow和flowclass记录
  270. //生成新流程
  271. SystemServiceFlow newFlow = new SystemServiceFlow();
  272. newFlow.setName(oldFlow.getName()+timestamp);
  273. newFlow.setCode(oldFlow.getCode()+timestamp);
  274. newFlow.setChart(oldFlow.getChart());
  275. newFlow.setValid(1);
  276. newFlow.setCreateDate(new Date());
  277. newFlow.setFileType(Constants.CLASS);
  278. flowDao.saveEntity(newFlow);
  279. SystemServiceFlowClass newFlowClass = new SystemServiceFlowClass();
  280. newFlowClass.setPackageName(flowTemp.getPackageName());
  281. newFlowClass.setClassName(newClassName);
  282. newFlowClass.setClassPath(newClassPath);
  283. newFlowClass.setFlowId(newFlow.getId());
  284. newFlowClass.setType(Constants.FLOW_TYPE_ROUTE);
  285. flowClassDao.saveEntity(newFlowClass);
  286. //新增processor记录
  287. for (SystemServiceFlowTemp process:flowClassProces){
  288. SystemServiceFlowClass processClass = new SystemServiceFlowClass();
  289. processClass.setPackageName(process.getPackageName());
  290. processClass.setClassName(process.getClassName());
  291. processClass.setClassPath(process.getClassPath());
  292. processClass.setFlowId(newFlow.getId());
  293. processClass.setType(Constants.FLOW_TYPE_PROCESSOR);
  294. flowClassDao.saveEntity(processClass);
  295. }
  296. return true;
  297. }
  298. return false;
  299. }
  300. }