FlowManager.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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.services.ServiceFlowEventService;
  5. import com.yihu.hos.system.dao.FlowClassDao;
  6. import com.yihu.hos.system.dao.FlowTempDao;
  7. import com.yihu.hos.system.dao.intf.IFlowClassDao;
  8. import com.yihu.hos.system.dao.intf.IFlowDao;
  9. import com.yihu.hos.system.dao.intf.IFlowTempDao;
  10. import com.yihu.hos.system.model.SystemServiceFlow;
  11. import com.yihu.hos.system.model.SystemServiceFlowClass;
  12. import com.yihu.hos.system.model.SystemServiceFlowTemp;
  13. import com.yihu.hos.system.service.intf.IFlowManage;
  14. import com.yihu.hos.web.framework.model.DictItem;
  15. import com.yihu.hos.web.framework.model.DictionaryResult;
  16. import com.yihu.hos.web.framework.model.Result;
  17. import org.springframework.beans.factory.annotation.Autowired;
  18. import org.springframework.stereotype.Service;
  19. import org.springframework.transaction.annotation.Transactional;
  20. import org.springframework.web.multipart.MultipartFile;
  21. import javax.annotation.Resource;
  22. import java.io.IOException;
  23. import java.util.ArrayList;
  24. import java.util.Date;
  25. import java.util.List;
  26. import java.util.Map;
  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. @Resource(name = "flowDao")
  38. private IFlowDao flowDao;
  39. @Resource(name = FlowClassDao.BEAN_ID)
  40. private IFlowClassDao flowClassDao;
  41. @Resource(name = FlowTempDao.BEAN_ID)
  42. private IFlowTempDao flowTempDao;
  43. @Autowired
  44. ServiceFlowEventService serviceFlowEventService;
  45. @Override
  46. public Result getFlowList(Map<String, Object> params) throws Exception {
  47. return flowDao.getFlowList(params);
  48. }
  49. @Override
  50. public SystemServiceFlow getFlowById(Integer id) throws Exception {
  51. return flowDao.getEntity(SystemServiceFlow.class, id);
  52. }
  53. @Transactional
  54. public Result addFlow(SystemServiceFlow obj) throws Exception {
  55. obj.setCreateDate(new Date());
  56. flowDao.saveEntity(obj);
  57. if (Constants.CLASS.equals(obj.getFileType())){
  58. List<SystemServiceFlowClass> flowClassList = obj.getFlowClassArray();
  59. for (SystemServiceFlowClass flowClass:flowClassList){
  60. flowClass.setFlowId(obj.getId());
  61. flowDao.saveEntity(flowClass);
  62. //发送消息到MQ对列
  63. sendUpdateMessage(obj.getCode(), flowClass, Constants.FLOW_OP_ADD);
  64. }
  65. }else if (Constants.JAVA.equals(obj.getFileType())){
  66. List<SystemServiceFlowTemp> flowTempList = obj.getFlowTempArray();
  67. for (SystemServiceFlowTemp flowTemp:flowTempList){
  68. flowTemp.setFlowId(obj.getId());
  69. flowDao.saveEntity(flowTemp);
  70. }
  71. }
  72. return Result.success("保存成功");
  73. }
  74. @Transactional
  75. public Result updateFlow(SystemServiceFlow obj) throws Exception {
  76. SystemServiceFlow flow = flowDao.getEntity(SystemServiceFlow.class, obj.getId());
  77. flow.setCode(obj.getCode());
  78. flow.setName(obj.getName());
  79. flow.setPath(obj.getPath());
  80. flow.setChart(obj.getChart());
  81. flow.setValid(obj.getValid());
  82. flow.setFileType(obj.getFileType());
  83. if (Constants.JAVA.equals(flow.getFileType())){
  84. List<SystemServiceFlowTemp> flowTempList = obj.getFlowTempArray();
  85. boolean succ = flowTempDao.deleteFlowTempByFlowId(obj.getId());
  86. if (succ){
  87. for (SystemServiceFlowTemp flowTemp:flowTempList){
  88. flowTempDao.saveEntity(flowTemp);
  89. }
  90. }
  91. }else if (Constants.CLASS.equals(flow.getFileType())){
  92. List<Integer> classIds = flowClassDao.getFlowClassIds(obj.getId());//原flowclass集合
  93. List<SystemServiceFlowClass> flowClassList = obj.getFlowClassArray();
  94. for (SystemServiceFlowClass flowClass:flowClassList){
  95. if (flowClass.getId()!=null) {
  96. classIds.remove(flowClass.getId());
  97. flowClassDao.updateEntity(flowClass);
  98. sendUpdateMessage(flow.getCode(), flowClass, Constants.FLOW_OP_UPDATE);
  99. }else {
  100. flowClassDao.saveEntity(flowClass);
  101. sendUpdateMessage(flow.getCode(), flowClass, Constants.FLOW_OP_ADD);
  102. }
  103. }
  104. //删除判断
  105. if (classIds !=null && classIds.size()>0){
  106. for (Integer id:classIds){
  107. SystemServiceFlowClass flowClass = getFlowClassById(id);
  108. flowClassDao.deleteEntity(flowClass);
  109. sendDeleteMessage(flow.getCode(), flowClass);
  110. }
  111. }
  112. }
  113. flowDao.updateEntity(flow);
  114. return Result.success("更新成功");
  115. }
  116. @Transactional
  117. public Result deleteFlow(Integer id) throws Exception {
  118. SystemServiceFlow flow = flowDao.getEntity(SystemServiceFlow.class, id);
  119. List<SystemServiceFlowClass> flowClassList = flowClassDao.getFlowClassByFlowId(id);
  120. for (SystemServiceFlowClass flowClass:flowClassList){
  121. flowClassDao.deleteEntity(flowClass);
  122. //发送消息到MQ对列
  123. sendDeleteMessage(flow.getCode(), flowClass);
  124. }
  125. boolean succ = flowTempDao.deleteFlowTempByFlowId(id);
  126. flowDao.deleteEntity(flow);
  127. return Result.success("删除成功");
  128. }
  129. /* =================================== flowClass 部分================================================*/
  130. @Override
  131. public Result getFlowClassList(Map<String, Object> params) throws Exception {
  132. return null;
  133. }
  134. @Override
  135. public List<SystemServiceFlowClass> getFlowClassByFlowId(Integer flowId) throws Exception {
  136. return flowClassDao.getFlowClassByFlowId(flowId);
  137. }
  138. public SystemServiceFlowClass getFlowClassById(Integer id) throws Exception {
  139. return flowClassDao.getEntity(SystemServiceFlowClass.class,id);
  140. }
  141. @Transactional
  142. public Result addFlowClass(SystemServiceFlowClass obj) throws Exception {
  143. flowDao.saveEntity(obj);
  144. return Result.success("保存成功");
  145. }
  146. @Transactional
  147. public Result updateFlowClass(SystemServiceFlowClass obj) throws Exception {
  148. return null;
  149. }
  150. @Transactional
  151. public boolean deleteFlowClassByFlowId(Integer flowId) {
  152. boolean succ =flowClassDao.deleteFlowClassByFlowId(flowId);
  153. return succ;
  154. }
  155. @Override
  156. public List<SystemServiceFlowTemp> getFlowTempByFlowId(Integer id) throws Exception {
  157. return flowTempDao.getFlowTempByFlowId(id);
  158. }
  159. @Override
  160. public String uploadFile(MultipartFile file, String baseSavePath) {
  161. String fileName = file.getOriginalFilename();
  162. boolean succ = false;
  163. try {
  164. succ = FileUtil.writeFile(baseSavePath +"/"+ fileName, file.getBytes(), "utf-8");
  165. if (succ){
  166. return fileName;
  167. }
  168. } catch (IOException e) {
  169. e.printStackTrace();
  170. }
  171. return null;
  172. }
  173. /**
  174. * 发送MQ消息-更新路由
  175. * @param flowCode 服务流程Code标识
  176. * @param flowClass
  177. * @param operate
  178. */
  179. public void sendUpdateMessage(String flowCode,SystemServiceFlowClass flowClass,String operate){
  180. //发送消息到MQ对列
  181. if ("1".equals(flowClass.getIsUpdate()) && Constants.FLOW_TYPE_ROUTE.equals(flowClass.getType())) {
  182. //route
  183. switch (operate){
  184. case "add" : serviceFlowEventService.routeDefineAdded(flowCode, flowClass.getPackageName(), flowClass.getClassName(), flowClass.getClassPath()); break;
  185. case "delete" : serviceFlowEventService.routeDefineDelete(flowCode, flowClass.getPackageName(), flowClass.getClassName()); break;
  186. case "update" : serviceFlowEventService.routeDefineChanged(flowCode, flowClass.getPackageName(), flowClass.getClassName(), flowClass.getClassPath()); break;
  187. default : break;
  188. }
  189. } else if ( "1".equals(flowClass.getIsUpdate()) && Constants.FLOW_TYPE_PROCESSOR.equals(flowClass.getType())) {
  190. //processor
  191. switch (operate){
  192. case "add" : serviceFlowEventService.processorAdded(flowCode, flowClass.getPackageName(), flowClass.getClassName(), flowClass.getClassPath()); break;
  193. case "delete" : serviceFlowEventService.processorDataDeleted(flowCode, flowClass.getPackageName(), flowClass.getClassName(), flowClass.getClassPath()); break;
  194. case "update" : serviceFlowEventService.processorDataChanged(flowCode, flowClass.getPackageName(), flowClass.getClassName(), flowClass.getClassPath()); break;
  195. default : break;
  196. }
  197. }
  198. }
  199. /**
  200. * 发送MQ消息-删除路由
  201. * @param flowCode 服务流程Code标识
  202. * @param flowClass
  203. */
  204. public void sendDeleteMessage(String flowCode,SystemServiceFlowClass flowClass){
  205. //发送消息到MQ对列
  206. if ( Constants.FLOW_TYPE_ROUTE.equals(flowClass.getType())) {
  207. //route
  208. serviceFlowEventService.routeDefineDelete(flowCode, flowClass.getPackageName(), flowClass.getClassName());
  209. } else if (Constants.FLOW_TYPE_PROCESSOR.equals(flowClass.getType())) {
  210. //processor
  211. serviceFlowEventService.processorDataDeleted(flowCode, flowClass.getPackageName(), flowClass.getClassName(), flowClass.getClassPath());
  212. }
  213. }
  214. /**
  215. * 获取流程列表
  216. * @param type 流程的文件类型
  217. * @return
  218. * @throws Exception
  219. */
  220. @Override
  221. public DictionaryResult getFlowList(String type) throws Exception {
  222. List<SystemServiceFlow> flowList = flowDao.getFlowList(type);
  223. DictionaryResult re = new DictionaryResult("FLOW_LIST");
  224. if(flowList!=null&&flowList.size()>0)
  225. {
  226. List<DictItem> dictList = new ArrayList<>();
  227. for(SystemServiceFlow item:flowList){
  228. DictItem dict = new DictItem();
  229. dict.setCode(item.getId().toString());
  230. dict.setValue(item.getName());
  231. dict.setExtend("");
  232. dictList.add(dict);
  233. }
  234. re.setDetailModelList(dictList);
  235. }
  236. return re;
  237. }
  238. }