flow.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. // ==========================================================================
  2. // 生成日期:2020-08-24 17:13:46
  3. // 生成人:gfast
  4. // ==========================================================================
  5. package wf_flow
  6. import (
  7. "github.com/gogf/gf/errors/gerror"
  8. "github.com/gogf/gf/frame/g"
  9. "github.com/gogf/gf/os/gtime"
  10. "github.com/gogf/gf/util/gconv"
  11. )
  12. // AddReq 用于存储新增请求的请求参数
  13. type AddReq struct {
  14. Type string `p:"type" `
  15. FlowName string `p:"flowName" v:"required#流程名称不能为空"`
  16. FlowDesc string `p:"flowDesc" `
  17. SortOrder uint `p:"sortOrder" `
  18. Status uint `p:"status" v:"required#状态不能为空"`
  19. Uid uint64
  20. AddTime int
  21. }
  22. // EditReq 用于存储修改请求参数
  23. type EditReq struct {
  24. Id int64 `p:"id" v:"required#主键ID不能为空"`
  25. Type string `p:"type" `
  26. FlowName string `p:"flowName" v:"required#流程名称不能为空"`
  27. FlowDesc string `p:"flowDesc" `
  28. SortOrder uint `p:"sortOrder" `
  29. Status uint `p:"status" v:"required#状态不能为空"`
  30. }
  31. type RemoveReq struct {
  32. Ids []int `p:"ids"` //删除id
  33. }
  34. type StatusReq struct {
  35. Id int `p:"id" v:"required|min:1#ID不能为空|ID不能为空"`
  36. Status uint `p:"status" v:"required|in:0,1#状态不能为空|状态不能为空"`
  37. }
  38. // SelectPageReq 用于存储分页查询的请求参数
  39. type SelectPageReq struct {
  40. Type string `p:"type"` //流程类别
  41. FlowName string `p:"flowName"` //流程名称
  42. Status string `p:"status"` //状态
  43. BeginTime string `p:"beginTime"` //开始时间
  44. EndTime string `p:"endTime"` //结束时间
  45. PageNum int64 `p:"pageNum"` //当前页码
  46. PageSize int `p:"pageSize"` //每页数
  47. }
  48. //发起工作流参数
  49. type StartFlowReq struct {
  50. WfType string `p:"wf_type" v:"required#业务表类型不能为空" json:"wf_type"`
  51. WfStatusField string `p:"wf_status_field" v:"required#业务表状态字段名称不能为空" json:"wf_status_field"`
  52. WfTitle string `p:"wf_title" v:"required#wf_title字段值不能为空" json:"wf_title"`
  53. WfFid uint `p:"wf_fid" v:"required|min:1#业务ID不能为空|业务ID不能为空" json:"wf_fid"`
  54. }
  55. //保存发起信息请求参数
  56. type SaveWfFlowReq struct {
  57. WfType string `p:"wfType" v:"required#业务表类型不能为空" json:"wf_type"`
  58. WfFid int `p:"wfFid" v:"required|min:1#业务ID不能为空|业务ID不能为空" json:"wf_fid"`
  59. WfId int64 `p:"wfId" v:"required|min:1#工作流ID不能为空|工作流ID不能为空" json:"wf_id"`
  60. WfTitle string `p:"WfTitle" v:"required#标题字段名称不能为空"` //业务表标题字段名称
  61. WfStatusField string `p:"WfStatusField" v:"required#状态字段名称不能为空"` //业务表状态字段名称
  62. NewType uint `p:"newType"` //审批紧急程度
  63. CheckCon string `p:"checkCon"`
  64. RunProcess uint `p:"runProcess"`
  65. RunId uint `p:"runId"`
  66. Art string `p:"art"`
  67. UserId uint64
  68. }
  69. //获取流程审批数据参数
  70. type CheckWfReq struct {
  71. WfTitle string `p:"wf_title" json:"wf_title"`
  72. WfFid uint `p:"wf_fid" v:"required|min:1#业务ID不能为空|业务ID不能为空" json:"wf_fid"`
  73. WfType string `p:"wf_type" v:"required#业务表类型不能为空" json:"wf_type"`
  74. WfStatusField string `p:"wf_status_field" v:"required#业务表状态字段名称不能为空" json:"wf_status_field"`
  75. }
  76. //保存流程审批参数
  77. type CheckWfSaveReq struct {
  78. WfTitle string `p:"wfTitle" json:"wf_title"`
  79. WfFid uint `p:"wfFid" json:"wf_fid" v:"required|min:1#业务ID不能为空|业务ID不能为空"`
  80. WfType string `p:"wfType" v:"required#业务表类型不能为空" json:"wf_type"`
  81. WfStatusField string `p:"WfStatusField" v:"required#状态字段名称不能为空"` //业务表状态字段名称
  82. FlowId int64 `p:"flowId" v:"required|min:1#流程Id不能为空|流程Id不能为空" json:"flow_id"`
  83. FlowProcess uint `p:"flowProcess" v:"required|min:1#流程步骤Id不能为空|流程步骤Id不能为空" json:"flow_process"`
  84. RunId uint `p:"runId" json:"run_id" v:"required|min:1#当前运行流程Id不能为空|当前运行流程Id不能为空"`
  85. RunProcess uint `p:"runProcess" json:"run_process" v:"required|min:1#当前运行流程步骤Id不能为空|当前运行流程步骤Id不能为空"`
  86. Npid string `p:"npid" json:"npid"`
  87. WfMode uint `p:"wfMode" json:"wf_mode"`
  88. CheckCon string `p:"checkCon" json:"check_con"`
  89. Art string `p:"art" json:"art"`
  90. SingSt uint `p:"singSt" json:"sing_st"`
  91. SubmitToSave string `p:"submitToSave" json:"submit_to_save" v:"required#审批方式不能为空"`
  92. UserId uint64 `json:"user_id"`
  93. Sup int `p:"sup" json:"sup"`
  94. Todo string `p:"todo" json:"todo"`
  95. WfSingFlow uint64 `p:"WfSingFlow"` //会签人
  96. WfBackFlow uint `p:"WfBackFlow"` //退回的步骤ID,如果等于0则默认是第一步
  97. BTodo string `p:"bTodo"`
  98. }
  99. type BackTodoReq struct {
  100. Pid string `p:"backId" v:required#流程步骤ID必须`
  101. RunId uint `p:"runId" v:required|min:1#流程运行ID必须|流程步骤ID必须`
  102. }
  103. // GetByID 根据ID查询记录
  104. func GetByID(id int64) (*Entity, error) {
  105. entity, err := Model.FindOne(id)
  106. if err != nil {
  107. g.Log().Error(err)
  108. return nil, gerror.New("根据ID查询记录出错")
  109. }
  110. if entity == nil {
  111. return nil, gerror.New("根据ID未能查询到记录")
  112. }
  113. return entity, nil
  114. }
  115. // AddSave 添加
  116. func AddSave(req *AddReq) error {
  117. entity := new(Entity)
  118. entity.Type = req.Type
  119. entity.FlowName = req.FlowName
  120. entity.FlowDesc = req.FlowDesc
  121. entity.SortOrder = req.SortOrder
  122. entity.Status = req.Status
  123. entity.Uid = req.Uid
  124. entity.AddTime = gconv.Int(gtime.Timestamp())
  125. result, err := entity.Insert()
  126. if err != nil {
  127. return err
  128. }
  129. _, err = result.LastInsertId()
  130. if err != nil {
  131. return err
  132. }
  133. return nil
  134. }
  135. // 删除
  136. func DeleteByIds(Ids []int) error {
  137. _, err := Model.Where("id in(?)", Ids).Update(g.Map{"is_del": 1})
  138. if err != nil {
  139. g.Log().Error(err)
  140. return gerror.New("删除失败")
  141. }
  142. return nil
  143. }
  144. func SetStatus(req *StatusReq) error {
  145. entity, err := Model.Where("id", req.Id).One()
  146. if err != nil {
  147. g.Log().Debug(err)
  148. return gerror.New("设置失败")
  149. }
  150. if entity != nil {
  151. entity.Status = req.Status
  152. _, err := Model.Save(entity)
  153. if err != nil {
  154. g.Log().Debug(err)
  155. return gerror.New("设置失败")
  156. }
  157. }
  158. return nil
  159. }
  160. // 根据ID来修改信息
  161. func EditSave(req *EditReq) error {
  162. // 先根据ID来查询要修改的记录
  163. entity, err := GetByID(req.Id)
  164. if err != nil {
  165. return err
  166. }
  167. // 修改实体
  168. entity.Type = req.Type
  169. entity.FlowName = req.FlowName
  170. entity.FlowDesc = req.FlowDesc
  171. entity.SortOrder = req.SortOrder
  172. entity.Status = req.Status
  173. _, err = Model.Save(entity)
  174. if err != nil {
  175. g.Log().Error(err)
  176. return gerror.New("修改失败")
  177. }
  178. return nil
  179. }
  180. // 分页查询,返回值total总记录数,page当前页
  181. func SelectListByPage(req *SelectPageReq) (total int, page int64, list []*Entity, err error) {
  182. model := Model
  183. if req != nil {
  184. if req.Type != "" {
  185. model = model.Where("type = ?", req.Type)
  186. }
  187. if req.FlowName != "" {
  188. model = model.Where("flow_name like ?", "%"+req.FlowName+"%")
  189. }
  190. if req.Status != "" {
  191. model = model.Where("status", gconv.Int(req.Status))
  192. }
  193. }
  194. model = model.Where("is_del", 0)
  195. // 查询总记录数(总行数)
  196. total, err = model.Count()
  197. if err != nil {
  198. g.Log().Error(err)
  199. err = gerror.New("获取总记录数失败")
  200. return
  201. }
  202. if req.PageNum == 0 {
  203. req.PageNum = 1
  204. }
  205. page = req.PageNum
  206. if req.PageSize == 0 {
  207. req.PageSize = 10
  208. }
  209. // 分页排序查询
  210. list, err = model.Page(int(page), int(req.PageSize)).Order("sort_order asc , id asc").All()
  211. if err != nil {
  212. g.Log().Error(err)
  213. err = gerror.New("分页查询失败")
  214. return
  215. }
  216. return
  217. }
  218. // 获取所有数据
  219. func SelectListAll(req *SelectPageReq) (list []*Entity, err error) {
  220. model := Model
  221. if req != nil {
  222. if req.Type != "" {
  223. model.Where("type = ?", req.Type)
  224. }
  225. if req.FlowName != "" {
  226. model.Where("flow_name like ?", "%"+req.FlowName+"%")
  227. }
  228. }
  229. // 查询
  230. list, err = model.Order("id asc").All()
  231. if err != nil {
  232. g.Log().Error(err)
  233. err = gerror.New("查询失败")
  234. return
  235. }
  236. return
  237. }
  238. //获取对应类型的工作流
  239. func GetWorkFlowByType(wfType string) (flows []*Entity, err error) {
  240. flows, err = Model.Where(Columns.Type, wfType).Where(Columns.Status, 1).
  241. Where(Columns.IsDel, 0).FindAll()
  242. if err != nil {
  243. g.Log().Error(err)
  244. err = gerror.New("获取工作流列表数据失败")
  245. }
  246. return
  247. }