chart_framework.go 22 KB

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