flow_process.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. // ============================================================================
  2. // This is auto-generated by gf cli tool only once. Fill this file as you wish.
  3. // ============================================================================
  4. package wf_flow_process
  5. import (
  6. "github.com/gogf/gf/container/garray"
  7. "github.com/gogf/gf/database/gdb"
  8. "github.com/gogf/gf/encoding/gjson"
  9. "github.com/gogf/gf/errors/gerror"
  10. "github.com/gogf/gf/frame/g"
  11. "github.com/gogf/gf/os/gtime"
  12. "github.com/gogf/gf/text/gstr"
  13. "github.com/gogf/gf/util/gconv"
  14. )
  15. type ProcessData struct {
  16. Id int `json:"id"`
  17. Mode string `json:"mode"`
  18. Name string `json:"name"`
  19. FlowId uint `json:"flow_id"`
  20. ProcessName string `json:"process_name"`
  21. ProcessTo string `json:"process_to"`
  22. Style string `json:"style"`
  23. Process *Entity `json:"process"`
  24. }
  25. //流程转出信息
  26. type ProcessStepData struct {
  27. PrevId int `json:"prev_id"`
  28. Id int `json:"id"`
  29. ProcessName string `json:"process_name"`
  30. ProcessType string `json:"process_type"`
  31. Condition []string `json:"condition"`
  32. }
  33. type SaveProcessReq struct {
  34. Id int `p:"id" v:"required#流程ID不能为空"`
  35. Left string `p:"left" v:"required#流程坐标left不能为空"`
  36. Top string `p:"top" v:"required#流程坐标top不能为空"`
  37. ProcessTo string `p:"process_to" `
  38. }
  39. type SaveProcessReqBatch struct {
  40. Data []*SaveProcessReq `p:"data" v:"required#数据不能为空"`
  41. }
  42. type SaveAttrReq struct {
  43. FlowId int `p:"flow_id" v:"required#工作流id不能为空"`
  44. ProcessId int `p:"process_id" v:required#流程id不能为空`
  45. ProcessName string `p:"process_name" v:required#流程名称不能为空`
  46. StyleWidth string `p:"style_width"`
  47. StyleHeight string `p:"style_height"`
  48. ProcessType string `p:"process_type"`
  49. WfAction string `p:"wf_action"`
  50. IsSing string `p:"is_sing"`
  51. IsBack string `p:"is_back"`
  52. AutoPerson string `p:"auto_person"`
  53. RangeUserIds string `p:"range_user_ids"`
  54. RangeUserText string `p:"range_user_text"`
  55. AutoSponsorIds string `p:"auto_sponsor_ids"`
  56. AutoSponsorText string `p:"auto_sponsor_text"`
  57. AutoRoleIds string `p:"auto_role_ids"`
  58. AutoRoleText string `p:"auto_role_text"`
  59. WorkText string `p:"work_text"`
  60. WorkIds string `p:"work_ids"`
  61. WfMode string `p:"wf_mode"`
  62. ProcessInSet []ProcessInSetReq `p:"process_in_set"`
  63. WorkSql string `p:"work_sql"`
  64. WorkMsg string `p:"work_msg"`
  65. ProcessCondition string `p:"process_condition"`
  66. }
  67. type ProcessInSetReq struct {
  68. Id int `p:"id"`
  69. Condition []string `p:"condition"`
  70. }
  71. //流程管理表字段信息
  72. type FlowTableField struct {
  73. FieldName string `json:"field_name"`
  74. FieldText string `json:"field_text"`
  75. }
  76. type DeleteProcessReq struct {
  77. ProcessId int `p:"process_id" v:"required#流程ID不能为空" json:"process_id"`
  78. FlowId int `p:"flow_id" v:"required#工作流Id不能为空" json:"flow_id"`
  79. }
  80. //获取流程下所有步骤信息
  81. func ProcessAll(flowId int64) (total int64, processData []*ProcessData, err error) {
  82. var list []*Entity
  83. list, err = Model.Where("flow_id", flowId).Where("is_del", 0).Order("id asc").FindAll()
  84. if err != nil {
  85. g.Log().Error(err)
  86. err = gerror.New("获取流程数据失败")
  87. return
  88. }
  89. processData = make([]*ProcessData, len(list))
  90. for k, entity := range list {
  91. total += 1
  92. style := gjson.New(entity.Style)
  93. model := `<span style="color:red">未设置</span>`
  94. name := `<span style="color:red">未设置</span>`
  95. var work map[string]string
  96. if entity.AutoPerson == 3 {
  97. model = "办理人员"
  98. name = entity.RangeUserText
  99. }
  100. if entity.AutoPerson == 4 {
  101. model = "办理人员"
  102. name = entity.AutoSponsorText
  103. }
  104. if entity.AutoPerson == 5 {
  105. model = "办理角色"
  106. name = entity.AutoRoleText
  107. }
  108. if entity.AutoPerson == 6 {
  109. work = map[string]string{
  110. "1": "制单人员",
  111. "2": "制单人员领导",
  112. }
  113. model = `<span style="color:blue">事务处理</span>`
  114. name = work[entity.WorkIds]
  115. }
  116. nameAttr := ""
  117. if entity.ProcessType == "is_one" {
  118. nameAttr = `<span style="color:blue">[开始]</span>`
  119. } else {
  120. if entity.WfMode == 0 {
  121. nameAttr = "[直线]"
  122. } else if entity.WfMode == 1 {
  123. nameAttr = `<span style="color:lightgreen">[转出]</span>`
  124. } else {
  125. nameAttr = `<span style="color:red">[同步]</span>`
  126. }
  127. }
  128. heightUnit := "px"
  129. if style.GetString("height") == "auto" {
  130. heightUnit = ""
  131. }
  132. processData[k] = &ProcessData{
  133. Id: entity.Id,
  134. Mode: model,
  135. Name: name,
  136. FlowId: entity.FlowId,
  137. ProcessName: nameAttr + entity.ProcessName,
  138. ProcessTo: entity.ProcessTo,
  139. Style: "width:" + style.GetString("width") + "px;height:" + style.GetString("height") +
  140. heightUnit + ";line-height:30px;color:#0e76a8;left:" + gconv.String(entity.Setleft) + "px;top:" + gconv.String(entity.Settop) + "px;",
  141. Process: entity,
  142. }
  143. }
  144. return
  145. }
  146. //保存流程步骤
  147. func ProcessAdd(flowId int64) error {
  148. processList, err := Model.Order("id desc").FindAll("flow_id", flowId)
  149. if err != nil {
  150. g.Log().Error(err)
  151. return gerror.New("获取流程数据失败")
  152. }
  153. processCount := len(processList)
  154. processType := "is_step"
  155. var processSetLeft uint = 100
  156. var processSetTop uint = 100
  157. if processCount == 0 {
  158. processType = "is_one"
  159. processSetLeft = 100
  160. processSetTop = 100
  161. } else {
  162. //新建步骤显示在上一个步骤下方
  163. processType = "is_step"
  164. processSetLeft = processList[0].Setleft + 30
  165. processSetTop = processList[0].Settop + 30
  166. }
  167. style := gjson.New(nil)
  168. style.Set("width", "120")
  169. style.Set("height", "auto")
  170. style.Set("color", "#0e76a8")
  171. entity := &Entity{
  172. FlowId: gconv.Uint(flowId),
  173. Setleft: processSetLeft,
  174. Settop: processSetTop,
  175. ProcessType: processType,
  176. Style: style.MustToJsonString(),
  177. AutoPerson: 4,
  178. ProcessName: "步骤",
  179. IsSing: 1,
  180. IsBack: 1,
  181. WfAction: "view",
  182. }
  183. _, err = Model.Save(entity)
  184. if err != nil {
  185. g.Log().Error(err)
  186. return gerror.New("保存流程步骤失败")
  187. }
  188. return nil
  189. }
  190. //保存设计
  191. func SaveProcess(req *SaveProcessReqBatch) error {
  192. if req != nil && req.Data != nil {
  193. tx, err := g.DB().Begin()
  194. if err != nil {
  195. g.Log().Error(err)
  196. return gerror.New("保存失败")
  197. }
  198. entities := make([]*Entity, len(req.Data))
  199. for k, rq := range req.Data {
  200. entity, err := Model.FindOne(rq.Id)
  201. if err != nil || entity == nil {
  202. if err != nil {
  203. g.Log().Error(err)
  204. }
  205. tx.Rollback()
  206. return gerror.New("不存在的流程信息,请刷新重试。")
  207. }
  208. entity.Setleft = gconv.Uint(rq.Left)
  209. entity.Settop = gconv.Uint(rq.Top)
  210. entity.ProcessTo = rq.ProcessTo
  211. entities[k] = entity
  212. }
  213. _, err = Model.TX(tx).Data(entities).Save()
  214. if err != nil {
  215. g.Log().Error(err)
  216. tx.Rollback()
  217. return gerror.New("保存数据失败")
  218. }
  219. tx.Commit()
  220. }
  221. return nil
  222. }
  223. //获取流程管理表字段信息
  224. func GetFlowTableFields(dbPrefix, database, tableName string) (fields []*FlowTableField, err error) {
  225. sql := "SELECT COLUMN_NAME as field_name,column_comment as field_text FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = ? " +
  226. "AND table_schema = ? "
  227. var result gdb.Result
  228. result, err = g.DB().GetAll(sql, dbPrefix+tableName, database)
  229. if err != nil {
  230. g.Log().Error(err)
  231. err = gerror.New("查询表字段数据失败")
  232. }
  233. result.Structs(&fields)
  234. return
  235. }
  236. //删除流程节点
  237. func ProcessDelete(req *DeleteProcessReq) error {
  238. tx, err := g.DB().Begin()
  239. if err != nil {
  240. g.Log().Error(err)
  241. return gerror.New("开启处理事务失败")
  242. }
  243. _, err = Model.TX(tx).Delete(g.Map{
  244. Columns.Id: req.ProcessId,
  245. Columns.FlowId: req.FlowId,
  246. Columns.IsDel: 0,
  247. })
  248. if err != nil {
  249. g.Log().Error(err)
  250. tx.Rollback()
  251. return gerror.New("删除失败")
  252. }
  253. list, err := Model.Where(Columns.FlowId, req.FlowId).Where(Columns.IsDel, 0).
  254. Where("FIND_IN_SET(?,process_to)", gconv.String(req.ProcessId)).FindAll()
  255. if err != nil {
  256. g.Log().Error(err)
  257. return gerror.New("获取节点连接失败")
  258. }
  259. if list != nil {
  260. for _, value := range list {
  261. s := gstr.Split(value.ProcessTo, ",")
  262. arr := garray.NewStrArrayFrom(s)
  263. key := arr.Search(gconv.String(req.ProcessId))
  264. arr.Remove(key)
  265. processTo := ""
  266. if arr.Len() > 0 {
  267. processTo = arr.Join(",")
  268. }
  269. value.Updatetime = gconv.Uint(gtime.Timestamp())
  270. value.ProcessTo = processTo
  271. _, err := Model.TX(tx).Save(value)
  272. if err != nil {
  273. g.Log().Error(err)
  274. tx.Rollback()
  275. return gerror.New("删除节点连线失败")
  276. }
  277. }
  278. }
  279. tx.Commit()
  280. return nil
  281. }
  282. //通过ID获取流程步骤信息
  283. func GetProcessInfoById(id uint) (entity *Entity, err error) {
  284. entity, err = Model.FindOne(id)
  285. if err != nil {
  286. g.Log().Error(err)
  287. err = gerror.New("查询流程信息失败")
  288. }
  289. return
  290. }
  291. //查询流程信息
  292. func GetProcessesByMap(where g.Map) (entities []*Entity, err error) {
  293. entities, err = Model.Where(where).FindAll()
  294. if err != nil {
  295. g.Log().Error(err)
  296. err = gerror.New("查询流程信息失败")
  297. }
  298. return
  299. }
  300. //通过ids获取多条流程步骤信息
  301. func GetProcessInfosByIds(ids []uint) (entities []*Entity, err error) {
  302. entities, err = Model.Where("id in (?)", ids).FindAll()
  303. if err != nil {
  304. g.Log().Error(err)
  305. err = gerror.New("查询流程信息失败")
  306. }
  307. return
  308. }