classify.go 26 KB

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