classify.go 27 KB

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