classify.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  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, visibleUserIds []int) (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, parentId, visibleUserIds)
  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. // err = models.UpdateClassifyVisible(classify.Id, visibleUserIds)
  269. // if err != nil {
  270. // errMsg = "添加分类可见权限失败"
  271. // return
  272. // }
  273. }
  274. }
  275. // 更新分类的LevelPath
  276. if parentId > 0 {
  277. levelPath = fmt.Sprintf("%s,%d", levelPath, classify.Id)
  278. }
  279. if parentId == 0 {
  280. levelPath = strconv.Itoa(classify.Id)
  281. }
  282. classify.LevelPath = levelPath
  283. if e := classify.UpdateClassify([]string{"LevelPath"}); e != nil {
  284. err = fmt.Errorf("更新分类层级路径失败, %v", e)
  285. return
  286. }
  287. return
  288. }
  289. // inheritParentClassifyVisibleUser 继承父类的可见用户
  290. // func inheritParentClassifyVisibleUser(parentClassifyId, currClassifyId int) (err error) {
  291. // parentUserIds, err := models.GetClassifyVisibleUserIdByClassifyId(parentClassifyId)
  292. // if err != nil {
  293. // return
  294. // }
  295. // currUserIds, err := models.GetClassifyVisibleUserIdByClassifyId(currClassifyId)
  296. // if err != nil {
  297. // return
  298. // }
  299. // parentUserIds = append(parentUserIds, currUserIds...)
  300. // err = models.ExtendClassifyVisible(parentClassifyId, currClassifyId, parentUserIds)
  301. // return
  302. // }
  303. // checkClassifyApprove
  304. // @Description: 判断分类是否存在待操作的审批单
  305. // @author: Roc
  306. // @datetime 2024-06-27 13:19:15
  307. // @param currClassify *models.Classify
  308. // @return err error
  309. // @return errMsg string
  310. func checkClassifyApprove(currClassify *models.Classify) (err error, errMsg string) {
  311. errMsg = `判断是否有审批流关联失败`
  312. var firstClassifyId, secondClassifyId int
  313. if currClassify.ParentId > 0 {
  314. parentClassifyItem, tmpErr := models.GetClassifyById(currClassify.ParentId)
  315. if tmpErr != nil {
  316. err = tmpErr
  317. errMsg = "获取父级分类信息失败"
  318. if utils.IsErrNoRow(tmpErr) {
  319. errMsg = "父级分类不存在"
  320. }
  321. return
  322. }
  323. firstClassifyId = parentClassifyItem.Id
  324. secondClassifyId = currClassify.Id
  325. } else {
  326. firstClassifyId = currClassify.Id
  327. }
  328. // 校验审批流是否关联了进行中的审批
  329. {
  330. flowOb := new(report_approve.ReportApproveFlow)
  331. 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)
  332. existPars := make([]interface{}, 0)
  333. existPars = append(existPars, report_approve.FlowReportTypeChinese, firstClassifyId, secondClassifyId, 0)
  334. flowItem, e := flowOb.GetItemByCondition(existCond, existPars, "")
  335. if e != nil {
  336. // 父级分类如果没有审批流,那么就正常进行就好了
  337. if !utils.IsErrNoRow(e) {
  338. err = errors.New("获取审批流是否已存在失败, Err: " + e.Error())
  339. return
  340. }
  341. err = nil
  342. return
  343. }
  344. if flowItem == nil {
  345. return
  346. }
  347. approvingOb := new(report_approve.ReportApprove)
  348. approvingCond := fmt.Sprintf(` AND %s = ? AND %s = ? AND %s = ?`, report_approve.ReportApproveCols.FlowId, report_approve.ReportApproveCols.FlowVersion, report_approve.ReportApproveCols.State)
  349. approvingPars := make([]interface{}, 0)
  350. approvingPars = append(approvingPars, flowItem.ReportApproveFlowId, flowItem.CurrVersion, report_approve.ReportApproveStateApproving)
  351. count, e := approvingOb.GetCountByCondition(approvingCond, approvingPars)
  352. if e != nil {
  353. err = errors.New("获取审批流关联进行中的审批数失败. Err: " + e.Error())
  354. return
  355. }
  356. if count > 0 {
  357. errMsg = "当前有未走完流程的报告,请走完流程后再做变更"
  358. err = errors.New(errMsg)
  359. return
  360. }
  361. }
  362. return
  363. }
  364. // 关于研报分类,因只允许报告或品种关联在最小分类下,所以当某个父分类(非三级分类)已关联报告或品种,需要在该父分类下增加子分类,则第一个子分类添加成功时,默认把该父分类关联的品种和报告转移至新创建的子分类(第一个子分类)下
  365. // moveReportByAddClassify
  366. // @Description: 报告和章节的转移
  367. // @author: Roc
  368. // @datetime 2024-06-17 16:29:56
  369. // @param parentClassifyInfo *models.Classify
  370. // @param currClassifyInfo *models.Classify
  371. // @return err error
  372. func moveReportByAddClassify(parentClassifyInfo, currClassifyInfo *models.Classify) (err error) {
  373. defer func() {
  374. if err != nil {
  375. utils.FileLog.Error(fmt.Sprint("历史报告更改分类失败,父级分类ID:", parentClassifyInfo.Id, ";当前分类ID:", currClassifyInfo.Id, ";错误信息:", err.Error()))
  376. }
  377. }()
  378. if currClassifyInfo.Level > 3 {
  379. err = errors.New("父级分类不支持三级分类以上")
  380. return
  381. }
  382. // 报告的分类归属调整,转为下一级的分类
  383. var classifyIdFirst, classifyIdSecond, classifyIdThird, originIdFirst, originIdSecond int
  384. var condition, updateStr string
  385. pars := make([]interface{}, 0)
  386. switch currClassifyInfo.Level {
  387. case 3: // 当前分类是3级分类
  388. updateStr += ` classify_id_third = ?,classify_name_third = ?`
  389. condition += ` AND classify_id_second = ? `
  390. classifyIdFirst = parentClassifyInfo.ParentId
  391. classifyIdSecond = parentClassifyInfo.Id
  392. classifyIdThird = currClassifyInfo.Id
  393. originIdFirst = classifyIdFirst
  394. originIdSecond = classifyIdSecond
  395. case 2: // 当前分类是2级分类
  396. updateStr += ` classify_id_second = ?,classify_name_second = ?`
  397. condition += ` AND classify_id_first = ? `
  398. classifyIdFirst = parentClassifyInfo.Id
  399. classifyIdSecond = currClassifyInfo.Id
  400. classifyIdThird = 0
  401. originIdFirst = classifyIdFirst
  402. originIdSecond = 0
  403. default:
  404. err = errors.New("错误的分类层级")
  405. return
  406. }
  407. pars = append(pars, currClassifyInfo.Id, currClassifyInfo.ClassifyName, parentClassifyInfo.Id)
  408. // 获取当前分类下的所有章节类型
  409. currReportChapterTypeList, err := models.GetAllReportChapterTypeListByClassifyId(currClassifyInfo.Id)
  410. if err != nil {
  411. return
  412. }
  413. // 当前的章节类型ID ---> 继承的章节类型ID
  414. chapterTypeIdMap := make(map[int]int)
  415. for _, v := range currReportChapterTypeList {
  416. chapterTypeIdMap[v.ReportChapterTypeId] = v.InheritReportChapterTypeId
  417. }
  418. // 报告转移后,历史章节报告中的type_id也要修复成最新的type_id
  419. err = models.ModifyReportClassifyAndReportChapterTypeByCondition(condition, pars, updateStr, chapterTypeIdMap, parentClassifyInfo.Id, currClassifyInfo.Id, currClassifyInfo.ClassifyName)
  420. if err != nil {
  421. return
  422. }
  423. // 分类ID-NAME
  424. classifyOb := new(models.Classify)
  425. classifies, e := classifyOb.GetItemsByCondition(``, make([]interface{}, 0), []string{}, "")
  426. if e != nil {
  427. err = fmt.Errorf("获取分类列表失败, %v", e)
  428. return
  429. }
  430. classifyIdName := make(map[int]string)
  431. for _, v := range classifies {
  432. classifyIdName[v.Id] = v.ClassifyName
  433. }
  434. // 转移上级分类下的所有报告至该分类,区分研报和PPT
  435. if currClassifyInfo.ClassifyType == utils.ReportTypeDefault {
  436. if e := models.MoveReportClassify(classifyIdFirst, classifyIdSecond, classifyIdThird, classifyIdName[classifyIdFirst], classifyIdName[classifyIdSecond], classifyIdName[classifyIdThird], originIdFirst, originIdSecond); e != nil {
  437. err = fmt.Errorf("转移研报分类失败, %v", e)
  438. return
  439. }
  440. }
  441. if currClassifyInfo.ClassifyType == utils.ReportTypePPT {
  442. if e := models.MovePptReportClassify(currClassifyInfo.Id, parentClassifyInfo.Id); e != nil {
  443. err = fmt.Errorf("转移PPT研报分类失败, %v", e)
  444. return
  445. }
  446. }
  447. // 更新分类报告计数
  448. go UpdateClassifyReportNum(currClassifyInfo.Id)
  449. return
  450. }
  451. // inheritReportChapterType
  452. // @Description: 继承父级分类下的章节类型
  453. // @author: Roc
  454. // @datetime 2024-06-17 14:41:04
  455. // @param parentClassifyId int
  456. // @param currClassifyId int
  457. // @return err error
  458. func inheritReportChapterType(parentClassifyId, currClassifyId int) (err error) {
  459. defer func() {
  460. if err != nil {
  461. utils.FileLog.Error(fmt.Sprint("继承父级分类下的章节类型失败,父级分类ID:", parentClassifyId, ";当前分类ID:", currClassifyId, ";错误信息:", err.Error()))
  462. }
  463. }()
  464. parentReportChapterTypeList, err := models.GetAllReportChapterTypeListByClassifyId(parentClassifyId)
  465. if err != nil {
  466. return
  467. }
  468. // 如果没有章节类型,那么就直接返回
  469. if len(parentReportChapterTypeList) <= 0 {
  470. return
  471. }
  472. addList := make([]*models.ReportChapterType, 0)
  473. for _, v := range parentReportChapterTypeList {
  474. addList = append(addList, &models.ReportChapterType{
  475. //ReportChapterTypeId: 0,
  476. ReportChapterTypeKey: v.ReportChapterTypeKey,
  477. ReportChapterTypeThumb: v.ReportChapterTypeThumb,
  478. BannerUrl: v.BannerUrl,
  479. ReportChapterTypeName: v.ReportChapterTypeName,
  480. Sort: v.Sort,
  481. Enabled: v.Enabled,
  482. CreatedTime: time.Now(),
  483. LastUpdatedTime: time.Now(),
  484. ResearchType: v.ResearchType,
  485. SelectedImage: v.SelectedImage,
  486. UnselectedImage: v.UnselectedImage,
  487. PcSelectedImage: v.PcSelectedImage,
  488. PcUnselectedImage: v.PcUnselectedImage,
  489. EditImgUrl: v.EditImgUrl,
  490. TickerTitle: v.TickerTitle,
  491. IsShow: v.IsShow,
  492. PauseStartTime: v.PauseStartTime,
  493. PauseEndTime: v.PauseEndTime,
  494. IsSet: v.IsSet,
  495. YbIconUrl: v.YbIconUrl,
  496. YbBottomIcon: v.YbBottomIcon,
  497. ReportClassifyId: currClassifyId,
  498. InheritReportChapterTypeId: v.ReportChapterTypeId,
  499. })
  500. }
  501. obj := models.ReportChapterType{}
  502. err = obj.MultiCreate(addList)
  503. // 修改CRM权限
  504. go func() {
  505. var syncReq ChapterTypeSyncReq
  506. _, _ = ReportChapterTypeSync(&syncReq)
  507. }()
  508. return
  509. }
  510. // inheritReportApproveFlow
  511. // @Description: 继承父级分类下的审批流
  512. // @author: Roc
  513. // @datetime 2024-06-17 14:41:04
  514. // @param parentClassifyId int
  515. // @param currClassifyId int
  516. // @return err error
  517. func inheritReportApproveFlow(parentClassifyItem, currClassifyItem *models.Classify) (err error) {
  518. defer func() {
  519. if err != nil {
  520. utils.FileLog.Error(fmt.Sprint("继承父级分类下的审批流失败,父级分类ID:", parentClassifyItem.Id, ";当前分类ID:", currClassifyItem.Id, ";错误信息:", err.Error()))
  521. }
  522. }()
  523. var firstClassify, secondClassify, thirdClassify *models.Classify
  524. if parentClassifyItem.ParentId > 0 {
  525. // 获取父级分类信息
  526. firstClassify, err = models.GetClassifyById(parentClassifyItem.ParentId)
  527. if err != nil {
  528. return
  529. }
  530. secondClassify = parentClassifyItem
  531. thirdClassify = currClassifyItem
  532. } else {
  533. firstClassify = parentClassifyItem
  534. secondClassify = currClassifyItem
  535. }
  536. flowObj := report_approve.ReportApproveFlow{}
  537. // 获取父级的审批流
  538. existCond := fmt.Sprintf(` AND %s = ? AND %s = ? `, report_approve.ReportApproveFlowCols.ReportType, report_approve.ReportApproveFlowCols.ClassifyFirstId)
  539. existPars := make([]interface{}, 0)
  540. existPars = append(existPars, report_approve.FlowReportTypeChinese, firstClassify.Id)
  541. // 如果这是第三级,那么说明只需要查找第二级的审批配置就好了
  542. if thirdClassify != nil {
  543. existCond = fmt.Sprintf(`%s AND %s = ?`, existCond, report_approve.ReportApproveFlowCols.ClassifySecondId)
  544. existPars = append(existPars, secondClassify.Id)
  545. }
  546. parentFlow, err := flowObj.GetItemByCondition(existCond, existPars, "")
  547. if err != nil {
  548. // 如果没有配置审批流,那么就直接返回
  549. if utils.IsErrNoRow(err) {
  550. err = nil
  551. }
  552. return
  553. }
  554. // 获取父级的审批节点
  555. nodeObj := report_approve.ReportApproveNode{}
  556. nodeCond := fmt.Sprintf(` AND %s = ? AND %s = ?`, report_approve.ReportApproveNodeCols.ReportApproveFlowId, report_approve.ReportApproveNodeCols.CurrVersion)
  557. nodePars := make([]interface{}, 0)
  558. nodePars = append(nodePars, parentFlow.ReportApproveFlowId, parentFlow.CurrVersion)
  559. parentNodeList, err := nodeObj.GetItemsByCondition(nodeCond, nodePars, []string{}, "")
  560. if err != nil {
  561. return
  562. }
  563. // 新审批流
  564. currFlow := &report_approve.ReportApproveFlow{
  565. ReportApproveFlowId: 0,
  566. FlowName: currClassifyItem.ClassifyName,
  567. ReportType: parentFlow.ReportType,
  568. ClassifyFirstId: firstClassify.Id,
  569. ClassifySecondId: secondClassify.Id,
  570. //ClassifyThirdId: 0,
  571. CurrVersion: 1,
  572. Enabled: 1,
  573. CreateTime: time.Now().Local(),
  574. ModifyTime: time.Now().Local(),
  575. }
  576. if thirdClassify != nil {
  577. currFlow.ClassifyThirdId = thirdClassify.Id
  578. }
  579. // 新审批流的节点
  580. nodeItems := make([]*report_approve.ReportApproveNode, 0)
  581. for _, v := range parentNodeList {
  582. n := &report_approve.ReportApproveNode{
  583. //ReportApproveNodeId: 0,
  584. //ReportApproveFlowId: 0,
  585. PrevNodeId: 0,
  586. NextNodeId: 0,
  587. NodeType: v.NodeType,
  588. ApproveType: v.ApproveType,
  589. Users: v.Users,
  590. CurrVersion: 1,
  591. CreateTime: time.Now().Local(),
  592. }
  593. nodeItems = append(nodeItems, n)
  594. }
  595. // 新增审批流和节点
  596. err = flowObj.CreateFlowAndNodes(currFlow, nodeItems)
  597. if err != nil {
  598. return
  599. }
  600. parentFlow.Enabled = 0
  601. err = parentFlow.Update([]string{"Enabled"})
  602. return
  603. }
  604. // EditReportClassify
  605. // @Description: 编辑报告分类
  606. // @author: Roc
  607. // @datetime 2024-06-17 13:31:33
  608. // @param classifyId int
  609. // @param classifyName string
  610. // @param isRemind int
  611. // @param remindTime string
  612. // @return err error
  613. // @return errMsg string
  614. // @return isSendEmail bool
  615. func EditReportClassify(classifyId int, classifyName string, isRemind int, remindTime string, visibleUserIds []int) (err error, errMsg string, isSendEmail bool) {
  616. isSendEmail = true
  617. errMsg = `修改失败`
  618. item, err := models.GetClassifyById(classifyId)
  619. if err != nil {
  620. errMsg = "获取分类信息失败"
  621. if utils.IsErrNoRow(err) {
  622. errMsg = "分类不存在, 或已被删除"
  623. isSendEmail = false
  624. }
  625. return
  626. }
  627. //originName := item.ClassifyName
  628. // 分类层级路径
  629. if item.ParentId == 0 {
  630. item.LevelPath = strconv.Itoa(item.Id)
  631. }
  632. if item.ParentId > 0 {
  633. classifyParent, e := models.GetClassifyById(item.ParentId)
  634. if e != nil {
  635. errMsg = "父级分类有误"
  636. err = fmt.Errorf("获取父级分类失败, %v", e)
  637. return
  638. }
  639. item.LevelPath = fmt.Sprintf("%s,%d", classifyParent.LevelPath, item.Id)
  640. }
  641. // 重名校验
  642. existName, e := models.GetClassifyByName(classifyName, item.ParentId)
  643. if e != nil && !utils.IsErrNoRow(e) {
  644. errMsg = "获取信息失败"
  645. err = errors.New("获取重名分类失败, Err: " + err.Error())
  646. return
  647. }
  648. if existName != nil && existName.Id > 0 && existName.Id != item.Id {
  649. errMsg = "分类名称:" + classifyName + "已存在"
  650. err = errors.New(errMsg)
  651. isSendEmail = false
  652. return
  653. }
  654. item.ClassifyName = classifyName
  655. item.IsRemind = isRemind
  656. item.RemindTime = remindTime
  657. item.ModifyTime = time.Now().Local()
  658. cols := make([]string, 0)
  659. cols = append(cols, "ClassifyName", "IsRemind", "RemindTime", "ModifyTime", "LevelPath")
  660. err = item.UpdateClassify(cols)
  661. if err != nil {
  662. return
  663. }
  664. err = models.UpdateClassifyVisible(item.Id, visibleUserIds)
  665. if err != nil {
  666. errMsg = "更新可见权限失败"
  667. return
  668. }
  669. return
  670. }
  671. // GetClassifyTreeRecursive 递归获取分类树形结构
  672. func GetClassifyTreeRecursive(list []*models.ClassifyItem, parentId int) []*models.ClassifyItem {
  673. res := make([]*models.ClassifyItem, 0)
  674. for _, v := range list {
  675. if v.ParentId == parentId {
  676. v.Child = GetClassifyTreeRecursive(list, v.Id)
  677. res = append(res, v)
  678. }
  679. }
  680. return res
  681. }
  682. // GetParentClassifyListByParentIdList
  683. // @Description: 递归获取父级分类信息,正常来讲只有三次
  684. // @author: Roc
  685. // @datetime 2024-06-19 13:23:33
  686. // @param parentClassifyIdList []int
  687. // @return list []*models.ClassifyList
  688. // @return err error
  689. func GetParentClassifyListByParentIdList(parentClassifyIdList []int) (list []*models.ClassifyList, err error) {
  690. num := len(parentClassifyIdList)
  691. if num <= 0 {
  692. return
  693. }
  694. list, err = models.GetClassifyListByParentIdList(parentClassifyIdList)
  695. if err != nil {
  696. return
  697. }
  698. // 是否还有上级
  699. {
  700. currParentClassifyIdList := make([]int, 0)
  701. for _, v := range list {
  702. if v.ParentId > 0 {
  703. currParentClassifyIdList = append(currParentClassifyIdList, v.ParentId)
  704. }
  705. }
  706. if len(currParentClassifyIdList) > 0 {
  707. tmpList, tmpErr := GetParentClassifyListByParentIdList(currParentClassifyIdList)
  708. if tmpErr != nil {
  709. err = tmpErr
  710. return
  711. }
  712. list = append(tmpList, list...)
  713. }
  714. }
  715. return
  716. }
  717. // GetClassifyListTreeRecursive
  718. // @Description: 递归获取分类树形结构
  719. // @author: Roc
  720. // @datetime 2024-06-19 13:23:28
  721. // @param list []*models.ClassifyList
  722. // @param parentId int
  723. // @return []*models.ClassifyList
  724. func GetClassifyListTreeRecursive(list []*models.ClassifyList, parentId int) []*models.ClassifyList {
  725. res := make([]*models.ClassifyList, 0)
  726. for _, v := range list {
  727. if v.ParentId == parentId {
  728. v.Child = GetClassifyListTreeRecursive(list, v.Id)
  729. res = append(res, v)
  730. }
  731. }
  732. // 前端的JP需要我这么返回
  733. if len(res) <= 0 {
  734. res = nil
  735. }
  736. return res
  737. }
  738. // RecursiveFilterNoChildTreeClassify 递归过滤没有子分类的分类
  739. func RecursiveFilterNoChildTreeClassify(list []*models.ClassifyList) []*models.ClassifyList {
  740. res := make([]*models.ClassifyList, 0)
  741. for _, v := range list {
  742. v.Child = RecursiveFilterNoChildTreeClassify(v.Child)
  743. if len(v.Child) == 0 && v.HasChild == 1 {
  744. continue
  745. }
  746. if len(v.Child) == 0 {
  747. v.Child = nil
  748. }
  749. res = append(res, v)
  750. }
  751. return res
  752. }
  753. // BySortAndCreateTime 用来排序,先按Sort字段升序排序,若Sort相同,则按照CreateTime字段升序排序。
  754. type BySortAndCreateTime []*models.ClassifyList
  755. func (a BySortAndCreateTime) Len() int {
  756. return len(a)
  757. }
  758. func (a BySortAndCreateTime) Swap(i, j int) {
  759. a[i], a[j] = a[j], a[i]
  760. }
  761. func (a BySortAndCreateTime) Less(i, j int) bool {
  762. if a[i].Sort == a[j].Sort {
  763. return a[i].CreateTime.Before(a[j].CreateTime)
  764. }
  765. return a[i].Sort < a[j].Sort
  766. }
  767. // SortClassifyListBySortAndCreateTime sorts the ClassifyList slice by Sort and then CreateTime in ascending order.
  768. func SortClassifyListBySortAndCreateTime(classifyList []*models.ClassifyList) {
  769. sort.Sort(BySortAndCreateTime(classifyList))
  770. }
  771. // GetClassifyChildIdsTreeRecursive 递归获取分类所有子分类ID
  772. func GetClassifyChildIdsTreeRecursive(list []*models.Classify, parentId int) []int {
  773. var res []int
  774. for _, v := range list {
  775. if v.ParentId == parentId {
  776. res = append(res, v.Id)
  777. // 子分类的子类
  778. childIds := GetClassifyChildIdsTreeRecursive(list, v.Id)
  779. if len(childIds) > 0 {
  780. res = append(res, childIds...)
  781. }
  782. }
  783. }
  784. return res
  785. }