chart_classify.go 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517
  1. package data
  2. import (
  3. "errors"
  4. "eta_gn/eta_api/models/data_manage"
  5. "eta_gn/eta_api/models/system"
  6. "eta_gn/eta_api/services/data/data_manage_permission"
  7. "eta_gn/eta_api/utils"
  8. "fmt"
  9. "strconv"
  10. "time"
  11. )
  12. func chartClassifyHaveChild(allNode []*data_manage.ChartClassifyItems, node *data_manage.ChartClassifyItems) (childs []*data_manage.ChartClassifyItems, yes bool) {
  13. for _, v := range allNode {
  14. if v.ParentId == node.ChartClassifyId {
  15. childs = append(childs, v)
  16. }
  17. }
  18. if len(childs) > 0 {
  19. yes = true
  20. }
  21. return
  22. }
  23. func ChartClassifyItemsMakeTree(sysUser *system.Admin, allNode []*data_manage.ChartClassifyItems, node *data_manage.ChartClassifyItems, permissionClassifyIdList []int) {
  24. node.HaveOperaAuth = data_manage_permission.CheckEdbClassifyPermissionByPermissionIdList(node.IsJoinPermission, node.ChartClassifyId, permissionClassifyIdList)
  25. button := GetChartClassifyOpButton(sysUser, node.SysUserId, node.HaveOperaAuth)
  26. node.Button = button
  27. childs, _ := chartClassifyHaveChild(allNode, node) //判断节点是否有子节点并返回
  28. if len(childs) > 0 {
  29. for _, child := range childs {
  30. child.HaveOperaAuth = data_manage_permission.CheckEdbClassifyPermissionByPermissionIdList(child.IsJoinPermission, child.ChartClassifyId, permissionClassifyIdList)
  31. childButton := GetChartClassifyOpButton(sysUser, child.SysUserId, child.HaveOperaAuth)
  32. if child.Level == 3 {
  33. childButton.AddButton = false //第三级的话,默认图表都是没有添加按钮的
  34. }
  35. child.Button = childButton
  36. //node.Children = append(node.Children, child)
  37. }
  38. node.Children = append(node.Children, childs[0:]...) //添加子节点
  39. for _, v := range childs {
  40. v.HaveOperaAuth = data_manage_permission.CheckEdbClassifyPermissionByPermissionIdList(v.IsJoinPermission, v.ChartClassifyId, permissionClassifyIdList)
  41. //查询子节点的子节点,并添加到子节点
  42. _, has := chartClassifyHaveChild(allNode, v)
  43. if has {
  44. ChartClassifyItemsMakeTree(sysUser, allNode, v, permissionClassifyIdList) //递归添加节点
  45. } else {
  46. childrenArr := make([]*data_manage.ChartClassifyItems, 0)
  47. v.Children = childrenArr
  48. }
  49. }
  50. } else {
  51. childrenArr := make([]*data_manage.ChartClassifyItems, 0)
  52. node.Children = childrenArr
  53. }
  54. }
  55. func ChartClassifyItemsMakeTreeV2(allNode []*data_manage.ChartClassifyItems, node *data_manage.ChartClassifyItems) {
  56. //node.HaveOperaAuth = data_manage_permission.CheckEdbClassifyPermissionByPermissionIdList(node.IsJoinPermission, node.ChartClassifyId, permissionClassifyIdList)
  57. //button := GetChartClassifyOpButton(sysUser, node.SysUserId, node.HaveOperaAuth)
  58. //node.Button = button
  59. childs, _ := chartClassifyHaveChild(allNode, node) //判断节点是否有子节点并返回
  60. if len(childs) > 0 {
  61. //for _, child := range childs {
  62. //child.HaveOperaAuth = data_manage_permission.CheckEdbClassifyPermissionByPermissionIdList(child.IsJoinPermission, child.ChartClassifyId, permissionClassifyIdList)
  63. //childButton := GetChartClassifyOpButton(sysUser, child.SysUserId, child.HaveOperaAuth)
  64. //if child.Level == 3 {
  65. // childButton.AddButton = false //第三级的话,默认图表都是没有添加按钮的
  66. //}
  67. //child.Button = childButton
  68. //node.Children = append(node.Children, child)
  69. //}
  70. node.Children = append(node.Children, childs[0:]...) //添加子节点
  71. for _, v := range childs {
  72. //v.HaveOperaAuth = data_manage_permission.CheckEdbClassifyPermissionByPermissionIdList(v.IsJoinPermission, v.ChartClassifyId, permissionClassifyIdList)
  73. //查询子节点的子节点,并添加到子节点
  74. _, has := chartClassifyHaveChild(allNode, v)
  75. if has {
  76. ChartClassifyItemsMakeTreeV2(allNode, v) //递归添加节点
  77. } else {
  78. childrenArr := make([]*data_manage.ChartClassifyItems, 0)
  79. v.Children = childrenArr
  80. }
  81. }
  82. } else {
  83. childrenArr := make([]*data_manage.ChartClassifyItems, 0)
  84. node.Children = childrenArr
  85. }
  86. }
  87. // GetChartClassifyOpButton 获取ETA图库分类的操作权限
  88. func GetChartClassifyOpButton(sysUser *system.Admin, belongUserId int, haveOperaAuth bool) (button data_manage.ChartClassifyItemsButton) {
  89. // 没有数据权限就直接返回
  90. if !haveOperaAuth {
  91. return
  92. }
  93. //ficc管理员和超管和ficc研究员有权限创建和管理分类,可以编辑分类名称(分类名称不允许重复),可以拖动分类,改变分类顺序,可以拖动分类下模型,改变顺序,可以删除分类,若分类下有预测指标,则不允许删除;
  94. //if utils.InArrayByStr([]string{utils.ROLE_TYPE_CODE_ADMIN, utils.ROLE_TYPE_CODE_FICC_ADMIN, utils.ROLE_TYPE_CODE_RESEARCHR, utils.ROLE_TYPE_CODE_FICC_RESEARCHR}, sysUser.RoleTypeCode) {
  95. button.AddButton = true
  96. button.OpButton = true
  97. button.DeleteButton = true
  98. button.MoveButton = true
  99. //}
  100. return
  101. }
  102. // GetChartOpButton 获取ETA图库的操作权限
  103. func GetChartOpButton(sysUser *system.Admin, belongUserId, chartInfoId int, haveOperaAuth bool, editShareChartMap map[int]bool) (button data_manage.ChartClassifyItemsButton) {
  104. // 数据权限判断
  105. if !haveOperaAuth {
  106. return
  107. }
  108. if sysUser.AdminId == belongUserId {
  109. //button.AddButton = true
  110. button.OpButton = true
  111. button.DeleteButton = true
  112. button.MoveButton = true
  113. } else {
  114. // 拥有别人分享的操作权限
  115. if _, ok := editShareChartMap[chartInfoId]; ok {
  116. button.OpButton = true
  117. }
  118. }
  119. return
  120. }
  121. // HandleNoPermissionChart 图表列表返回,将没有权限的图表移除
  122. func HandleNoPermissionChart(allNodes []*data_manage.ChartClassifyItems, noPermissionChartIdMap map[int]bool, sysUserId int) (newAllNodes []*data_manage.ChartClassifyItems) {
  123. // 涉及分类id
  124. classifyIdList := make([]int, 0)
  125. // 移除没有权限的图表
  126. newAllNodes = make([]*data_manage.ChartClassifyItems, 0)
  127. for _, node := range allNodes {
  128. // 二级分类
  129. tmpNodeInfo := *node
  130. tmpNodeList := make([]*data_manage.ChartClassifyItems, 0)
  131. if node.Children != nil {
  132. for _, chartInfo := range node.Children {
  133. // 如果指标不可见,那么就不返回该指标
  134. if _, ok := noPermissionChartIdMap[chartInfo.ChartInfoId]; ok {
  135. continue
  136. }
  137. tmpNodeList = append(tmpNodeList, chartInfo)
  138. classifyIdList = append(classifyIdList, chartInfo.ChartClassifyId)
  139. }
  140. }
  141. tmpNodeInfo.Children = tmpNodeList
  142. newAllNodes = append(newAllNodes, &tmpNodeInfo)
  143. classifyIdList = append(classifyIdList, node.ChartClassifyId)
  144. }
  145. // 数据权限
  146. if len(classifyIdList) > 0 {
  147. // 图表分类
  148. chartClassifyList, err := data_manage.GetChartClassifyByIdList(classifyIdList)
  149. if err != nil {
  150. return
  151. }
  152. chartClassifyMap := make(map[int]*data_manage.ChartClassify)
  153. for _, v := range chartClassifyList {
  154. chartClassifyMap[v.ChartClassifyId] = v
  155. }
  156. // 已授权分类id
  157. permissionChartIdList, permissionClassifyIdList, err := data_manage_permission.GetUserChartAndClassifyPermissionList(sysUserId, 0, 0)
  158. if err != nil {
  159. return
  160. }
  161. for _, node := range newAllNodes {
  162. if node.Children != nil {
  163. for _, chartInfo := range node.Children {
  164. if currClassify, ok := chartClassifyMap[chartInfo.ChartClassifyId]; ok {
  165. chartInfo.HaveOperaAuth = data_manage_permission.CheckChartPermissionByPermissionIdList(chartInfo.IsJoinPermission, currClassify.IsJoinPermission, chartInfo.ChartInfoId, chartInfo.ChartClassifyId, permissionChartIdList, permissionClassifyIdList)
  166. }
  167. }
  168. }
  169. if currClassify, ok := chartClassifyMap[node.ChartClassifyId]; ok {
  170. node.HaveOperaAuth = data_manage_permission.CheckChartPermissionByPermissionIdList(node.IsJoinPermission, currClassify.IsJoinPermission, node.ChartInfoId, node.ChartClassifyId, permissionChartIdList, permissionClassifyIdList)
  171. }
  172. }
  173. }
  174. return
  175. }
  176. // AddChartClassify
  177. // @Description: 添加图表分类
  178. // @author: Roc
  179. // @datetime 2024-04-17 14:48:23
  180. // @param chartClassifyName string
  181. // @param parentId int
  182. // @param level int
  183. // @param source int
  184. // @param lang string
  185. // @param sysUser *system.Admin
  186. // @return classifyInfo *data_manage.ChartClassify
  187. // @return err error
  188. // @return errMsg string
  189. // @return isSendEmail bool
  190. func AddChartClassify(chartClassifyName string, parentId, level, source int, lang string, sysUser *system.Admin) (classifyInfo *data_manage.ChartClassify, err error, errMsg string, isSendEmail bool) {
  191. isSendEmail = true
  192. errMsg = "保存分类失败"
  193. // 校验分类名称相同的数量(eta2.3,图表名称允许重复)
  194. //{
  195. // var count int
  196. // switch lang {
  197. // case utils.EnLangVersion:
  198. // count, err = data_manage.GetChartClassifyEnCount(chartClassifyName, parentId, source)
  199. // default:
  200. // count, err = data_manage.GetChartClassifyCount(chartClassifyName, parentId, source)
  201. // }
  202. // if err != nil {
  203. // errMsg = "判断名称是否已存在失败"
  204. // return
  205. // }
  206. // if count > 0 {
  207. // errMsg = "分类名称已存在,请重新输入"
  208. // err = errors.New(errMsg)
  209. // isSendEmail = false
  210. // return
  211. // }
  212. //}
  213. if level > 6 {
  214. errMsg = `最高只支持添加6级分类`
  215. return
  216. }
  217. //获取该层级下最大的排序数
  218. maxSort, err := data_manage.GetChartClassifyMaxSort(parentId, source)
  219. if err != nil {
  220. errMsg = "获取失败"
  221. err = errors.New("查询排序信息失败,Err:" + err.Error())
  222. return
  223. }
  224. //查询顶级rootId
  225. rootId := 0
  226. var classifyNamePath, classifyIdPath string
  227. if parentId > 0 {
  228. parentClassify, tErr := data_manage.GetChartClassifyById(parentId)
  229. if tErr != nil {
  230. if utils.IsErrNoRow(tErr) {
  231. errMsg = "父级分类不存在"
  232. err = errors.New(errMsg)
  233. return
  234. }
  235. errMsg = "获取失败"
  236. err = errors.New("获取分类信息失败,Err:" + tErr.Error())
  237. return
  238. }
  239. rootId = parentClassify.RootId
  240. classifyNamePath = fmt.Sprint(parentClassify.ChartClassifyNamePath, "|", chartClassifyName)
  241. classifyIdPath = fmt.Sprint(parentClassify.ChartClassifyIdPath, ",")
  242. } else {
  243. classifyNamePath = chartClassifyName
  244. }
  245. classifyInfo = new(data_manage.ChartClassify)
  246. classifyInfo.ParentId = parentId
  247. classifyInfo.ChartClassifyName = chartClassifyName
  248. classifyInfo.ChartClassifyNameEn = chartClassifyName
  249. classifyInfo.HasData = 0
  250. classifyInfo.CreateTime = time.Now()
  251. classifyInfo.ModifyTime = time.Now()
  252. classifyInfo.SysUserId = sysUser.AdminId
  253. classifyInfo.SysUserRealName = sysUser.RealName
  254. classifyInfo.Level = level + 1
  255. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  256. classifyInfo.UniqueCode = utils.MD5(utils.CHART_PREFIX + "_" + timestamp)
  257. classifyInfo.Sort = maxSort + 1
  258. classifyInfo.Source = source
  259. classifyInfo.RootId = rootId
  260. classifyInfo.ChartClassifyNamePath = classifyNamePath
  261. err = data_manage.AddChartClassify(classifyInfo)
  262. if err != nil {
  263. return
  264. }
  265. // 更改分类id完整路径path
  266. updateCols := []string{"ClassifyIdPath"}
  267. classifyInfo.ChartClassifyIdPath = fmt.Sprint(classifyIdPath, classifyInfo.ChartClassifyId)
  268. if parentId == 0 { //一级目录的rootId等于自己本身
  269. classifyInfo.RootId = classifyInfo.ChartClassifyId
  270. updateCols = append(updateCols, "RootId")
  271. }
  272. err = classifyInfo.Update(updateCols)
  273. if err != nil {
  274. errMsg = "更新分类失败"
  275. return
  276. }
  277. //// 目前只有ETA图库需要继承分类权限
  278. //if classifyInfo.Source == utils.CHART_SOURCE_DEFAULT {
  279. // go data_manage_permission.InheritParentClassify(5, classifyInfo.Source, classifyInfo.ChartClassifyId, classifyInfo.ParentId, classifyInfo.ChartClassifyName)
  280. //}
  281. return
  282. }
  283. // EditChartClassify
  284. // @Description: 修改图表分类
  285. // @author: Roc
  286. // @datetime 2024-04-17 14:59:37
  287. // @param chartClassifyId int
  288. // @param source int
  289. // @param chartClassifyName string
  290. // @param lang string
  291. // @param sysUser *system.Admin
  292. // @return classifyInfo *data_manage.ChartClassify
  293. // @return err error
  294. // @return errMsg string
  295. // @return isSendEmail bool
  296. func EditChartClassify(chartClassifyId, source int, chartClassifyName, lang string, sysUser *system.Admin) (classifyInfo *data_manage.ChartClassify, err error, errMsg string, isSendEmail bool) {
  297. isSendEmail = true
  298. errMsg = "保存失败"
  299. // 获取分类信息
  300. classifyInfo, err = data_manage.GetChartClassifyById(chartClassifyId)
  301. if err != nil {
  302. return
  303. }
  304. // 分类来源校验
  305. if classifyInfo.Source != source {
  306. errMsg = "图表分类异常"
  307. err = errors.New(fmt.Sprintf("图表分类来源异常,修改来源:%d,分类来源:%d", source, classifyInfo.Source))
  308. isSendEmail = false
  309. return
  310. }
  311. // 需要变更的字段
  312. updateCols := make([]string, 0)
  313. // 旧完整路径 , 新的完整路径
  314. var oldClassifyNamePath, newClassifyNamePath string
  315. // 语言版本校验
  316. switch lang {
  317. case utils.EnLangVersion:
  318. if classifyInfo.ChartClassifyNameEn != chartClassifyName {
  319. count, tmpErr := data_manage.GetChartClassifyEnCount(chartClassifyName, classifyInfo.ParentId, source)
  320. if tmpErr != nil {
  321. errMsg = "判断名称是否已存在失败"
  322. err = errors.New("判断名称是否已存在失败,Err:" + tmpErr.Error())
  323. return
  324. }
  325. if count > 0 {
  326. errMsg = "分类名称已存在,请重新输入"
  327. err = errors.New(errMsg)
  328. isSendEmail = false
  329. return
  330. }
  331. classifyInfo.ChartClassifyNameEn = chartClassifyName
  332. classifyInfo.ModifyTime = time.Now()
  333. updateCols = append(updateCols, "ChartClassifyNameEn", "ModifyTime")
  334. }
  335. default:
  336. if classifyInfo.ChartClassifyName != chartClassifyName {
  337. //count, tmpErr := data_manage.GetChartClassifyCount(chartClassifyName, classifyInfo.ParentId, source)
  338. //if tmpErr != nil {
  339. // errMsg = "判断名称是否已存在失败"
  340. // err = errors.New("判断名称是否已存在失败,Err:" + tmpErr.Error())
  341. // return
  342. //}
  343. //if count > 0 {
  344. // errMsg = "分类名称已存在,请重新输入"
  345. // err = errors.New(errMsg)
  346. // isSendEmail = false
  347. // return
  348. //}
  349. // 旧完整路径
  350. oldClassifyNamePath = classifyInfo.ChartClassifyNamePath
  351. if classifyInfo.ParentId > 0 {
  352. parentItem, tmpErr := data_manage.GetChartClassifyById(classifyInfo.ParentId)
  353. if tmpErr != nil {
  354. err = tmpErr
  355. return
  356. }
  357. newClassifyNamePath = fmt.Sprint(parentItem.ChartClassifyNamePath, "|", chartClassifyName)
  358. } else {
  359. newClassifyNamePath = chartClassifyName
  360. }
  361. classifyInfo.ChartClassifyName = chartClassifyName
  362. classifyInfo.ModifyTime = time.Now()
  363. classifyInfo.ChartClassifyNamePath = newClassifyNamePath
  364. updateCols = append(updateCols, "ChartClassifyName", "ModifyTime", "ChartClassifyNamePath")
  365. }
  366. }
  367. if len(updateCols) > 0 {
  368. err = classifyInfo.UpdateClassifyNameAndNamePath(updateCols, oldClassifyNamePath, newClassifyNamePath)
  369. }
  370. return
  371. }
  372. // MoveChartClassify 移动图表分类
  373. func MoveChartClassify(req data_manage.MoveChartClassifyReq, sysUser *system.Admin, source int) (err error, errMsg string) {
  374. classifyId := req.ClassifyId
  375. parentClassifyId := req.ParentClassifyId
  376. prevClassifyId := req.PrevClassifyId
  377. nextClassifyId := req.NextClassifyId
  378. chartInfoId := req.ChartInfoId
  379. prevChartInfoId := req.PrevChartInfoId
  380. nextChartInfoId := req.NextChartInfoId
  381. //首先确定移动的对象是分类还是指标
  382. //判断上一个节点是分类还是指标
  383. //判断下一个节点是分类还是指标
  384. //同时更新分类目录下的分类sort和指标sort
  385. //更新当前移动的分类或者指标sort
  386. var parentChartClassifyInfo *data_manage.ChartClassify
  387. if parentClassifyId > 0 {
  388. parentChartClassifyInfo, err = data_manage.GetChartClassifyById(parentClassifyId)
  389. if err != nil {
  390. errMsg = "移动失败"
  391. err = errors.New("获取上级分类信息失败,Err:" + err.Error())
  392. return
  393. }
  394. }
  395. //如果有传入 上一个兄弟节点分类id
  396. var (
  397. chartClassifyInfo *data_manage.ChartClassify
  398. prevClassify *data_manage.ChartClassify
  399. nextClassify *data_manage.ChartClassify
  400. chartInfo *data_manage.ChartInfo
  401. prevChartInfo *data_manage.ChartInfo
  402. nextChartInfo *data_manage.ChartInfo
  403. prevSort int
  404. nextSort int
  405. )
  406. // 移动对象为分类, 判断权限
  407. if chartInfoId == 0 {
  408. chartClassifyInfo, err = data_manage.GetChartClassifyById(classifyId)
  409. if err != nil {
  410. if utils.IsErrNoRow(err) {
  411. errMsg = "当前分类不存在"
  412. err = errors.New("获取分类信息失败,Err:" + err.Error())
  413. return
  414. }
  415. errMsg = "移动失败"
  416. err = errors.New("获取分类信息失败,Err:" + err.Error())
  417. return
  418. }
  419. if parentClassifyId > 0 && parentChartClassifyInfo.Level == 6 {
  420. errMsg = "最高只支持添加6级分类"
  421. err = errors.New(errMsg)
  422. return
  423. }
  424. // 如果是移动目录, 那么校验一下父级目录下是否有重名目录
  425. exists, e := data_manage.GetChartClassifyByParentIdAndName(source, parentClassifyId, chartClassifyInfo.ChartClassifyName, classifyId)
  426. if e != nil && !utils.IsErrNoRow(e) {
  427. errMsg = "移动失败"
  428. err = fmt.Errorf("获取父级分类下的同名分类失败, Err: %s", e.Error())
  429. return
  430. }
  431. if exists != nil && exists.ChartClassifyId > 0 {
  432. errMsg = "移动失败,分类名称已存在"
  433. return
  434. }
  435. // 权限校验
  436. {
  437. // 已授权分类id
  438. permissionClassifyIdList, tmpErr := data_manage_permission.GetUserChartClassifyPermissionList(sysUser.AdminId, classifyId)
  439. if tmpErr != nil {
  440. errMsg = "移动失败"
  441. err = errors.New("获取已授权分类id数据失败,Err:" + tmpErr.Error())
  442. return
  443. }
  444. // 数据权限
  445. haveOperaAuth := data_manage_permission.CheckChartClassifyPermissionByPermissionIdList(chartClassifyInfo.IsJoinPermission, chartClassifyInfo.ChartClassifyId, permissionClassifyIdList)
  446. button := GetChartClassifyOpButton(sysUser, chartClassifyInfo.SysUserId, haveOperaAuth)
  447. if !button.MoveButton {
  448. errMsg = "无操作权限"
  449. err = errors.New(errMsg)
  450. return
  451. }
  452. }
  453. } else {
  454. chartInfo, err = data_manage.GetChartInfoById(req.ChartInfoId)
  455. if err != nil {
  456. if utils.IsErrNoRow(err) {
  457. errMsg = "当前图表不存在"
  458. err = errors.New("获取分类信息失败,Err:" + err.Error())
  459. return
  460. }
  461. errMsg = "移动失败"
  462. err = errors.New("获取分类信息失败,Err:" + err.Error())
  463. return
  464. }
  465. if parentClassifyId == 0 {
  466. errMsg = "移动失败,图表必须挂在分类下"
  467. err = errors.New(errMsg)
  468. return
  469. }
  470. var haveOperaAuth bool
  471. // 权限校验
  472. {
  473. haveOperaAuth, err = data_manage_permission.CheckChartPermissionByChartInfoId(chartInfo.ChartInfoId, chartInfo.ChartClassifyId, chartInfo.IsJoinPermission, sysUser.AdminId)
  474. if err != nil {
  475. errMsg = "移动失败"
  476. err = errors.New("获取图表权限信息失败,Err:" + err.Error())
  477. return
  478. }
  479. }
  480. editShareChartInfoIdMap, tmpErr := GetAllEditSharedChartInfoIdMapByReceivedUserIdAndChartInfoIdList(sysUser.AdminId, []int{chartInfo.ChartInfoId})
  481. if tmpErr != nil {
  482. errMsg = "移动失败"
  483. err = errors.New("获取所有有编辑权限的分享图表失败,Err:" + tmpErr.Error())
  484. return
  485. }
  486. // 移动权限校验
  487. button := GetChartOpButton(sysUser, chartInfo.SysUserId, chartInfo.ChartInfoId, haveOperaAuth, editShareChartInfoIdMap)
  488. if !button.MoveButton {
  489. errMsg = "无操作权限"
  490. err = errors.New(errMsg)
  491. return
  492. }
  493. }
  494. if prevClassifyId > 0 {
  495. prevClassify, err = data_manage.GetChartClassifyById(prevClassifyId)
  496. if err != nil {
  497. errMsg = "移动失败"
  498. err = errors.New("获取上一个兄弟节点分类信息失败,Err:" + err.Error())
  499. return
  500. }
  501. prevSort = prevClassify.Sort
  502. } else if prevChartInfoId > 0 {
  503. prevChartInfo, err = data_manage.GetChartInfoById(prevChartInfoId)
  504. if err != nil {
  505. errMsg = "移动失败"
  506. err = errors.New("获取上一个兄弟节点分类信息失败,Err:" + err.Error())
  507. return
  508. }
  509. prevSort = prevChartInfo.Sort
  510. }
  511. if nextClassifyId > 0 {
  512. //下一个兄弟节点
  513. nextClassify, err = data_manage.GetChartClassifyById(nextClassifyId)
  514. if err != nil {
  515. errMsg = "移动失败"
  516. err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + err.Error())
  517. return
  518. }
  519. nextSort = nextClassify.Sort
  520. } else if nextChartInfoId > 0 {
  521. //下一个兄弟节点
  522. nextChartInfo, err = data_manage.GetChartInfoById(nextChartInfoId)
  523. if err != nil {
  524. errMsg = "移动失败"
  525. err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + err.Error())
  526. return
  527. }
  528. nextSort = nextChartInfo.Sort
  529. }
  530. err, errMsg = moveChartOrClassify(parentChartClassifyInfo, chartClassifyInfo, prevClassify, nextClassify, chartInfo, prevChartInfo, nextChartInfo, parentClassifyId, prevSort, nextSort, source)
  531. return
  532. }
  533. func moveChartOrClassify(parentChartClassifyInfo, chartClassifyInfo, prevClassify, nextClassify *data_manage.ChartClassify, chartInfo, prevChartInfo, nextChartInfo *data_manage.ChartInfo, parentClassifyId, prevSort, nextSort, source int) (err error, errMsg string) {
  534. updateCol := make([]string, 0)
  535. // 移动对象为分类, 判断分类是否存在
  536. if chartClassifyInfo != nil {
  537. oldParentId := chartClassifyInfo.ParentId
  538. oldLevel := chartClassifyInfo.Level
  539. var classifyIds []int
  540. if oldParentId != parentClassifyId {
  541. //更新子分类对应的level
  542. childList, e, m := GetChildChartClassifyByClassifyId(chartClassifyInfo.ChartClassifyId)
  543. if e != nil {
  544. errMsg = "移动失败"
  545. err = errors.New("查询子分类失败,Err:" + e.Error() + m)
  546. return
  547. }
  548. if len(childList) > 0 {
  549. for _, v := range childList {
  550. if v.ChartClassifyId == chartClassifyInfo.ChartClassifyId {
  551. continue
  552. }
  553. classifyIds = append(classifyIds, v.ChartClassifyId)
  554. }
  555. }
  556. }
  557. //判断上级id是否一致,如果不一致的话,那么需要移动该分类层级
  558. if chartClassifyInfo.ParentId != parentClassifyId && parentClassifyId != 0 {
  559. if chartClassifyInfo.Level != parentChartClassifyInfo.Level+1 { //禁止层级调整
  560. errMsg = "移动失败"
  561. err = errors.New("不支持目录层级变更")
  562. return
  563. }
  564. chartClassifyInfo.ParentId = parentChartClassifyInfo.ChartClassifyId
  565. chartClassifyInfo.RootId = parentChartClassifyInfo.RootId
  566. chartClassifyInfo.Level = parentChartClassifyInfo.Level + 1
  567. chartClassifyInfo.ModifyTime = time.Now()
  568. updateCol = append(updateCol, "ParentId", "RootId", "Level", "ModifyTime")
  569. } else if chartClassifyInfo.ParentId != parentClassifyId && parentClassifyId == 0 {
  570. errMsg = "移动失败"
  571. err = errors.New("不支持目录层级变更")
  572. return
  573. }
  574. if prevSort > 0 {
  575. //如果是移动在两个兄弟节点之间
  576. if nextSort > 0 {
  577. //下一个兄弟节点
  578. //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
  579. if prevSort == nextSort || prevSort == chartClassifyInfo.Sort {
  580. //变更兄弟节点的排序
  581. updateSortStr := `sort + 2`
  582. //变更分类
  583. if prevClassify != nil {
  584. _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, prevClassify.ChartClassifyId, prevClassify.Sort, updateSortStr, source)
  585. } else {
  586. _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, 0, prevSort, updateSortStr, source)
  587. }
  588. //变更图表
  589. if prevChartInfo != nil {
  590. //变更兄弟节点的排序
  591. _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, prevSort, prevChartInfo.ChartInfoId, updateSortStr)
  592. } else {
  593. _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, prevSort, 0, updateSortStr)
  594. }
  595. } else {
  596. //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
  597. if nextSort-prevSort == 1 {
  598. //变更兄弟节点的排序
  599. updateSortStr := `sort + 1`
  600. //变更分类
  601. if prevClassify != nil {
  602. _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, prevClassify.ChartClassifyId, prevSort, updateSortStr, source)
  603. } else {
  604. _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, 0, prevSort, updateSortStr, source)
  605. }
  606. //变更图表
  607. if prevChartInfo != nil {
  608. //变更兄弟节点的排序
  609. _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, prevSort, prevChartInfo.ChartInfoId, updateSortStr)
  610. } else {
  611. _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, prevSort, 0, updateSortStr)
  612. }
  613. }
  614. }
  615. }
  616. chartClassifyInfo.Sort = prevSort + 1
  617. chartClassifyInfo.ModifyTime = time.Now()
  618. updateCol = append(updateCol, "Sort", "ModifyTime")
  619. } else if prevClassify == nil && nextClassify == nil && prevChartInfo == nil && nextChartInfo == nil && parentClassifyId > 0 {
  620. //处理只拖动到目录里,默认放到目录底部的情况
  621. var maxSort int
  622. maxSort, err = GetChartClassifyMaxSort(parentClassifyId, source)
  623. if err != nil {
  624. errMsg = "移动失败"
  625. err = errors.New("查询组内排序信息失败,Err:" + err.Error())
  626. return
  627. }
  628. chartClassifyInfo.Sort = maxSort + 1 //那就是排在组内最后一位
  629. chartClassifyInfo.ModifyTime = time.Now()
  630. updateCol = append(updateCol, "Sort", "ModifyTime")
  631. } else {
  632. // 拖动到父级分类的第一位
  633. firstClassify, tmpErr := data_manage.GetFirstChartClassifyByParentId(parentClassifyId)
  634. if tmpErr != nil && !utils.IsErrNoRow(tmpErr) {
  635. errMsg = "移动失败"
  636. err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tmpErr.Error())
  637. return
  638. }
  639. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  640. if firstClassify != nil && firstClassify.ChartClassifyId > 0 && firstClassify.Sort == 0 {
  641. updateSortStr := ` sort + 1 `
  642. _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, firstClassify.ChartClassifyId-1, 0, updateSortStr, source)
  643. //该分类下的所有图表也需要+1
  644. _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, 0, 0, updateSortStr)
  645. } else {
  646. //如果该分类下存在图表,且第一个图表的排序等于0,那么需要调整排序
  647. firstEdb, tErr := data_manage.GetFirstChartInfoByClassifyId(parentClassifyId)
  648. if tErr != nil && !utils.IsErrNoRow(tErr) {
  649. errMsg = "移动失败"
  650. err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tErr.Error())
  651. return
  652. }
  653. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  654. if firstEdb != nil && firstEdb.ChartClassifyId > 0 && firstEdb.Sort == 0 {
  655. updateSortStr := ` sort + 1 `
  656. _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, 0, firstEdb.ChartInfoId-1, updateSortStr)
  657. _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, 0, 0, updateSortStr, source)
  658. }
  659. }
  660. chartClassifyInfo.Sort = 0 //那就是排在第一位
  661. chartClassifyInfo.ModifyTime = time.Now()
  662. updateCol = append(updateCol, "Sort", "ModifyTime")
  663. }
  664. //更新
  665. if len(updateCol) > 0 {
  666. err = chartClassifyInfo.Update(updateCol)
  667. if err != nil {
  668. errMsg = "移动失败"
  669. err = errors.New("修改失败,Err:" + err.Error())
  670. return
  671. }
  672. //更新对应分类的root_id和层级
  673. if oldParentId != parentClassifyId {
  674. if len(classifyIds) > 0 {
  675. levelStep := chartClassifyInfo.Level - oldLevel
  676. err = data_manage.UpdateEdbClassifyChildByParentClassifyId(classifyIds, chartClassifyInfo.RootId, levelStep)
  677. if err != nil {
  678. errMsg = "移动失败"
  679. err = errors.New("更新子分类失败,Err:" + err.Error())
  680. return
  681. }
  682. }
  683. }
  684. }
  685. } else {
  686. if chartInfo == nil {
  687. errMsg = "当前图表不存在"
  688. err = errors.New(errMsg)
  689. return
  690. }
  691. //如果改变了分类,那么移动该图表数据
  692. if chartInfo.ChartClassifyId != parentClassifyId {
  693. chartInfo.ChartClassifyId = parentClassifyId
  694. chartInfo.ModifyTime = time.Now()
  695. updateCol = append(updateCol, "ChartClassifyId", "ModifyTime")
  696. }
  697. if prevSort > 0 {
  698. //如果是移动在两个兄弟节点之间
  699. if nextSort > 0 {
  700. //下一个兄弟节点
  701. //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
  702. if prevSort == nextSort || prevSort == chartInfo.Sort {
  703. //变更兄弟节点的排序
  704. updateSortStr := `sort + 2`
  705. //变更分类
  706. if prevClassify != nil {
  707. _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, prevClassify.ChartClassifyId, prevClassify.Sort, updateSortStr, source)
  708. } else {
  709. _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, 0, prevSort, updateSortStr, source)
  710. }
  711. //变更图表
  712. if prevChartInfo != nil {
  713. //变更兄弟节点的排序
  714. _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, prevSort, prevChartInfo.ChartInfoId, updateSortStr)
  715. } else {
  716. _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, prevSort, 0, updateSortStr)
  717. }
  718. } else {
  719. //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
  720. if nextSort-prevSort == 1 {
  721. //变更兄弟节点的排序
  722. updateSortStr := `sort + 1`
  723. //变更分类
  724. if prevClassify != nil {
  725. _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, prevClassify.ChartClassifyId, prevSort, updateSortStr, source)
  726. } else {
  727. _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, 0, prevSort, updateSortStr, source)
  728. }
  729. //变更图表
  730. if prevChartInfo != nil {
  731. //变更兄弟节点的排序
  732. _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, prevSort, prevChartInfo.ChartInfoId, updateSortStr)
  733. } else {
  734. _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, prevSort, 0, updateSortStr)
  735. }
  736. }
  737. }
  738. }
  739. chartInfo.Sort = prevSort + 1
  740. chartInfo.ModifyTime = time.Now()
  741. updateCol = append(updateCol, "Sort", "ModifyTime")
  742. } else if prevClassify == nil && nextClassify == nil && prevChartInfo == nil && nextChartInfo == nil && parentClassifyId > 0 {
  743. //处理只拖动到目录里,默认放到目录底部的情况
  744. var maxSort int
  745. maxSort, err = GetChartClassifyMaxSort(parentClassifyId, source)
  746. if err != nil {
  747. errMsg = "移动失败"
  748. err = errors.New("查询组内排序信息失败,Err:" + err.Error())
  749. return
  750. }
  751. chartInfo.Sort = maxSort + 1 //那就是排在组内最后一位
  752. chartInfo.ModifyTime = time.Now()
  753. updateCol = append(updateCol, "Sort", "ModifyTime")
  754. } else {
  755. // 拖动到父级分类的第一位
  756. firstClassify, tmpErr := data_manage.GetFirstChartClassifyByParentId(parentClassifyId)
  757. if tmpErr != nil && !utils.IsErrNoRow(tmpErr) {
  758. errMsg = "移动失败"
  759. err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tmpErr.Error())
  760. return
  761. }
  762. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  763. if firstClassify != nil && firstClassify.ChartClassifyId > 0 && firstClassify.Sort == 0 {
  764. updateSortStr := ` sort + 1 `
  765. _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, firstClassify.ChartClassifyId-1, 0, updateSortStr, source)
  766. //该分类下的所有图表也需要+1
  767. _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, 0, 0, updateSortStr)
  768. } else {
  769. //如果该分类下存在图表,且第一个图表的排序等于0,那么需要调整排序
  770. firstEdb, tErr := data_manage.GetFirstChartInfoByClassifyId(parentClassifyId)
  771. if tErr != nil && !utils.IsErrNoRow(tErr) {
  772. errMsg = "移动失败"
  773. err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tErr.Error())
  774. return
  775. }
  776. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  777. if firstEdb != nil && firstEdb.ChartClassifyId > 0 && firstEdb.Sort == 0 {
  778. updateSortStr := ` sort + 1 `
  779. _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, 0, firstEdb.ChartInfoId-1, updateSortStr)
  780. _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, 0, 0, updateSortStr, source)
  781. }
  782. }
  783. chartInfo.Sort = 0 //那就是排在第一位
  784. chartInfo.ModifyTime = time.Now()
  785. updateCol = append(updateCol, "Sort", "ModifyTime")
  786. }
  787. //更新
  788. if len(updateCol) > 0 {
  789. err = chartInfo.Update(updateCol)
  790. if err != nil {
  791. errMsg = "移动失败"
  792. err = errors.New("修改失败,Err:" + err.Error())
  793. return
  794. }
  795. }
  796. }
  797. return
  798. }
  799. func moveChart(parentChartClassifyInfo, chartClassifyInfo, prevClassify, nextClassify *data_manage.ChartClassify, chartInfo, prevChartInfo, nextChartInfo *data_manage.ChartInfo, parentClassifyId, prevSort, nextSort, source int) (err error, errMsg string) {
  800. updateCol := make([]string, 0)
  801. // 移动对象为分类, 判断分类是否存在
  802. if chartClassifyInfo != nil {
  803. oldParentId := chartClassifyInfo.ParentId
  804. oldLevel := chartClassifyInfo.Level
  805. var classifyIds []int
  806. if oldParentId != parentClassifyId {
  807. //更新子分类对应的level
  808. childList, e, m := GetChildChartClassifyByClassifyId(chartClassifyInfo.ChartClassifyId)
  809. if e != nil {
  810. errMsg = "移动失败"
  811. err = errors.New("查询子分类失败,Err:" + e.Error() + m)
  812. return
  813. }
  814. if len(childList) > 0 {
  815. for _, v := range childList {
  816. if v.ChartClassifyId == chartClassifyInfo.ChartClassifyId {
  817. continue
  818. }
  819. classifyIds = append(classifyIds, v.ChartClassifyId)
  820. }
  821. }
  822. }
  823. //判断上级id是否一致,如果不一致的话,那么需要移动该分类层级
  824. if chartClassifyInfo.ParentId != parentClassifyId && parentClassifyId != 0 {
  825. if chartClassifyInfo.Level != parentChartClassifyInfo.Level+1 { //禁止层级调整
  826. errMsg = "移动失败"
  827. err = errors.New("不支持目录层级变更")
  828. return
  829. }
  830. chartClassifyInfo.ParentId = parentChartClassifyInfo.ChartClassifyId
  831. chartClassifyInfo.RootId = parentChartClassifyInfo.RootId
  832. chartClassifyInfo.Level = parentChartClassifyInfo.Level + 1
  833. chartClassifyInfo.ModifyTime = time.Now()
  834. updateCol = append(updateCol, "ParentId", "RootId", "Level", "ModifyTime")
  835. } else if chartClassifyInfo.ParentId != parentClassifyId && parentClassifyId == 0 {
  836. errMsg = "移动失败"
  837. err = errors.New("不支持目录层级变更")
  838. return
  839. }
  840. if prevSort > 0 {
  841. //如果是移动在两个兄弟节点之间
  842. if nextSort > 0 {
  843. //下一个兄弟节点
  844. //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
  845. if prevSort == nextSort || prevSort == chartClassifyInfo.Sort {
  846. //变更兄弟节点的排序
  847. updateSortStr := `sort + 2`
  848. //变更分类
  849. if prevClassify != nil {
  850. _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, prevClassify.ChartClassifyId, prevClassify.Sort, updateSortStr, source)
  851. } else {
  852. _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, 0, prevSort, updateSortStr, source)
  853. }
  854. //变更图表
  855. if prevChartInfo != nil {
  856. //变更兄弟节点的排序
  857. _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, prevSort, prevChartInfo.ChartInfoId, updateSortStr)
  858. } else {
  859. _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, prevSort, 0, updateSortStr)
  860. }
  861. } else {
  862. //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
  863. if nextSort-prevSort == 1 {
  864. //变更兄弟节点的排序
  865. updateSortStr := `sort + 1`
  866. //变更分类
  867. if prevClassify != nil {
  868. _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, prevClassify.ChartClassifyId, prevSort, updateSortStr, source)
  869. } else {
  870. _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, 0, prevSort, updateSortStr, source)
  871. }
  872. //变更图表
  873. if prevChartInfo != nil {
  874. //变更兄弟节点的排序
  875. _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, prevSort, prevChartInfo.ChartInfoId, updateSortStr)
  876. } else {
  877. _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, prevSort, 0, updateSortStr)
  878. }
  879. }
  880. }
  881. }
  882. chartClassifyInfo.Sort = prevSort + 1
  883. chartClassifyInfo.ModifyTime = time.Now()
  884. updateCol = append(updateCol, "Sort", "ModifyTime")
  885. } else if prevClassify == nil && nextClassify == nil && prevChartInfo == nil && nextChartInfo == nil && parentClassifyId > 0 {
  886. //处理只拖动到目录里,默认放到目录底部的情况
  887. var maxSort int
  888. maxSort, err = GetChartClassifyMaxSort(parentClassifyId, source)
  889. if err != nil {
  890. errMsg = "移动失败"
  891. err = errors.New("查询组内排序信息失败,Err:" + err.Error())
  892. return
  893. }
  894. chartClassifyInfo.Sort = maxSort + 1 //那就是排在组内最后一位
  895. chartClassifyInfo.ModifyTime = time.Now()
  896. updateCol = append(updateCol, "Sort", "ModifyTime")
  897. } else {
  898. // 拖动到父级分类的第一位
  899. firstClassify, tmpErr := data_manage.GetFirstChartClassifyByParentId(parentClassifyId)
  900. if tmpErr != nil && !utils.IsErrNoRow(tmpErr) {
  901. errMsg = "移动失败"
  902. err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tmpErr.Error())
  903. return
  904. }
  905. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  906. if firstClassify != nil && firstClassify.ChartClassifyId > 0 && firstClassify.Sort == 0 {
  907. updateSortStr := ` sort + 1 `
  908. _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, firstClassify.ChartClassifyId-1, 0, updateSortStr, source)
  909. //该分类下的所有图表也需要+1
  910. _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, 0, 0, updateSortStr)
  911. } else {
  912. //如果该分类下存在图表,且第一个图表的排序等于0,那么需要调整排序
  913. firstEdb, tErr := data_manage.GetFirstChartInfoByClassifyId(parentClassifyId)
  914. if tErr != nil && !utils.IsErrNoRow(tErr) {
  915. errMsg = "移动失败"
  916. err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tErr.Error())
  917. return
  918. }
  919. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  920. if firstEdb != nil && firstEdb.ChartClassifyId > 0 && firstEdb.Sort == 0 {
  921. updateSortStr := ` sort + 1 `
  922. _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, 0, firstEdb.ChartInfoId-1, updateSortStr)
  923. _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, 0, 0, updateSortStr, source)
  924. }
  925. }
  926. chartClassifyInfo.Sort = 0 //那就是排在第一位
  927. chartClassifyInfo.ModifyTime = time.Now()
  928. updateCol = append(updateCol, "Sort", "ModifyTime")
  929. }
  930. //更新
  931. if len(updateCol) > 0 {
  932. err = chartClassifyInfo.Update(updateCol)
  933. if err != nil {
  934. errMsg = "移动失败"
  935. err = errors.New("修改失败,Err:" + err.Error())
  936. return
  937. }
  938. //更新对应分类的root_id和层级
  939. if oldParentId != parentClassifyId {
  940. if len(classifyIds) > 0 {
  941. levelStep := chartClassifyInfo.Level - oldLevel
  942. err = data_manage.UpdateEdbClassifyChildByParentClassifyId(classifyIds, chartClassifyInfo.RootId, levelStep)
  943. if err != nil {
  944. errMsg = "移动失败"
  945. err = errors.New("更新子分类失败,Err:" + err.Error())
  946. return
  947. }
  948. }
  949. }
  950. }
  951. } else {
  952. if chartInfo == nil {
  953. errMsg = "当前图表不存在"
  954. err = errors.New(errMsg)
  955. return
  956. }
  957. //如果改变了分类,那么移动该图表数据
  958. if chartInfo.ChartClassifyId != parentClassifyId {
  959. chartInfo.ChartClassifyId = parentClassifyId
  960. chartInfo.ModifyTime = time.Now()
  961. updateCol = append(updateCol, "ChartClassifyId", "ModifyTime")
  962. }
  963. if prevSort > 0 {
  964. //如果是移动在两个兄弟节点之间
  965. if nextSort > 0 {
  966. //下一个兄弟节点
  967. //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
  968. if prevSort == nextSort || prevSort == chartInfo.Sort {
  969. //变更兄弟节点的排序
  970. updateSortStr := `sort + 2`
  971. //变更分类
  972. if prevClassify != nil {
  973. _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, prevClassify.ChartClassifyId, prevClassify.Sort, updateSortStr, source)
  974. } else {
  975. _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, 0, prevSort, updateSortStr, source)
  976. }
  977. //变更图表
  978. if prevChartInfo != nil {
  979. //变更兄弟节点的排序
  980. _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, prevSort, prevChartInfo.ChartInfoId, updateSortStr)
  981. } else {
  982. _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, prevSort, 0, updateSortStr)
  983. }
  984. } else {
  985. //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
  986. if nextSort-prevSort == 1 {
  987. //变更兄弟节点的排序
  988. updateSortStr := `sort + 1`
  989. //变更分类
  990. if prevClassify != nil {
  991. _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, prevClassify.ChartClassifyId, prevSort, updateSortStr, source)
  992. } else {
  993. _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, 0, prevSort, updateSortStr, source)
  994. }
  995. //变更图表
  996. if prevChartInfo != nil {
  997. //变更兄弟节点的排序
  998. _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, prevSort, prevChartInfo.ChartInfoId, updateSortStr)
  999. } else {
  1000. _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, prevSort, 0, updateSortStr)
  1001. }
  1002. }
  1003. }
  1004. }
  1005. chartInfo.Sort = prevSort + 1
  1006. chartInfo.ModifyTime = time.Now()
  1007. updateCol = append(updateCol, "Sort", "ModifyTime")
  1008. } else if prevClassify == nil && nextClassify == nil && prevChartInfo == nil && nextChartInfo == nil && parentClassifyId > 0 {
  1009. //处理只拖动到目录里,默认放到目录底部的情况
  1010. var maxSort int
  1011. maxSort, err = GetChartClassifyMaxSort(parentClassifyId, source)
  1012. if err != nil {
  1013. errMsg = "移动失败"
  1014. err = errors.New("查询组内排序信息失败,Err:" + err.Error())
  1015. return
  1016. }
  1017. chartInfo.Sort = maxSort + 1 //那就是排在组内最后一位
  1018. chartInfo.ModifyTime = time.Now()
  1019. updateCol = append(updateCol, "Sort", "ModifyTime")
  1020. } else {
  1021. // 拖动到父级分类的第一位
  1022. firstClassify, tmpErr := data_manage.GetFirstChartClassifyByParentId(parentClassifyId)
  1023. if tmpErr != nil && !utils.IsErrNoRow(tmpErr) {
  1024. errMsg = "移动失败"
  1025. err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tmpErr.Error())
  1026. return
  1027. }
  1028. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  1029. if firstClassify != nil && firstClassify.ChartClassifyId > 0 && firstClassify.Sort == 0 {
  1030. updateSortStr := ` sort + 1 `
  1031. _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, firstClassify.ChartClassifyId-1, 0, updateSortStr, source)
  1032. //该分类下的所有图表也需要+1
  1033. _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, 0, 0, updateSortStr)
  1034. } else {
  1035. //如果该分类下存在图表,且第一个图表的排序等于0,那么需要调整排序
  1036. firstEdb, tErr := data_manage.GetFirstChartInfoByClassifyId(parentClassifyId)
  1037. if tErr != nil && !utils.IsErrNoRow(tErr) {
  1038. errMsg = "移动失败"
  1039. err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tErr.Error())
  1040. return
  1041. }
  1042. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  1043. if firstEdb != nil && firstEdb.ChartClassifyId > 0 && firstEdb.Sort == 0 {
  1044. updateSortStr := ` sort + 1 `
  1045. _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, 0, firstEdb.ChartInfoId-1, updateSortStr)
  1046. _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, 0, 0, updateSortStr, source)
  1047. }
  1048. }
  1049. chartInfo.Sort = 0 //那就是排在第一位
  1050. chartInfo.ModifyTime = time.Now()
  1051. updateCol = append(updateCol, "Sort", "ModifyTime")
  1052. }
  1053. //更新
  1054. if len(updateCol) > 0 {
  1055. err = chartInfo.Update(updateCol)
  1056. if err != nil {
  1057. errMsg = "移动失败"
  1058. err = errors.New("修改失败,Err:" + err.Error())
  1059. return
  1060. }
  1061. }
  1062. }
  1063. return
  1064. }
  1065. // moveChartClassify 移动图表分类
  1066. func moveChartClassify(parentChartClassifyInfo, chartClassifyInfo, prevClassify, nextClassify *data_manage.ChartClassify, prevChartInfo, nextChartInfo *data_manage.ChartInfo, parentClassifyId, prevSort, nextSort, source int) (err error, errMsg string) {
  1067. updateCol := make([]string, 0)
  1068. oldParentId := chartClassifyInfo.ParentId
  1069. oldLevel := chartClassifyInfo.Level
  1070. var classifyIds []int
  1071. if oldParentId != parentClassifyId {
  1072. //更新子分类对应的level
  1073. childList, e, m := GetChildChartClassifyByClassifyId(chartClassifyInfo.ChartClassifyId)
  1074. if e != nil {
  1075. errMsg = "移动失败"
  1076. err = errors.New("查询子分类失败,Err:" + e.Error() + m)
  1077. return
  1078. }
  1079. if len(childList) > 0 {
  1080. for _, v := range childList {
  1081. if v.ChartClassifyId == chartClassifyInfo.ChartClassifyId {
  1082. continue
  1083. }
  1084. classifyIds = append(classifyIds, v.ChartClassifyId)
  1085. }
  1086. }
  1087. }
  1088. //判断上级id是否一致,如果不一致的话,那么需要移动该分类层级
  1089. if chartClassifyInfo.ParentId != parentClassifyId && parentClassifyId != 0 {
  1090. if chartClassifyInfo.Level != parentChartClassifyInfo.Level+1 { //禁止层级调整
  1091. errMsg = "移动失败"
  1092. err = errors.New("不支持目录层级变更")
  1093. return
  1094. }
  1095. chartClassifyInfo.ParentId = parentChartClassifyInfo.ChartClassifyId
  1096. chartClassifyInfo.RootId = parentChartClassifyInfo.RootId
  1097. chartClassifyInfo.Level = parentChartClassifyInfo.Level + 1
  1098. chartClassifyInfo.ModifyTime = time.Now()
  1099. updateCol = append(updateCol, "ParentId", "RootId", "Level", "ModifyTime")
  1100. } else if chartClassifyInfo.ParentId != parentClassifyId && parentClassifyId == 0 {
  1101. errMsg = "移动失败"
  1102. err = errors.New("不支持目录层级变更")
  1103. return
  1104. }
  1105. if prevSort > 0 {
  1106. //如果是移动在两个兄弟节点之间
  1107. if nextSort > 0 {
  1108. //下一个兄弟节点
  1109. //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
  1110. if prevSort == nextSort || prevSort == chartClassifyInfo.Sort {
  1111. //变更兄弟节点的排序
  1112. updateSortStr := `sort + 2`
  1113. //变更分类
  1114. if prevClassify != nil {
  1115. _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, prevClassify.ChartClassifyId, prevClassify.Sort, updateSortStr, source)
  1116. } else {
  1117. _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, 0, prevSort, updateSortStr, source)
  1118. }
  1119. //变更图表
  1120. if prevChartInfo != nil {
  1121. //变更兄弟节点的排序
  1122. _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, prevSort, prevChartInfo.ChartInfoId, updateSortStr)
  1123. } else {
  1124. _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, prevSort, 0, updateSortStr)
  1125. }
  1126. } else {
  1127. //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
  1128. if nextSort-prevSort == 1 {
  1129. //变更兄弟节点的排序
  1130. updateSortStr := `sort + 1`
  1131. //变更分类
  1132. if prevClassify != nil {
  1133. _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, prevClassify.ChartClassifyId, prevSort, updateSortStr, source)
  1134. } else {
  1135. _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, 0, prevSort, updateSortStr, source)
  1136. }
  1137. //变更图表
  1138. if prevChartInfo != nil {
  1139. //变更兄弟节点的排序
  1140. _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, prevSort, prevChartInfo.ChartInfoId, updateSortStr)
  1141. } else {
  1142. _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, prevSort, 0, updateSortStr)
  1143. }
  1144. }
  1145. }
  1146. }
  1147. chartClassifyInfo.Sort = prevSort + 1
  1148. chartClassifyInfo.ModifyTime = time.Now()
  1149. updateCol = append(updateCol, "Sort", "ModifyTime")
  1150. } else if prevClassify == nil && nextClassify == nil && prevChartInfo == nil && nextChartInfo == nil && parentClassifyId > 0 {
  1151. //处理只拖动到目录里,默认放到目录底部的情况
  1152. var maxSort int
  1153. maxSort, err = GetChartClassifyMaxSort(parentClassifyId, source)
  1154. if err != nil {
  1155. errMsg = "移动失败"
  1156. err = errors.New("查询组内排序信息失败,Err:" + err.Error())
  1157. return
  1158. }
  1159. chartClassifyInfo.Sort = maxSort + 1 //那就是排在组内最后一位
  1160. chartClassifyInfo.ModifyTime = time.Now()
  1161. updateCol = append(updateCol, "Sort", "ModifyTime")
  1162. } else {
  1163. // 拖动到父级分类的第一位
  1164. firstClassify, tmpErr := data_manage.GetFirstChartClassifyByParentId(parentClassifyId)
  1165. if tmpErr != nil && !utils.IsErrNoRow(tmpErr) {
  1166. errMsg = "移动失败"
  1167. err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tmpErr.Error())
  1168. return
  1169. }
  1170. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  1171. if firstClassify != nil && firstClassify.ChartClassifyId > 0 && firstClassify.Sort == 0 {
  1172. updateSortStr := ` sort + 1 `
  1173. _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, firstClassify.ChartClassifyId-1, 0, updateSortStr, source)
  1174. //该分类下的所有图表也需要+1
  1175. _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, 0, 0, updateSortStr)
  1176. } else {
  1177. //如果该分类下存在图表,且第一个图表的排序等于0,那么需要调整排序
  1178. firstEdb, tErr := data_manage.GetFirstChartInfoByClassifyId(parentClassifyId)
  1179. if tErr != nil && !utils.IsErrNoRow(tErr) {
  1180. errMsg = "移动失败"
  1181. err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tErr.Error())
  1182. return
  1183. }
  1184. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  1185. if firstEdb != nil && firstEdb.ChartClassifyId > 0 && firstEdb.Sort == 0 {
  1186. updateSortStr := ` sort + 1 `
  1187. _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, 0, firstEdb.ChartInfoId-1, updateSortStr)
  1188. _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, 0, 0, updateSortStr, source)
  1189. }
  1190. }
  1191. chartClassifyInfo.Sort = 0 //那就是排在第一位
  1192. chartClassifyInfo.ModifyTime = time.Now()
  1193. updateCol = append(updateCol, "Sort", "ModifyTime")
  1194. }
  1195. oldClassifyIdPath := chartClassifyInfo.ChartClassifyIdPath
  1196. oldClassifyNamePath := chartClassifyInfo.ChartClassifyNamePath
  1197. newClassifyNamePath := fmt.Sprint(chartClassifyInfo.ChartClassifyNamePath, `|`, chartClassifyInfo.ChartClassifyName)
  1198. newClassifyIdPath := fmt.Sprint(chartClassifyInfo.ChartClassifyIdPath, `,`, chartClassifyInfo.ChartClassifyId)
  1199. //更新
  1200. if len(updateCol) > 0 {
  1201. err = chartClassifyInfo.Update(updateCol)
  1202. if err != nil {
  1203. errMsg = "移动失败"
  1204. err = errors.New("修改失败,Err:" + err.Error())
  1205. return
  1206. }
  1207. //更新对应分类的root_id和层级
  1208. if oldParentId != parentClassifyId {
  1209. if len(classifyIds) > 0 {
  1210. levelStep := chartClassifyInfo.Level - oldLevel
  1211. err = data_manage.UpdateEdbClassifyChildByParentClassifyId(classifyIds, chartClassifyInfo.RootId, levelStep)
  1212. if err != nil {
  1213. errMsg = "移动失败"
  1214. err = errors.New("更新子分类失败,Err:" + err.Error())
  1215. return
  1216. }
  1217. }
  1218. // 更改了上级分类,那么需要同步去更改自己的分类全路径
  1219. tmpErr := data_manage.UpdateChartClassifyNameAndNamePathByOldClassifyIdPath(oldClassifyIdPath, newClassifyIdPath, oldClassifyNamePath, newClassifyNamePath)
  1220. if tmpErr != nil {
  1221. utils.FileLog.Error(fmt.Sprintf("更新图表分类全路径失败,分类id:%d;失败原因Err:%s", chartClassifyInfo.ChartClassifyId, tmpErr.Error()))
  1222. }
  1223. }
  1224. }
  1225. return
  1226. }
  1227. func GetChildChartClassifyByClassifyId(targetClassifyId int) (targetList []*data_manage.ChartClassifyIdItems, err error, errMsg string) {
  1228. //判断是否是挂在顶级目录下
  1229. targetClassify, err := data_manage.GetChartClassifyById(targetClassifyId)
  1230. if err != nil {
  1231. if utils.IsErrNoRow(err) {
  1232. errMsg = "当前分类不存在"
  1233. err = errors.New(errMsg)
  1234. return
  1235. }
  1236. errMsg = "获取失败"
  1237. err = errors.New("获取分类信息失败,Err:" + err.Error())
  1238. return
  1239. }
  1240. orderStr := ` order by level asc, sort asc, chart_classify_id asc`
  1241. tmpList, err := data_manage.GetChartClassifyByRootIdLevel(targetClassify.RootId, targetClassify.Source, orderStr)
  1242. if err != nil && !utils.IsErrNoRow(err) {
  1243. errMsg = "获取失败"
  1244. err = errors.New("获取数据失败,Err:" + err.Error())
  1245. return
  1246. }
  1247. idMap := make(map[int]struct{})
  1248. if len(tmpList) > 0 {
  1249. for _, v := range tmpList {
  1250. if v.ChartClassifyId == targetClassify.ChartClassifyId {
  1251. idMap[v.ChartClassifyId] = struct{}{}
  1252. }
  1253. }
  1254. for _, v := range tmpList {
  1255. if _, ok := idMap[v.ParentId]; ok {
  1256. idMap[v.ChartClassifyId] = struct{}{}
  1257. }
  1258. }
  1259. for _, v := range tmpList {
  1260. if _, ok := idMap[v.ChartClassifyId]; ok {
  1261. targetItem := new(data_manage.ChartClassifyIdItems)
  1262. targetItem.ChartClassifyId = v.ChartClassifyId
  1263. targetItem.ParentId = v.ParentId
  1264. targetItem.RootId = v.RootId
  1265. targetItem.UniqueCode = v.UniqueCode
  1266. targetItem.Level = v.Level
  1267. targetItem.ChartClassifyName = v.ChartClassifyName
  1268. targetItem.IsJoinPermission = v.IsJoinPermission
  1269. targetList = append(targetList, targetItem)
  1270. }
  1271. }
  1272. }
  1273. return
  1274. }
  1275. func GetChartClassifyMaxSort(parentId, source int) (maxSort int, err error) {
  1276. //获取该层级下最大的排序数
  1277. classifyMaxSort, err := data_manage.GetChartClassifyMaxSort(parentId, source)
  1278. if err != nil {
  1279. return
  1280. }
  1281. maxSort = classifyMaxSort
  1282. edbMaxSort, err := data_manage.GetChartInfoMaxSortByClassifyId(parentId)
  1283. if err != nil {
  1284. return
  1285. }
  1286. if maxSort < edbMaxSort {
  1287. maxSort = edbMaxSort
  1288. }
  1289. return
  1290. }
  1291. // GetChartClassifyTreeRecursive 递归获取分类树形结构
  1292. func GetChartClassifyTreeRecursive(list []*data_manage.ChartClassifyItems, parentId int) []*data_manage.ChartClassifyItems {
  1293. res := make([]*data_manage.ChartClassifyItems, 0)
  1294. for _, v := range list {
  1295. if v.ParentId == parentId {
  1296. v.Children = GetChartClassifyTreeRecursive(list, v.ChartClassifyId)
  1297. res = append(res, v)
  1298. }
  1299. }
  1300. return res
  1301. }
  1302. // GetChartClassifyTreeRecursiveAndInsert
  1303. // @Description: 递归获取分类树形结构,同时根据分类id插入对应的图表数据
  1304. // @author: Roc
  1305. // @datetime 2024-12-12 14:42:28
  1306. // @param list []*data_manage.ChartClassifyItems
  1307. // @param parentId int
  1308. // @param chartClassifyItemListMap map[int][]*data_manage.ChartClassifyItems
  1309. // @return []*data_manage.ChartClassifyItems
  1310. func GetChartClassifyTreeRecursiveAndInsert(list []*data_manage.ChartClassifyItems, parentId int, chartClassifyItemListMap map[int][]*data_manage.ChartClassifyItems) []*data_manage.ChartClassifyItems {
  1311. // 先获取树形结构
  1312. list = GetChartClassifyTreeRecursive(list, parentId)
  1313. // 将图表数据插入到分类树中
  1314. InsertChartClassifyItemsToClassifyTree(list, chartClassifyItemListMap)
  1315. return list
  1316. }
  1317. // InsertChartClassifyItemsToClassifyTree
  1318. // @Description: 递归将图表数据插入到分类树中
  1319. // @author: Roc
  1320. // @datetime 2024-12-12 14:42:00
  1321. // @param list []*data_manage.ChartClassifyItems
  1322. // @param chartClassifyItemListMap map[int][]*data_manage.ChartClassifyItems
  1323. // @return []*data_manage.ChartClassifyItems
  1324. func InsertChartClassifyItemsToClassifyTree(list []*data_manage.ChartClassifyItems, chartClassifyItemListMap map[int][]*data_manage.ChartClassifyItems) []*data_manage.ChartClassifyItems {
  1325. for k, v := range list {
  1326. children := v.Children
  1327. if children != nil {
  1328. children = InsertChartClassifyItemsToClassifyTree(children, chartClassifyItemListMap)
  1329. } else {
  1330. children = make([]*data_manage.ChartClassifyItems, 0)
  1331. }
  1332. if chartClassifyItemList, ok := chartClassifyItemListMap[v.ChartClassifyId]; ok {
  1333. children = append(children, chartClassifyItemList...)
  1334. }
  1335. list[k].Children = children
  1336. }
  1337. return list
  1338. }
  1339. // GetChartClassifyParentRecursive 根据子目录递归父级目录
  1340. func GetChartClassifyParentRecursive(list []*data_manage.ChartClassifyItems, classifyId int) []*data_manage.ChartClassifyItems {
  1341. res := make([]*data_manage.ChartClassifyItems, 0)
  1342. for _, v := range list {
  1343. if v.ChartClassifyId == classifyId {
  1344. if v.ParentId > 0 {
  1345. res = GetChartClassifyParentRecursive(list, v.ParentId)
  1346. }
  1347. res = append(res, v)
  1348. }
  1349. }
  1350. return res
  1351. }
  1352. func GetChartClassifyChildIds(classifyId int, classifymap map[int]*data_manage.ChartClassifyItems, childIds *[]int) {
  1353. for _, item := range classifymap {
  1354. if item.ParentId == classifyId {
  1355. *childIds = append(*childIds, item.ChartClassifyId)
  1356. GetChartClassifyChildIds(item.ChartClassifyId, classifymap, childIds)
  1357. }
  1358. }
  1359. }
  1360. // GetAllChartChildClassifyByParentId
  1361. // @Description: 根据上级id获取所有的下级id
  1362. // @author: Roc
  1363. // @datetime 2024-12-02 15:14:13
  1364. // @param parentId int
  1365. // @return targetList []*data_manage.EdbClassifyItems
  1366. // @return err error
  1367. // @return errMsg string
  1368. func GetAllChartChildClassifyByParentId(parentId int) (targetList []*data_manage.ChartClassifyItems, err error, errMsg string) {
  1369. //判断是否是挂在顶级目录下
  1370. parentClassify, err := data_manage.GetChartClassifyById(parentId)
  1371. if err != nil {
  1372. if utils.IsErrNoRow(err) {
  1373. errMsg = "当前分类不存在"
  1374. err = errors.New(errMsg)
  1375. return
  1376. }
  1377. errMsg = "获取失败"
  1378. err = errors.New("获取分类信息失败,Err:" + err.Error())
  1379. return
  1380. }
  1381. orderStr := ` order by level asc, sort asc, chart_classify_id asc`
  1382. targetList, err = data_manage.GetChildChartClassifyListByParentId(fmt.Sprint(parentClassify.ChartClassifyIdPath, ",%"), orderStr)
  1383. return
  1384. }