flow.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. package flow
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "hongze/hz_crm_api/models/system"
  7. "strings"
  8. "time"
  9. )
  10. // 获取审批工作流(基础)
  11. func GetApprovalFlow(flowId int) (approvalFlow *system.ApprovalFlowItem, err error) {
  12. approvalFlow, err = system.GetByFlowItemId(flowId)
  13. if err != nil {
  14. err = errors.New("查询审批流程失败:Err:" + err.Error())
  15. return
  16. }
  17. flowNodeList := make([]*system.ApprovalFlowNodeList, 0)
  18. approvalFlowNodeList, err := system.GetListByFlowId(approvalFlow.FlowId, approvalFlow.CurrVersion)
  19. if err != nil {
  20. err = errors.New("查询审批节点失败:Err:" + err.Error())
  21. return
  22. }
  23. for _, approvalFlowNode := range approvalFlowNodeList {
  24. userList, tmpErr := getAdminListByUser(approvalFlowNode.User, approvalFlow.ProductId)
  25. if tmpErr != nil {
  26. err = errors.New("获取审批人失败:Err:" + tmpErr.Error())
  27. return
  28. }
  29. for _, user := range userList {
  30. userInfo := system.User{
  31. AdminId: user.AdminId,
  32. RoleId: user.RoleId,
  33. Name: user.RealName,
  34. RoleName: user.RoleName,
  35. RoleTypeCode: user.RoleTypeCode,
  36. Mobile: user.Mobile,
  37. }
  38. approvalFlowNode.UserList = append(approvalFlowNode.UserList, userInfo)
  39. }
  40. if len(approvalFlowNode.UserList) > 0 {
  41. flowNodeList = append(flowNodeList, approvalFlowNode)
  42. }
  43. }
  44. approvalFlow.NodeList = flowNodeList
  45. return
  46. }
  47. // 节点用户
  48. type NodeUser struct {
  49. UserType string `json:"user_type";description:"用户类型,枚举值,user:指定人,manager:主管,role:角色"`
  50. User string `json:"user";description:"所属用户,多个用英文,隔开;用户类型为user代表指定人id,manager代表几级主管,role代表角色id"`
  51. }
  52. // 获取审批人信息
  53. func getAdminListByUser(userStrList string, productId int) (adminList []*system.Admin, err error) {
  54. nodeUserList := make([]NodeUser, 0)
  55. err = json.Unmarshal([]byte(userStrList), &nodeUserList)
  56. if err != nil {
  57. return
  58. }
  59. firstAdminList := make([]*system.Admin, 0)
  60. for _, userInfo := range nodeUserList {
  61. userType := userInfo.UserType
  62. user := userInfo.User
  63. tmpAdminList := make([]*system.Admin, 0)
  64. switch userType {
  65. case "user":
  66. tmpAdminList, err = system.GetAdminListByIds(user)
  67. case "manager":
  68. roleTypeCode := ``
  69. if productId == 1 {
  70. roleTypeCode = `ficc_admin`
  71. } else if productId == 1 {
  72. roleTypeCode = `rai_admin`
  73. }
  74. if roleTypeCode == `` {
  75. err = errors.New("找不到对应的主管")
  76. return
  77. }
  78. tmpAdminList, err = system.GetAdminListByRoleCode(roleTypeCode)
  79. case "role": //角色类型
  80. roleSlice := strings.Split(user, ",")
  81. for _, roleId := range roleSlice {
  82. list, tmpErr := system.GetAdminListByRoleId(roleId)
  83. if tmpErr != nil {
  84. err = tmpErr
  85. return
  86. }
  87. tmpAdminList = append(tmpAdminList, list...)
  88. }
  89. }
  90. firstAdminList = append(firstAdminList, tmpAdminList...)
  91. }
  92. userMap := make(map[int]*system.Admin)
  93. for _, adminInfo := range firstAdminList {
  94. fmt.Println(userMap)
  95. //如果不在,那么就插入用户列表,避免同一个节点重复添加用户
  96. if _, ok := userMap[adminInfo.AdminId]; !ok {
  97. adminList = append(adminList, adminInfo)
  98. userMap[adminInfo.AdminId] = adminInfo
  99. }
  100. }
  101. return
  102. }
  103. // 获取审批工作流(基础)
  104. func GetApprovalFlowDetail(flowId int) (approvalFlow *system.ApprovalFlowItem, err error) {
  105. approvalFlow, err = system.GetByFlowItemId(flowId)
  106. if err != nil {
  107. err = errors.New("查询审批流程失败:Err:" + err.Error())
  108. return
  109. }
  110. flowNodeList := make([]*system.ApprovalFlowNodeList, 0)
  111. approvalFlowNodeList, err := system.GetListByFlowId(approvalFlow.FlowId, approvalFlow.CurrVersion)
  112. if err != nil {
  113. err = errors.New("查询审批节点失败:Err:" + err.Error())
  114. return
  115. }
  116. for _, approvalFlowNode := range approvalFlowNodeList {
  117. userList, tmpErr := getAdminListByUser(approvalFlowNode.User, approvalFlow.ProductId)
  118. if tmpErr != nil {
  119. err = errors.New("获取审批人失败:Err:" + tmpErr.Error())
  120. return
  121. }
  122. for _, user := range userList {
  123. userInfo := system.User{
  124. AdminId: user.AdminId,
  125. RoleId: user.RoleId,
  126. Name: user.RealName,
  127. RoleName: user.RoleName,
  128. RoleTypeCode: user.RoleTypeCode,
  129. Mobile: user.Mobile,
  130. }
  131. approvalFlowNode.UserList = append(approvalFlowNode.UserList, userInfo)
  132. }
  133. var userArr []system.NodeUser
  134. tmpErr = json.Unmarshal([]byte(approvalFlowNode.User), &userArr)
  135. if tmpErr != nil {
  136. err = errors.New("获取审批人失败:Err:" + tmpErr.Error())
  137. return
  138. }
  139. approvalFlowNode.UserArr = userArr
  140. if len(approvalFlowNode.UserList) > 0 {
  141. flowNodeList = append(flowNodeList, approvalFlowNode)
  142. }
  143. }
  144. approvalFlow.NodeList = flowNodeList
  145. return
  146. }
  147. // 节点配置
  148. func EditFlow(editFlowReq system.EditFlowReq) (err error) {
  149. flowId := editFlowReq.FlowId
  150. //获取流程配置详情
  151. approvalFlow, err := system.GetByFlowItemId(flowId)
  152. if err != nil {
  153. return
  154. }
  155. if approvalFlow == nil {
  156. err = errors.New("找不到该审批单")
  157. return
  158. }
  159. nodeList := editFlowReq.NodeList
  160. flowNodeList := make([]*system.ApprovalFlowNode, 0)
  161. //newVer := approvalFlow.CurrVersion + 1
  162. for _, node := range nodeList {
  163. if len(node.User) <= 0 {
  164. err = errors.New("审批/抄送节点用户不能为空")
  165. return
  166. }
  167. for _, nodeUser := range node.User {
  168. if nodeUser.User == "" {
  169. err = errors.New("数据异常")
  170. return
  171. }
  172. }
  173. userByte, tmpErr := json.Marshal(node.User)
  174. if tmpErr != nil {
  175. err = errors.New("格式化用户失败,Err:" + tmpErr.Error())
  176. return
  177. }
  178. nodeName := ""
  179. if node.NodeType == "check" {
  180. nodeName = `审批人`
  181. } else {
  182. nodeName = `抄送人`
  183. }
  184. flowNode := &system.ApprovalFlowNode{
  185. //NodeId :1,
  186. FlowId: flowId,
  187. NodeName: nodeName, // 流程节点名称
  188. PrevNodeId: 0, //上级节点id,为0代表是开始节点
  189. NextNodeId: 0, //下级节点id,为0代表是结束节点
  190. NodeType: node.NodeType, //节点类型,审批人:check;抄送人:cc(Carbon Copy),默认是:check
  191. AuditType: 1, //多人审批时,1:或签(一名审批人同意即可),2:会签(需所有审批人同意)
  192. User: string(userByte), //所属用户,多个用英文,隔开;用户类型为user代表指定人id,manager代表几级主管,role代表角色类型
  193. //Version: newVer, // 所属流程版本
  194. CreateTime: time.Now(), // 创建时间
  195. }
  196. flowNodeList = append(flowNodeList, flowNode)
  197. }
  198. err = system.UpdateFlow(flowId, flowNodeList)
  199. return
  200. }