process.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. package work_flow_service
  2. import (
  3. "encoding/json"
  4. "gfast/app/model/admin/user"
  5. "gfast/app/model/admin/wf_flow_process"
  6. "gfast/app/model/admin/wf_run"
  7. "gfast/app/model/admin/wf_run_process"
  8. "github.com/gogf/gf/container/garray"
  9. "github.com/gogf/gf/database/gdb"
  10. "github.com/gogf/gf/encoding/gjson"
  11. "github.com/gogf/gf/errors/gerror"
  12. "github.com/gogf/gf/frame/g"
  13. "github.com/gogf/gf/text/gstr"
  14. "github.com/gogf/gf/util/gconv"
  15. )
  16. //流程运行数据
  17. type ProcessData struct {
  18. *wf_flow_process.Entity
  19. Todo g.MapStrAny `json:"todo"`
  20. UserInfo *user.Entity `json:"user_info"`
  21. }
  22. //获取流程下所有步骤信息
  23. func ProcessAll(flowId int64) (total int64, list []*wf_flow_process.ProcessData, err error) {
  24. return wf_flow_process.ProcessAll(flowId)
  25. }
  26. //获取流程转出信息
  27. func GetProcessStep(processInfo *wf_flow_process.ProcessData, processList []*wf_flow_process.ProcessData) (
  28. list []*wf_flow_process.ProcessStepData, err error) {
  29. if processInfo == nil || processInfo.ProcessTo == "" || processList == nil {
  30. return
  31. }
  32. processArr := garray.NewStrArrayFrom(gstr.Split(processInfo.ProcessTo, ","))
  33. for _, process := range processList {
  34. if processArr.Contains(gconv.String(process.Id)) {
  35. var con []string
  36. con, err = getProcessCondition(processInfo.Process.OutCondition, process.Id)
  37. list = append(list, &wf_flow_process.ProcessStepData{
  38. PrevId: processInfo.Id,
  39. Id: process.Id,
  40. ProcessName: process.ProcessName,
  41. ProcessType: process.Process.ProcessType,
  42. Condition: con,
  43. })
  44. }
  45. }
  46. return
  47. }
  48. //获取转出条件
  49. func getProcessCondition(condition string, id interface{}) ([]string, error) {
  50. if condition == "" {
  51. return nil, nil
  52. }
  53. j, err := gjson.DecodeToJson(condition)
  54. if err != nil {
  55. g.Log().Error(err)
  56. return nil, gerror.New("转换转出条件失败")
  57. }
  58. return j.GetStrings(gconv.String(id) + ".condition"), nil
  59. }
  60. //添加流程步骤
  61. func ProcessAdd(flowId int64) error {
  62. return wf_flow_process.ProcessAdd(flowId)
  63. }
  64. //保存流程设计
  65. func SaveProcess(req *wf_flow_process.SaveProcessReqBatch) error {
  66. return wf_flow_process.SaveProcess(req)
  67. }
  68. //获取流程管理表字段信息
  69. func GetFlowTableFields(tableName string) (fields []*wf_flow_process.FlowTableField, err error) {
  70. //表前缀
  71. dbPrefix := g.DB().GetPrefix()
  72. //数据库
  73. database := g.Cfg().GetString("database.name")
  74. return wf_flow_process.GetFlowTableFields(dbPrefix, database, tableName)
  75. }
  76. //pid 步骤ID wf_flow_process 主键
  77. //runId 运行步骤ID wf_run 主键
  78. func GetProcessInfo(pid, runId uint) (data *ProcessData, err error) {
  79. var flowProcessInfo *wf_flow_process.Entity
  80. flowProcessInfo, err = wf_flow_process.GetProcessInfoById(pid)
  81. if err != nil {
  82. return
  83. }
  84. if flowProcessInfo == nil {
  85. err = gerror.New("不存在流程信息")
  86. return
  87. }
  88. data = &ProcessData{
  89. Entity: flowProcessInfo,
  90. }
  91. if flowProcessInfo.AutoPerson == 3 { //自由选择
  92. data.Todo = g.MapStrAny{"ids": gstr.Split(flowProcessInfo.RangeUserIds, ","), "text": gstr.Split(flowProcessInfo.RangeUserText, ",")}
  93. } else if flowProcessInfo.AutoPerson == 4 { //办理人员
  94. data.Todo = g.MapStrAny{"text": flowProcessInfo.AutoSponsorText}
  95. } else if flowProcessInfo.AutoPerson == 5 { //办理部门
  96. data.Todo = g.MapStrAny{"text": flowProcessInfo.AutoRoleText}
  97. } else if flowProcessInfo.AutoPerson == 6 { //事务接受
  98. var runInfo *wf_run.Entity
  99. runInfo, err = wf_run.GetRunById(runId)
  100. if err != nil {
  101. return
  102. }
  103. if runInfo == nil {
  104. err = gerror.New("不存在运行状态信息")
  105. return
  106. }
  107. var userId interface{}
  108. userId, err = GetBilValue(runInfo.FromTable, runInfo.FromId, flowProcessInfo.WorkText)
  109. if err != nil {
  110. return
  111. }
  112. if userId == nil {
  113. err = gerror.New("获取事务接受用户ID失败,请检测流程设计是否设置的是用户ID字段")
  114. return
  115. }
  116. var userInfo *user.Entity
  117. userInfo, err = user.GetUserById(gconv.Uint64(userId))
  118. if err != nil {
  119. return
  120. }
  121. data.Todo = g.MapStrAny{"text": userInfo.UserNickname}
  122. data.UserInfo = userInfo
  123. }
  124. return
  125. }
  126. //同步步骤信息
  127. func GetProcessInfos(ids []uint, runId uint) (datas []*ProcessData, err error) {
  128. var list []*wf_flow_process.Entity
  129. list, err = wf_flow_process.GetProcessInfosByIds(ids)
  130. if err != nil {
  131. return
  132. }
  133. datas = make([]*ProcessData, len(list))
  134. for k, v := range list {
  135. var todo g.MapStrAny
  136. var userInfo *user.Entity
  137. if v.AutoPerson == 3 { //自由选择 办理人员
  138. todo = g.MapStrAny{
  139. "ids": gstr.Split(v.AutoSponsorIds, ","),
  140. "text": gstr.Split(v.AutoSponsorText, ","),
  141. }
  142. } else if v.AutoPerson == 4 { //办理人员
  143. todo = g.MapStrAny{"text": v.AutoSponsorText}
  144. } else if v.AutoPerson == 5 { //办理部门
  145. todo = g.MapStrAny{"text": v.AutoRoleText}
  146. } else if v.AutoPerson == 6 { //事务接受
  147. var runInfo *wf_run.Entity
  148. runInfo, err = wf_run.GetRunById(runId)
  149. if err != nil {
  150. return
  151. }
  152. if runInfo == nil {
  153. err = gerror.New("不存在运行状态信息")
  154. return
  155. }
  156. var userId interface{}
  157. userId, err = GetBilValue(runInfo.FromTable, runInfo.FromId, v.WorkText)
  158. if err != nil {
  159. return
  160. }
  161. if userId == nil {
  162. err = gerror.New("获取事务接受用户ID失败,请检测流程设计是否设置的是用户ID字段")
  163. return
  164. }
  165. userInfo, err = user.GetUserById(gconv.Uint64(userId))
  166. if err != nil {
  167. return
  168. }
  169. todo = g.MapStrAny{"text": userInfo.UserNickname}
  170. }
  171. datas[k] = &ProcessData{
  172. Entity: v,
  173. Todo: todo,
  174. UserInfo: userInfo,
  175. }
  176. }
  177. return
  178. }
  179. //获取事务接受信息
  180. func GetBilValue(fromTable string, fromId int, workText string) (value interface{}, err error) {
  181. var rec gdb.Record
  182. rec, err = GetBill(fromId, fromTable)
  183. if err != nil {
  184. return
  185. }
  186. value = rec[workText]
  187. return
  188. }
  189. //获取工作流业务表信息
  190. func GetBill(fromId int, fromTable string) (rec gdb.Record, err error) {
  191. rec, err = g.DB().Table(fromTable).FindOne(fromId)
  192. if err != nil {
  193. g.Log().Error(err)
  194. err = gerror.New("获取工作里业务数据失败")
  195. return
  196. }
  197. return
  198. }
  199. //更新业务表信息
  200. func UpdateBill(wfFid int, wfType string, statusField string, status int, tx *gdb.TX) error {
  201. _, err := tx.Table(wfType).WherePri(wfFid).Update(g.Map{
  202. statusField: status,
  203. })
  204. if err != nil {
  205. g.Log().Error(err)
  206. return gerror.New("更新单据信息失败")
  207. }
  208. return nil
  209. }
  210. //删除流程节点
  211. func ProcessDelete(req *wf_flow_process.DeleteProcessReq) error {
  212. return wf_flow_process.ProcessDelete(req)
  213. }
  214. //清空节点
  215. func ProcessDeleteAll(flowId int) error {
  216. _, err := wf_flow_process.Model.Where("flow_id", flowId).Delete()
  217. if err != nil {
  218. g.Log().Error(err)
  219. return gerror.New("删除失败")
  220. }
  221. return nil
  222. }
  223. //获取下个审批流信息
  224. //wfType 单据表
  225. //wfFid 单据id
  226. //pid 流程id
  227. //runId 运行id
  228. func GetNexProcessInfo(wfType string, wfFid uint, pid uint, runId uint) ([]*ProcessData, error) {
  229. if pid == 0 {
  230. return nil, nil
  231. }
  232. nex, err := wf_flow_process.GetProcessInfoById(pid)
  233. if err != nil {
  234. return nil, err
  235. }
  236. var process *ProcessData
  237. var processes []*ProcessData
  238. //先判断下上一个流程是什么模式
  239. if nex.ProcessTo != "" {
  240. //下一个流程的下一步
  241. nexPidStr := gstr.Split(nex.ProcessTo, ",")
  242. nexPid := make([]uint, len(nexPidStr))
  243. for k, v := range nexPidStr {
  244. nexPid[k] = gconv.Uint(v)
  245. }
  246. //下一个流程转出条件
  247. var outCondition g.MapStrAny
  248. err = json.Unmarshal([]byte(nex.OutCondition), &outCondition)
  249. if err != nil {
  250. outCondition = g.MapStrAny{}
  251. }
  252. switch nex.WfMode {
  253. case 0: //单一模式
  254. process, err = GetProcessInfo(gconv.Uint(nex.ProcessTo), runId)
  255. if err != nil {
  256. return nil, err
  257. }
  258. processes = append(processes, process)
  259. case 1: //多个审批流
  260. var nextProcessId uint
  261. for key, val := range outCondition {
  262. mapVal := gconv.Map(val)
  263. condition := gstr.Join(gconv.SliceStr(mapVal["condition"]), " ")
  264. //根据条件寻找匹配符合的工作流id
  265. if gstr.ContainsI(condition, "include") {
  266. condition = gstr.ReplaceI(condition, "include", " REGEXP ")
  267. }
  268. info, err := g.DB().Table(wfType).Where(condition).WherePri(wfFid).FindOne()
  269. if err != nil {
  270. g.Log().Error(err)
  271. return nil, gerror.New("获取转出条件对应的审批信息失败")
  272. }
  273. if info != nil {
  274. nextProcessId = gconv.Uint(key)
  275. break
  276. }
  277. }
  278. if nextProcessId == 0 { //没有权限
  279. return nil, nil
  280. }
  281. process, err = GetProcessInfo(nextProcessId, runId)
  282. if err != nil {
  283. return nil, err
  284. }
  285. processes = append(processes, process)
  286. case 2: //同步模式
  287. processes, err = GetProcessInfos(nexPid, runId)
  288. if err != nil {
  289. return nil, err
  290. }
  291. }
  292. } else {
  293. process = &ProcessData{
  294. Entity: &wf_flow_process.Entity{
  295. Id: 0,
  296. AutoPerson: 0,
  297. ProcessName: "END",
  298. },
  299. Todo: g.MapStrAny{"text": "结束"},
  300. }
  301. processes = append(processes, process)
  302. }
  303. return processes, nil
  304. }
  305. func GetPreProcessInfo(runId uint) (map[int]string, error) {
  306. var pre []*wf_flow_process.Entity
  307. preRun, err := wf_run_process.GetProcessById(runId)
  308. if err != nil {
  309. return nil, err
  310. }
  311. //获取本流程中小于本次ID的步骤信息
  312. var preP []*wf_run_process.Entity
  313. preP, err = wf_run_process.GetProcessLtId(preRun)
  314. if err != nil {
  315. return nil, err
  316. }
  317. //遍历获取小于本次ID中的相关步骤
  318. for _, v := range preP {
  319. wfp, err := wf_flow_process.GetProcessInfoById(v.RunFlowProcess)
  320. if err != nil {
  321. return nil, err
  322. }
  323. pre = append(pre, wfp)
  324. }
  325. preMap := make(map[int]string, 5)
  326. preMap[0] = "退回制单人修改"
  327. if len(pre) > 0 {
  328. for _, v := range pre {
  329. todo := ""
  330. if v.AutoPerson == 4 { //办理人员
  331. todo = v.AutoSponsorText
  332. } else if v.AutoPerson == 5 { //办理部门
  333. todo = v.AutoRoleText
  334. }
  335. preMap[v.Id] = v.ProcessName + "(" + todo + ")"
  336. }
  337. }
  338. return preMap, nil
  339. }
  340. //保存流程节点属性
  341. func SaveProcessAttr(req *wf_flow_process.SaveAttrReq) error {
  342. if req == nil {
  343. return gerror.New("参数错误")
  344. }
  345. processCondition := gstr.Split(req.ProcessCondition, ",")
  346. outCondition := make(g.MapStrAny, 10)
  347. if len(processCondition) > 1 && req.WfMode == "1" {
  348. return gerror.New(msgNoAction)
  349. }
  350. ocbt, _ := json.Marshal(outCondition)
  351. entity, err := wf_flow_process.FindOne(req.ProcessId)
  352. if err != nil {
  353. g.Log().Error(err)
  354. return gerror.New("获取流程信息失败")
  355. }
  356. style := gjson.New(nil)
  357. style.Set("width", req.StyleWidth)
  358. style.Set("height", req.StyleHeight)
  359. style.Set("color", "#0e76a8")
  360. entity.ProcessName = req.ProcessName
  361. entity.ProcessType = req.ProcessType
  362. entity.AutoPerson = gconv.Uint(req.AutoPerson)
  363. entity.WfMode = gconv.Uint(req.WfMode)
  364. entity.WfAction = req.WfAction
  365. entity.AutoSponsorIds = req.AutoSponsorIds
  366. entity.AutoSponsorText = req.AutoSponsorText
  367. entity.AutoRoleIds = req.AutoRoleIds
  368. entity.AutoRoleText = req.AutoRoleText
  369. entity.RangeUserIds = req.RangeUserIds
  370. entity.RangeUserText = req.RangeUserText
  371. entity.WorkText = req.WorkText
  372. entity.WorkIds = req.WorkIds
  373. entity.WorkMsg = req.WorkMsg
  374. entity.WorkSql = req.WorkSql
  375. entity.IsSing = gconv.Uint(req.IsSing)
  376. entity.IsBack = gconv.Uint(req.IsBack)
  377. entity.OutCondition = string(ocbt)
  378. entity.Style = style.MustToJsonString()
  379. _, err = wf_flow_process.Model.Save(entity)
  380. if err != nil {
  381. g.Log().Error(err)
  382. return gerror.New("保存失败")
  383. }
  384. return nil
  385. }
  386. //获取流程步骤第一步
  387. func getFirstProcess(processes []*wf_flow_process.Entity) *wf_flow_process.Entity {
  388. //找到 流程第一步
  389. var firstProcess *wf_flow_process.Entity
  390. for _, process := range processes {
  391. if process.ProcessType == "is_one" {
  392. firstProcess = process
  393. break
  394. }
  395. }
  396. return firstProcess
  397. }
  398. //获取步骤所有下级步骤
  399. func GetAllNextProcessIds(id uint) (ids []uint, err error) {
  400. processData := ([]*wf_flow_process.ProcessData)(nil)
  401. processData, err = GetAllProcessesByProcessId(id)
  402. if err != nil {
  403. return
  404. }
  405. ids = GetNextProcessesById(id, processData)
  406. return
  407. }
  408. //获取步骤所有上级步骤
  409. func GetAllPreProcessIds(id uint) (ids []uint, err error) {
  410. processData := ([]*wf_flow_process.ProcessData)(nil)
  411. processData, err = GetAllProcessesByProcessId(id)
  412. if err != nil {
  413. return
  414. }
  415. ids = GetPreProcessesById(id, processData)
  416. return
  417. }
  418. //获取对应步骤所属流程的所有步骤数据
  419. func GetAllProcessesByProcessId(id uint) (processData []*wf_flow_process.ProcessData, err error) {
  420. process := (*wf_flow_process.Entity)(nil)
  421. process, err = wf_flow_process.GetProcessInfoById(id)
  422. if err != nil {
  423. return
  424. }
  425. if process == nil {
  426. err = gerror.New("流程步骤信息不存在")
  427. return
  428. }
  429. //获取流程下所有步骤
  430. _, processData, err = wf_flow_process.ProcessAll(gconv.Int64(process.FlowId))
  431. return
  432. }
  433. //获取所有上级步骤切片id
  434. func GetPreProcessesById(id uint, processData []*wf_flow_process.ProcessData) (ids []uint) {
  435. ids = make([]uint, 0, len(processData))
  436. for _, v := range processData {
  437. processTo := gstr.Split(v.ProcessTo, ",")
  438. processToArr := garray.NewStrArrayFrom(processTo)
  439. if processToArr.Contains(gconv.String(id)) {
  440. id := gconv.Uint(v.Id)
  441. ids = append(ids, id)
  442. idsChd := GetPreProcessesById(id, processData)
  443. if len(idsChd) > 0 {
  444. ids = append(ids, idsChd...)
  445. }
  446. }
  447. }
  448. return
  449. }
  450. //获取所有下级步骤id切片
  451. func GetNextProcessesById(id uint, processData []*wf_flow_process.ProcessData) (ids []uint) {
  452. ids = make([]uint, 0, len(processData))
  453. for _, v := range processData {
  454. if id == gconv.Uint(v.Id) {
  455. processTo := gstr.Split(v.ProcessTo, ",")
  456. for _, v := range processTo {
  457. id := gconv.Uint(v)
  458. if id == 0 {
  459. continue
  460. }
  461. ids = append(ids, id)
  462. idsChd := make([]uint, 0, len(processData))
  463. idsChd = GetNextProcessesById(id, processData)
  464. if len(idsChd) > 0 {
  465. ids = append(ids, idsChd...)
  466. }
  467. }
  468. }
  469. }
  470. return
  471. }