classify.go 25 KB

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