classify.go 27 KB

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