collect_edb_classify.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. package data_manage
  2. import (
  3. "encoding/json"
  4. "eta_gn/eta_api/controllers"
  5. "eta_gn/eta_api/models"
  6. "eta_gn/eta_api/models/data_manage"
  7. "eta_gn/eta_api/services/data"
  8. "eta_gn/eta_api/utils"
  9. "fmt"
  10. "sort"
  11. "strconv"
  12. "strings"
  13. "time"
  14. )
  15. // EdbCollectClassifyController 指标收藏分类
  16. type EdbCollectClassifyController struct {
  17. controllers.BaseAuthController
  18. }
  19. // List
  20. // @Title 分类列表-含指标
  21. // @Description 分类列表-含指标
  22. // @Param ParentId query int false "父级ID"
  23. // @Success 200 {object} data_manage.EdbCollectClassifyListItem
  24. // @router /edb_collect/classify/list [get]
  25. func (this *EdbCollectClassifyController) List() {
  26. br := new(models.BaseResponse).Init()
  27. defer func() {
  28. if br.ErrMsg == "" {
  29. br.IsSendEmail = false
  30. }
  31. this.Data["json"] = br
  32. this.ServeJSON()
  33. }()
  34. sysUser := this.SysUser
  35. if sysUser == nil {
  36. br.Msg = "请登录"
  37. br.ErrMsg = "请登录,SysUser Is Empty"
  38. br.Ret = 408
  39. return
  40. }
  41. parentId, _ := this.GetInt("ParentId")
  42. resp := make([]*data_manage.EdbCollectClassifyListItem, 0)
  43. // 查询分类
  44. classifyOb := new(data_manage.EdbCollectClassify)
  45. {
  46. cond := fmt.Sprintf(" AND %s = ? AND %s = ?", classifyOb.Cols().ParentId, classifyOb.Cols().SysUserId)
  47. pars := make([]interface{}, 0)
  48. pars = append(pars, parentId, sysUser.AdminId)
  49. list, e := classifyOb.GetItemsByCondition(cond, pars, []string{}, fmt.Sprintf("%s ASC", classifyOb.Cols().Sort))
  50. if e != nil {
  51. br.Msg = "获取失败"
  52. br.ErrMsg = fmt.Sprintf("获取子分类失败, %v", e)
  53. return
  54. }
  55. for _, v := range list {
  56. resp = append(resp, &data_manage.EdbCollectClassifyListItem{
  57. NodeType: 1,
  58. ClassifyId: v.EdbCollectClassifyId,
  59. ClassifyName: v.ClassifyName,
  60. ParentId: v.ParentId,
  61. Level: v.Level,
  62. Sort: v.Sort,
  63. UniqueCode: v.UniqueCode,
  64. })
  65. }
  66. }
  67. // 查询分类下收藏的指标
  68. {
  69. list, e := data_manage.GetCollectEdbInfoByClassifyId(parentId)
  70. if e != nil {
  71. br.Msg = "获取失败"
  72. br.ErrMsg = fmt.Sprintf("获取分类下指标失败, %v", e)
  73. return
  74. }
  75. for _, v := range list {
  76. resp = append(resp, &data_manage.EdbCollectClassifyListItem{
  77. NodeType: 2,
  78. EdbInfoId: v.EdbInfoId,
  79. EdbCode: v.EdbCode,
  80. EdbName: v.EdbName,
  81. ParentId: parentId,
  82. Sort: v.Sort,
  83. UniqueCode: v.UniqueCode,
  84. })
  85. }
  86. }
  87. sort.Slice(resp, func(i, j int) bool {
  88. return resp[i].Sort < resp[j].Sort
  89. })
  90. br.Data = resp
  91. br.Ret = 200
  92. br.Success = true
  93. br.Msg = "获取成功"
  94. }
  95. // Tree
  96. // @Title 分类树
  97. // @Description 分类树
  98. // @Success 200 {object} data_manage.EdbCollectClassifyItem
  99. // @router /edb_collect/classify/tree [get]
  100. func (this *EdbCollectClassifyController) Tree() {
  101. br := new(models.BaseResponse).Init()
  102. defer func() {
  103. if br.ErrMsg == "" {
  104. br.IsSendEmail = false
  105. }
  106. this.Data["json"] = br
  107. this.ServeJSON()
  108. }()
  109. sysUser := this.SysUser
  110. if sysUser == nil {
  111. br.Msg = "请登录"
  112. br.ErrMsg = "请登录,SysUser Is Empty"
  113. br.Ret = 408
  114. return
  115. }
  116. // 获取所有分类
  117. classifyOb := new(data_manage.EdbCollectClassify)
  118. cond := fmt.Sprintf(" AND %s = ?", classifyOb.Cols().SysUserId)
  119. pars := make([]interface{}, 0)
  120. pars = append(pars, sysUser.AdminId)
  121. list, e := classifyOb.GetItemsByCondition(cond, pars, []string{}, fmt.Sprintf("%s ASC, %s ASC", classifyOb.Cols().ParentId, classifyOb.Cols().Sort))
  122. if e != nil {
  123. br.Msg = "获取失败"
  124. br.ErrMsg = fmt.Sprintf("获取分类列表失败, %v", e)
  125. return
  126. }
  127. items := make([]*data_manage.EdbCollectClassifyItem, 0)
  128. for _, v := range list {
  129. items = append(items, v.Format2Item())
  130. }
  131. tree := data.GetEdbCollectClassifyTreeRecursive(items, 0)
  132. br.Data = tree
  133. br.Ret = 200
  134. br.Success = true
  135. br.Msg = "获取成功"
  136. }
  137. // Add
  138. // @Title 新增分类
  139. // @Description 新增分类
  140. // @Param request body data_manage.EdbCollectClassifyAddReq true "type json string"
  141. // @Success Ret=200 保存成功
  142. // @router /edb_collect/classify/add [post]
  143. func (this *EdbCollectClassifyController) Add() {
  144. br := new(models.BaseResponse).Init()
  145. defer func() {
  146. if br.ErrMsg == "" {
  147. br.IsSendEmail = false
  148. }
  149. this.Data["json"] = br
  150. this.ServeJSON()
  151. }()
  152. sysUser := this.SysUser
  153. if sysUser == nil {
  154. br.Msg = "请登录"
  155. br.ErrMsg = "请登录,SysUser Is Empty"
  156. br.Ret = 408
  157. return
  158. }
  159. var req data_manage.EdbCollectClassifyAddReq
  160. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  161. br.Msg = "参数解析异常"
  162. br.ErrMsg = fmt.Sprintf("参数解析异常: %v", e)
  163. return
  164. }
  165. req.ClassifyName = strings.TrimSpace(req.ClassifyName)
  166. if req.ClassifyName == "" {
  167. br.Msg = "请输入名称"
  168. return
  169. }
  170. classifyOb := new(data_manage.EdbCollectClassify)
  171. // 校验分类名称
  172. {
  173. cond := fmt.Sprintf(" AND %s = ? AND %s = ? AND %s = ? ", classifyOb.Cols().ParentId, classifyOb.Cols().ClassifyName, classifyOb.Cols().SysUserId)
  174. pars := make([]interface{}, 0)
  175. pars = append(pars, req.ParentId, req.ClassifyName, this.SysUser.AdminId)
  176. count, e := classifyOb.GetCountByCondition(cond, pars)
  177. if e != nil {
  178. br.Msg = "操作失败"
  179. br.ErrMsg = fmt.Sprintf("获取分类名称重复数失败, %v", e)
  180. return
  181. }
  182. if count > 0 {
  183. br.Msg = "分类名称已存在"
  184. return
  185. }
  186. }
  187. // 层级路径
  188. var levelPath string
  189. var rootId int
  190. if req.ParentId > 0 {
  191. parent, e := classifyOb.GetItemById(req.ParentId)
  192. if e != nil {
  193. br.Msg = "上级分类有误"
  194. br.ErrMsg = fmt.Sprintf("获取上级分类失败, %v", e)
  195. return
  196. }
  197. levelPath = parent.LevelPath
  198. rootId = parent.RootId
  199. }
  200. sortMax, e := classifyOb.GetSortMax(req.ParentId)
  201. if e != nil {
  202. br.Msg = "操作失败"
  203. br.ErrMsg = fmt.Sprintf("获取分类最大排序失败, %v", e)
  204. return
  205. }
  206. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  207. classifyOb.ParentId = req.ParentId
  208. classifyOb.ClassifyName = req.ClassifyName
  209. classifyOb.Level = req.Level + 1
  210. classifyOb.Sort = sortMax + 1
  211. classifyOb.SysUserId = sysUser.AdminId
  212. classifyOb.SysUserRealName = sysUser.RealName
  213. classifyOb.UniqueCode = utils.MD5(classifyOb.TableName() + "_" + timestamp)
  214. classifyOb.CreateTime = time.Now().Local()
  215. classifyOb.ModifyTime = time.Now().Local()
  216. if e = classifyOb.Create(); e != nil {
  217. br.Msg = "操作失败"
  218. br.ErrMsg = fmt.Sprintf("新增分类失败, %v", e)
  219. return
  220. }
  221. if req.ParentId > 0 {
  222. classifyOb.LevelPath = fmt.Sprintf("%s,%d", levelPath, classifyOb.EdbCollectClassifyId)
  223. classifyOb.RootId = rootId
  224. } else {
  225. classifyOb.LevelPath = fmt.Sprint(classifyOb.EdbCollectClassifyId)
  226. classifyOb.RootId = classifyOb.EdbCollectClassifyId
  227. }
  228. if e = classifyOb.Update([]string{classifyOb.Cols().LevelPath, classifyOb.Cols().RootId}); e != nil {
  229. br.Msg = "操作失败"
  230. br.ErrMsg = fmt.Sprintf("更新分类失败, %v", e)
  231. return
  232. }
  233. br.Data = classifyOb.Format2Item()
  234. br.Ret = 200
  235. br.Success = true
  236. br.Msg = "操作成功"
  237. }
  238. // Edit
  239. // @Title 编辑分类
  240. // @Description 编辑分类
  241. // @Param request body data_manage.EdbCollectClassifyEditReq true "type json string"
  242. // @Success Ret=200 保存成功
  243. // @router /edb_collect/classify/edit [post]
  244. func (this *EdbCollectClassifyController) Edit() {
  245. br := new(models.BaseResponse).Init()
  246. defer func() {
  247. if br.ErrMsg == "" {
  248. br.IsSendEmail = false
  249. }
  250. this.Data["json"] = br
  251. this.ServeJSON()
  252. }()
  253. sysUser := this.SysUser
  254. if sysUser == nil {
  255. br.Msg = "请登录"
  256. br.ErrMsg = "请登录,SysUser Is Empty"
  257. br.Ret = 408
  258. return
  259. }
  260. var req data_manage.EdbCollectClassifyEditReq
  261. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  262. br.Msg = "参数解析异常"
  263. br.ErrMsg = fmt.Sprintf("参数解析异常: %v", e)
  264. return
  265. }
  266. if req.ClassifyId < 0 {
  267. br.Msg = "请选择分类"
  268. return
  269. }
  270. req.ClassifyName = strings.TrimSpace(req.ClassifyName)
  271. if req.ClassifyName == "" {
  272. br.Msg = "请输入分类名称"
  273. return
  274. }
  275. classifyOb := new(data_manage.EdbCollectClassify)
  276. classifyItem, e := classifyOb.GetItemById(req.ClassifyId)
  277. if e != nil {
  278. if utils.IsErrNoRow(e) {
  279. br.Msg = "分类不存在, 请刷新页面"
  280. return
  281. }
  282. br.Msg = "操作失败"
  283. br.ErrMsg = fmt.Sprintf("获取分类失败, %v", e)
  284. return
  285. }
  286. // 校验分类名称
  287. {
  288. cond := fmt.Sprintf(" AND %s != ? AND %s = ? AND %s = ? AND %s = ? ", classifyOb.Cols().PrimaryId, classifyOb.Cols().ParentId, classifyOb.Cols().ClassifyName, classifyOb.Cols().SysUserId)
  289. pars := make([]interface{}, 0)
  290. pars = append(pars, classifyItem.EdbCollectClassifyId, classifyItem.ParentId, req.ClassifyName, this.SysUser.AdminId)
  291. count, e := classifyOb.GetCountByCondition(cond, pars)
  292. if e != nil {
  293. br.Msg = "操作失败"
  294. br.ErrMsg = fmt.Sprintf("获取分类名称重复数失败, %v", e)
  295. return
  296. }
  297. if count > 0 {
  298. br.Msg = "分类名称已存在"
  299. return
  300. }
  301. }
  302. classifyItem.ClassifyName = req.ClassifyName
  303. classifyItem.ModifyTime = time.Now().Local()
  304. updateCols := []string{classifyOb.Cols().ClassifyName, classifyOb.Cols().ModifyTime}
  305. if e = classifyItem.Update(updateCols); e != nil {
  306. br.Msg = "操作失败"
  307. br.ErrMsg = fmt.Sprintf("更新分类失败, %v", e)
  308. return
  309. }
  310. br.Data = classifyItem.Format2Item()
  311. br.Ret = 200
  312. br.Success = true
  313. br.Msg = "操作成功"
  314. }
  315. // Remove
  316. // @Title 删除分类
  317. // @Description 删除分类
  318. // @Param request body data_manage.EdbCollectClassifyRemoveReq true "type json string"
  319. // @Success 200 string "操作成功"
  320. // @router /edb_collect/classify/remove [post]
  321. func (this *EdbCollectClassifyController) Remove() {
  322. br := new(models.BaseResponse).Init()
  323. defer func() {
  324. if br.ErrMsg == "" {
  325. br.IsSendEmail = false
  326. }
  327. this.Data["json"] = br
  328. this.ServeJSON()
  329. }()
  330. sysUser := this.SysUser
  331. if sysUser == nil {
  332. br.Msg = "请登录"
  333. br.ErrMsg = "请登录,SysUser Is Empty"
  334. br.Ret = 408
  335. return
  336. }
  337. var req data_manage.EdbCollectClassifyRemoveReq
  338. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  339. br.Msg = "参数解析异常"
  340. br.ErrMsg = fmt.Sprintf("参数解析异常, %v", e)
  341. return
  342. }
  343. if req.ClassifyId < 0 {
  344. br.Msg = "请选择分类"
  345. return
  346. }
  347. classifyOb := new(data_manage.EdbCollectClassify)
  348. _, e := classifyOb.GetItemById(req.ClassifyId)
  349. if e != nil {
  350. if utils.IsErrNoRow(e) {
  351. br.Ret = 200
  352. br.Success = true
  353. br.Msg = "操作成功"
  354. return
  355. }
  356. br.Msg = "操作失败"
  357. br.ErrMsg = fmt.Sprintf("获取分类失败, %v", e)
  358. return
  359. }
  360. // 获取子分类IDs
  361. classifyIds, e := data.GetEdbCollectClassifyChildIds(req.ClassifyId, sysUser.AdminId, true)
  362. if e != nil {
  363. br.Msg = "操作失败"
  364. br.ErrMsg = fmt.Sprintf("获取子分类IDs失败, %v", e)
  365. return
  366. }
  367. if len(classifyIds) == 0 {
  368. br.Ret = 200
  369. br.Success = true
  370. br.Msg = "操作成功"
  371. return
  372. }
  373. // 移除所有子分类以及子分类所收藏的指标
  374. if e = classifyOb.RemoveClassifyAndCollect(classifyIds); e != nil {
  375. br.Msg = "操作失败"
  376. br.ErrMsg = fmt.Sprintf("移除分类及收藏失败, %v", e)
  377. return
  378. }
  379. br.Ret = 200
  380. br.Success = true
  381. br.Msg = "操作成功"
  382. }
  383. // Move
  384. // @Title 移动分类
  385. // @Description 移动分类
  386. // @Param request body data_manage.EdbCollectClassifyMoveReq true "type json string"
  387. // @Success 200 string "操作成功"
  388. // @router /edb_collect/classify/move [post]
  389. func (this *EdbCollectClassifyController) Move() {
  390. br := new(models.BaseResponse).Init()
  391. defer func() {
  392. if br.ErrMsg == "" {
  393. br.IsSendEmail = false
  394. }
  395. this.Data["json"] = br
  396. this.ServeJSON()
  397. }()
  398. sysUser := this.SysUser
  399. if sysUser == nil {
  400. br.Msg = "请登录"
  401. br.ErrMsg = "请登录,SysUser Is Empty"
  402. br.Ret = 408
  403. return
  404. }
  405. var req data_manage.EdbCollectClassifyMoveReq
  406. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  407. br.Msg = "参数解析异常!"
  408. br.ErrMsg = fmt.Sprintf("参数解析失败, %v", e)
  409. return
  410. }
  411. classifyOb := new(data_manage.EdbCollectClassify)
  412. item, e := classifyOb.GetItemById(req.ClassifyId)
  413. if e != nil {
  414. if utils.IsErrNoRow(e) {
  415. br.Msg = "分类不存在, 请刷新页面"
  416. return
  417. }
  418. br.Msg = "操作失败"
  419. br.ErrMsg = fmt.Sprintf("获取分类失败, %v", e)
  420. return
  421. }
  422. updateCol := make([]string, 0)
  423. //如果有传入 上一个兄弟节点分类id
  424. if req.PrevClassifyId > 0 {
  425. prevClassify, err := data_manage.GetEdbCollectClassifyById(sysUser.AdminId, req.PrevClassifyId)
  426. if err != nil {
  427. br.Msg = "移动失败"
  428. br.ErrMsg = "获取上一个兄弟节点分类信息失败,Err:" + err.Error()
  429. return
  430. }
  431. //如果是移动在两个兄弟节点之间
  432. if req.NextClassifyId > 0 {
  433. //下一个兄弟节点
  434. nextClassify, err := data_manage.GetEdbCollectClassifyById(sysUser.AdminId, req.NextClassifyId)
  435. if err != nil {
  436. br.Msg = "移动失败"
  437. br.ErrMsg = "获取下一个兄弟节点分类信息失败,Err:" + err.Error()
  438. return
  439. }
  440. //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
  441. if prevClassify.Sort == nextClassify.Sort || prevClassify.Sort == item.Sort {
  442. //变更兄弟节点的排序
  443. updateSortStr := `sort + 2`
  444. _ = data_manage.UpdateEdbCollectClassifySortByClassifyId(sysUser.AdminId, prevClassify.EdbCollectClassifyId, prevClassify.Sort, updateSortStr)
  445. } else {
  446. //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
  447. if nextClassify.Sort-prevClassify.Sort == 1 {
  448. //变更兄弟节点的排序
  449. updateSortStr := `sort + 1`
  450. _ = data_manage.UpdateEdbCollectClassifySortByClassifyId(sysUser.AdminId, 0, prevClassify.Sort, updateSortStr)
  451. }
  452. }
  453. }
  454. item.Sort = prevClassify.Sort + 1
  455. item.ModifyTime = time.Now()
  456. updateCol = append(updateCol, "Sort", "ModifyTime")
  457. } else {
  458. firstClassify, err := data_manage.GetFirstEdbCollectClassifyByAdminId(sysUser.AdminId)
  459. if err != nil && !utils.IsErrNoRow(err) {
  460. br.Msg = "移动失败"
  461. br.ErrMsg = "获取获取当前账号下的排序第一条的分类信息失败,Err:" + err.Error()
  462. return
  463. }
  464. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  465. if firstClassify != nil && firstClassify.EdbCollectClassifyId > 0 && firstClassify.Sort == 0 {
  466. updateSortStr := ` sort + 1 `
  467. _ = data_manage.UpdateEdbCollectClassifySortByClassifyId(sysUser.AdminId, firstClassify.EdbCollectClassifyId-1, 0, updateSortStr)
  468. }
  469. item.Sort = 0 //那就是排在第一位
  470. item.ModifyTime = time.Now()
  471. updateCol = append(updateCol, "Sort", "ModifyTime")
  472. }
  473. if len(updateCol) > 0 {
  474. if e = item.Update(updateCol); e != nil {
  475. br.Msg = "操作失败"
  476. br.ErrMsg = fmt.Sprintf("更新分类失败, %v", e)
  477. return
  478. }
  479. }
  480. br.Ret = 200
  481. br.Success = true
  482. br.Msg = "操作成功"
  483. }