classify.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. package services
  2. import (
  3. "errors"
  4. "eta_gn/eta_api/models"
  5. "eta_gn/eta_api/models/report_approve"
  6. "eta_gn/eta_api/utils"
  7. "fmt"
  8. "sort"
  9. "time"
  10. )
  11. // MoveReportClassify 移动分类
  12. func MoveReportClassify(req models.ClassifyMoveReq) (err error, errMsg string) {
  13. classifyId := req.ClassifyId
  14. prevClassifyId := req.PrevClassifyId
  15. nextClassifyId := req.NextClassifyId
  16. //如果有传入 上一个兄弟节点分类id
  17. var (
  18. classifyInfo *models.Classify
  19. prevClassify *models.Classify
  20. nextClassify *models.Classify
  21. prevSort int
  22. nextSort int
  23. )
  24. // 移动对象为分类, 判断权限
  25. classifyInfo, err = models.GetClassifyById(classifyId)
  26. if err != nil {
  27. if utils.IsErrNoRow(err) {
  28. errMsg = "分类不存在, 请刷新页面"
  29. err = fmt.Errorf("获取分类信息失败,Err:" + err.Error())
  30. return
  31. }
  32. errMsg = "移动失败"
  33. err = fmt.Errorf("获取分类信息失败,Err:" + err.Error())
  34. return
  35. } else if classifyInfo.Id == 0 {
  36. errMsg = "分类不存在, 请刷新页面"
  37. err = fmt.Errorf("获取分类信息失败,Err:" + err.Error())
  38. return
  39. }
  40. parentClassifyId := classifyInfo.ParentId
  41. if prevClassifyId > 0 {
  42. prevClassify, err = models.GetClassifyById(prevClassifyId)
  43. if err != nil {
  44. if utils.IsErrNoRow(err) {
  45. errMsg = "上一个分类不存在, 请刷新页面"
  46. err = fmt.Errorf("获取分类信息失败,Err:" + err.Error())
  47. return
  48. }
  49. errMsg = "移动失败"
  50. err = fmt.Errorf("获取上一个兄弟节点分类信息失败,Err:" + err.Error())
  51. return
  52. }
  53. if prevClassify.ParentId != parentClassifyId {
  54. errMsg = "禁止拖动到其他节点"
  55. err = fmt.Errorf(errMsg)
  56. return
  57. }
  58. prevSort = prevClassify.Sort
  59. }
  60. if nextClassifyId > 0 {
  61. //下一个兄弟节点
  62. nextClassify, err = models.GetClassifyById(nextClassifyId)
  63. if err != nil {
  64. if utils.IsErrNoRow(err) {
  65. errMsg = "下一个分类不存在, 请刷新页面"
  66. err = fmt.Errorf("获取分类信息失败,Err:" + err.Error())
  67. return
  68. }
  69. errMsg = "移动失败"
  70. err = fmt.Errorf("获取下一个兄弟节点分类信息失败,Err:" + err.Error())
  71. return
  72. }
  73. if nextClassify.ParentId != parentClassifyId {
  74. errMsg = "禁止拖动到其他节点"
  75. err = fmt.Errorf(errMsg)
  76. return
  77. }
  78. nextSort = nextClassify.Sort
  79. }
  80. err, errMsg = moveReportClassify(classifyInfo, prevClassify, nextClassify, parentClassifyId, prevSort, nextSort)
  81. return
  82. }
  83. // moveReportClassify 移动分类
  84. func moveReportClassify(classifyInfo, prevClassify, nextClassify *models.Classify, parentId, prevSort, nextSort int) (err error, errMsg string) {
  85. ob := new(models.Classify)
  86. updateCol := make([]string, 0)
  87. //判断上级id是否一致,如果不一致的话,那么需要移动该分类层级
  88. if classifyInfo.ParentId != parentId {
  89. errMsg = "移动失败"
  90. err = fmt.Errorf("不支持目录层级变更")
  91. return
  92. }
  93. if prevSort > 0 {
  94. //如果是移动在两个兄弟节点之间
  95. if nextSort > 0 {
  96. //下一个兄弟节点
  97. //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
  98. if prevSort == nextSort || prevSort == classifyInfo.Sort {
  99. //变更兄弟节点的排序
  100. updateSortStr := `sort + 2`
  101. //变更分类
  102. if prevClassify != nil {
  103. _ = models.UpdateClassifySortByParentId(parentId, prevClassify.Id, prevClassify.Sort, updateSortStr)
  104. } else {
  105. _ = models.UpdateClassifySortByParentId(parentId, 0, prevSort, updateSortStr)
  106. }
  107. } else {
  108. //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
  109. if nextSort-prevSort == 1 {
  110. //变更兄弟节点的排序
  111. updateSortStr := `sort + 1`
  112. //变更分类
  113. if prevClassify != nil {
  114. _ = models.UpdateClassifySortByParentId(parentId, prevClassify.Id, prevSort, updateSortStr)
  115. } else {
  116. _ = models.UpdateClassifySortByParentId(parentId, 0, prevSort, updateSortStr)
  117. }
  118. }
  119. }
  120. }
  121. classifyInfo.Sort = prevSort + 1
  122. classifyInfo.ModifyTime = time.Now()
  123. updateCol = append(updateCol, "Sort", "ModifyTime")
  124. } else if prevClassify == nil && nextClassify == nil && parentId > 0 {
  125. //处理只拖动到目录里,默认放到目录底部的情况
  126. var maxSort int
  127. maxSort, err = ob.GetMaxSortByParentId(parentId)
  128. if err != nil {
  129. errMsg = "移动失败"
  130. err = fmt.Errorf("查询组内排序信息失败,Err:" + err.Error())
  131. return
  132. }
  133. classifyInfo.Sort = maxSort + 1 //那就是排在组内最后一位
  134. classifyInfo.ModifyTime = time.Now()
  135. updateCol = append(updateCol, "Sort", "ModifyTime")
  136. } else {
  137. // 拖动到父级分类的第一位
  138. firstPermission, tmpErr := ob.GetFirstClassifyByParentId(parentId)
  139. if tmpErr != nil && !utils.IsErrNoRow(tmpErr) {
  140. errMsg = "移动失败"
  141. err = fmt.Errorf("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tmpErr.Error())
  142. return
  143. }
  144. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  145. if firstPermission != nil && firstPermission.Id > 0 && firstPermission.Sort == 0 {
  146. updateSortStr := ` sort + 1 `
  147. _ = models.UpdateClassifySortByParentId(parentId, firstPermission.Id-1, 0, updateSortStr)
  148. }
  149. classifyInfo.Sort = 0 //那就是排在第一位
  150. classifyInfo.ModifyTime = time.Now()
  151. updateCol = append(updateCol, "Sort", "ModifyTime")
  152. }
  153. //更新
  154. if len(updateCol) > 0 {
  155. err = classifyInfo.UpdateClassify(updateCol)
  156. if err != nil {
  157. errMsg = "移动失败"
  158. err = fmt.Errorf("修改失败,Err:" + err.Error())
  159. return
  160. }
  161. }
  162. return
  163. }
  164. // AddReportClassify
  165. // @Description: 添加报告分类
  166. // @author: Roc
  167. // @datetime 2024-06-17 11:01:21
  168. // @param classifyName string
  169. // @param parentId int
  170. // @param isRemind int
  171. // @param remindTime string
  172. // @return err error
  173. // @return errMsg string
  174. // @return isSendEmail bool
  175. func AddReportClassify(classifyName string, parentId int, classifyType, isRemind int, remindTime string) (err error, errMsg string, isSendEmail bool) {
  176. isSendEmail = true
  177. errMsg = `添加失败`
  178. item, err := models.GetClassifyByName(classifyName, parentId)
  179. if err != nil && !utils.IsErrNoRow(err) {
  180. errMsg = "获取分类信息失败"
  181. if utils.IsErrNoRow(err) {
  182. errMsg = "分类名称:" + classifyName + "已存在"
  183. isSendEmail = false
  184. }
  185. return
  186. }
  187. if item != nil && item.Id > 0 {
  188. errMsg = "分类名称:" + classifyName + "已存在"
  189. isSendEmail = false
  190. err = errors.New(errMsg)
  191. return
  192. }
  193. level := 1
  194. // 父级分类
  195. var parentClassifyItem *models.Classify
  196. // 父级分类下的子分类数量
  197. var childClassifyCount int
  198. if parentId > 0 {
  199. // 获取父级分类信息
  200. parentClassifyItem, err = models.GetClassifyById(parentId)
  201. if err != nil {
  202. errMsg = "获取父级分类信息失败"
  203. if utils.IsErrNoRow(err) {
  204. errMsg = "父级分类不存在"
  205. }
  206. return
  207. }
  208. level = parentClassifyItem.Level + 1
  209. if level > 3 {
  210. errMsg = "分类层级不可超过三级"
  211. isSendEmail = false
  212. return
  213. }
  214. // 判断是否分类存在待操作的审批单
  215. //err, errMsg = checkClassifyApprove(parentClassifyItem)
  216. //if err != nil {
  217. // return
  218. //}
  219. // 获取父级分类下的子分类数量
  220. childClassifyCount, err = models.GetCountClassifyChildByParentId(parentId)
  221. if err != nil {
  222. errMsg = "获取父级分类的子分类信息失败"
  223. return
  224. }
  225. }
  226. nowTime := time.Now().Local()
  227. classify := new(models.Classify)
  228. maxSort, err := classify.GetMaxSort()
  229. if err != nil {
  230. errMsg = "操作失败"
  231. err = errors.New("查询品种排序失败, Err: " + err.Error())
  232. return
  233. }
  234. classify.ClassifyName = classifyName
  235. classify.ParentId = parentId
  236. classify.CreateTime = nowTime
  237. classify.ModifyTime = nowTime
  238. classify.Sort = maxSort + 1
  239. classify.Enabled = 1
  240. classify.ShowType = 1 //默认列表格式
  241. classify.ReportDetailShowType = 1 //默认列表格式
  242. classify.IsShow = 1
  243. classify.Level = level
  244. classify.ClassifyType = classifyType
  245. classify.IsRemind = isRemind
  246. classify.RemindTime = remindTime
  247. err = models.AddClassify(classify)
  248. if err != nil {
  249. return
  250. }
  251. // 如果父级分类不为空的话,那么就标记有子级分类,同时
  252. if parentClassifyItem != nil {
  253. parentClassifyItem.HasChild = 1
  254. parentClassifyItem.UpdateClassify([]string{"HasChild"})
  255. // 如果以前没有子级分类,那么就继承父级分类下的章节类型(创建新的章节与分类的关系)
  256. if childClassifyCount <= 0 {
  257. // 继承父级分类下的章节类型(创建新的章节与分类的关系)
  258. tmpErr := inheritReportChapterType(parentId, classify.Id)
  259. if tmpErr != nil {
  260. return
  261. }
  262. // 继承父级分类审批流
  263. //go inheritReportApproveFlow(parentClassifyItem, classify)
  264. moveReportByAddClassify(parentClassifyItem, classify)
  265. }
  266. }
  267. return
  268. }
  269. // checkClassifyApprove
  270. // @Description: 判断分类是否存在待操作的审批单
  271. // @author: Roc
  272. // @datetime 2024-06-27 13:19:15
  273. // @param currClassify *models.Classify
  274. // @return err error
  275. // @return errMsg string
  276. func checkClassifyApprove(currClassify *models.Classify) (err error, errMsg string) {
  277. errMsg = `判断是否有审批流关联失败`
  278. var firstClassifyId, secondClassifyId int
  279. if currClassify.ParentId > 0 {
  280. parentClassifyItem, tmpErr := models.GetClassifyById(currClassify.ParentId)
  281. if tmpErr != nil {
  282. err = tmpErr
  283. errMsg = "获取父级分类信息失败"
  284. if utils.IsErrNoRow(tmpErr) {
  285. errMsg = "父级分类不存在"
  286. }
  287. return
  288. }
  289. firstClassifyId = parentClassifyItem.Id
  290. secondClassifyId = currClassify.Id
  291. } else {
  292. firstClassifyId = currClassify.Id
  293. }
  294. // 校验审批流是否关联了进行中的审批
  295. {
  296. flowOb := new(report_approve.ReportApproveFlow)
  297. existCond := fmt.Sprintf(` AND %s = ? AND %s = ? AND %s = ? AND %s = ?`, report_approve.ReportApproveFlowCols.ReportType, report_approve.ReportApproveFlowCols.ClassifyFirstId, report_approve.ReportApproveFlowCols.ClassifySecondId, report_approve.ReportApproveFlowCols.ClassifyThirdId)
  298. existPars := make([]interface{}, 0)
  299. existPars = append(existPars, report_approve.FlowReportTypeChinese, firstClassifyId, secondClassifyId, 0)
  300. flowItem, e := flowOb.GetItemByCondition(existCond, existPars, "")
  301. if e != nil {
  302. // 父级分类如果没有审批流,那么就正常进行就好了
  303. if !utils.IsErrNoRow(e) {
  304. err = errors.New("获取审批流是否已存在失败, Err: " + e.Error())
  305. return
  306. }
  307. err = nil
  308. return
  309. }
  310. if flowItem == nil {
  311. return
  312. }
  313. approvingOb := new(report_approve.ReportApprove)
  314. approvingCond := fmt.Sprintf(` AND %s = ? AND %s = ? AND %s = ?`, report_approve.ReportApproveCols.FlowId, report_approve.ReportApproveCols.FlowVersion, report_approve.ReportApproveCols.State)
  315. approvingPars := make([]interface{}, 0)
  316. approvingPars = append(approvingPars, flowItem.ReportApproveFlowId, flowItem.CurrVersion, report_approve.ReportApproveStateApproving)
  317. count, e := approvingOb.GetCountByCondition(approvingCond, approvingPars)
  318. if e != nil {
  319. err = errors.New("获取审批流关联进行中的审批数失败. Err: " + e.Error())
  320. return
  321. }
  322. if count > 0 {
  323. errMsg = "当前有未走完流程的报告,请走完流程后再做变更"
  324. err = errors.New(errMsg)
  325. return
  326. }
  327. }
  328. return
  329. }
  330. // 关于研报分类,因只允许报告或品种关联在最小分类下,所以当某个父分类(非三级分类)已关联报告或品种,需要在该父分类下增加子分类,则第一个子分类添加成功时,默认把该父分类关联的品种和报告转移至新创建的子分类(第一个子分类)下
  331. // moveReportByAddClassify
  332. // @Description: 报告和章节的转移
  333. // @author: Roc
  334. // @datetime 2024-06-17 16:29:56
  335. // @param parentClassifyInfo *models.Classify
  336. // @param currClassifyInfo *models.Classify
  337. // @return err error
  338. func moveReportByAddClassify(parentClassifyInfo, currClassifyInfo *models.Classify) (err error) {
  339. defer func() {
  340. if err != nil {
  341. utils.FileLog.Error(fmt.Sprint("历史报告更改分类失败,父级分类ID:", parentClassifyInfo.Id, ";当前分类ID:", currClassifyInfo.Id, ";错误信息:", err.Error()))
  342. }
  343. }()
  344. if currClassifyInfo.Level > 3 {
  345. err = errors.New("父级分类不支持三级分类以上")
  346. return
  347. }
  348. // 报告的分类归属调整,转为下一级的分类
  349. var condition, updateStr string
  350. pars := make([]interface{}, 0)
  351. switch currClassifyInfo.Level {
  352. case 3: // 当前分类是3级分类
  353. updateStr += ` classify_id_third = ?,classify_name_third = ?`
  354. condition += ` AND classify_id_second = ? `
  355. case 2: // 当前分类是2级分类
  356. updateStr += ` classify_id_second = ?,classify_name_second = ?`
  357. condition += ` AND classify_id_first = ? `
  358. default:
  359. err = errors.New("错误的分类层级")
  360. return
  361. }
  362. pars = append(pars, currClassifyInfo.Id, currClassifyInfo.ClassifyName, parentClassifyInfo.Id)
  363. // 获取当前分类下的所有章节类型
  364. currReportChapterTypeList, err := models.GetAllReportChapterTypeListByClassifyId(currClassifyInfo.Id)
  365. if err != nil {
  366. return
  367. }
  368. // 当前的章节类型ID ---> 继承的章节类型ID
  369. chapterTypeIdMap := make(map[int]int)
  370. for _, v := range currReportChapterTypeList {
  371. chapterTypeIdMap[v.ReportChapterTypeId] = v.InheritReportChapterTypeId
  372. }
  373. // 报告转移后,历史章节报告中的type_id也要修复成最新的type_id
  374. err = models.ModifyReportClassifyAndReportChapterTypeByCondition(condition, pars, updateStr, chapterTypeIdMap, parentClassifyInfo.Id, currClassifyInfo.Id, currClassifyInfo.ClassifyName)
  375. if err != nil {
  376. return
  377. }
  378. return
  379. }
  380. // inheritReportChapterType
  381. // @Description: 继承父级分类下的章节类型
  382. // @author: Roc
  383. // @datetime 2024-06-17 14:41:04
  384. // @param parentClassifyId int
  385. // @param currClassifyId int
  386. // @return err error
  387. func inheritReportChapterType(parentClassifyId, currClassifyId int) (err error) {
  388. defer func() {
  389. if err != nil {
  390. utils.FileLog.Error(fmt.Sprint("继承父级分类下的章节类型失败,父级分类ID:", parentClassifyId, ";当前分类ID:", currClassifyId, ";错误信息:", err.Error()))
  391. }
  392. }()
  393. parentReportChapterTypeList, err := models.GetAllReportChapterTypeListByClassifyId(parentClassifyId)
  394. if err != nil {
  395. return
  396. }
  397. // 如果没有章节类型,那么就直接返回
  398. if len(parentReportChapterTypeList) <= 0 {
  399. return
  400. }
  401. addList := make([]*models.ReportChapterType, 0)
  402. for _, v := range parentReportChapterTypeList {
  403. addList = append(addList, &models.ReportChapterType{
  404. //ReportChapterTypeId: 0,
  405. ReportChapterTypeKey: v.ReportChapterTypeKey,
  406. ReportChapterTypeThumb: v.ReportChapterTypeThumb,
  407. BannerUrl: v.BannerUrl,
  408. ReportChapterTypeName: v.ReportChapterTypeName,
  409. Sort: v.Sort,
  410. Enabled: v.Enabled,
  411. CreatedTime: time.Now(),
  412. LastUpdatedTime: time.Now(),
  413. ResearchType: v.ResearchType,
  414. SelectedImage: v.SelectedImage,
  415. UnselectedImage: v.UnselectedImage,
  416. PcSelectedImage: v.PcSelectedImage,
  417. PcUnselectedImage: v.PcUnselectedImage,
  418. EditImgUrl: v.EditImgUrl,
  419. TickerTitle: v.TickerTitle,
  420. IsShow: v.IsShow,
  421. PauseStartTime: v.PauseStartTime,
  422. PauseEndTime: v.PauseEndTime,
  423. IsSet: v.IsSet,
  424. YbIconUrl: v.YbIconUrl,
  425. YbBottomIcon: v.YbBottomIcon,
  426. ReportClassifyId: currClassifyId,
  427. InheritReportChapterTypeId: v.ReportChapterTypeId,
  428. })
  429. }
  430. obj := models.ReportChapterType{}
  431. err = obj.MultiCreate(addList)
  432. // 修改CRM权限
  433. go func() {
  434. var syncReq ChapterTypeSyncReq
  435. _, _ = ReportChapterTypeSync(&syncReq)
  436. }()
  437. return
  438. }
  439. // inheritReportApproveFlow
  440. // @Description: 继承父级分类下的审批流
  441. // @author: Roc
  442. // @datetime 2024-06-17 14:41:04
  443. // @param parentClassifyId int
  444. // @param currClassifyId int
  445. // @return err error
  446. func inheritReportApproveFlow(parentClassifyItem, currClassifyItem *models.Classify) (err error) {
  447. defer func() {
  448. if err != nil {
  449. utils.FileLog.Error(fmt.Sprint("继承父级分类下的审批流失败,父级分类ID:", parentClassifyItem.Id, ";当前分类ID:", currClassifyItem.Id, ";错误信息:", err.Error()))
  450. }
  451. }()
  452. var firstClassify, secondClassify, thirdClassify *models.Classify
  453. if parentClassifyItem.ParentId > 0 {
  454. // 获取父级分类信息
  455. firstClassify, err = models.GetClassifyById(parentClassifyItem.ParentId)
  456. if err != nil {
  457. return
  458. }
  459. secondClassify = parentClassifyItem
  460. thirdClassify = currClassifyItem
  461. } else {
  462. firstClassify = parentClassifyItem
  463. secondClassify = currClassifyItem
  464. }
  465. flowObj := report_approve.ReportApproveFlow{}
  466. // 获取父级的审批流
  467. existCond := fmt.Sprintf(` AND %s = ? AND %s = ? `, report_approve.ReportApproveFlowCols.ReportType, report_approve.ReportApproveFlowCols.ClassifyFirstId)
  468. existPars := make([]interface{}, 0)
  469. existPars = append(existPars, report_approve.FlowReportTypeChinese, firstClassify.Id)
  470. // 如果这是第三级,那么说明只需要查找第二级的审批配置就好了
  471. if thirdClassify != nil {
  472. existCond = fmt.Sprintf(`%s AND %s = ?`, existCond, report_approve.ReportApproveFlowCols.ClassifySecondId)
  473. existPars = append(existPars, secondClassify.Id)
  474. }
  475. parentFlow, err := flowObj.GetItemByCondition(existCond, existPars, "")
  476. if err != nil {
  477. // 如果没有配置审批流,那么就直接返回
  478. if utils.IsErrNoRow(err) {
  479. err = nil
  480. }
  481. return
  482. }
  483. // 获取父级的审批节点
  484. nodeObj := report_approve.ReportApproveNode{}
  485. nodeCond := fmt.Sprintf(` AND %s = ? AND %s = ?`, report_approve.ReportApproveNodeCols.ReportApproveFlowId, report_approve.ReportApproveNodeCols.CurrVersion)
  486. nodePars := make([]interface{}, 0)
  487. nodePars = append(nodePars, parentFlow.ReportApproveFlowId, parentFlow.CurrVersion)
  488. parentNodeList, err := nodeObj.GetItemsByCondition(nodeCond, nodePars, []string{}, "")
  489. if err != nil {
  490. return
  491. }
  492. // 新审批流
  493. currFlow := &report_approve.ReportApproveFlow{
  494. ReportApproveFlowId: 0,
  495. FlowName: currClassifyItem.ClassifyName,
  496. ReportType: parentFlow.ReportType,
  497. ClassifyFirstId: firstClassify.Id,
  498. ClassifySecondId: secondClassify.Id,
  499. //ClassifyThirdId: 0,
  500. CurrVersion: 1,
  501. Enabled: 1,
  502. CreateTime: time.Now().Local(),
  503. ModifyTime: time.Now().Local(),
  504. }
  505. if thirdClassify != nil {
  506. currFlow.ClassifyThirdId = thirdClassify.Id
  507. }
  508. // 新审批流的节点
  509. nodeItems := make([]*report_approve.ReportApproveNode, 0)
  510. for _, v := range parentNodeList {
  511. n := &report_approve.ReportApproveNode{
  512. //ReportApproveNodeId: 0,
  513. //ReportApproveFlowId: 0,
  514. PrevNodeId: 0,
  515. NextNodeId: 0,
  516. NodeType: v.NodeType,
  517. ApproveType: v.ApproveType,
  518. Users: v.Users,
  519. CurrVersion: 1,
  520. CreateTime: time.Now().Local(),
  521. }
  522. nodeItems = append(nodeItems, n)
  523. }
  524. // 新增审批流和节点
  525. err = flowObj.CreateFlowAndNodes(currFlow, nodeItems)
  526. if err != nil {
  527. return
  528. }
  529. parentFlow.Enabled = 0
  530. err = parentFlow.Update([]string{"Enabled"})
  531. return
  532. }
  533. // EditReportClassify
  534. // @Description: 编辑报告分类
  535. // @author: Roc
  536. // @datetime 2024-06-17 13:31:33
  537. // @param classifyId int
  538. // @param classifyName string
  539. // @param isRemind int
  540. // @param remindTime string
  541. // @return err error
  542. // @return errMsg string
  543. // @return isSendEmail bool
  544. func EditReportClassify(classifyId int, classifyName string, isRemind int, remindTime string) (err error, errMsg string, isSendEmail bool) {
  545. isSendEmail = true
  546. errMsg = `修改失败`
  547. item, err := models.GetClassifyById(classifyId)
  548. if err != nil {
  549. errMsg = "获取分类信息失败"
  550. if utils.IsErrNoRow(err) {
  551. errMsg = "分类不存在, 或已被删除"
  552. isSendEmail = false
  553. }
  554. return
  555. }
  556. //originName := item.ClassifyName
  557. // 重名校验
  558. existName, e := models.GetClassifyByName(classifyName, item.ParentId)
  559. if e != nil && !utils.IsErrNoRow(e) {
  560. errMsg = "获取信息失败"
  561. err = errors.New("获取重名分类失败, Err: " + err.Error())
  562. return
  563. }
  564. if existName != nil && existName.Id > 0 && existName.Id != item.Id {
  565. errMsg = "分类名称:" + classifyName + "已存在"
  566. err = errors.New(errMsg)
  567. isSendEmail = false
  568. return
  569. }
  570. item.ClassifyName = classifyName
  571. item.IsRemind = isRemind
  572. item.RemindTime = remindTime
  573. item.ModifyTime = time.Now().Local()
  574. cols := make([]string, 0)
  575. cols = append(cols, "ClassifyName", "IsRemind", "RemindTime", "ModifyTime")
  576. err = item.UpdateClassify(cols)
  577. if err != nil {
  578. return
  579. }
  580. return
  581. }
  582. // GetClassifyTreeRecursive 递归获取分类树形结构
  583. func GetClassifyTreeRecursive(list []*models.ClassifyItem, parentId int) []*models.ClassifyItem {
  584. res := make([]*models.ClassifyItem, 0)
  585. for _, v := range list {
  586. if v.ParentId == parentId {
  587. v.Child = GetClassifyTreeRecursive(list, v.Id)
  588. res = append(res, v)
  589. }
  590. }
  591. return res
  592. }
  593. // GetParentClassifyListByParentIdList
  594. // @Description: 递归获取父级分类信息,正常来讲只有三次
  595. // @author: Roc
  596. // @datetime 2024-06-19 13:23:33
  597. // @param parentClassifyIdList []int
  598. // @return list []*models.ClassifyList
  599. // @return err error
  600. func GetParentClassifyListByParentIdList(parentClassifyIdList []int) (list []*models.ClassifyList, err error) {
  601. num := len(parentClassifyIdList)
  602. if num <= 0 {
  603. return
  604. }
  605. list, err = models.GetClassifyListByParentIdList(parentClassifyIdList)
  606. if err != nil {
  607. return
  608. }
  609. // 是否还有上级
  610. {
  611. currParentClassifyIdList := make([]int, 0)
  612. for _, v := range list {
  613. if v.ParentId > 0 {
  614. currParentClassifyIdList = append(currParentClassifyIdList, v.ParentId)
  615. }
  616. }
  617. if len(currParentClassifyIdList) > 0 {
  618. tmpList, tmpErr := GetParentClassifyListByParentIdList(currParentClassifyIdList)
  619. if tmpErr != nil {
  620. err = tmpErr
  621. return
  622. }
  623. list = append(tmpList, list...)
  624. }
  625. }
  626. return
  627. }
  628. // GetClassifyListTreeRecursive
  629. // @Description: 递归获取分类树形结构
  630. // @author: Roc
  631. // @datetime 2024-06-19 13:23:28
  632. // @param list []*models.ClassifyList
  633. // @param parentId int
  634. // @return []*models.ClassifyList
  635. func GetClassifyListTreeRecursive(list []*models.ClassifyList, parentId int) []*models.ClassifyList {
  636. res := make([]*models.ClassifyList, 0)
  637. for _, v := range list {
  638. if v.ParentId == parentId {
  639. v.Child = GetClassifyListTreeRecursive(list, v.Id)
  640. res = append(res, v)
  641. }
  642. }
  643. // 前端的JP需要我这么返回
  644. if len(res) <= 0 {
  645. res = nil
  646. }
  647. return res
  648. }
  649. // BySortAndCreateTime 用来排序,先按Sort字段升序排序,若Sort相同,则按照CreateTime字段升序排序。
  650. type BySortAndCreateTime []*models.ClassifyList
  651. func (a BySortAndCreateTime) Len() int {
  652. return len(a)
  653. }
  654. func (a BySortAndCreateTime) Swap(i, j int) {
  655. a[i], a[j] = a[j], a[i]
  656. }
  657. func (a BySortAndCreateTime) Less(i, j int) bool {
  658. if a[i].Sort == a[j].Sort {
  659. return a[i].CreateTime.Before(a[j].CreateTime)
  660. }
  661. return a[i].Sort < a[j].Sort
  662. }
  663. // SortClassifyListBySortAndCreateTime sorts the ClassifyList slice by Sort and then CreateTime in ascending order.
  664. func SortClassifyListBySortAndCreateTime(classifyList []*models.ClassifyList) {
  665. sort.Sort(BySortAndCreateTime(classifyList))
  666. }
  667. // GetClassifyChildIdsTreeRecursive 递归获取分类所有子分类ID
  668. func GetClassifyChildIdsTreeRecursive(list []*models.Classify, parentId int) []int {
  669. var res []int
  670. for _, v := range list {
  671. if v.ParentId == parentId {
  672. res = append(res, v.Id)
  673. // 子分类的子类
  674. childIds := GetClassifyChildIdsTreeRecursive(list, v.Id)
  675. if len(childIds) > 0 {
  676. res = append(res, childIds...)
  677. }
  678. }
  679. }
  680. return res
  681. }