procdefService.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. package service
  2. import (
  3. "errors"
  4. "sync"
  5. "time"
  6. "github.com/go-workflow/go-workflow/workflow-engine/flow"
  7. "github.com/mumushuiding/util"
  8. "github.com/go-workflow/go-workflow/workflow-engine/model"
  9. )
  10. var saveLock sync.Mutex
  11. // Procdef 流程定义表
  12. type Procdef struct {
  13. Name string `json:"name"`
  14. // 流程定义json字符串
  15. Resource *flow.Node `json:"resource"`
  16. // 用户id
  17. Userid string `json:"userid"`
  18. Username string `json:"username"`
  19. // 用户所在公司
  20. Company string `json:"company"`
  21. PageSize int `json:"pageSize"`
  22. PageIndex int `json:"pageIndex"`
  23. }
  24. // GetProcdefByID 根据流程定义id获取流程定义
  25. func GetProcdefByID(id int) (*model.Procdef, error) {
  26. return model.GetProcdefByID(id)
  27. }
  28. // GetProcdefLatestByNameAndCompany GetProcdefLatestByNameAndCompany
  29. // 根据流程定义名字和公司查询流程定义
  30. func GetProcdefLatestByNameAndCompany(name, company string) (*model.Procdef, error) {
  31. return model.GetProcdefLatestByNameAndCompany(name, company)
  32. }
  33. // GetResourceByNameAndCompany GetResourceByNameAndCompany
  34. // 获取流程定义配置信息
  35. func GetResourceByNameAndCompany(name, company string) (*flow.Node, int, string, error) {
  36. prodef, err := GetProcdefLatestByNameAndCompany(name, company)
  37. if err != nil {
  38. return nil, 0, "", err
  39. }
  40. if prodef == nil {
  41. return nil, 0, "", errors.New("流程【" + name + "】不存在")
  42. }
  43. node := &flow.Node{}
  44. err = util.Str2Struct(prodef.Resource, node)
  45. return node, prodef.ID, prodef.Name, err
  46. }
  47. // GetResourceByID GetResourceByID
  48. // 根据id查询流程定义
  49. func GetResourceByID(id int) (*flow.Node, int, error) {
  50. prodef, err := GetProcdefByID(id)
  51. if err != nil {
  52. return nil, 0, err
  53. }
  54. node := &flow.Node{}
  55. err = util.Str2Struct(prodef.Resource, node)
  56. return node, prodef.ID, err
  57. }
  58. // SaveProcdefByToken SaveProcdefByToken
  59. func (p *Procdef) SaveProcdefByToken(token string) (int, error) {
  60. // 根据 token 获取用户信息
  61. userinfo, err := GetUserinfoFromRedis(token)
  62. if err != nil {
  63. return 0, err
  64. }
  65. if len(userinfo.Company) == 0 {
  66. return 0, errors.New("保存在redis中的【用户信息 userinfo】字段 company 不能为空")
  67. }
  68. if len(userinfo.Username) == 0 {
  69. return 0, errors.New("保存在redis中的【用户信息 userinfo】字段 username 不能为空")
  70. }
  71. if len(userinfo.ID) == 0 {
  72. return 0, errors.New("保存在redis中的【用户信息 userinfo】字段 ID 不能为空")
  73. }
  74. p.Company = userinfo.Company
  75. p.Userid = userinfo.ID
  76. p.Username = userinfo.Username
  77. return p.SaveProcdef()
  78. }
  79. // SaveProcdef 保存
  80. func (p *Procdef) SaveProcdef() (id int, err error) {
  81. // 流程定义有效性检验
  82. err = IsProdefValid(p.Resource)
  83. if err != nil {
  84. return 0, err
  85. }
  86. resource, err := util.ToJSONStr(p.Resource)
  87. if err != nil {
  88. return 0, err
  89. }
  90. // fmt.Println(resource)
  91. var procdef = model.Procdef{
  92. Name: p.Name,
  93. Userid: p.Userid,
  94. Username: p.Username,
  95. Company: p.Company,
  96. Resource: resource,
  97. }
  98. return SaveProcdef(&procdef)
  99. }
  100. // SaveProcdef 保存
  101. func SaveProcdef(p *model.Procdef) (id int, err error) {
  102. // 参数是否为空判定
  103. saveLock.Lock()
  104. defer saveLock.Unlock()
  105. old, err := GetProcdefLatestByNameAndCompany(p.Name, p.Company)
  106. if err != nil {
  107. return 0, err
  108. }
  109. p.DeployTime = util.FormatDate(time.Now(), util.YYYY_MM_DD_HH_MM_SS)
  110. if old == nil {
  111. p.Version = 1
  112. return p.Save()
  113. }
  114. tx := model.GetTx()
  115. // 保存新版本
  116. p.Version = old.Version + 1
  117. err = p.SaveTx(tx)
  118. if err != nil {
  119. tx.Rollback()
  120. return 0, err
  121. }
  122. // 转移旧版本
  123. err = model.MoveProcdefToHistoryByIDTx(old.ID, tx)
  124. if err != nil {
  125. tx.Rollback()
  126. return 0, err
  127. }
  128. tx.Commit()
  129. return p.ID, nil
  130. }
  131. // ExistsProcdefByNameAndCompany if exists
  132. // 查询流程定义是否存在
  133. func ExistsProcdefByNameAndCompany(name, company string) (yes bool, version int, err error) {
  134. p, err := GetProcdefLatestByNameAndCompany(name, company)
  135. if p == nil {
  136. return false, 1, err
  137. }
  138. version = p.Version + 1
  139. return true, version, err
  140. }
  141. // FindAllPageAsJSON find by page and transform result to string
  142. // 分页查询并将结果转换成 json 字符串
  143. func (p *Procdef) FindAllPageAsJSON() (string, error) {
  144. datas, count, err := p.FindAll()
  145. if err != nil {
  146. return "", err
  147. }
  148. return util.ToPageJSON(datas, count, p.PageIndex, p.PageSize)
  149. }
  150. // FindAll FindAll
  151. func (p *Procdef) FindAll() ([]*model.Procdef, int, error) {
  152. var page = util.Page{}
  153. page.PageRequest(p.PageIndex, p.PageSize)
  154. maps := p.getMaps()
  155. return model.FindProcdefsWithCountAndPaged(page.PageIndex, page.PageSize, maps)
  156. }
  157. func (p *Procdef) getMaps() map[string]interface{} {
  158. maps := make(map[string]interface{})
  159. if len(p.Name) > 0 {
  160. maps["name"] = p.Name
  161. }
  162. if len(p.Company) > 0 {
  163. maps["company"] = p.Company
  164. }
  165. return maps
  166. }
  167. // DelProcdefByID del by id
  168. func DelProcdefByID(id int) error {
  169. return model.DelProcdefByID(id)
  170. }
  171. // IsProdefValid 流程定义格式是否有效
  172. func IsProdefValid(node *flow.Node) error {
  173. return flow.IfProcessConifgIsValid(node)
  174. }