collect_chart.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. "github.com/rdlucklib/rdluck_tools/paging"
  11. "sort"
  12. "strings"
  13. "time"
  14. )
  15. // ChartCollectController 图表收藏
  16. type ChartCollectController struct {
  17. controllers.BaseAuthController
  18. }
  19. // List
  20. // @Title 收藏列表-分页
  21. // @Description 收藏列表-分页
  22. // @Param PageSize query int false "每页数据量"
  23. // @Param CurrentIndex query int false "页码"
  24. // @Param ClassifyId query int false "分类ID"
  25. // @Param Keyword query string false "搜索关键词:图表ID/图表名称"
  26. // @Success Ret=200 保存成功
  27. // @router /chart_collect/list [get]
  28. func (this *ChartCollectController) List() {
  29. br := new(models.BaseResponse).Init()
  30. defer func() {
  31. if br.ErrMsg == "" {
  32. br.IsSendEmail = false
  33. }
  34. this.Data["json"] = br
  35. this.ServeJSON()
  36. }()
  37. sysUser := this.SysUser
  38. if sysUser == nil {
  39. br.Msg = "请登录"
  40. br.ErrMsg = "请登录,SysUser Is Empty"
  41. br.Ret = 408
  42. return
  43. }
  44. //resp := new(response.ChartInfoChartListResp)
  45. resp := new(data_manage.CollectChartInfoListResp)
  46. resp.List = make([]*data_manage.CollectChartInfoItem, 0)
  47. // 分页
  48. pageSize, _ := this.GetInt("PageSize")
  49. currentIndex, _ := this.GetInt("CurrentIndex")
  50. var total int
  51. page := paging.GetPaging(currentIndex, pageSize, total)
  52. var startSize int
  53. if pageSize <= 0 {
  54. pageSize = utils.PageSize20
  55. }
  56. if currentIndex <= 0 {
  57. currentIndex = 1
  58. }
  59. startSize = paging.StartIndex(currentIndex, pageSize)
  60. // 当前用户
  61. var cond string
  62. var pars []interface{}
  63. cond += ` AND a.sys_user_id = ?`
  64. pars = append(pars, sysUser.AdminId)
  65. // 分类筛选
  66. classifyId, _ := this.GetInt("ClassifyId")
  67. if classifyId > 0 {
  68. classifyIds, e := data.GetChartCollectClassifyChildIds(classifyId, sysUser.AdminId, true)
  69. if e != nil {
  70. br.Msg = "获取失败"
  71. br.ErrMsg = fmt.Sprintf("获取子分类IDs失败, %v", e)
  72. return
  73. }
  74. if len(classifyIds) == 0 {
  75. resp.Paging = page
  76. br.Data = resp
  77. br.Ret = 200
  78. br.Success = true
  79. br.Msg = "获取成功"
  80. return
  81. }
  82. cond += fmt.Sprintf(` AND a.chart_collect_classify_id IN (%s) `, utils.GetOrmInReplace(len(classifyIds)))
  83. pars = append(pars, classifyIds)
  84. }
  85. // 关键词搜索
  86. keywords := this.GetString("Keyword")
  87. keywords = strings.TrimSpace(keywords)
  88. if keywords != "" {
  89. kw := fmt.Sprint("%", keywords, "%")
  90. cond += fmt.Sprintf(` AND (b.chart_code LIKE ? OR b.chart_name LIKE ?)`)
  91. pars = append(pars, kw, kw)
  92. }
  93. // 获取图表列表
  94. dataCount, e := data_manage.GetCollectChartInfoCount(cond, pars)
  95. if e != nil {
  96. br.Msg = "获取失败"
  97. br.ErrMsg = fmt.Sprintf("获取收藏图表总数失败, %v", e)
  98. return
  99. }
  100. if dataCount == 0 {
  101. resp.Paging = page
  102. br.Data = resp
  103. br.Ret = 200
  104. br.Success = true
  105. br.Msg = "获取成功"
  106. return
  107. }
  108. list, e := data_manage.GetCollectChartInfoPageList(cond, pars, startSize, pageSize)
  109. if e != nil {
  110. br.Msg = "获取失败"
  111. br.ErrMsg = fmt.Sprintf("获取收藏图表列表失败, %v", e)
  112. return
  113. }
  114. for _, v := range list {
  115. resp.List = append(resp.List, data_manage.FormatChartInfo2CollectItem(v))
  116. }
  117. sort.Slice(resp.List, func(i, j int) bool {
  118. return resp.List[i].Sort < resp.List[j].Sort
  119. })
  120. page = paging.GetPaging(currentIndex, pageSize, int(dataCount))
  121. resp.Paging = page
  122. br.Data = resp
  123. br.Ret = 200
  124. br.Success = true
  125. br.Msg = "获取成功"
  126. }
  127. // Collect
  128. // @Title 新增收藏
  129. // @Description 新增收藏
  130. // @Param request body data_manage.ChartCollectReq true "type json string"
  131. // @Success Ret=200 保存成功
  132. // @router /chart_collect/collect [post]
  133. func (this *ChartCollectController) Collect() {
  134. br := new(models.BaseResponse).Init()
  135. defer func() {
  136. if br.ErrMsg == "" {
  137. br.IsSendEmail = false
  138. }
  139. this.Data["json"] = br
  140. this.ServeJSON()
  141. }()
  142. sysUser := this.SysUser
  143. if sysUser == nil {
  144. br.Msg = "请登录"
  145. br.ErrMsg = "请登录,SysUser Is Empty"
  146. br.Ret = 408
  147. return
  148. }
  149. var req data_manage.ChartCollectReq
  150. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  151. br.Msg = "参数解析异常"
  152. br.ErrMsg = fmt.Sprintf("参数解析异常: %v", e)
  153. return
  154. }
  155. if req.ChartInfoId <= 0 {
  156. br.Msg = "请选择图表"
  157. return
  158. }
  159. chartItem, e := data_manage.GetChartInfoById(req.ChartInfoId)
  160. if e != nil {
  161. if utils.IsErrNoRow(e) {
  162. br.Msg = "图表不存在, 请刷新页面"
  163. return
  164. }
  165. br.Msg = "操作失败"
  166. br.ErrMsg = fmt.Sprintf("获取图表信息失败: %v", e)
  167. return
  168. }
  169. if chartItem.ChartInfoId <= 0 {
  170. br.Msg = "图表不存在, 请刷新页面"
  171. br.IsSendEmail = false
  172. return
  173. }
  174. collectOb := new(data_manage.ChartCollect)
  175. // 待添加的分类配置
  176. addList := make([]*data_manage.ChartCollect, 0)
  177. for _, classifyId := range req.ClassifyIdList {
  178. addList = append(addList, &data_manage.ChartCollect{
  179. ChartCollectClassifyId: classifyId,
  180. ChartInfoId: req.ChartInfoId,
  181. SysUserId: sysUser.AdminId,
  182. SysRealName: sysUser.RealName,
  183. CreateTime: time.Now().Local(),
  184. ModifyTime: time.Now().Local(),
  185. })
  186. }
  187. cond := fmt.Sprintf(" %s = ? AND %s = ?", collectOb.Cols().SysUserId, collectOb.Cols().ChartInfoId)
  188. pars := make([]interface{}, 0)
  189. pars = append(pars, sysUser.AdminId, req.ChartInfoId)
  190. // 清除原有保存收藏配置,并新增保存收藏
  191. e = collectOb.RemoveAndCreateMulti(cond, pars, addList)
  192. if e != nil {
  193. br.Msg = "操作失败"
  194. br.ErrMsg = fmt.Sprintf("新增图表收藏失败: %v", e)
  195. return
  196. }
  197. br.Ret = 200
  198. br.Success = true
  199. br.Msg = "操作成功"
  200. }
  201. // CancelCollect
  202. // @Title 取消收藏
  203. // @Description 取消收藏
  204. // @Param request body data_manage.ChartCollectReq true "type json string"
  205. // @Success Ret=200 保存成功
  206. // @router /chart_collect/cancel_collect [post]
  207. func (this *ChartCollectController) CancelCollect() {
  208. br := new(models.BaseResponse).Init()
  209. defer func() {
  210. if br.ErrMsg == "" {
  211. br.IsSendEmail = false
  212. }
  213. this.Data["json"] = br
  214. this.ServeJSON()
  215. }()
  216. sysUser := this.SysUser
  217. if sysUser == nil {
  218. br.Msg = "请登录"
  219. br.ErrMsg = "请登录,SysUser Is Empty"
  220. br.Ret = 408
  221. return
  222. }
  223. var req data_manage.ChartCollectReq
  224. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  225. br.Msg = "参数解析异常"
  226. br.ErrMsg = fmt.Sprintf("参数解析异常: %v", e)
  227. return
  228. }
  229. if req.ChartInfoId <= 0 {
  230. br.Msg = "请选择图表"
  231. return
  232. }
  233. if req.ClassifyId <= 0 {
  234. br.Msg = "请选择分类"
  235. return
  236. }
  237. // 取消收藏
  238. collectOb := new(data_manage.ChartCollect)
  239. cond := fmt.Sprintf("%s = ? AND %s = ? AND %s = ?", collectOb.Cols().SysUserId, collectOb.Cols().ChartCollectClassifyId, collectOb.Cols().ChartInfoId)
  240. pars := make([]interface{}, 0)
  241. pars = append(pars, sysUser.AdminId, req.ClassifyId, req.ChartInfoId)
  242. if e := collectOb.RemoveByCondition(cond, pars); e != nil {
  243. br.Msg = "操作失败"
  244. br.ErrMsg = fmt.Sprintf("取消收藏失败: %v", e)
  245. return
  246. }
  247. br.Ret = 200
  248. br.Success = true
  249. br.Msg = "操作成功"
  250. }
  251. // Move
  252. // @Title 移动收藏
  253. // @Description 移动收藏
  254. // @Success 200 {object} data_manage.ChartCollectMoveReq
  255. // @router /chart_collect/move [post]
  256. func (this *ChartCollectController) Move() {
  257. br := new(models.BaseResponse).Init()
  258. defer func() {
  259. if br.ErrMsg == "" {
  260. br.IsSendEmail = false
  261. }
  262. this.Data["json"] = br
  263. this.ServeJSON()
  264. }()
  265. sysUser := this.SysUser
  266. if sysUser == nil {
  267. br.Msg = "请登录"
  268. br.ErrMsg = "请登录,SysUser Is Empty"
  269. br.Ret = 408
  270. return
  271. }
  272. var req data_manage.ChartCollectMoveReq
  273. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  274. br.Msg = "参数解析异常!"
  275. br.ErrMsg = fmt.Sprintf("参数解析失败, %v", e)
  276. return
  277. }
  278. var newSort int
  279. if req.PrevChartInfoId <= 0 {
  280. firstOne, err := data_manage.GetChartCollectSort(sysUser.AdminId, req.ClassifyId, 0)
  281. if err != nil {
  282. br.Msg = "获取数据失败!"
  283. br.ErrMsg = "获取数据失败,GetMyChartClassifyMapping,Err:" + err.Error()
  284. return
  285. }
  286. newSort = firstOne.Sort - 1
  287. } else if req.NextChartInfoId <= 0 {
  288. lastOne, err := data_manage.GetChartCollectSort(sysUser.AdminId, req.ClassifyId, 1)
  289. if err != nil {
  290. br.Msg = "获取数据失败!"
  291. br.ErrMsg = "获取数据失败,GetMyChartClassifyMapping,Err:" + err.Error()
  292. return
  293. }
  294. newSort = lastOne.Sort + 1
  295. } else {
  296. preMapItem, err := data_manage.GetChartCollectByChartInfoId(sysUser.AdminId, req.PrevChartInfoId, req.ClassifyId)
  297. if err != nil {
  298. br.Msg = "获取数据失败!"
  299. br.ErrMsg = "获取数据失败,GetMyChartClassifyMapping,Err:" + err.Error()
  300. return
  301. }
  302. nextMapItem, err := data_manage.GetChartCollectByChartInfoId(sysUser.AdminId, req.NextChartInfoId, req.ClassifyId)
  303. if err != nil {
  304. br.Msg = "获取数据失败!"
  305. br.ErrMsg = "获取数据失败,GetMyChartClassifyMapping,Err:" + err.Error()
  306. return
  307. }
  308. newSort = (preMapItem.Sort + nextMapItem.Sort) / 2
  309. newSort = preMapItem.Sort + 1
  310. var updateSortStr string
  311. if preMapItem.Sort == nextMapItem.Sort {
  312. updateSortStr = `sort + 2`
  313. } else if nextMapItem.Sort-preMapItem.Sort == 1 {
  314. updateSortStr = `sort + 1`
  315. }
  316. if updateSortStr != `` {
  317. _ = data_manage.UpdateChartCollectSortByClassifyId(req.ClassifyId, preMapItem.Sort, preMapItem.ChartCollectId, updateSortStr)
  318. }
  319. }
  320. if e := data_manage.UpdateChartCollectMove(newSort, sysUser.AdminId, req.ChartInfoId, req.ClassifyId); e != nil {
  321. br.Msg = "操作失败"
  322. br.ErrMsg = fmt.Sprintf("更新图表收藏失败, %v", e)
  323. return
  324. }
  325. br.Ret = 200
  326. br.Success = true
  327. br.Msg = "操作成功"
  328. }