executionService.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package service
  2. import (
  3. "errors"
  4. "fmt"
  5. "sync"
  6. "github.com/jinzhu/gorm"
  7. "github.com/mumushuiding/util"
  8. "github.com/go-workflow/go-workflow/workflow-engine/flow"
  9. "github.com/go-workflow/go-workflow/workflow-engine/model"
  10. )
  11. var execLock sync.Mutex
  12. // SaveExecution SaveExecution
  13. func SaveExecution(e *model.Execution) (ID int, err error) {
  14. execLock.Lock()
  15. defer execLock.Unlock()
  16. // check if exists by procInst
  17. yes, err := model.ExistsExecByProcInst(e.ProcInstID)
  18. if err != nil {
  19. return 0, err
  20. }
  21. if yes {
  22. return 0, errors.New("流程实例【" + fmt.Sprintf("%d", e.ProcInstID) + "】已经存在执行流")
  23. }
  24. // save
  25. return e.Save()
  26. }
  27. // SaveExecTx SaveExecTx
  28. func SaveExecTx(e *model.Execution, tx *gorm.DB) (ID int, err error) {
  29. execLock.Lock()
  30. defer execLock.Unlock()
  31. // check if exists by procInst
  32. yes, err := model.ExistsExecByProcInst(e.ProcInstID)
  33. if err != nil {
  34. return 0, err
  35. }
  36. if yes {
  37. return 0, errors.New("流程实例【" + fmt.Sprintf("%d", e.ProcInstID) + "】已经存在执行流")
  38. }
  39. // save
  40. return e.SaveTx(tx)
  41. }
  42. // GetExecByProcInst 根据流程实例查询执行流
  43. func GetExecByProcInst(procInst int) (*model.Execution, error) {
  44. return model.GetExecByProcInst(procInst)
  45. }
  46. // GenerateExec GenerateExec
  47. // 根据流程定义node生成执行流
  48. func GenerateExec(e *model.Execution, node *flow.Node, userID string, variable *map[string]string, tx *gorm.DB) (int, error) {
  49. list, err := flow.ParseProcessConfig(node, variable)
  50. if err != nil {
  51. return 0, err
  52. }
  53. list.PushBack(flow.NodeInfo{
  54. NodeID: "结束",
  55. })
  56. list.PushFront(flow.NodeInfo{
  57. NodeID: "开始",
  58. Type: flow.NodeInfoTypes[flow.STARTER],
  59. Aprover: userID,
  60. })
  61. arr := util.List2Array(list)
  62. str, err := util.ToJSONStr(arr)
  63. if err != nil {
  64. return 0, err
  65. }
  66. e.NodeInfos = str
  67. ID, err := SaveExecTx(e, tx)
  68. return ID, err
  69. }
  70. // GetExecNodeInfosByProcInstID GetExecNodeInfosByProcInstID
  71. // 获取执行流经过的节点信息
  72. func GetExecNodeInfosByProcInstID(procInstID int) ([]*flow.NodeInfo, error) {
  73. nodeinfoStr, err := model.GetExecNodeInfosByProcInstID(procInstID)
  74. if err != nil {
  75. return nil, err
  76. }
  77. var nodeInfos []*flow.NodeInfo
  78. err = util.Str2Struct(nodeinfoStr, &nodeInfos)
  79. return nodeInfos, err
  80. }