classify.go 27 KB

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