chart_framework.go 21 KB

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