chart_collect_stat.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. package controllers
  2. import (
  3. "eta/eta_forum_admin/models"
  4. "eta/eta_forum_admin/models/chart_collect"
  5. "eta/eta_forum_admin/models/eta_business"
  6. "eta/eta_forum_admin/utils"
  7. "github.com/rdlucklib/rdluck_tools/paging"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. type ChartCollectStatController struct {
  13. BaseAuthController
  14. }
  15. // UserStat
  16. // @Title 用户纬度统计收藏数据
  17. // @Description 用户纬度统计收藏数据
  18. // @Success 200 {object} data_manage.EdbClassifyListResp
  19. // @router /stat/user [get]
  20. func (this *ChartCollectStatController) UserStat() {
  21. br := new(models.BaseResponse).Init()
  22. defer func() {
  23. this.Data["json"] = br
  24. this.ServeJSON()
  25. }()
  26. // 默认查一级分类和一级分类下的指标信息,
  27. // 如果是 子级分类,查询该子级分类的下一级分类和指标信息
  28. // 增加标识判断是文件夹还是指标列表
  29. startDate := this.GetString("StartDate")
  30. endDate := this.GetString("EndDate")
  31. etaBusinessIds := this.GetString("EtaBusinessIds")
  32. pageSize, _ := this.GetInt("PageSize")
  33. currentIndex, _ := this.GetInt("CurrentIndex")
  34. sortType := this.GetString("SortType")
  35. sortParam := this.GetString("SortParam")
  36. var startSize int
  37. if pageSize <= 0 {
  38. pageSize = utils.PageSize20
  39. }
  40. if currentIndex <= 0 {
  41. currentIndex = 1
  42. }
  43. startSize = paging.StartIndex(currentIndex, pageSize)
  44. var condition string
  45. var pars []interface{}
  46. if etaBusinessIds != "" {
  47. businessIdsSlice := make([]int, 0)
  48. businessIds := strings.Split(etaBusinessIds, ",")
  49. if len(businessIds) == 0 {
  50. br.Msg = "请选择正确的商家"
  51. return
  52. }
  53. for _, id := range businessIds {
  54. if id != "" {
  55. adminIdInt, e := strconv.Atoi(id)
  56. if e != nil {
  57. br.Msg = "请选择正确的商家"
  58. return
  59. }
  60. businessIdsSlice = append(businessIdsSlice, adminIdInt)
  61. }
  62. }
  63. if len(businessIdsSlice) > 0 {
  64. condition += ` AND eta_business_id IN (` + utils.GetOrmInReplace(len(businessIdsSlice)) + `) `
  65. pars = append(pars, businessIdsSlice)
  66. }
  67. }
  68. if startDate != "" {
  69. // 校验日期格式
  70. _, err := time.ParseInLocation(utils.FormatDate, startDate, time.Local)
  71. if err != nil {
  72. br.Msg = "请选择正确的开始日期"
  73. return
  74. }
  75. condition += ` AND collect_date >= ? `
  76. pars = append(pars, startDate)
  77. }
  78. if endDate != "" {
  79. // 校验日期格式
  80. endTime, err := time.ParseInLocation(utils.FormatDate, endDate, time.Local)
  81. if err != nil {
  82. br.Msg = "请选择正确的结束日期"
  83. return
  84. }
  85. condition += ` AND collect_date < ? `
  86. nextDate := endTime.AddDate(0, 0, 1).Format(utils.FormatDate)
  87. pars = append(pars, nextDate)
  88. }
  89. order := ""
  90. if sortParam == "LastCollectChartTime" {
  91. if sortType == "desc" || sortType == "asc" {
  92. order = " last_collect_chart_time " + sortType
  93. }
  94. }
  95. statObj := new(chart_collect.UserCollectChartStat)
  96. total, err := statObj.GetGroupUserCountByCondition(condition, pars)
  97. if err != nil {
  98. br.Msg = "获取失败"
  99. br.ErrMsg = "获取数据总数失败,Err:" + err.Error()
  100. return
  101. }
  102. page := paging.GetPaging(currentIndex, pageSize, total)
  103. resp := new(chart_collect.UserStatListResp)
  104. list := make([]*chart_collect.UserCollectChartStatItem, 0)
  105. if total == 0 {
  106. resp.List = list
  107. resp.Paging = page
  108. br.Ret = 200
  109. br.Success = true
  110. br.Msg = "获取成功"
  111. br.Data = resp
  112. return
  113. }
  114. list, err = statObj.GetGroupUserPageListByCondition(condition, pars, order, startSize, pageSize)
  115. if err != nil {
  116. br.Msg = "获取失败"
  117. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  118. return
  119. }
  120. // 查找商户信息
  121. businessCodeStr := make([]string, 0)
  122. businessCodeMap := make(map[string]string)
  123. for _, v := range list {
  124. businessCodeStr = append(businessCodeStr, v.BusinessCode)
  125. }
  126. obj := new(eta_business.EtaBusiness)
  127. condition1 := " AND business_code IN (" + utils.GetOrmInReplace(len(businessCodeStr)) + ") "
  128. var pars1 []interface{}
  129. pars1 = append(pars1, businessCodeStr)
  130. businessList, err := obj.GetItemsByCondition(condition1, pars1, []string{"business_code", "business_name"}, "")
  131. if err != nil {
  132. br.Msg = "获取失败"
  133. br.ErrMsg = "获取商户信息失败,Err:" + err.Error()
  134. return
  135. }
  136. for _, v := range businessList {
  137. businessCodeMap[v.BusinessCode] = v.BusinessName
  138. }
  139. for _, v := range list {
  140. if businessName, ok := businessCodeMap[v.BusinessCode]; ok {
  141. v.BusinessName = businessName
  142. }
  143. }
  144. resp.List = list
  145. resp.Paging = page
  146. br.Ret = 200
  147. br.Success = true
  148. br.Msg = "获取成功"
  149. br.Data = resp
  150. }
  151. // ChartStat
  152. // @Title 图表纬度统计收藏数据
  153. // @Description 图表纬度统计收藏数据
  154. // @Success 200 {object} data_manage.EdbClassifyListResp
  155. // @router /stat/chart [get]
  156. func (this *ChartCollectStatController) ChartStat() {
  157. br := new(models.BaseResponse).Init()
  158. defer func() {
  159. this.Data["json"] = br
  160. this.ServeJSON()
  161. }()
  162. statObj := new(chart_collect.ChartCollectStat)
  163. resp := new(chart_collect.ChartStatListResp)
  164. list, err := statObj.GetChartInfoStatusNum()
  165. if err != nil {
  166. br.Msg = "获取失败"
  167. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  168. return
  169. }
  170. if len(list) == 0 {
  171. resp.List = make([]*chart_collect.ChartCollectChartStatItem, 0)
  172. br.Ret = 200
  173. br.Success = true
  174. br.Msg = "获取成功"
  175. br.Data = resp
  176. return
  177. }
  178. // 查询图表名称
  179. chartInfoIds := make([]int, 0)
  180. for _, v := range list {
  181. chartInfoIds = append(chartInfoIds, v.ChartInfoId)
  182. }
  183. chartList, err := models.GetChartInfoListByChartIds(chartInfoIds)
  184. if err != nil {
  185. br.Msg = "获取失败"
  186. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  187. return
  188. }
  189. chartInfoMap := make(map[int]string)
  190. for _, v := range chartList {
  191. chartInfoMap[v.ChartInfoId] = v.ChartName
  192. }
  193. for _, v := range list {
  194. if name, ok := chartInfoMap[v.ChartInfoId]; ok {
  195. v.ChartName = name
  196. }
  197. }
  198. resp.List = list
  199. br.Ret = 200
  200. br.Success = true
  201. br.Msg = "获取成功"
  202. br.Data = resp
  203. }
  204. // UserDetailList
  205. // @Title 用户纬度统计收藏图表列表
  206. // @Description 用户纬度统计收藏图表列表
  207. // @Success 200 {object} data_manage.EdbClassifyListResp
  208. // @router /stat/user/detailList [get]
  209. func (this *ChartCollectStatController) UserDetailList() {
  210. br := new(models.BaseResponse).Init()
  211. defer func() {
  212. this.Data["json"] = br
  213. this.ServeJSON()
  214. }()
  215. // 默认查一级分类和一级分类下的指标信息,
  216. // 如果是 子级分类,查询该子级分类的下一级分类和指标信息
  217. // 增加标识判断是文件夹还是指标列表
  218. startDate := this.GetString("StartDate")
  219. endDate := this.GetString("EndDate")
  220. userId, _ := this.GetInt("UserId")
  221. pageSize, _ := this.GetInt("PageSize")
  222. currentIndex, _ := this.GetInt("CurrentIndex")
  223. keyword := this.GetString("Keyword")
  224. var startSize int
  225. if pageSize <= 0 {
  226. pageSize = utils.PageSize20
  227. }
  228. if currentIndex <= 0 {
  229. currentIndex = 1
  230. }
  231. startSize = paging.StartIndex(currentIndex, pageSize)
  232. var condition string
  233. var pars []interface{}
  234. if userId == 0 {
  235. br.Msg = "请选择用户"
  236. return
  237. }
  238. condition += ` AND user_id = ? `
  239. pars = append(pars, userId)
  240. if startDate != "" {
  241. // 校验日期格式
  242. _, err := time.ParseInLocation(utils.FormatDate, startDate, time.Local)
  243. if err != nil {
  244. br.Msg = "请选择正确的开始日期"
  245. return
  246. }
  247. condition += ` AND a.collect_time >= ? `
  248. pars = append(pars, startDate)
  249. }
  250. if endDate != "" {
  251. // 校验日期格式
  252. endTime, err := time.ParseInLocation(utils.FormatDate, endDate, time.Local)
  253. if err != nil {
  254. br.Msg = "请选择正确的结束日期"
  255. return
  256. }
  257. condition += ` AND a.collect_time < ? `
  258. nextDate := endTime.AddDate(0, 0, 1).Format(utils.FormatDate)
  259. pars = append(pars, nextDate)
  260. }
  261. if keyword != "" {
  262. // 根据空格拆分
  263. keys := strings.Split(keyword, " ")
  264. condition += ` AND ( `
  265. for _, v := range keys {
  266. if v != "" {
  267. condition += ` b.chart_name like ? and`
  268. pars = append(pars, "%"+v+"%")
  269. }
  270. }
  271. condition = condition[:len(condition)-3]
  272. condition += ` )`
  273. }
  274. total, err := chart_collect.GetChartCollectCountByCondition(condition, pars)
  275. if err != nil {
  276. br.Msg = "获取失败"
  277. br.ErrMsg = "获取数据总数失败,Err:" + err.Error()
  278. return
  279. }
  280. page := paging.GetPaging(currentIndex, pageSize, int(total))
  281. resp := new(chart_collect.ChartCollectListResp)
  282. list := make([]*chart_collect.ChartCollectView, 0)
  283. if total == 0 {
  284. resp.List = list
  285. resp.Paging = page
  286. br.Ret = 200
  287. br.Success = true
  288. br.Msg = "获取成功"
  289. br.Data = resp
  290. return
  291. }
  292. list, err = chart_collect.GetChartCollectPageByCondition(condition, pars, startSize, pageSize)
  293. if err != nil {
  294. br.Msg = "获取失败"
  295. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  296. return
  297. }
  298. resp.List = list
  299. resp.Paging = page
  300. br.Ret = 200
  301. br.Success = true
  302. br.Msg = "获取成功"
  303. br.Data = resp
  304. }