bi_approve.go 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130
  1. package biapprove
  2. import (
  3. "encoding/json"
  4. "eta_gn/eta_api/models"
  5. biapprove "eta_gn/eta_api/models/bi_approve"
  6. "eta_gn/eta_api/models/bi_approve/response"
  7. "eta_gn/eta_api/models/bi_dashboard"
  8. "eta_gn/eta_api/utils"
  9. "fmt"
  10. "sort"
  11. "strings"
  12. "time"
  13. )
  14. var (
  15. timeField = map[int]string{1: fmt.Sprintf("a.%s", biapprove.BiApproveRecordCols.CreateTime), 2: fmt.Sprintf("a.%s", biapprove.BiApproveRecordCols.NodeApproveTime), 3: fmt.Sprintf("b.%s", biapprove.BiApproveCols.ApproveTime)}
  16. myTimeField = map[int]string{1: biapprove.BiApproveCols.CreateTime, 3: biapprove.BiApproveCols.ApproveTime}
  17. orderRules = map[int]string{1: "ASC", 2: "DESC"}
  18. )
  19. func GetPulicBiClassifyList() (res []*bi_dashboard.BiDashboardClassifyItem, msg string, err error) {
  20. classifyList, err := bi_dashboard.GetBiDashboardClassifyAllList()
  21. if err != nil {
  22. msg = "获取公共分类列表失败"
  23. return
  24. }
  25. res = toClassifyItem(classifyList)
  26. return
  27. }
  28. func PassBiApprove(approveId int, adminId int) (msg string, err error) {
  29. approveItem, e := biapprove.GetBiApproveById(approveId)
  30. if e != nil {
  31. if utils.IsErrNoRow(e) {
  32. msg = "审批不存在, 请刷新页面"
  33. err = e
  34. return
  35. }
  36. msg = "操作失败"
  37. return
  38. }
  39. if approveItem.State != BiApproveStateApproving {
  40. msg = "审批状态有误, 请刷新页面"
  41. err = fmt.Errorf("审批状态有误, State: %d", approveItem.State)
  42. return
  43. }
  44. var ApprovePars []interface{}
  45. Approveconds := ` AND bi_approve_id =? AND approve_user_id =? AND state =? `
  46. ApprovePars = append(ApprovePars, approveId, adminId, BiApproveStateApproving)
  47. recordItem, er := biapprove.GetBiApproveRecordByCondition(Approveconds, ApprovePars)
  48. if er != nil {
  49. if utils.IsErrNoRow(er) {
  50. msg = "无权审批"
  51. err = er
  52. return
  53. }
  54. msg = "操作失败"
  55. return
  56. }
  57. // 查询审批流和审批流节点
  58. flowItem, e := biapprove.GetBiApproveFlowById(approveItem.FlowId)
  59. if e != nil {
  60. err = fmt.Errorf("获取审批流失败, Err: %s", e.Error())
  61. return
  62. }
  63. nodePars := make([]interface{}, 0)
  64. nodeCond := ` AND bi_approve_flow_id =? AND curr_version =? `
  65. nodePars = append(nodePars, flowItem.BiApproveFlowId, flowItem.CurrVersion)
  66. nodeItems, e := biapprove.GetBiApproveNodeByCondition(nodeCond, nodePars)
  67. if e != nil {
  68. err = fmt.Errorf("ApproveNodes GetItemsByCondition err: %s", e.Error())
  69. return
  70. }
  71. if len(nodeItems) == 0 {
  72. err = fmt.Errorf("无审批节点")
  73. return
  74. }
  75. nodeMap := make(map[int]*biapprove.BiApproveNode)
  76. for _, v := range nodeItems {
  77. nodeMap[v.BiApproveNodeId] = v
  78. }
  79. // 取出审批记录的节点
  80. currNodeItem := nodeMap[recordItem.NodeId]
  81. if currNodeItem == nil {
  82. err = fmt.Errorf("当前节点信息有误")
  83. return
  84. }
  85. currNode, e := FormatBiApproveNode2Item(currNodeItem)
  86. if e != nil {
  87. err = fmt.Errorf("当前节点信息有误, Err: %s", e.Error())
  88. return
  89. }
  90. now := time.Now().Local()
  91. recordItem.State = BiApproveStatePass
  92. recordItem.ApproveTime = now
  93. recordItem.ModifyTime = now
  94. recordItem.NodeState = BiApproveStatePass
  95. recordItem.NodeApproveUserId = recordItem.ApproveUserId
  96. recordItem.NodeApproveUserName = recordItem.ApproveUserName
  97. recordItem.NodeApproveTime = now
  98. recordCols := []string{"State", "ApproveTime", "ModifyTime", "NodeState", "NodeApproveUserId", "NodeApproveUserName", "NodeApproveTime"}
  99. lastApprove := false
  100. // 依次审批
  101. if currNode.ApproveType == NodeApproveTypeRoll {
  102. if e = recordItem.Update(recordCols); e != nil {
  103. err = fmt.Errorf("更新审批记录状态失败, Err: %s", e.Error())
  104. return
  105. }
  106. // 检查依次审批情况
  107. sort.Slice(currNode.Users, func(k, j int) bool {
  108. return currNode.Users[k].Sort < currNode.Users[j].Sort
  109. })
  110. userLen := len(currNode.Users)
  111. //lastRoll := false
  112. nextUser := new(response.BiApproveNodeUser) // 下一个审批人, 为nil则表示当前审批人即为最后
  113. for k, v := range currNode.Users {
  114. // 当前审批人
  115. if v.UserId == adminId && recordItem.ApproveUserSort == v.Sort {
  116. if (k + 1) < userLen {
  117. nextUser = currNode.Users[k+1]
  118. }
  119. }
  120. }
  121. // 当前节点下一个审批人, 生成下一个审批记录且return
  122. if nextUser.UserId > 0 {
  123. newRecord := new(biapprove.BiApproveRecord)
  124. newRecord.BiApproveId = recordItem.BiApproveId
  125. newRecord.State = BiApproveStateApproving
  126. newRecord.NodeId = currNode.BiApproveNodeId
  127. newRecord.PrevNodeId = currNode.PrevNodeId
  128. newRecord.NextNodeId = currNode.NextNodeId
  129. newRecord.ApproveType = currNode.ApproveType
  130. newRecord.ApproveUserId = nextUser.UserId
  131. newRecord.ApproveUserName = nextUser.UserName
  132. newRecord.ApproveUserSort = nextUser.Sort
  133. newRecord.CreateTime = now
  134. newRecord.ModifyTime = now
  135. newRecord.NodeState = BiApproveStateApproving
  136. if e = newRecord.Create(); e != nil {
  137. err = fmt.Errorf("生成审批记录失败, Err: %s", e.Error())
  138. return
  139. }
  140. // 推送审批消息
  141. go func() {
  142. messageItem := new(biapprove.BiApproveMessage)
  143. messageItem.SendUserId = approveItem.ApplyUserId
  144. messageItem.ReceiveUserId = nextUser.UserId
  145. messageItem.Content = "您有新的待办任务"
  146. messageItem.Remark = fmt.Sprintf("%s提交的【%s】需要您审批,请及时处理", approveItem.ApplyUserName, approveItem.BiTitle)
  147. messageItem.BiApproveId = approveItem.BiApproveId
  148. messageItem.ApproveState = BiApproveStateApproving
  149. messageItem.CreateTime = now
  150. messageItem.ModifyTime = now
  151. if e = messageItem.Create(); e != nil {
  152. utils.FileLog.Info(fmt.Sprintf("PassBiApprove message err: %s", e.Error()))
  153. return
  154. }
  155. }()
  156. return
  157. }
  158. // 更新审批当前节点并进入下一个节点
  159. if currNode.NextNodeId > 0 {
  160. nextNode := nodeMap[currNode.NextNodeId]
  161. approveItem.CurrNodeId = currNode.NextNodeId
  162. approveItem.ModifyTime = now
  163. if e = approveItem.Update([]string{"CurrNodeId", "ModifyTime"}); e != nil {
  164. err = fmt.Errorf("更新审批当前节点失败, Err: %s", e.Error())
  165. return
  166. }
  167. err = BuildNextNodeRecordAndMsg(nextNode, approveItem.BiApproveId, approveItem.ApplyUserId, approveItem.ApplyUserName, approveItem.BiTitle)
  168. return
  169. } else {
  170. // 最后一个节点
  171. lastApprove = true
  172. }
  173. }
  174. // 会签
  175. if currNode.ApproveType == NodeApproveTypeAll {
  176. // 查询其他审批人是否已审批
  177. otherCond := ` AND bi_approve_id =? AND node_id =? AND approve_user_id <> ? `
  178. otherPars := make([]interface{}, 0)
  179. otherPars = append(otherPars, approveItem.BiApproveId, recordItem.NodeId, adminId)
  180. otherRecords, e := biapprove.GetBiApproveRecordItemsByCondition(otherCond, otherPars)
  181. if e != nil {
  182. err = fmt.Errorf("获取节点审批记录失败, Err: %s", e.Error())
  183. return
  184. }
  185. otherPass := true
  186. for _, v := range otherRecords {
  187. if v.State != BiApproveStatePass {
  188. otherPass = false
  189. }
  190. }
  191. // 其他人未审批, 仅更新当前审批记录
  192. if e = recordItem.Update(recordCols); e != nil {
  193. err = fmt.Errorf("更新审批记录状态失败, Err: %s", e.Error())
  194. return
  195. }
  196. // 其他人已审批且为最后节点
  197. if otherPass && currNode.NextNodeId == 0 {
  198. lastApprove = true
  199. }
  200. // 其他人已审批且不为最后节点, 进入下一节点
  201. if otherPass && currNode.NextNodeId > 0 {
  202. nextNode := nodeMap[currNode.NextNodeId]
  203. approveItem.CurrNodeId = currNode.NextNodeId
  204. approveItem.ModifyTime = now
  205. if e = approveItem.Update([]string{"CurrNodeId", "ModifyTime"}); e != nil {
  206. err = fmt.Errorf("更新审批当前节点失败, Err: %s", e.Error())
  207. return
  208. }
  209. err = BuildNextNodeRecordAndMsg(nextNode, approveItem.BiApproveId, approveItem.ApplyUserId, approveItem.ApplyUserName, approveItem.BiTitle)
  210. return
  211. }
  212. }
  213. // 或签
  214. if currNode.ApproveType == NodeApproveTypeAny {
  215. // 需检查一下审批的当前节点和记录的节点是否匹配, 不匹配可能是因为另外的审批人已通过, 所以此处应给提示
  216. // 前端也有做相应的判断,但是两个人同时进入审批详情页时就可能出现这种情况
  217. if approveItem.CurrNodeId != recordItem.NodeId {
  218. msg = "该节点已完成审批, 请刷新页面"
  219. return
  220. }
  221. if e = recordItem.Update(recordCols); e != nil {
  222. err = fmt.Errorf("更新审批记录状态失败, Err: %s", e.Error())
  223. return
  224. }
  225. // 将该审批的同一个节点的记录标记为已审批
  226. if e = recordItem.UpdateNodeState(recordItem.BiApproveId, recordItem.NodeId, recordItem.NodeState, recordItem.NodeApproveUserId, recordItem.NodeApproveUserName, recordItem.NodeApproveTime); e != nil {
  227. err = fmt.Errorf("更新同一节点的其他审批记录状态失败, Err: %s", e.Error())
  228. return
  229. }
  230. if currNode.NextNodeId == 0 {
  231. lastApprove = true
  232. }
  233. if currNode.NextNodeId > 0 {
  234. nextNode := nodeMap[currNode.NextNodeId]
  235. approveItem.CurrNodeId = currNode.NextNodeId
  236. approveItem.ModifyTime = now
  237. if e = approveItem.Update([]string{"CurrNodeId", "ModifyTime"}); e != nil {
  238. err = fmt.Errorf("更新审批当前节点失败, Err: %s", e.Error())
  239. return
  240. }
  241. err = BuildNextNodeRecordAndMsg(nextNode, approveItem.BiApproveId, approveItem.ApplyUserId, approveItem.ApplyUserName, approveItem.BiTitle)
  242. return
  243. }
  244. }
  245. // 最后一个审批, 更新审批记录、审批、报告状态、推送消息给申请人
  246. if lastApprove {
  247. if e = recordItem.Update(recordCols); e != nil {
  248. err = fmt.Errorf("更新审批记录状态失败, Err: %s", e.Error())
  249. return
  250. }
  251. approveItem.State = BiApproveStatePass
  252. approveItem.ApproveTime = now
  253. approveItem.ModifyTime = now
  254. approveCols := []string{"State", "ApproveTime", "ModifyTime"}
  255. if e = approveItem.Update(approveCols); e != nil {
  256. err = fmt.Errorf("更新审批信息失败, Err: %s", e.Error())
  257. return
  258. }
  259. if e = updateBiApproveState(approveItem.BiId, BiStatePass); e != nil {
  260. err = fmt.Errorf("更新报告审批状态失败, Err: %s", e.Error())
  261. return
  262. }
  263. go func() {
  264. messageItem := new(biapprove.BiApproveMessage)
  265. messageItem.SendUserId = adminId
  266. messageItem.ReceiveUserId = approveItem.ApplyUserId
  267. messageItem.Content = "您提交的审批已通过"
  268. messageItem.Remark = fmt.Sprintf("您提交的【%s】已通过", approveItem.BiTitle)
  269. messageItem.BiApproveId = approveItem.BiApproveId
  270. messageItem.ApproveState = BiApproveStatePass
  271. messageItem.CreateTime = now
  272. messageItem.ModifyTime = now
  273. if e = messageItem.Create(); e != nil {
  274. utils.FileLog.Info(fmt.Sprintf("PassBiApprove message err: %s", e.Error()))
  275. return
  276. }
  277. }()
  278. }
  279. return
  280. }
  281. func toClassifyItem(src []*bi_dashboard.BiDashboardClassify) (dst []*bi_dashboard.BiDashboardClassifyItem) {
  282. for _, item := range src {
  283. dst = append(dst, &bi_dashboard.BiDashboardClassifyItem{
  284. BiDashboardClassifyId: item.BiDashboardClassifyId,
  285. BiDashboardClassifyName: item.BiDashboardClassifyName,
  286. })
  287. }
  288. return
  289. }
  290. // BuildNextNodeRecordAndMsg 生成下一个节点的审批记录并推送消息
  291. func BuildNextNodeRecordAndMsg(approveNodeItem *biapprove.BiApproveNode, approveId, sysAdminId int, sysAdminName, biTitle string) (err error) {
  292. if approveNodeItem == nil {
  293. err = fmt.Errorf("approve node nil")
  294. return
  295. }
  296. // 根据节点审批方式生成审批记录
  297. now := time.Now().Local()
  298. approveNode, e := FormatBiApproveNode2Item(approveNodeItem)
  299. if e != nil {
  300. err = fmt.Errorf("FormatBiApproveNode2Item err: %s", e.Error())
  301. return
  302. }
  303. if len(approveNode.Users) == 0 {
  304. err = fmt.Errorf("审批节点用户有误")
  305. return
  306. }
  307. newRecords := make([]*biapprove.BiApproveRecord, 0)
  308. sort.Slice(approveNode.Users, func(k, j int) bool {
  309. return approveNode.Users[k].Sort < approveNode.Users[j].Sort
  310. })
  311. for _, u := range approveNode.Users {
  312. r := new(biapprove.BiApproveRecord)
  313. r.BiApproveId = approveId
  314. r.State = BiApproveStateApproving
  315. r.NodeId = approveNode.BiApproveNodeId
  316. r.PrevNodeId = approveNode.PrevNodeId
  317. r.NextNodeId = approveNode.NextNodeId
  318. r.ApproveType = approveNode.ApproveType
  319. r.ApproveUserId = u.UserId
  320. r.ApproveUserName = u.UserName
  321. r.ApproveUserSort = u.Sort
  322. r.CreateTime = now
  323. r.ModifyTime = now
  324. r.NodeState = BiApproveStateApproving // 当前节点审批状态
  325. newRecords = append(newRecords, r)
  326. // 依次审批仅生成一条记录
  327. if approveNode.ApproveType == NodeApproveTypeRoll {
  328. break
  329. }
  330. }
  331. recordOb := new(biapprove.BiApproveRecord)
  332. if e = recordOb.CreateMulti(newRecords); e != nil {
  333. err = fmt.Errorf("生成节点审批记录失败, Err: %s", e.Error())
  334. return
  335. }
  336. // 推送审批消息
  337. go func() {
  338. messageOb := new(biapprove.BiApproveMessage)
  339. messages := make([]*biapprove.BiApproveMessage, 0)
  340. for _, v := range newRecords {
  341. m := new(biapprove.BiApproveMessage)
  342. m.SendUserId = sysAdminId
  343. m.ReceiveUserId = v.ApproveUserId
  344. m.Content = "您有新的待办任务"
  345. m.Remark = fmt.Sprintf("%s提交的【%s】需要您审批,请及时处理", sysAdminName, biTitle)
  346. m.BiApproveId = approveId
  347. m.ApproveState = BiApproveStateApproving
  348. m.CreateTime = now
  349. m.ModifyTime = now
  350. messages = append(messages, m)
  351. }
  352. e = messageOb.CreateMulti(messages)
  353. if e != nil {
  354. utils.FileLog.Info(fmt.Sprintf("BuildNextNodeRecordAndMsg messages err: %s", e.Error()))
  355. return
  356. }
  357. }()
  358. return
  359. }
  360. // updateBiApproveState 更新Bi看板审批状态
  361. func updateBiApproveState(biId, state int) (err error) {
  362. // updateCols := []string{"ApproveId", "State", "ModifyTime"}
  363. updateCols := []string{"State", "ModifyTime"}
  364. BiItem, e := bi_dashboard.GetDashboardById(biId)
  365. if e != nil && !utils.IsErrNoRow(e) {
  366. err = fmt.Errorf("获取Bi看板失败, Err: %s", e.Error())
  367. return
  368. }
  369. if BiItem != nil && BiItem.BiDashboardId > 0 {
  370. // BiItem.ApproveId = approveId
  371. BiItem.State = state
  372. BiItem.ModifyTime = time.Now().Local()
  373. // if state == models.BiStatePass || state == models.BiStateRefused {
  374. // updateCols = append(updateCols, "ApproveTime")
  375. // // BiItem.ApproveTime = time.Now().Local()
  376. // }
  377. // if state == models.BiStatePass {
  378. // updateCols = append(updateCols, "PublishTime")
  379. // // BiItem.PublishTime = time.Now().Local()
  380. // }
  381. if e = BiItem.Update(updateCols); e != nil {
  382. err = fmt.Errorf("更新Bi看板审批状态失败, Err: %s", e.Error())
  383. return
  384. }
  385. }
  386. return
  387. }
  388. func ProcessingBiApprove(adminId, classifyId, timeType, sortField, sortRule, startSize, pageSize int, adminName, startTime, endTime, keyword string) (respList []*response.BiApproveItemOrmResp, respTotal int, msg string, err error) {
  389. cond := fmt.Sprintf(` AND a.%s = ? AND b.%s = ? AND a.%s = ?`, biapprove.BiApproveRecordCols.State, biapprove.BiApproveCols.State, biapprove.BiApproveRecordCols.ApproveUserId)
  390. pars := make([]interface{}, 0)
  391. pars = append(pars, BiApproveStateApproving, BiApproveStateApproving, adminId)
  392. order := ""
  393. // 筛选条件
  394. if classifyId > 0 {
  395. cond += fmt.Sprintf(` AND b.%s = ?`, biapprove.BiApproveCols.ClassifyId)
  396. pars = append(pars, classifyId)
  397. }
  398. if timeType <= 0 {
  399. timeType = 1
  400. }
  401. if timeType == 1 && startTime != "" && endTime != "" {
  402. _, e := time.Parse(utils.FormatDate, startTime)
  403. if e != nil {
  404. msg = "开始时间格式有误"
  405. err = e
  406. return
  407. }
  408. tmpEndTime, e := time.Parse(utils.FormatDate, endTime)
  409. if e != nil {
  410. msg = "结束时间格式有误"
  411. err = e
  412. return
  413. }
  414. tmpEndTime = tmpEndTime.AddDate(0, 0, 1)
  415. cond += fmt.Sprintf(` AND (b.%s BETWEEN ? AND ?)`, biapprove.BiApproveCols.CreateTime)
  416. pars = append(pars, startTime, tmpEndTime)
  417. }
  418. keyword = strings.TrimSpace(keyword)
  419. if keyword != "" {
  420. kw := fmt.Sprint("%", keyword, "%")
  421. cond += fmt.Sprintf(` AND b.%s LIKE ?`, biapprove.BiApproveCols.BiTitle)
  422. pars = append(pars, kw)
  423. }
  424. if sortField > 0 && sortRule > 0 {
  425. orderField := timeField[sortField]
  426. if orderField == "" {
  427. msg = "时间排序字段有误"
  428. return
  429. }
  430. orderRule := orderRules[sortRule]
  431. if orderRule == "" {
  432. msg = "时间排序方式有误"
  433. return
  434. }
  435. order = fmt.Sprintf("%s %s", orderField, orderRule)
  436. }
  437. total, e := biapprove.GetApprovingBiApproveCount(cond, pars)
  438. if e != nil {
  439. msg = "获取失败"
  440. err = fmt.Errorf("GetApprovingBiApproveCount err: %s", e.Error())
  441. return
  442. }
  443. list, e := biapprove.GetApprovingBiApprovePageList(cond, pars, order, startSize, pageSize)
  444. if e != nil {
  445. msg = "获取失败"
  446. err = fmt.Errorf("GetApprovingBiApprovePageList err: %s", e.Error())
  447. return
  448. }
  449. respList = toBiApproveItemOrmResp(list)
  450. respTotal = total
  451. return
  452. }
  453. // SolvedBiApprove 已处理的审批
  454. func SolvedBiApprove(adminId, classifyId, timeType, sortField, sortRule, approveState, startSize, pageSize int, adminName, startTime, endTime, keyword string) (respList []*response.BiApproveItemOrmResp, respTotal int, msg string, err error) {
  455. cond := fmt.Sprintf(` AND a.%s = ? AND a.%s IN (?)`, biapprove.BiApproveRecordCols.ApproveUserId, biapprove.BiApproveRecordCols.NodeState)
  456. pars := make([]interface{}, 0)
  457. pars = append(pars, adminId, []int{BiApproveStatePass, BiApproveStateRefuse})
  458. order := ""
  459. // 筛选条件
  460. if classifyId > 0 {
  461. cond += fmt.Sprintf(` AND b.%s = ?`, biapprove.BiApproveCols.ClassifyId)
  462. pars = append(pars, classifyId)
  463. }
  464. if timeType > 0 && startTime != "" && endTime != "" {
  465. _, e := time.Parse(utils.FormatDate, startTime)
  466. if e != nil {
  467. msg = "开始时间格式有误"
  468. err = e
  469. return
  470. }
  471. tmpEndTime, e := time.Parse(utils.FormatDate, endTime)
  472. if e != nil {
  473. msg = "结束时间格式有误"
  474. err = e
  475. return
  476. }
  477. tmpEndTime = tmpEndTime.AddDate(0, 0, 1)
  478. cond += fmt.Sprintf(` AND (%s BETWEEN ? AND ?)`, timeField[timeType])
  479. pars = append(pars, startTime, tmpEndTime)
  480. }
  481. keyword = strings.TrimSpace(keyword)
  482. if keyword != "" {
  483. kw := fmt.Sprint("%", keyword, "%")
  484. cond += fmt.Sprintf(` AND b.%s LIKE ?`, biapprove.BiApproveCols.BiTitle)
  485. pars = append(pars, kw)
  486. }
  487. if sortField > 0 && sortRule > 0 {
  488. orderField := timeField[sortField]
  489. if orderField == "" {
  490. msg = "时间排序字段有误"
  491. return
  492. }
  493. orderRule := orderRules[sortRule]
  494. if orderRule == "" {
  495. msg = "时间排序方式有误"
  496. return
  497. }
  498. order = fmt.Sprintf("%s %s", orderField, orderRule)
  499. }
  500. if approveState > 0 {
  501. cond += fmt.Sprintf(` AND a.%s = ?`, biapprove.BiApproveRecordCols.NodeState)
  502. pars = append(pars, approveState)
  503. }
  504. total, e := biapprove.GetApprovedBiApproveCount(cond, pars)
  505. if e != nil {
  506. msg = "获取失败"
  507. err = fmt.Errorf("GetApprovedBiApproveCount err: %s", e.Error())
  508. return
  509. }
  510. list, e := biapprove.GetApprovedBiApprovePageList(cond, pars, order, startSize, pageSize)
  511. if e != nil {
  512. msg = "获取失败"
  513. err = fmt.Errorf("GetApprovedBiApprovePageList err: %s", e.Error())
  514. return
  515. }
  516. for _, v := range list {
  517. // 这个时候的状态,用审批状态
  518. v.RecordState = v.NodeState
  519. v.ApproveTime = v.NodeApproveTime
  520. }
  521. respList = toBiApproveItemOrmResp(list)
  522. respTotal = total
  523. return
  524. }
  525. func MyApplyBiApproves(adminId, classifyId, timeType, sortField, sortRule, approveState, startSize, pageSize int, adminName, startTime, endTime, keyword string) (respList []*response.BiApproveItemOrmResp, respTotal int, msg string, err error) {
  526. cond := fmt.Sprintf(` AND a.%s = ?`, biapprove.BiApproveCols.ApplyUserId)
  527. pars := make([]interface{}, 0)
  528. pars = append(pars, adminId)
  529. order := ""
  530. // 筛选条件
  531. if classifyId > 0 {
  532. cond += fmt.Sprintf(` AND a.%s = ?`, biapprove.BiApproveCols.ClassifyId)
  533. pars = append(pars, classifyId)
  534. }
  535. if timeType > 0 && startTime != "" && endTime != "" {
  536. _, e := time.Parse(utils.FormatDate, startTime)
  537. if e != nil {
  538. msg = "开始时间格式有误"
  539. err = e
  540. return
  541. }
  542. tmpEndTime, e := time.Parse(utils.FormatDate, endTime)
  543. if e != nil {
  544. msg = "结束时间格式有误"
  545. err = e
  546. return
  547. }
  548. tmpEndTimeStr := tmpEndTime.AddDate(0, 0, 1).Format(utils.FormatDate)
  549. cond += fmt.Sprintf(` AND (%s BETWEEN ? AND ?)`, myTimeField[timeType])
  550. pars = append(pars, startTime, tmpEndTimeStr)
  551. }
  552. keyword = strings.TrimSpace(keyword)
  553. if keyword != "" {
  554. kw := fmt.Sprint("%", keyword, "%")
  555. cond += fmt.Sprintf(` AND a.%s LIKE ?`, biapprove.BiApproveCols.BiTitle)
  556. pars = append(pars, kw)
  557. }
  558. if sortField > 0 && sortRule > 0 {
  559. orderField := myTimeField[sortField]
  560. if orderField == "" {
  561. msg = "时间排序字段有误"
  562. return
  563. }
  564. orderRule := orderRules[sortRule]
  565. if orderRule == "" {
  566. msg = "时间排序方式有误"
  567. return
  568. }
  569. order = fmt.Sprintf("%s %s", orderField, orderRule)
  570. }
  571. if approveState > 0 {
  572. cond += fmt.Sprintf(` AND a.%s = ?`, biapprove.BiApproveRecordCols.State)
  573. pars = append(pars, approveState)
  574. }
  575. total, e := biapprove.GetApplyBiApproveCount(cond, pars)
  576. if e != nil {
  577. msg = "获取失败"
  578. err = fmt.Errorf("GetApplyBiApproveCount err: %s", e.Error())
  579. return
  580. }
  581. respTotal = total
  582. list, e := biapprove.GetApplyBiApprovePageList(cond, pars, order, startSize, pageSize)
  583. if e != nil {
  584. msg = "获取失败"
  585. err = fmt.Errorf("GetApplyBiApprovePageList err: %s", e.Error())
  586. return
  587. }
  588. respList = toBiApproveItemOrmResp(list)
  589. return
  590. }
  591. func GetApproveDetail(approveId int) (resp *response.BiApproveDetail, msg string, err error) {
  592. approveItem, e := biapprove.GetBiApproveById(approveId)
  593. if e != nil {
  594. if utils.IsErrNoRow(e) {
  595. msg = "审批已被删除, 请刷新页面"
  596. err = e
  597. return
  598. }
  599. msg = "获取失败"
  600. err = fmt.Errorf("GetItemById err: %s", e.Error())
  601. return
  602. }
  603. // 审批信息
  604. detail := new(response.BiApproveDetail)
  605. detail.Approve = new(response.BiApproveDetailItem)
  606. detail.Approve.BiApproveId = approveItem.BiApproveId
  607. detail.Approve.State = approveItem.State
  608. detail.Approve.FlowId = approveItem.FlowId
  609. detail.Approve.FlowVersion = approveItem.FlowVersion
  610. detail.Approve.StartNodeId = approveItem.StartNodeId
  611. detail.Approve.CurrNodeId = approveItem.CurrNodeId
  612. detail.Approve.ApplyUserId = approveItem.ApplyUserId
  613. detail.Approve.ApplyUserName = approveItem.ApplyUserName
  614. detail.Approve.ApproveTime = utils.TimeTransferString(utils.FormatDateTime, approveItem.ApproveTime)
  615. detail.Approve.CreateTime = utils.TimeTransferString(utils.FormatDateTime, approveItem.CreateTime)
  616. detail.Approve.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, approveItem.ModifyTime)
  617. // 审批节点
  618. nodeOb := new(biapprove.BiApproveNode)
  619. nodeCond := fmt.Sprintf(` AND %s = ? AND %s = ?`, biapprove.BiApproveNodeCols.BiApproveFlowId, biapprove.BiApproveNodeCols.CurrVersion)
  620. nodePars := make([]interface{}, 0)
  621. nodePars = append(nodePars, approveItem.FlowId, approveItem.FlowVersion)
  622. nodeItems, e := nodeOb.GetItemsByCondition(nodeCond, nodePars, []string{}, "")
  623. if e != nil {
  624. msg = "获取失败"
  625. err = fmt.Errorf("GetItemsByCondition err: %s", e.Error())
  626. return
  627. }
  628. // 审批记录
  629. recordOb := new(biapprove.BiApproveRecord)
  630. recordCond := fmt.Sprintf(` AND %s = ?`, biapprove.BiApproveRecordCols.BiApproveId)
  631. recordPars := make([]interface{}, 0)
  632. recordPars = append(recordPars, approveItem.BiApproveId)
  633. recordItems, e := recordOb.GetItemsByCondition(recordCond, recordPars, []string{}, fmt.Sprintf("%s DESC", biapprove.BiApproveRecordCols.ApproveTime))
  634. if e != nil {
  635. msg = "获取失败"
  636. err = fmt.Errorf("GetItemsByCondition err: %s", e.Error())
  637. return
  638. }
  639. recordMap := make(map[string]*biapprove.BiApproveRecord)
  640. for _, v := range recordItems {
  641. k := fmt.Sprintf("%d-%d", v.NodeId, v.ApproveUserId)
  642. recordMap[k] = v
  643. }
  644. // 审批流节点详情
  645. detail.ApproveFlowNodes = make([]*response.BiApproveDetailNodes, 0)
  646. for _, v := range nodeItems {
  647. t := new(response.BiApproveDetailNodes)
  648. t.BiApproveNodeId = v.BiApproveNodeId
  649. t.BiApproveFlowId = v.BiApproveFlowId
  650. t.PrevNodeId = v.PrevNodeId
  651. t.NextNodeId = v.NextNodeId
  652. t.NodeType = v.NodeType
  653. t.ApproveType = v.ApproveType
  654. t.Users = make([]*response.BiApproveDetailNodeUser, 0)
  655. us := make([]*response.BiApproveNodeUserReq, 0)
  656. if v.Users != "" {
  657. e = json.Unmarshal([]byte(v.Users), &us)
  658. if e != nil {
  659. msg = "获取失败"
  660. err = fmt.Errorf("json.Unmarshal err: %s", e.Error())
  661. return
  662. }
  663. }
  664. for _, vu := range us {
  665. u := new(response.BiApproveDetailNodeUser)
  666. u.UserType = vu.UserType
  667. u.UserId = vu.UserId
  668. u.UserName = vu.UserName
  669. u.Sort = vu.Sort
  670. // 审批记录
  671. k := fmt.Sprintf("%d-%d", v.BiApproveNodeId, vu.UserId)
  672. r := recordMap[k]
  673. if r != nil {
  674. u.ApproveRecord = new(response.BiApproveDetailNodeUserRecord)
  675. u.ApproveRecord.BiApproveRecordId = r.BiApproveRecordId
  676. u.ApproveRecord.State = r.State
  677. u.ApproveRecord.ApproveUserId = r.ApproveUserId
  678. u.ApproveRecord.ApproveUserName = r.ApproveUserName
  679. u.ApproveRecord.ApproveRemark = r.ApproveRemark
  680. u.ApproveRecord.ApproveTime = utils.TimeTransferString(utils.FormatDateTime, r.ApproveTime)
  681. }
  682. t.Users = append(t.Users, u)
  683. }
  684. sort.Slice(t.Users, func(k, j int) bool {
  685. return t.Users[k].Sort < t.Users[j].Sort
  686. })
  687. detail.ApproveFlowNodes = append(detail.ApproveFlowNodes, t)
  688. }
  689. // 看板信息
  690. cnClassifyIdName := make(map[int]string)
  691. cnClassify, e := bi_dashboard.GetBiDashboardClassifyAllList()
  692. if e != nil {
  693. msg = "获取失败"
  694. err = fmt.Errorf("GetBiDashboardClassifyAllList err: %s", e.Error())
  695. return
  696. }
  697. for _, v := range cnClassify {
  698. cnClassifyIdName[v.BiDashboardClassifyId] = v.BiDashboardClassifyName
  699. }
  700. detail.Bi = new(response.BiApproveDetailBi)
  701. detail.Bi.BiId = approveItem.BiId
  702. detail.Bi.BiTitle = approveItem.BiTitle
  703. detail.Bi.BiClassify = cnClassifyIdName[approveItem.ClassifyId]
  704. resp = detail
  705. return
  706. }
  707. func BiApproveRefuse(biApproveId, adminId int, approveRemark string) (msg string, err error) {
  708. approveItem, e := biapprove.GetBiApproveById(biApproveId)
  709. if e != nil {
  710. if utils.IsErrNoRow(e) {
  711. msg = "审批不存在, 请刷新页面"
  712. err = e
  713. return
  714. }
  715. msg = "操作失败"
  716. err = fmt.Errorf("GetBiApproveById err: %s", e.Error())
  717. return
  718. }
  719. if approveItem.State != BiApproveStateApproving {
  720. msg = "审批状态有误, 请刷新页面"
  721. err = fmt.Errorf("审批状态有误, State: %d", approveItem.State)
  722. return
  723. }
  724. // 校验审批记录和审批
  725. recordOb := new(biapprove.BiApproveRecord)
  726. recordCond := fmt.Sprintf(` AND %s = ? AND %s = ? AND %s = ?`, biapprove.BiApproveRecordCols.BiApproveId, biapprove.BiApproveRecordCols.ApproveUserId, biapprove.BiApproveRecordCols.State)
  727. recordPars := make([]interface{}, 0)
  728. recordPars = append(recordPars, approveItem.BiApproveId, adminId, BiApproveStateApproving)
  729. recordItem, e := recordOb.GetItemByCondition(recordCond, recordPars, "")
  730. if e != nil {
  731. if utils.IsErrNoRow(e) {
  732. msg = "无权审批"
  733. err = e
  734. return
  735. }
  736. msg = "操作失败"
  737. err = fmt.Errorf("GetItemByCondition err: %s", e.Error())
  738. return
  739. }
  740. // 驳回审批
  741. if e = refuseBiApprove(approveItem, recordItem, approveRemark, adminId); e != nil {
  742. msg = "操作失败"
  743. err = fmt.Errorf("RefuseBiApprove err: %s", e.Error())
  744. return
  745. }
  746. return
  747. }
  748. // refuseBiApprove 驳回审批
  749. func refuseBiApprove(approveItem *biapprove.BiApprove, recordItem *biapprove.BiApproveRecord, approveRemark string, sysAdminId int) (err error) {
  750. if approveItem == nil {
  751. err = fmt.Errorf("审批信息有误")
  752. return
  753. }
  754. if recordItem == nil {
  755. err = fmt.Errorf("审批记录有误")
  756. return
  757. }
  758. // 更新审批记录
  759. now := time.Now().Local()
  760. recordItem.State = BiApproveStateRefuse
  761. recordItem.ApproveRemark = approveRemark
  762. recordItem.ApproveTime = now
  763. recordItem.ModifyTime = now
  764. recordItem.NodeState = BiApproveStateRefuse
  765. recordItem.NodeApproveUserId = recordItem.ApproveUserId
  766. recordItem.NodeApproveUserName = recordItem.ApproveUserName
  767. recordItem.NodeApproveTime = now
  768. recordCols := []string{"State", "ApproveRemark", "ApproveTime", "ModifyTime", "NodeState", "NodeApproveUserId", "NodeApproveUserName", "NodeApproveTime"}
  769. if e := recordItem.Update(recordCols); e != nil {
  770. err = fmt.Errorf("更新审批记录状态失败, Err: %s", e.Error())
  771. return
  772. }
  773. // 将该审批的同一个节点的记录标记为已审批
  774. if e := recordItem.UpdateNodeState(recordItem.BiApproveId, recordItem.NodeId, recordItem.NodeState, recordItem.NodeApproveUserId, recordItem.NodeApproveUserName, recordItem.NodeApproveTime); e != nil {
  775. err = fmt.Errorf("更新同一节点的其他审批记录状态失败, Err: %s", e.Error())
  776. return
  777. }
  778. // 驳回-更新审批, 报告状态, 推送消息
  779. approveItem.State = BiApproveStateRefuse
  780. approveItem.ApproveRemark = approveRemark
  781. approveItem.ApproveTime = now
  782. approveItem.ModifyTime = now
  783. approveCols := []string{"State", "ApproveRemark", "ApproveTime", "ModifyTime"}
  784. if e := approveItem.Update(approveCols); e != nil {
  785. err = fmt.Errorf("更新审批状态失败, Err: %s", e.Error())
  786. return
  787. }
  788. if e := updateBiApproveState(approveItem.BiId, BiStateRefused); e != nil {
  789. err = fmt.Errorf("更新报告状态失败, Err: %s", e.Error())
  790. return
  791. }
  792. // 推送驳回消息给申请人
  793. go func() {
  794. messageItem := new(biapprove.BiApproveMessage)
  795. messageItem.SendUserId = sysAdminId
  796. messageItem.ReceiveUserId = approveItem.ApplyUserId
  797. messageItem.Content = "您提交的审批被驳回"
  798. messageItem.Remark = fmt.Sprintf("您提交的【%s】已被驳回", approveItem.BiTitle)
  799. messageItem.BiApproveId = approveItem.BiApproveId
  800. messageItem.ApproveState = BiApproveStateRefuse
  801. messageItem.CreateTime = now
  802. messageItem.ModifyTime = now
  803. if e := messageItem.Create(); e != nil {
  804. utils.FileLog.Info(fmt.Sprintf("ApproveBi message err: %s", e.Error()))
  805. return
  806. }
  807. }()
  808. return
  809. }
  810. func BiApproveCancel(biApproveId, adminId int, adminName string) (msg string, err error) {
  811. approveItem, e := biapprove.GetBiApproveById(biApproveId)
  812. if e != nil {
  813. if utils.IsErrNoRow(e) {
  814. msg = "审批已被删除, 请刷新页面"
  815. err = e
  816. return
  817. }
  818. msg = "操作失败"
  819. err = fmt.Errorf("GetBiApproveById err: %s", e.Error())
  820. return
  821. }
  822. if approveItem.ApplyUserId != adminId {
  823. msg = "非申请人不可撤销"
  824. err = fmt.Errorf("非申请人不可撤销")
  825. return
  826. }
  827. // 撤销审批
  828. e = cancelBiApprove(approveItem.BiId, approveItem.BiApproveId, adminId, adminName)
  829. if e != nil {
  830. msg = "操作失败"
  831. err = fmt.Errorf("cancelBiApprove err: %s", e.Error())
  832. return
  833. }
  834. return
  835. }
  836. // cancelBiApprove 撤回审批
  837. func cancelBiApprove(biId, approveId, sysAdminId int, sysAdminName string) (err error) {
  838. // 默认内部审批, 如果是走的第三方审批, 那么仅修改状态
  839. confMap, e := models.GetBusinessConf()
  840. if e != nil {
  841. err = fmt.Errorf("GetBusinessConf err: %s", e.Error())
  842. return
  843. }
  844. openMap := map[string]bool{"false": false, "true": true}
  845. openApprove := openMap[confMap[models.BusinessConfIsBIApprove]]
  846. if !openApprove {
  847. //err = fmt.Errorf("未开启审批")
  848. return
  849. }
  850. // 修改审批信息状态
  851. approveItem, e := biapprove.GetBiApproveById(approveId)
  852. if e != nil {
  853. if utils.IsErrNoRow(e) {
  854. err = e
  855. return
  856. }
  857. err = fmt.Errorf("approve GetItemById err: %s", e.Error())
  858. return
  859. }
  860. if approveItem.State == BiApproveStateCancel {
  861. return
  862. }
  863. approveItem.State = BiApproveStateCancel
  864. approveItem.ModifyTime = time.Now()
  865. cols := []string{"State", "ModifyTime"}
  866. if e = approveItem.Update(cols); e != nil {
  867. err = fmt.Errorf("approve Update err: %s", e.Error())
  868. return
  869. }
  870. // 修改报告状态
  871. e = updateBiApproveState(biId, BiStateUnpublished)
  872. if e != nil {
  873. err = fmt.Errorf("更新报告审批撤回失败, Err: %s", e.Error())
  874. return
  875. }
  876. // 推送撤回消息
  877. go func() {
  878. recordOb := new(biapprove.BiApproveRecord)
  879. recordCond := fmt.Sprintf(` AND %s = ?`, biapprove.BiApproveRecordCols.BiApproveId)
  880. recordPars := make([]interface{}, 0)
  881. recordPars = append(recordPars, approveId)
  882. recordItems, e := recordOb.GetItemsByCondition(recordCond, recordPars, []string{}, "")
  883. if e != nil {
  884. utils.FileLog.Info(fmt.Sprintf("approve record GetItemsByCondition err: %s", e.Error()))
  885. return
  886. }
  887. messageOb := new(biapprove.BiApproveMessage)
  888. messages := make([]*biapprove.BiApproveMessage, 0)
  889. for _, v := range recordItems {
  890. m := new(biapprove.BiApproveMessage)
  891. m.SendUserId = sysAdminId
  892. m.ReceiveUserId = v.ApproveUserId
  893. m.Content = fmt.Sprintf("%s提交的【%s】已撤回", sysAdminName, approveItem.BiTitle)
  894. m.BiApproveId = approveId
  895. m.ApproveState = BiApproveStateCancel
  896. m.CreateTime = time.Now().Local()
  897. m.ModifyTime = time.Now().Local()
  898. messages = append(messages, m)
  899. }
  900. e = messageOb.CreateMulti(messages)
  901. if e != nil {
  902. utils.FileLog.Info(fmt.Sprintf("CancelBiApprove messages err: %s", e.Error()))
  903. return
  904. }
  905. }()
  906. return
  907. }
  908. // CheckBiOpenApprove 校验报告是否开启了审批流
  909. func CheckBiOpenApprove(classifyId int) (opening bool, err error) {
  910. // 获取审批配置
  911. confMap, e := models.GetBusinessConf()
  912. if e != nil {
  913. err = fmt.Errorf("GetBusinessConf err: %s", e.Error())
  914. return
  915. }
  916. openMap := map[string]bool{"false": false, "true": true}
  917. openApprove := openMap[confMap[models.BusinessConfIsBIApprove]]
  918. // 查询对应分类是否有审批流
  919. flowOb := new(biapprove.BiApproveFlow)
  920. flowCond := fmt.Sprintf(` AND %s = ?`, biapprove.BiApproveFlowCols.ClassifyId)
  921. flowPars := make([]interface{}, 0)
  922. flowPars = append(flowPars, classifyId)
  923. flowItem, e := flowOb.GetItemByCondition(flowCond, flowPars, "")
  924. if e != nil {
  925. err = fmt.Errorf("ApproveFlow GetItemByCondition err: %s", e.Error())
  926. return
  927. }
  928. // 开启审批/有审批流
  929. if openApprove && (flowItem != nil) {
  930. opening = true
  931. return
  932. }
  933. return
  934. }
  935. // SubmitBiApprove 提交审批
  936. func SubmitBiApprove(biId int, biTitle string, classifyId int, sysAdminId int, sysAdminName string) (approveId int, err error) {
  937. // 默认内部审批, 如果是走的第三方审批, 那么仅修改状态
  938. confMap, e := models.GetBusinessConf()
  939. if e != nil {
  940. err = fmt.Errorf("GetBusinessConf err: %s", e.Error())
  941. return
  942. }
  943. openMap := map[string]bool{"false": false, "true": true}
  944. openApprove := openMap[confMap[models.BusinessConfIsBIApprove]]
  945. if !openApprove {
  946. err = fmt.Errorf("未开启审批")
  947. return
  948. }
  949. // 查询审批流
  950. flowOb := new(biapprove.BiApproveFlow)
  951. flowCond := fmt.Sprintf(` AND %s = ?`, biapprove.BiApproveFlowCols.ClassifyId)
  952. flowPars := make([]interface{}, 0)
  953. flowPars = append(flowPars, classifyId)
  954. flowItem, e := flowOb.GetItemByCondition(flowCond, flowPars, "")
  955. if e != nil {
  956. err = fmt.Errorf("ApproveFlow GetItemByCondition err: %s", e.Error())
  957. return
  958. }
  959. // 查询审批节点
  960. nodeOb := new(biapprove.BiApproveNode)
  961. nodeCond := fmt.Sprintf(` AND %s = ? AND %s = ?`, biapprove.BiApproveNodeCols.BiApproveFlowId, biapprove.BiApproveNodeCols.CurrVersion)
  962. nodePars := make([]interface{}, 0)
  963. nodePars = append(nodePars, flowItem.BiApproveFlowId, flowItem.CurrVersion)
  964. nodeItems, e := nodeOb.GetItemsByCondition(nodeCond, nodePars, []string{}, "")
  965. if e != nil {
  966. err = fmt.Errorf("ApproveNodes GetItemsByCondition err: %s", e.Error())
  967. return
  968. }
  969. if len(nodeItems) == 0 {
  970. err = fmt.Errorf("无审批节点")
  971. return
  972. }
  973. // 取出首个节点
  974. firstNodeItem := new(biapprove.BiApproveNode)
  975. for _, v := range nodeItems {
  976. if v.PrevNodeId == 0 {
  977. firstNodeItem = v
  978. continue
  979. }
  980. }
  981. if firstNodeItem == nil {
  982. err = fmt.Errorf("首个审批节点有误")
  983. return
  984. }
  985. // 审批信息
  986. now := time.Now().Local()
  987. newApprove := new(biapprove.BiApprove)
  988. newApprove.BiId = biId
  989. newApprove.BiTitle = biTitle
  990. newApprove.ClassifyId = classifyId
  991. newApprove.State = BiApproveStateApproving
  992. newApprove.FlowId = flowItem.BiApproveFlowId
  993. newApprove.FlowVersion = flowItem.CurrVersion
  994. newApprove.StartNodeId = firstNodeItem.BiApproveNodeId
  995. newApprove.CurrNodeId = firstNodeItem.BiApproveNodeId
  996. newApprove.ApplyUserId = sysAdminId
  997. newApprove.ApplyUserName = sysAdminName
  998. newApprove.CreateTime = now
  999. newApprove.ModifyTime = now
  1000. if e = newApprove.Create(); e != nil {
  1001. err = fmt.Errorf("生成审批信息失败, Err: %s", e.Error())
  1002. return
  1003. }
  1004. approveId = newApprove.BiApproveId
  1005. // 生成节点审批记录
  1006. err = BuildNextNodeRecordAndMsg(firstNodeItem, newApprove.BiApproveId, sysAdminId, sysAdminName, newApprove.BiTitle)
  1007. return
  1008. }
  1009. func toBiApproveItemOrmResp(src []*biapprove.BiApproveItemOrm) (res []*response.BiApproveItemOrmResp) {
  1010. for _, v := range src {
  1011. r := new(response.BiApproveItemOrmResp)
  1012. r.BiApproveId = v.BiApproveId
  1013. r.BiApproveRecordId = v.BiApproveRecordId
  1014. r.BiId = v.BiId
  1015. r.BiTitle = v.BiTitle
  1016. r.ClassifyId = v.ClassifyId
  1017. r.State = v.State
  1018. r.RecordState = v.RecordState
  1019. r.FlowId = v.FlowId
  1020. r.FlowVersion = v.FlowVersion
  1021. r.StartNodeId = v.StartNodeId
  1022. r.CurrNodeId = v.CurrNodeId
  1023. r.ApplyUserId = v.ApplyUserId
  1024. r.ApplyUserName = v.ApplyUserName
  1025. r.ApproveRemark = v.ApproveRemark
  1026. r.ApproveTime = utils.TimeTransferString(utils.FormatDateTime, v.ApproveTime)
  1027. r.HandleTime = utils.TimeTransferString(utils.FormatDateTime, v.HandleTime)
  1028. r.CreateTime = utils.TimeTransferString(utils.FormatDateTime, v.CreateTime)
  1029. r.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, v.ModifyTime)
  1030. r.NodeState = v.NodeState
  1031. res = append(res, r)
  1032. }
  1033. return
  1034. }
  1035. func GetBiClassifyAll() (list []*bi_dashboard.BiDashboardClassify, msg string, err error) {
  1036. ClassifyList, e := bi_dashboard.GetBiDashboardClassifyAllList()
  1037. if e != nil {
  1038. msg = "获取失败"
  1039. err = fmt.Errorf("GetBiDashboardClassifyAllList err: %s", e.Error())
  1040. return
  1041. }
  1042. list = ClassifyList
  1043. return
  1044. }
  1045. func CheckHasApprovingBi() (ok bool, err error) {
  1046. count, err := biapprove.GetBiApproveCountByState(BiApproveStateApproving)
  1047. if err != nil {
  1048. return
  1049. }
  1050. if count > 0 {
  1051. ok = true
  1052. }
  1053. return
  1054. }