chart_classify.go 58 KB

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