edb_collect_classify.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. type EdbCollectClassifyController struct {
  16. controllers.BaseAuthController
  17. }
  18. // @router /edb_collect/classify/list [get]
  19. func (this *EdbCollectClassifyController) List() {
  20. br := new(models.BaseResponse).Init()
  21. defer func() {
  22. if br.ErrMsg == "" {
  23. br.IsSendEmail = false
  24. }
  25. this.Data["json"] = br
  26. this.ServeJSON()
  27. }()
  28. sysUser := this.SysUser
  29. if sysUser == nil {
  30. br.Msg = "请登录"
  31. br.ErrMsg = "请登录,SysUser Is Empty"
  32. br.Ret = 408
  33. return
  34. }
  35. parentId, _ := this.GetInt("ParentId")
  36. resp := make([]*data_manage.EdbCollectClassifyListItem, 0)
  37. classifyOb := new(data_manage.EdbCollectClassify)
  38. {
  39. cond := fmt.Sprintf(" AND %s = ? AND %s = ?", classifyOb.Cols().ParentId, classifyOb.Cols().SysUserId)
  40. pars := make([]interface{}, 0)
  41. pars = append(pars, parentId, sysUser.AdminId)
  42. list, e := classifyOb.GetItemsByCondition(cond, pars, []string{}, fmt.Sprintf("%s ASC", classifyOb.Cols().Sort))
  43. if e != nil {
  44. br.Msg = "获取失败"
  45. br.ErrMsg = fmt.Sprintf("获取子分类失败, %v", e)
  46. return
  47. }
  48. for _, v := range list {
  49. resp = append(resp, &data_manage.EdbCollectClassifyListItem{
  50. NodeType: 1,
  51. ClassifyId: v.EdbCollectClassifyId,
  52. ClassifyName: v.ClassifyName,
  53. ParentId: v.ParentId,
  54. Level: v.Level,
  55. Sort: v.Sort,
  56. UniqueCode: v.UniqueCode,
  57. })
  58. }
  59. }
  60. {
  61. list, e := data_manage.GetCollectEdbInfoByClassifyId(parentId)
  62. if e != nil {
  63. br.Msg = "获取失败"
  64. br.ErrMsg = fmt.Sprintf("获取分类下指标失败, %v", e)
  65. return
  66. }
  67. for _, v := range list {
  68. resp = append(resp, &data_manage.EdbCollectClassifyListItem{
  69. NodeType: 2,
  70. EdbInfoId: v.EdbInfoId,
  71. EdbCode: v.EdbCode,
  72. EdbName: v.EdbName,
  73. ParentId: parentId,
  74. Sort: v.Sort,
  75. UniqueCode: v.UniqueCode,
  76. })
  77. }
  78. }
  79. sort.Slice(resp, func(i, j int) bool {
  80. return resp[i].Sort < resp[j].Sort
  81. })
  82. br.Data = resp
  83. br.Ret = 200
  84. br.Success = true
  85. br.Msg = "获取成功"
  86. }
  87. // @router /edb_collect/classify/tree [get]
  88. func (this *EdbCollectClassifyController) Tree() {
  89. br := new(models.BaseResponse).Init()
  90. defer func() {
  91. if br.ErrMsg == "" {
  92. br.IsSendEmail = false
  93. }
  94. this.Data["json"] = br
  95. this.ServeJSON()
  96. }()
  97. sysUser := this.SysUser
  98. if sysUser == nil {
  99. br.Msg = "请登录"
  100. br.ErrMsg = "请登录,SysUser Is Empty"
  101. br.Ret = 408
  102. return
  103. }
  104. classifyOb := new(data_manage.EdbCollectClassify)
  105. cond := fmt.Sprintf(" AND %s = ?", classifyOb.Cols().SysUserId)
  106. pars := make([]interface{}, 0)
  107. pars = append(pars, sysUser.AdminId)
  108. list, e := classifyOb.GetItemsByCondition(cond, pars, []string{}, fmt.Sprintf("%s ASC, %s ASC", classifyOb.Cols().ParentId, classifyOb.Cols().Sort))
  109. if e != nil {
  110. br.Msg = "获取失败"
  111. br.ErrMsg = fmt.Sprintf("获取分类列表失败, %v", e)
  112. return
  113. }
  114. items := make([]*data_manage.EdbCollectClassifyItem, 0)
  115. for _, v := range list {
  116. items = append(items, v.Format2Item())
  117. }
  118. tree := data.GetEdbCollectClassifyTreeRecursive(items, 0)
  119. br.Data = tree
  120. br.Ret = 200
  121. br.Success = true
  122. br.Msg = "获取成功"
  123. }
  124. // @router /edb_collect/classify/add [post]
  125. func (this *EdbCollectClassifyController) Add() {
  126. br := new(models.BaseResponse).Init()
  127. defer func() {
  128. if br.ErrMsg == "" {
  129. br.IsSendEmail = false
  130. }
  131. this.Data["json"] = br
  132. this.ServeJSON()
  133. }()
  134. sysUser := this.SysUser
  135. if sysUser == nil {
  136. br.Msg = "请登录"
  137. br.ErrMsg = "请登录,SysUser Is Empty"
  138. br.Ret = 408
  139. return
  140. }
  141. var req data_manage.EdbCollectClassifyAddReq
  142. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  143. br.Msg = "参数解析异常"
  144. br.ErrMsg = fmt.Sprintf("参数解析异常: %v", e)
  145. return
  146. }
  147. req.ClassifyName = strings.TrimSpace(req.ClassifyName)
  148. if req.ClassifyName == "" {
  149. br.Msg = "请输入名称"
  150. return
  151. }
  152. classifyOb := new(data_manage.EdbCollectClassify)
  153. var levelPath string
  154. var rootId int
  155. if req.ParentId > 0 {
  156. parent, e := classifyOb.GetItemById(req.ParentId)
  157. if e != nil {
  158. br.Msg = "上级分类有误"
  159. br.ErrMsg = fmt.Sprintf("获取上级分类失败, %v", e)
  160. return
  161. }
  162. levelPath = parent.LevelPath
  163. rootId = parent.RootId
  164. }
  165. sortMax, e := classifyOb.GetSortMax(req.ParentId)
  166. if e != nil {
  167. br.Msg = "操作失败"
  168. br.ErrMsg = fmt.Sprintf("获取分类最大排序失败, %v", e)
  169. return
  170. }
  171. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  172. classifyOb.ParentId = req.ParentId
  173. classifyOb.ClassifyName = req.ClassifyName
  174. classifyOb.Level = req.Level + 1
  175. classifyOb.Sort = sortMax + 1
  176. classifyOb.SysUserId = sysUser.AdminId
  177. classifyOb.SysUserRealName = sysUser.RealName
  178. classifyOb.UniqueCode = utils.MD5(classifyOb.TableName() + "_" + timestamp)
  179. classifyOb.CreateTime = time.Now().Local()
  180. classifyOb.ModifyTime = time.Now().Local()
  181. if e = classifyOb.Create(); e != nil {
  182. br.Msg = "操作失败"
  183. br.ErrMsg = fmt.Sprintf("新增分类失败, %v", e)
  184. return
  185. }
  186. if req.ParentId > 0 {
  187. classifyOb.LevelPath = fmt.Sprintf("%s,%d", levelPath, classifyOb.EdbCollectClassifyId)
  188. classifyOb.RootId = rootId
  189. } else {
  190. classifyOb.LevelPath = fmt.Sprint(classifyOb.EdbCollectClassifyId)
  191. classifyOb.RootId = classifyOb.EdbCollectClassifyId
  192. }
  193. if e = classifyOb.Update([]string{classifyOb.Cols().LevelPath, classifyOb.Cols().RootId}); e != nil {
  194. br.Msg = "操作失败"
  195. br.ErrMsg = fmt.Sprintf("更新分类失败, %v", e)
  196. return
  197. }
  198. br.Data = classifyOb.Format2Item()
  199. br.Ret = 200
  200. br.Success = true
  201. br.Msg = "操作成功"
  202. }
  203. // @router /edb_collect/classify/edit [post]
  204. func (this *EdbCollectClassifyController) Edit() {
  205. br := new(models.BaseResponse).Init()
  206. defer func() {
  207. if br.ErrMsg == "" {
  208. br.IsSendEmail = false
  209. }
  210. this.Data["json"] = br
  211. this.ServeJSON()
  212. }()
  213. sysUser := this.SysUser
  214. if sysUser == nil {
  215. br.Msg = "请登录"
  216. br.ErrMsg = "请登录,SysUser Is Empty"
  217. br.Ret = 408
  218. return
  219. }
  220. var req data_manage.EdbCollectClassifyEditReq
  221. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  222. br.Msg = "参数解析异常"
  223. br.ErrMsg = fmt.Sprintf("参数解析异常: %v", e)
  224. return
  225. }
  226. if req.ClassifyId < 0 {
  227. br.Msg = "请选择分类"
  228. return
  229. }
  230. req.ClassifyName = strings.TrimSpace(req.ClassifyName)
  231. if req.ClassifyName == "" {
  232. br.Msg = "请输入分类名称"
  233. return
  234. }
  235. classifyOb := new(data_manage.EdbCollectClassify)
  236. classifyItem, e := classifyOb.GetItemById(req.ClassifyId)
  237. if e != nil {
  238. if utils.IsErrNoRow(e) {
  239. br.Msg = "分类不存在, 请刷新页面"
  240. return
  241. }
  242. br.Msg = "操作失败"
  243. br.ErrMsg = fmt.Sprintf("获取分类失败, %v", e)
  244. return
  245. }
  246. classifyItem.ClassifyName = req.ClassifyName
  247. classifyItem.ModifyTime = time.Now().Local()
  248. updateCols := []string{classifyOb.Cols().ClassifyName, classifyOb.Cols().ModifyTime}
  249. if e = classifyItem.Update(updateCols); e != nil {
  250. br.Msg = "操作失败"
  251. br.ErrMsg = fmt.Sprintf("更新分类失败, %v", e)
  252. return
  253. }
  254. br.Data = classifyItem.Format2Item()
  255. br.Ret = 200
  256. br.Success = true
  257. br.Msg = "操作成功"
  258. }
  259. // @router /edb_collect/classify/remove [post]
  260. func (this *EdbCollectClassifyController) Remove() {
  261. br := new(models.BaseResponse).Init()
  262. defer func() {
  263. if br.ErrMsg == "" {
  264. br.IsSendEmail = false
  265. }
  266. this.Data["json"] = br
  267. this.ServeJSON()
  268. }()
  269. sysUser := this.SysUser
  270. if sysUser == nil {
  271. br.Msg = "请登录"
  272. br.ErrMsg = "请登录,SysUser Is Empty"
  273. br.Ret = 408
  274. return
  275. }
  276. var req data_manage.EdbCollectClassifyRemoveReq
  277. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  278. br.Msg = "参数解析异常"
  279. br.ErrMsg = fmt.Sprintf("参数解析异常, %v", e)
  280. return
  281. }
  282. if req.ClassifyId < 0 {
  283. br.Msg = "请选择分类"
  284. return
  285. }
  286. classifyOb := new(data_manage.EdbCollectClassify)
  287. _, e := classifyOb.GetItemById(req.ClassifyId)
  288. if e != nil {
  289. if utils.IsErrNoRow(e) {
  290. br.Ret = 200
  291. br.Success = true
  292. br.Msg = "操作成功"
  293. return
  294. }
  295. br.Msg = "操作失败"
  296. br.ErrMsg = fmt.Sprintf("获取分类失败, %v", e)
  297. return
  298. }
  299. classifyIds, e := data.GetEdbCollectClassifyChildIds(req.ClassifyId, sysUser.AdminId, true)
  300. if e != nil {
  301. br.Msg = "操作失败"
  302. br.ErrMsg = fmt.Sprintf("获取子分类IDs失败, %v", e)
  303. return
  304. }
  305. if len(classifyIds) == 0 {
  306. br.Ret = 200
  307. br.Success = true
  308. br.Msg = "操作成功"
  309. return
  310. }
  311. if e = classifyOb.RemoveClassifyAndCollect(classifyIds); e != nil {
  312. br.Msg = "操作失败"
  313. br.ErrMsg = fmt.Sprintf("移除分类及收藏失败, %v", e)
  314. return
  315. }
  316. br.Ret = 200
  317. br.Success = true
  318. br.Msg = "操作成功"
  319. }
  320. // @router /edb_collect/classify/move [post]
  321. func (this *EdbCollectClassifyController) Move() {
  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.EdbCollectClassifyMoveReq
  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. classifyOb := new(data_manage.EdbCollectClassify)
  344. item, e := classifyOb.GetItemById(req.ClassifyId)
  345. if e != nil {
  346. if utils.IsErrNoRow(e) {
  347. br.Msg = "分类不存在, 请刷新页面"
  348. return
  349. }
  350. br.Msg = "操作失败"
  351. br.ErrMsg = fmt.Sprintf("获取分类失败, %v", e)
  352. return
  353. }
  354. updateCol := make([]string, 0)
  355. if req.PrevClassifyId > 0 {
  356. prevClassify, err := data_manage.GetEdbCollectClassifyById(sysUser.AdminId, req.PrevClassifyId)
  357. if err != nil {
  358. br.Msg = "移动失败"
  359. br.ErrMsg = "获取上一个兄弟节点分类信息失败,Err:" + err.Error()
  360. return
  361. }
  362. if req.NextClassifyId > 0 {
  363. nextClassify, err := data_manage.GetEdbCollectClassifyById(sysUser.AdminId, req.NextClassifyId)
  364. if err != nil {
  365. br.Msg = "移动失败"
  366. br.ErrMsg = "获取下一个兄弟节点分类信息失败,Err:" + err.Error()
  367. return
  368. }
  369. if prevClassify.Sort == nextClassify.Sort || prevClassify.Sort == item.Sort {
  370. updateSortStr := `sort + 2`
  371. _ = data_manage.UpdateEdbCollectClassifySortByClassifyId(sysUser.AdminId, prevClassify.EdbCollectClassifyId, prevClassify.Sort, updateSortStr)
  372. } else {
  373. if nextClassify.Sort-prevClassify.Sort == 1 {
  374. updateSortStr := `sort + 1`
  375. _ = data_manage.UpdateEdbCollectClassifySortByClassifyId(sysUser.AdminId, 0, prevClassify.Sort, updateSortStr)
  376. }
  377. }
  378. }
  379. item.Sort = prevClassify.Sort + 1
  380. item.ModifyTime = time.Now()
  381. updateCol = append(updateCol, "Sort", "ModifyTime")
  382. } else {
  383. firstClassify, err := data_manage.GetFirstEdbCollectClassifyByAdminId(sysUser.AdminId)
  384. if err != nil && !utils.IsErrNoRow(err) {
  385. br.Msg = "移动失败"
  386. br.ErrMsg = "获取获取当前账号下的排序第一条的分类信息失败,Err:" + err.Error()
  387. return
  388. }
  389. if firstClassify != nil && firstClassify.EdbCollectClassifyId > 0 && firstClassify.Sort == 0 {
  390. updateSortStr := ` sort + 1 `
  391. _ = data_manage.UpdateEdbCollectClassifySortByClassifyId(sysUser.AdminId, firstClassify.EdbCollectClassifyId-1, 0, updateSortStr)
  392. }
  393. item.Sort = 0 //那就是排在第一位
  394. item.ModifyTime = time.Now()
  395. updateCol = append(updateCol, "Sort", "ModifyTime")
  396. }
  397. if len(updateCol) > 0 {
  398. if e = item.Update(updateCol); e != nil {
  399. br.Msg = "操作失败"
  400. br.ErrMsg = fmt.Sprintf("更新分类失败, %v", e)
  401. return
  402. }
  403. }
  404. br.Ret = 200
  405. br.Success = true
  406. br.Msg = "操作成功"
  407. }