chart_framework.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  1. package data_manage
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/controllers"
  5. "eta/eta_api/models"
  6. "eta/eta_api/models/data_manage"
  7. "eta/eta_api/models/system"
  8. "eta/eta_api/services/data"
  9. "eta/eta_api/utils"
  10. "fmt"
  11. "github.com/rdlucklib/rdluck_tools/paging"
  12. "strings"
  13. "time"
  14. )
  15. // ChartFrameworkController 图库框架
  16. type ChartFrameworkController struct {
  17. controllers.BaseAuthController
  18. }
  19. // List
  20. // @Title 列表
  21. // @Description 列表
  22. // @Param AdminId query int false "创建人ID"
  23. // @Param Visibility query int false "范围: 0-所有; 1-私有; 2-公开"
  24. // @Param Keyword query string false "关键词"
  25. // @Success 200 Ret=200 获取成功
  26. // @router /list [get]
  27. func (this *ChartFrameworkController) List() {
  28. br := new(models.BaseResponse).Init()
  29. defer func() {
  30. if br.ErrMsg == "" {
  31. br.IsSendEmail = false
  32. }
  33. this.Data["json"] = br
  34. this.ServeJSON()
  35. }()
  36. sysUser := this.SysUser
  37. if sysUser == nil {
  38. br.Msg = "请登录"
  39. br.ErrMsg = "请登录,SysUser Is Empty"
  40. br.Ret = 408
  41. return
  42. }
  43. pageSize, _ := this.GetInt("PageSize")
  44. currentIndex, _ := this.GetInt("CurrentIndex")
  45. adminId, _ := this.GetInt("AdminId")
  46. visibility, _ := this.GetInt("Visibility")
  47. if visibility == 1 && adminId <= 0 {
  48. adminId = sysUser.AdminId
  49. }
  50. keyword := this.GetString("Keyword")
  51. keyword = strings.TrimSpace(keyword)
  52. frameworkOb := new(data_manage.ChartFramework)
  53. cond := ``
  54. pars := make([]interface{}, 0)
  55. if adminId > 0 {
  56. cond += fmt.Sprintf(` AND %s = ?`, data_manage.ChartFrameworkColumns.AdminId)
  57. pars = append(pars, adminId)
  58. }
  59. if keyword != "" {
  60. cond += fmt.Sprintf(` AND %s LIKE ?`, data_manage.ChartFrameworkColumns.FrameworkName)
  61. pars = append(pars, "%"+keyword+"%")
  62. }
  63. if visibility > 0 {
  64. visibilityArr := map[int]int{1: 0, 2: 1}
  65. cond += fmt.Sprintf(` AND %s = ?`, data_manage.ChartFrameworkColumns.IsPublic)
  66. pars = append(pars, visibilityArr[visibility])
  67. }
  68. var total, startSize int
  69. if pageSize <= 0 {
  70. pageSize = utils.PageSize15
  71. }
  72. if currentIndex <= 0 {
  73. currentIndex = 1
  74. }
  75. startSize = paging.StartIndex(currentIndex, pageSize)
  76. t, e := frameworkOb.GetCountByCondition(cond, pars)
  77. if e != nil {
  78. br.Msg = "获取失败"
  79. br.ErrMsg = "获取框架列表总数失败, Err: " + e.Error()
  80. return
  81. }
  82. total = t
  83. orderRule := `sort ASC, create_time DESC`
  84. list, e := frameworkOb.GetPageItemsByCondition(cond, pars, []string{}, orderRule, startSize, pageSize)
  85. if e != nil {
  86. br.Msg = "获取失败"
  87. br.ErrMsg = "获取框架列表失败, Err: " + e.Error()
  88. return
  89. }
  90. resp := new(data_manage.ChartFrameworkListResp)
  91. resp.List = make([]*data_manage.ChartFrameworkItem, 0)
  92. for _, v := range list {
  93. t := data_manage.FormatChartFramework2Item(v, make([]*data_manage.ChartFrameworkNodeItem, 0))
  94. resp.List = append(resp.List, t)
  95. }
  96. resp.Paging = paging.GetPaging(currentIndex, pageSize, total)
  97. br.Data = resp
  98. br.Ret = 200
  99. br.Success = true
  100. br.Msg = "获取成功"
  101. }
  102. // PublicMenu
  103. // @Title 公开框架目录
  104. // @Description 公开框架目录
  105. // @Success 200 Ret=200 获取成功
  106. // @router /public_menu [get]
  107. func (this *ChartFrameworkController) PublicMenu() {
  108. br := new(models.BaseResponse).Init()
  109. defer func() {
  110. if br.ErrMsg == "" {
  111. br.IsSendEmail = false
  112. }
  113. this.Data["json"] = br
  114. this.ServeJSON()
  115. }()
  116. sysUser := this.SysUser
  117. if sysUser == nil {
  118. br.Msg = "请登录"
  119. br.ErrMsg = "请登录,SysUser Is Empty"
  120. br.Ret = 408
  121. return
  122. }
  123. frameworkOb := new(data_manage.ChartFramework)
  124. cond := fmt.Sprintf(` AND %s = ?`, data_manage.ChartFrameworkColumns.IsPublic)
  125. pars := make([]interface{}, 0)
  126. pars = append(pars, 1)
  127. orderRule := `public_time ASC, sort ASC, create_time DESC`
  128. list, e := frameworkOb.GetItemsByCondition(cond, pars, []string{}, orderRule)
  129. if e != nil {
  130. br.Msg = "获取失败"
  131. br.ErrMsg = "获取公开框架失败, Err: " + e.Error()
  132. return
  133. }
  134. // 用户被删除不展示框架
  135. admins, e := system.GetSysAdminList(``, make([]interface{}, 0), []string{}, "")
  136. if e != nil {
  137. br.Msg = "获取失败"
  138. br.ErrMsg = "获取系统用户列表失败, Err: " + e.Error()
  139. return
  140. }
  141. adminExist := make(map[int]bool)
  142. for _, v := range admins {
  143. adminExist[v.AdminId] = true
  144. }
  145. userExist := make(map[int]bool)
  146. userFrameworks := make(map[int][]*data_manage.ChartFrameworkItem)
  147. resp := make([]*data_manage.ChartFrameworkPublicMenuItem, 0)
  148. for _, v := range list {
  149. if !adminExist[v.AdminId] {
  150. continue
  151. }
  152. if !userExist[v.AdminId] {
  153. u := new(data_manage.ChartFrameworkPublicMenuItem)
  154. u.AdminId = v.AdminId
  155. u.MenuName = fmt.Sprintf("%s的框架", v.AdminName)
  156. resp = append(resp, u)
  157. userExist[v.AdminId] = true
  158. }
  159. t := data_manage.FormatChartFramework2Item(v, make([]*data_manage.ChartFrameworkNodeItem, 0))
  160. if userFrameworks[v.AdminId] == nil {
  161. userFrameworks[v.AdminId] = make([]*data_manage.ChartFrameworkItem, 0)
  162. }
  163. userFrameworks[v.AdminId] = append(userFrameworks[v.AdminId], t)
  164. }
  165. for _, v := range resp {
  166. v.Frameworks = userFrameworks[v.AdminId]
  167. }
  168. br.Data = resp
  169. br.Ret = 200
  170. br.Success = true
  171. br.Msg = "获取成功"
  172. }
  173. // Add
  174. // @Title 新增框架
  175. // @Description 新增框架
  176. // @Param request body data_manage.ChartFrameworkAddReq true "type json string"
  177. // @Success 200 Ret=200 操作成功
  178. // @router /add [post]
  179. func (this *ChartFrameworkController) Add() {
  180. br := new(models.BaseResponse).Init()
  181. defer func() {
  182. if br.ErrMsg == "" {
  183. br.IsSendEmail = false
  184. }
  185. this.Data["json"] = br
  186. this.ServeJSON()
  187. }()
  188. sysUser := this.SysUser
  189. if sysUser == nil {
  190. br.Msg = "请登录"
  191. br.ErrMsg = "请登录,SysUser Is Empty"
  192. br.Ret = 408
  193. return
  194. }
  195. // 参数校验
  196. var req data_manage.ChartFrameworkAddReq
  197. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  198. br.Msg = "参数解析异常!"
  199. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  200. return
  201. }
  202. req.FrameworkName = strings.TrimSpace(req.FrameworkName)
  203. if req.FrameworkName == "" {
  204. br.Msg = "框架名称不可为空"
  205. return
  206. }
  207. // 重名校验
  208. {
  209. ob := new(data_manage.ChartFramework)
  210. cond := fmt.Sprintf(` AND %s = ? AND %s = ?`, data_manage.ChartFrameworkColumns.FrameworkName, data_manage.ChartFrameworkColumns.AdminId)
  211. pars := make([]interface{}, 0)
  212. pars = append(pars, req.FrameworkName, sysUser.AdminId)
  213. exist, e := ob.GetItemByCondition(cond, pars)
  214. if e != nil && e.Error() != utils.ErrNoRow() {
  215. br.Msg = "操作失败"
  216. br.ErrMsg = "获取重名框架失败, Err: " + e.Error()
  217. return
  218. }
  219. if exist != nil {
  220. br.Msg = "框架名称已存在"
  221. return
  222. }
  223. }
  224. // 获取图表分类下各自的图表数
  225. chartsNumMap, e := data.GetMyChartClassifyIdNumMap(sysUser.AdminId)
  226. if e != nil {
  227. br.Msg = "获取失败"
  228. br.ErrMsg = "获取失败, GetMyChartClassifyIdNumMap Err: " + e.Error()
  229. return
  230. }
  231. now := time.Now().Local()
  232. frameworkCode := utils.MD5(fmt.Sprint(now.UnixMilli()))
  233. item := new(data_manage.ChartFramework)
  234. item.FrameworkName = req.FrameworkName
  235. item.FrameworkCode = frameworkCode
  236. item.FrameworkImg = req.FrameworkImg
  237. item.FrameworkContent = req.FrameworkContent
  238. item.AdminId = sysUser.AdminId
  239. item.AdminName = sysUser.RealName
  240. item.CreateTime = now
  241. item.ModifyTime = now
  242. nodes := make([]*data_manage.ChartFrameworkNode, 0)
  243. itemNodes := make([]*data_manage.ChartFrameworkNodeItem, 0)
  244. if len(req.Nodes) > 0 {
  245. for _, v := range req.Nodes {
  246. if v.MyChartClassifyId <= 0 {
  247. continue
  248. }
  249. t := new(data_manage.ChartFrameworkNode)
  250. t.FrameworkName = req.FrameworkName
  251. t.NodeId = v.NodeId
  252. t.NodeName = v.NodeName
  253. t.MyChartClassifyId = v.MyChartClassifyId
  254. t.CreateTime = now
  255. nodes = append(nodes, t)
  256. num, ok := chartsNumMap[v.MyChartClassifyId]
  257. if !ok {
  258. num = 0
  259. }
  260. // 响应节点数据
  261. td := data_manage.FormatChartFrameworkNode2Item(t, num)
  262. itemNodes = append(itemNodes, td)
  263. }
  264. }
  265. if e := item.CreateFrameworkAndNodes(item, nodes); e != nil {
  266. br.Msg = "操作失败"
  267. br.ErrMsg = "新增框架及节点失败, Err: " + e.Error()
  268. return
  269. }
  270. detail := data_manage.FormatChartFramework2Item(item, itemNodes)
  271. br.Data = detail
  272. br.Ret = 200
  273. br.Success = true
  274. br.Msg = "操作成功"
  275. br.IsAddLog = true
  276. }
  277. // Edit
  278. // @Title 编辑框架
  279. // @Description 编辑框架
  280. // @Param request body data_manage.ChartFrameworkEditReq true "type json string"
  281. // @Success 200 Ret=200 操作成功
  282. // @router /edit [post]
  283. func (this *ChartFrameworkController) Edit() {
  284. br := new(models.BaseResponse).Init()
  285. defer func() {
  286. if br.ErrMsg == "" {
  287. br.IsSendEmail = false
  288. }
  289. this.Data["json"] = br
  290. this.ServeJSON()
  291. }()
  292. sysUser := this.SysUser
  293. if sysUser == nil {
  294. br.Msg = "请登录"
  295. br.ErrMsg = "请登录,SysUser Is Empty"
  296. br.Ret = 408
  297. return
  298. }
  299. // 参数校验
  300. var req data_manage.ChartFrameworkEditReq
  301. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  302. br.Msg = "参数解析异常!"
  303. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  304. return
  305. }
  306. if req.ChartFrameworkId <= 0 {
  307. br.Msg = "参数有误"
  308. br.ErrMsg = fmt.Sprintf("参数有误, ChartFrameworkId: %d", req.ChartFrameworkId)
  309. return
  310. }
  311. req.FrameworkName = strings.TrimSpace(req.FrameworkName)
  312. if req.FrameworkName == "" {
  313. br.Msg = "框架名称不可为空"
  314. return
  315. }
  316. frameworkOb := new(data_manage.ChartFramework)
  317. item, e := frameworkOb.GetItemById(req.ChartFrameworkId)
  318. if e != nil {
  319. if e.Error() == utils.ErrNoRow() {
  320. br.Msg = "框架不存在, 请刷新页面"
  321. return
  322. }
  323. br.Msg = "操作失败"
  324. br.ErrMsg = "获取框架失败, Err: " + e.Error()
  325. return
  326. }
  327. // 重名校验
  328. {
  329. ob := new(data_manage.ChartFramework)
  330. cond := fmt.Sprintf(` AND %s <> ? AND %s = ? AND %s = ?`, ob.PrimaryId(), data_manage.ChartFrameworkColumns.FrameworkName, data_manage.ChartFrameworkColumns.AdminId)
  331. pars := make([]interface{}, 0)
  332. pars = append(pars, req.ChartFrameworkId, req.FrameworkName, sysUser.AdminId)
  333. exist, e := ob.GetItemByCondition(cond, pars)
  334. if e != nil && e.Error() != utils.ErrNoRow() {
  335. br.Msg = "操作失败"
  336. br.ErrMsg = "获取重名框架失败, Err: " + e.Error()
  337. return
  338. }
  339. if exist != nil {
  340. br.Msg = "框架名称已存在"
  341. return
  342. }
  343. }
  344. // 获取图表分类下各自的图表数
  345. chartsNumMap, e := data.GetMyChartClassifyIdNumMap(sysUser.AdminId)
  346. if e != nil {
  347. br.Msg = "获取失败"
  348. br.ErrMsg = "获取失败, GetMyChartClassifyIdNumMap Err: " + e.Error()
  349. return
  350. }
  351. now := time.Now().Local()
  352. item.FrameworkName = req.FrameworkName
  353. item.FrameworkImg = req.FrameworkImg
  354. item.FrameworkContent = req.FrameworkContent
  355. item.ModifyTime = now
  356. updateCols := []string{"FrameworkName", "FrameworkImg", "FrameworkContent", "ModifyTime"}
  357. nodes := make([]*data_manage.ChartFrameworkNode, 0)
  358. itemNodes := make([]*data_manage.ChartFrameworkNodeItem, 0)
  359. if len(req.Nodes) > 0 {
  360. for _, v := range req.Nodes {
  361. if v.MyChartClassifyId <= 0 {
  362. continue
  363. }
  364. t := new(data_manage.ChartFrameworkNode)
  365. t.ChartFrameworkId = req.ChartFrameworkId
  366. t.FrameworkName = req.FrameworkName
  367. t.NodeId = v.NodeId
  368. t.NodeName = v.NodeName
  369. t.MyChartClassifyId = v.MyChartClassifyId
  370. t.CreateTime = now
  371. nodes = append(nodes, t)
  372. // 响应节点数据
  373. td := data_manage.FormatChartFrameworkNode2Item(t, chartsNumMap[t.MyChartClassifyId])
  374. itemNodes = append(itemNodes, td)
  375. }
  376. }
  377. if e := item.EditFrameworkAndNodes(item, updateCols, nodes); e != nil {
  378. br.Msg = "操作失败"
  379. br.ErrMsg = "编辑框架及节点失败, Err: " + e.Error()
  380. return
  381. }
  382. detail := data_manage.FormatChartFramework2Item(item, itemNodes)
  383. br.Data = detail
  384. br.Ret = 200
  385. br.Success = true
  386. br.Msg = "操作成功"
  387. br.IsAddLog = true
  388. }
  389. // Remove
  390. // @Title 删除框架
  391. // @Description 删除视频
  392. // @Param request body data_manage.ChartFrameworkRemoveReq true "type json string"
  393. // @Success 200 Ret=200 操作成功
  394. // @router /remove [post]
  395. func (this *ChartFrameworkController) Remove() {
  396. br := new(models.BaseResponse).Init()
  397. defer func() {
  398. if br.ErrMsg == "" {
  399. br.IsSendEmail = false
  400. }
  401. this.Data["json"] = br
  402. this.ServeJSON()
  403. }()
  404. sysUser := this.SysUser
  405. if sysUser == nil {
  406. br.Msg = "请登录"
  407. br.ErrMsg = "请登录,SysUser Is Empty"
  408. br.Ret = 408
  409. return
  410. }
  411. // 参数校验
  412. var req data_manage.ChartFrameworkRemoveReq
  413. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  414. br.Msg = "参数解析异常!"
  415. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  416. return
  417. }
  418. if req.ChartFrameworkId <= 0 {
  419. br.Msg = "参数有误"
  420. br.ErrMsg = fmt.Sprintf("参数有误, ChartFrameworkId: %d", req.ChartFrameworkId)
  421. return
  422. }
  423. ob := new(data_manage.ChartFramework)
  424. item, e := ob.GetItemById(req.ChartFrameworkId)
  425. if e != nil {
  426. if e.Error() == utils.ErrNoRow() {
  427. br.Ret = 200
  428. br.Success = true
  429. br.Msg = "操作成功"
  430. return
  431. }
  432. br.Msg = "操作失败"
  433. br.ErrMsg = "获取框架失败, Err: " + e.Error()
  434. return
  435. }
  436. if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN && item.AdminId != sysUser.AdminId {
  437. br.Msg = "无权操作"
  438. return
  439. }
  440. if e := item.RemoveFrameworkAndNodes(req.ChartFrameworkId); e != nil {
  441. br.Msg = "操作失败"
  442. br.ErrMsg = "删除框架失败, Err: " + e.Error()
  443. return
  444. }
  445. br.Ret = 200
  446. br.Success = true
  447. br.Msg = "操作成功"
  448. br.IsAddLog = true
  449. }
  450. // Rename
  451. // @Title 重命名框架
  452. // @Description 重命名框架
  453. // @Param request body data_manage.ChartFrameworkRenameReq true "type json string"
  454. // @Success 200 Ret=200 操作成功
  455. // @router /rename [post]
  456. func (this *ChartFrameworkController) Rename() {
  457. br := new(models.BaseResponse).Init()
  458. defer func() {
  459. if br.ErrMsg == "" {
  460. br.IsSendEmail = false
  461. }
  462. this.Data["json"] = br
  463. this.ServeJSON()
  464. }()
  465. sysUser := this.SysUser
  466. if sysUser == nil {
  467. br.Msg = "请登录"
  468. br.ErrMsg = "请登录,SysUser Is Empty"
  469. br.Ret = 408
  470. return
  471. }
  472. // 参数校验
  473. var req data_manage.ChartFrameworkRenameReq
  474. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  475. br.Msg = "参数解析异常!"
  476. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  477. return
  478. }
  479. if req.ChartFrameworkId <= 0 {
  480. br.Msg = "参数有误"
  481. br.ErrMsg = fmt.Sprintf("参数有误, ChartFrameworkId: %d", req.ChartFrameworkId)
  482. return
  483. }
  484. req.FrameworkName = strings.TrimSpace(req.FrameworkName)
  485. if req.FrameworkName == "" {
  486. br.Msg = "框架名称不可为空"
  487. return
  488. }
  489. frameworkOb := new(data_manage.ChartFramework)
  490. item, e := frameworkOb.GetItemById(req.ChartFrameworkId)
  491. if e != nil {
  492. if e.Error() == utils.ErrNoRow() {
  493. br.Msg = "框架不存在, 请刷新页面"
  494. return
  495. }
  496. br.Msg = "操作失败"
  497. br.ErrMsg = "获取框架失败, Err: " + e.Error()
  498. return
  499. }
  500. // 重名校验
  501. {
  502. ob := new(data_manage.ChartFramework)
  503. cond := fmt.Sprintf(` AND %s <> ? AND %s = ? AND %s = ?`, ob.PrimaryId(), data_manage.ChartFrameworkColumns.FrameworkName, data_manage.ChartFrameworkColumns.AdminId)
  504. pars := make([]interface{}, 0)
  505. pars = append(pars, req.ChartFrameworkId, req.FrameworkName, sysUser.AdminId)
  506. exist, e := ob.GetItemByCondition(cond, pars)
  507. if e != nil && e.Error() != utils.ErrNoRow() {
  508. br.Msg = "操作失败"
  509. br.ErrMsg = "获取重名框架失败, Err: " + e.Error()
  510. return
  511. }
  512. if exist != nil {
  513. br.Msg = "框架名称已存在"
  514. return
  515. }
  516. }
  517. now := time.Now().Local()
  518. item.FrameworkName = req.FrameworkName
  519. item.ModifyTime = now
  520. updateCols := []string{"FrameworkName", "ModifyTime"}
  521. if e := item.Update(updateCols); e != nil {
  522. br.Msg = "操作失败"
  523. br.ErrMsg = "框架重命名失败, Err: " + e.Error()
  524. return
  525. }
  526. br.Ret = 200
  527. br.Success = true
  528. br.Msg = "操作成功"
  529. }
  530. // EditPublic
  531. // @Title 公开/隐藏框架
  532. // @Description 公开/隐藏框架
  533. // @Param request body data_manage.ChartFrameworkEditPublicReq true "type json string"
  534. // @Success 200 Ret=200 操作成功
  535. // @router /edit_public [post]
  536. func (this *ChartFrameworkController) EditPublic() {
  537. br := new(models.BaseResponse).Init()
  538. defer func() {
  539. if br.ErrMsg == "" {
  540. br.IsSendEmail = false
  541. }
  542. this.Data["json"] = br
  543. this.ServeJSON()
  544. }()
  545. sysUser := this.SysUser
  546. if sysUser == nil {
  547. br.Msg = "请登录"
  548. br.ErrMsg = "请登录,SysUser Is Empty"
  549. br.Ret = 408
  550. return
  551. }
  552. // 参数校验
  553. var req data_manage.ChartFrameworkEditPublicReq
  554. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  555. br.Msg = "参数解析异常!"
  556. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  557. return
  558. }
  559. if req.ChartFrameworkId <= 0 {
  560. br.Msg = "参数有误"
  561. br.ErrMsg = fmt.Sprintf("参数有误, ChartFrameworkId: %d", req.ChartFrameworkId)
  562. return
  563. }
  564. if req.IsPublic != 0 && req.IsPublic != 1 {
  565. br.Msg = "参数有误"
  566. br.ErrMsg = fmt.Sprintf("参数有误, IsPublic: %d", req.IsPublic)
  567. return
  568. }
  569. frameworkOb := new(data_manage.ChartFramework)
  570. item, e := frameworkOb.GetItemById(req.ChartFrameworkId)
  571. if e != nil {
  572. if e.Error() == utils.ErrNoRow() {
  573. br.Msg = "框架不存在, 请刷新页面"
  574. return
  575. }
  576. br.Msg = "操作失败"
  577. br.ErrMsg = "获取框架失败, Err: " + e.Error()
  578. return
  579. }
  580. now := time.Now().Local()
  581. updateCols := []string{"IsPublic", "PublicTime", "ModifyTime"}
  582. item.IsPublic = req.IsPublic
  583. if req.IsPublic == 1 {
  584. item.PublicTime = time.Now().Local()
  585. } else {
  586. item.PublicTime = time.Time{}
  587. }
  588. item.ModifyTime = now
  589. if e := item.Update(updateCols); e != nil {
  590. br.Msg = "操作失败"
  591. br.ErrMsg = "更新框架是否公开失败, Err: " + e.Error()
  592. return
  593. }
  594. br.Ret = 200
  595. br.Success = true
  596. br.Msg = "操作成功"
  597. }
  598. // Move
  599. // @Title 移动排序
  600. // @Description 移动排序
  601. // @Param request body data_manage.ChartFrameworkMoveReq true "type json string"
  602. // @Success 200 Ret=200 操作成功
  603. // @router /move [post]
  604. func (this *ChartFrameworkController) Move() {
  605. br := new(models.BaseResponse).Init()
  606. defer func() {
  607. if br.ErrMsg == "" {
  608. br.IsSendEmail = false
  609. }
  610. this.Data["json"] = br
  611. this.ServeJSON()
  612. }()
  613. sysUser := this.SysUser
  614. if sysUser == nil {
  615. br.Msg = "请登录"
  616. br.ErrMsg = "请登录,SysUser Is Empty"
  617. br.Ret = 408
  618. return
  619. }
  620. // 参数校验
  621. var req data_manage.ChartFrameworkMoveReq
  622. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  623. br.Msg = "参数解析异常!"
  624. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  625. return
  626. }
  627. if req.ChartFrameworkId <= 0 {
  628. br.Msg = "参数有误"
  629. br.ErrMsg = fmt.Sprintf("参数有误, ChartFrameworkId: %d", req.ChartFrameworkId)
  630. return
  631. }
  632. frameworkOb := new(data_manage.ChartFramework)
  633. item, e := frameworkOb.GetItemById(req.ChartFrameworkId)
  634. if e != nil {
  635. if e.Error() == utils.ErrNoRow() {
  636. br.Msg = "框架不存在, 请刷新页面"
  637. return
  638. }
  639. br.Msg = "操作失败"
  640. br.ErrMsg = "获取框架失败, Err: " + e.Error()
  641. return
  642. }
  643. updateCols := make([]string, 0)
  644. // 上一个兄弟节点
  645. if req.PrevChartFrameworkId > 0 {
  646. prev, e := frameworkOb.GetItemById(req.PrevChartFrameworkId)
  647. if e != nil {
  648. br.Msg = "操作失败"
  649. br.ErrMsg = "获取上一个兄弟节点失败, Err: " + e.Error()
  650. return
  651. }
  652. // 两个兄弟节点之间
  653. if req.NextChartFrameworkId > 0 {
  654. next, e := frameworkOb.GetItemById(req.PrevChartFrameworkId)
  655. if e != nil {
  656. br.Msg = "操作失败"
  657. br.ErrMsg = "获取下一个兄弟节点失败, Err: " + e.Error()
  658. return
  659. }
  660. // 如果上一个与下一个排序权重是一致的, 那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2, 自己变成上一个兄弟的排序权重+1
  661. if prev.Sort == next.Sort || prev.Sort == item.Sort {
  662. strUpdate := `sort + 2`
  663. _ = data_manage.UpdateChartFrameworkSort(sysUser.AdminId, prev.ChartFrameworkId, prev.Sort, strUpdate)
  664. } else {
  665. // 如果下一个排序权重正好是上一个节点的下一层, 那么需要再加一层了
  666. if next.Sort-prev.Sort == 1 {
  667. //变更兄弟节点的排序
  668. strUpdate := `sort + 1`
  669. _ = data_manage.UpdateChartFrameworkSort(sysUser.AdminId, 0, prev.Sort, strUpdate)
  670. }
  671. }
  672. }
  673. // 上一个兄弟节点sort+1
  674. item.Sort = prev.Sort + 1
  675. item.ModifyTime = time.Now()
  676. updateCols = append(updateCols, "Sort", "ModifyTime")
  677. } else {
  678. first, err := data_manage.GetFirstChartFramework(sysUser.AdminId)
  679. if err != nil && err.Error() != utils.ErrNoRow() {
  680. br.Msg = "操作失败"
  681. br.ErrMsg = "获取我的图库框架排首位的数据失败, Err:" + err.Error()
  682. return
  683. }
  684. if first != nil && first.Sort == 0 {
  685. strUpdate := ` sort + 1 `
  686. _ = data_manage.UpdateChartFrameworkSort(sysUser.AdminId, first.ChartFrameworkId-1, 0, strUpdate)
  687. }
  688. // 排首位
  689. item.Sort = 0
  690. item.ModifyTime = time.Now()
  691. updateCols = append(updateCols, "Sort", "ModifyTime")
  692. }
  693. if len(updateCols) > 0 {
  694. if e := item.Update(updateCols); e != nil {
  695. br.Msg = "操作失败"
  696. br.ErrMsg = "更新框架排序失败, Err: " + e.Error()
  697. return
  698. }
  699. }
  700. br.Ret = 200
  701. br.Success = true
  702. br.Msg = "操作成功"
  703. }
  704. // Detail
  705. // @Title 框架详情
  706. // @Description 框架详情
  707. // @Param ChartFrameworkId query int true "框架ID"
  708. // @Success 200 Ret=200 操作成功
  709. // @router /detail [get]
  710. func (this *ChartFrameworkController) Detail() {
  711. br := new(models.BaseResponse).Init()
  712. defer func() {
  713. if br.ErrMsg == "" {
  714. br.IsSendEmail = false
  715. }
  716. this.Data["json"] = br
  717. this.ServeJSON()
  718. }()
  719. sysUser := this.SysUser
  720. if sysUser == nil {
  721. br.Msg = "请登录"
  722. br.ErrMsg = "请登录,SysUser Is Empty"
  723. br.Ret = 408
  724. return
  725. }
  726. frameworkId, _ := this.GetInt("ChartFrameworkId")
  727. if frameworkId <= 0 {
  728. br.Msg = "参数有误"
  729. br.ErrMsg = fmt.Sprintf("参数有误, ChartFrameworkId: %d", frameworkId)
  730. return
  731. }
  732. frameworkOb := new(data_manage.ChartFramework)
  733. item, e := frameworkOb.GetItemById(frameworkId)
  734. if e != nil {
  735. if e.Error() == utils.ErrNoRow() {
  736. br.Msg = "框架不存在, 请刷新页面"
  737. return
  738. }
  739. br.Msg = "获取失败"
  740. br.ErrMsg = "获取框架失败, Err: " + e.Error()
  741. return
  742. }
  743. // 获取节点
  744. nodeOb := new(data_manage.ChartFrameworkNode)
  745. nodeCond := ` AND chart_framework_id = ?`
  746. nodePars := make([]interface{}, 0)
  747. nodePars = append(nodePars, frameworkId)
  748. nodes, e := nodeOb.GetItemsByCondition(nodeCond, nodePars, []string{}, "")
  749. if e != nil {
  750. br.Msg = "获取失败"
  751. br.ErrMsg = "获取框架节点失败, Err: " + e.Error()
  752. return
  753. }
  754. // 获取图表分类下各自的图表数
  755. chartsNumMap, e := data.GetMyChartClassifyIdNumMap(sysUser.AdminId)
  756. if e != nil {
  757. br.Msg = "获取失败"
  758. br.ErrMsg = "获取失败, GetMyChartClassifyIdNumMap Err: " + e.Error()
  759. return
  760. }
  761. // 格式化响应数据
  762. itemNodes := make([]*data_manage.ChartFrameworkNodeItem, 0)
  763. for _, v := range nodes {
  764. if v.NodeId == "" {
  765. continue
  766. }
  767. num, ok := chartsNumMap[v.MyChartClassifyId]
  768. if !ok {
  769. num = 0
  770. }
  771. itemNodes = append(itemNodes, data_manage.FormatChartFrameworkNode2Item(v, num))
  772. }
  773. detail := data_manage.FormatChartFramework2Item(item, itemNodes)
  774. br.Data = detail
  775. br.Ret = 200
  776. br.Success = true
  777. br.Msg = "操作成功"
  778. }