approve.go 41 KB

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