chart_collect.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. package controllers
  2. import (
  3. "eta/eta_forum_hub/models"
  4. "eta/eta_forum_hub/models/chart_collect"
  5. "eta/eta_forum_hub/services"
  6. "eta/eta_forum_hub/utils"
  7. "github.com/rdlucklib/rdluck_tools/paging"
  8. "strconv"
  9. "strings"
  10. )
  11. // 我的图库
  12. type ChartCollectController struct {
  13. BaseAuthController
  14. }
  15. // ClassifyList
  16. // @Title 收藏-分类列表接口
  17. // @Description 收藏-分类列表接口
  18. // @Success 200 {object} chart_collect.ChartCollectClassifyResp
  19. // @router /classify/list [get]
  20. func (this *ChartCollectController) ClassifyList() {
  21. br := new(models.BaseResponse).Init()
  22. defer func() {
  23. this.Data["json"] = br
  24. this.ServeJSON()
  25. }()
  26. userMobile := this.GetString("UserMobile")
  27. businessCode := this.GetString("BusinessCode")
  28. userTelAreaCode := this.GetString("UserTelAreaCode")
  29. if userMobile == "" {
  30. br.Msg = "请选择用户"
  31. return
  32. }
  33. if businessCode == "" {
  34. br.Msg = "请选择商户"
  35. return
  36. }
  37. resp := new(chart_collect.ChartCollectClassifyResp)
  38. // 获取当前的客户信息
  39. sysUser, err := models.GetUserByBusinessCodeAndMobile(businessCode, userMobile, userTelAreaCode)
  40. if err != nil {
  41. if err.Error() == utils.ErrNoRow() {
  42. items := make([]*chart_collect.ChartCollectClassifyItem, 0)
  43. resp.List = items
  44. br.Ret = 200
  45. br.Success = true
  46. br.Msg = "获取成功"
  47. br.Data = resp
  48. return
  49. }
  50. br.Msg = "获取用户信息失败"
  51. br.ErrMsg = "获取用户信息失败,Err:" + err.Error()
  52. return
  53. }
  54. // 获取有权限的分类ID
  55. permissionClassifyIds, err := services.GetPermissionClassifyIdByUserId(sysUser.UserId)
  56. if err != nil {
  57. br.Msg = "获取有权限的分类信息失败"
  58. br.ErrMsg = "获取有权限的分类信息失败,Err:" + err.Error()
  59. return
  60. }
  61. lenPermissionClassifyIds := len(permissionClassifyIds)
  62. if lenPermissionClassifyIds == 0 {
  63. items := make([]*chart_collect.ChartCollectClassifyItem, 0)
  64. resp.List = items
  65. br.Ret = 200
  66. br.Success = true
  67. br.Msg = "获取成功"
  68. br.Data = resp
  69. return
  70. }
  71. //获取图表信息
  72. list, err := chart_collect.GetChartCollectClassifyAll(sysUser.UserId)
  73. if err != nil && err.Error() != utils.ErrNoRow() {
  74. br.Msg = "获取分类信息失败"
  75. br.ErrMsg = "获取分类信息失败,Err:" + err.Error()
  76. return
  77. }
  78. if list == nil || len(list) <= 0 || (err != nil && err.Error() == utils.ErrNoRow()) {
  79. items := make([]*chart_collect.ChartCollectClassifyItem, 0)
  80. resp.List = items
  81. br.Ret = 200
  82. br.Success = true
  83. br.Msg = "获取成功"
  84. br.Data = resp
  85. return
  86. }
  87. results := make([]*chart_collect.ChartCollectClassifyItem, 0)
  88. for _, v := range list {
  89. tmp := new(chart_collect.ChartCollectClassifyItem)
  90. tmp.ClassifyName = v.ClassifyName
  91. tmp.CollectClassifyId = v.CollectClassifyId
  92. tmp.UserId = v.UserId
  93. results = append(results, tmp)
  94. }
  95. resp.List = results
  96. language := `CN`
  97. // 指标显示的语言
  98. /*{
  99. configDetail, _ := system.GetConfigDetailByCode(system.ChartLanguageVar)
  100. if configDetail != nil {
  101. language = configDetail.ConfigValue
  102. } else {
  103. configDetail, _ = system.GetDefaultConfigDetailByCode(system.ChartLanguageVar)
  104. if configDetail != nil {
  105. language = configDetail.ConfigValue
  106. }
  107. }
  108. }*/
  109. resp.Language = language
  110. br.Ret = 200
  111. br.Success = true
  112. br.Msg = "获取成功"
  113. br.Data = resp
  114. }
  115. // ChartList
  116. // @Title 收藏-列表接口
  117. // @Description 收藏-列表接口
  118. // @Param PageSize query int true "每页数据条数"
  119. // @Param CurrentIndex query int true "当前页页码,从1开始"
  120. // @Param ChartCollectClassifyIds query int true "收藏分类id"
  121. // @Success 200 {object} chart_collect.MyChartListResp
  122. // @router /chart/list [get]
  123. func (this *ChartCollectController) ChartList() {
  124. br := new(models.BaseResponse).Init()
  125. defer func() {
  126. this.Data["json"] = br
  127. this.ServeJSON()
  128. }()
  129. collectClassifyIds := this.GetString("CollectClassifyIds")
  130. pageSize, _ := this.GetInt("PageSize")
  131. currentIndex, _ := this.GetInt("CurrentIndex")
  132. keyword := this.GetString("Keyword")
  133. userMobile := this.GetString("UserMobile")
  134. businessCode := this.GetString("BusinessCode")
  135. userTelAreaCode := this.GetString("UserTelAreaCode")
  136. if userMobile == "" {
  137. br.Msg = "请选择用户"
  138. return
  139. }
  140. if businessCode == "" {
  141. br.Msg = "请选择商户"
  142. return
  143. }
  144. // 获取当前的客户信息
  145. sysUser, err := models.GetUserByBusinessCodeAndMobile(businessCode, userMobile, userTelAreaCode)
  146. if err != nil {
  147. if err.Error() == utils.ErrNoRow() {
  148. br.Msg = "用户不存在"
  149. return
  150. }
  151. br.Msg = "获取用户信息失败"
  152. br.ErrMsg = "获取用户信息失败,Err:" + err.Error()
  153. return
  154. }
  155. var total int64
  156. var startSize int
  157. if pageSize <= 0 {
  158. pageSize = utils.PageSize20
  159. }
  160. if currentIndex <= 0 {
  161. currentIndex = 1
  162. }
  163. startSize = paging.StartIndex(currentIndex, pageSize)
  164. var condition string
  165. var pars []interface{}
  166. chartAdminId := sysUser.UserId
  167. condition += " AND (a.user_id = ?)"
  168. pars = append(pars, chartAdminId)
  169. collectClassifyIdSlice := make([]string, 0)
  170. if collectClassifyIds != "" {
  171. classifyIds := strings.Split(collectClassifyIds, ",")
  172. if len(classifyIds) == 0 {
  173. br.Msg = "请选择正确的分类"
  174. return
  175. }
  176. classifyIdSlice := make([]int, 0)
  177. for _, id := range classifyIds {
  178. if id != "" {
  179. collectClassifyIdSlice = append(collectClassifyIdSlice, id+",")
  180. tmp, e := strconv.Atoi(id)
  181. if e != nil {
  182. br.Msg = "请选择正确的分类"
  183. return
  184. }
  185. classifyIdSlice = append(classifyIdSlice, tmp)
  186. }
  187. }
  188. condition += " AND a.collect_classify_id in (" + utils.GetOrmInReplace(len(classifyIdSlice)) + ") "
  189. pars = append(pars, classifyIdSlice)
  190. }
  191. adminIdsSlice := make([]int, 0)
  192. // 获取有权限的分类ID
  193. permissionClassifyIds, err := services.GetPermissionClassifyIdByUserId(sysUser.UserId)
  194. if err != nil {
  195. br.Msg = "获取有权限的分类信息失败"
  196. br.ErrMsg = "获取有权限的分类信息失败,Err:" + err.Error()
  197. return
  198. }
  199. page := paging.GetPaging(currentIndex, pageSize, int(total))
  200. resp := new(chart_collect.ChartCollectListResp)
  201. lenPermissionClassifyIds := len(permissionClassifyIds)
  202. if lenPermissionClassifyIds > 0 {
  203. condition += ` AND b.chart_classify_id in (` + utils.GetOrmInReplace(lenPermissionClassifyIds) + `) `
  204. pars = append(pars, permissionClassifyIds)
  205. } else {
  206. items := make([]*chart_collect.ChartCollectView, 0)
  207. resp.Paging = page
  208. resp.List = items
  209. br.Ret = 200
  210. br.Success = true
  211. br.Data = resp
  212. br.Msg = "获取成功"
  213. return
  214. }
  215. list := make([]*chart_collect.ChartCollectView, 0)
  216. if keyword != "" {
  217. // 从 es中查询收藏的图表列表
  218. chartList := make([]*chart_collect.ChartCollectEsItem, 0)
  219. chartList, total, err = services.EsSearchChartCollectData(keyword, chartAdminId, permissionClassifyIds, collectClassifyIdSlice, adminIdsSlice, startSize, pageSize)
  220. if err != nil {
  221. br.Msg = "获取收藏数据失败"
  222. br.ErrMsg = "获取收藏数据失败,Err:" + err.Error()
  223. return
  224. }
  225. for _, v := range chartList {
  226. tmp := &chart_collect.ChartCollectView{
  227. ChartInfoId: v.ChartInfoId,
  228. UserId: v.UserId,
  229. CreateTime: v.CreateTime,
  230. CollectTime: v.CollectTime,
  231. ChartName: v.ChartName,
  232. ChartNameEn: v.ChartNameEn,
  233. ChartImage: v.ChartImage,
  234. UniqueCode: v.UniqueCode,
  235. }
  236. list = append(list, tmp)
  237. }
  238. // 根据空格拆分
  239. /*keys := strings.Split(keyword, " ")
  240. condition += ` AND ( `
  241. for _, v := range keys {
  242. if v != "" {
  243. condition += ` b.chart_name like ? and`
  244. pars = append(pars, "%"+v+"%")
  245. }
  246. }
  247. condition = condition[:len(condition)-3]
  248. condition += ` )`*/
  249. } else {
  250. total, err = chart_collect.GetChartCollectCountByCondition(condition, pars)
  251. if err != nil && err.Error() != utils.ErrNoRow() {
  252. br.Msg = "获取信息失败"
  253. br.ErrMsg = "获取收藏数据总数失败,Err:" + err.Error()
  254. return
  255. }
  256. //获取图表信息
  257. list, err = chart_collect.GetChartCollectPageByCondition(condition, pars, startSize, pageSize)
  258. if err != nil && err.Error() != utils.ErrNoRow() {
  259. br.Success = true
  260. br.Msg = "获取图表信息失败"
  261. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  262. return
  263. }
  264. }
  265. if list == nil || len(list) <= 0 || (err != nil && err.Error() == utils.ErrNoRow()) {
  266. items := make([]*chart_collect.ChartCollectView, 0)
  267. resp.Paging = page
  268. resp.List = items
  269. br.Ret = 200
  270. br.Success = true
  271. br.Data = resp
  272. br.Msg = "获取成功"
  273. return
  274. }
  275. page = paging.GetPaging(currentIndex, pageSize, int(total))
  276. resp.Paging = page
  277. resp.List = list
  278. br.Ret = 200
  279. br.Success = true
  280. br.Msg = "获取成功"
  281. br.Data = resp
  282. }