report.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package controllers
  2. import (
  3. "eta/eta_mini_crm/models"
  4. "eta/eta_mini_crm/models/request"
  5. "eta/eta_mini_crm/models/response"
  6. "eta/eta_mini_crm/services"
  7. "eta/eta_mini_crm/utils"
  8. "fmt"
  9. "github.com/rdlucklib/rdluck_tools/paging"
  10. "strconv"
  11. "strings"
  12. )
  13. // ReportController 报告接口
  14. type ReportController struct {
  15. BaseAuthController
  16. }
  17. // ClassifyTree
  18. // @Title 分类树
  19. // @Description 分类树
  20. // @Success 200 {object} models.ClassifyItem
  21. // @router /classify_tree [get]
  22. func (this *ReportController) ClassifyTree() {
  23. br := new(models.BaseResponse).Init()
  24. defer func() {
  25. if br.ErrMsg == "" {
  26. br.IsSendEmail = false
  27. }
  28. this.Data["json"] = br
  29. this.ServeJSON()
  30. }()
  31. sysUser := this.SysUser
  32. if sysUser == nil {
  33. br.Msg = "请登录"
  34. br.ErrMsg = "请登录,SysUser Is Empty"
  35. br.Ret = 408
  36. return
  37. }
  38. classifyOb := new(models.Classify)
  39. list, e := classifyOb.GetItemsByCondition(``, make([]interface{}, 0), []string{}, "")
  40. if e != nil {
  41. br.Msg = "获取失败"
  42. br.ErrMsg = fmt.Sprintf("获取分类列表失败, %v", e)
  43. return
  44. }
  45. classifies := make([]*models.ClassifyItem, 0)
  46. for _, v := range list {
  47. classifies = append(classifies, v.Format2Item())
  48. }
  49. classifies = services.GetClassifyTreeRecursive(classifies, 0)
  50. br.Data = classifies
  51. br.Ret = 200
  52. br.Msg = "获取成功"
  53. br.Success = true
  54. }
  55. // ReadRecord
  56. // @Title 阅读统计
  57. // @Description 阅读统计
  58. // @Param request body request.ReadRecordListForm true "type json string"
  59. // @Success 200 {object} response.ReadRecordListResp
  60. // @router /read_record [get]
  61. func (this *ReportController) ReadRecord() {
  62. br := new(models.BaseResponse).Init()
  63. defer func() {
  64. if br.ErrMsg == "" {
  65. br.IsSendEmail = false
  66. }
  67. this.Data["json"] = br
  68. this.ServeJSON()
  69. }()
  70. sysUser := this.SysUser
  71. if sysUser == nil {
  72. br.Msg = "请登录"
  73. br.ErrMsg = "请登录,SysUser Is Empty"
  74. br.Ret = 408
  75. return
  76. }
  77. params := new(request.ReadRecordListForm)
  78. if e := this.ParseForm(params); e != nil {
  79. br.Msg = "参数解析异常"
  80. br.ErrMsg = fmt.Sprintf("参数解析异常, %v", e)
  81. return
  82. }
  83. if params.UserId <= 0 {
  84. br.Msg = "参数有误"
  85. br.ErrMsg = fmt.Sprintf("参数有误, UserId: %d", params.UserId)
  86. return
  87. }
  88. resp := new(response.ReadRecordListResp)
  89. respList := make([]*models.UserReadRecordItem, 0)
  90. recordOb := new(models.UserReadRecord)
  91. // 分页
  92. var startSize int
  93. if params.PageSize <= 0 {
  94. params.PageSize = utils.PageSize20
  95. }
  96. if params.CurrentIndex <= 0 {
  97. params.CurrentIndex = 1
  98. }
  99. startSize = utils.StartIndex(params.CurrentIndex, params.PageSize)
  100. // 分类筛选
  101. cond := ``
  102. pars := make([]interface{}, 0)
  103. if params.ClassifyIds != "" {
  104. idArr := strings.Split(params.ClassifyIds, ",")
  105. var ids []int
  106. for _, v := range idArr {
  107. id, _ := strconv.Atoi(v)
  108. ids = append(ids, id)
  109. }
  110. if len(ids) > 0 {
  111. page := paging.GetPaging(params.CurrentIndex, params.PageSize, 0)
  112. resp.Paging = page
  113. resp.List = respList
  114. br.Data = resp
  115. br.Ret = 200
  116. br.Msg = "获取成功"
  117. br.Success = true
  118. return
  119. }
  120. classifyOb := new(models.Classify)
  121. classifyCond := fmt.Sprintf(` AND %s IN (%s)`, classifyOb.Cols().PrimaryId, utils.GetOrmInReplace(len(ids)))
  122. classifyPars := make([]interface{}, 0)
  123. classifyPars = append(classifyPars, ids)
  124. classifies, e := classifyOb.GetItemsByCondition(classifyCond, classifyPars, []string{}, "")
  125. if e != nil {
  126. br.Msg = "获取失败"
  127. br.ErrMsg = fmt.Sprintf("获取分类列表失败, %v", e)
  128. return
  129. }
  130. // 根据层级
  131. var firstClassifyIds, secondClassifyIds, thirdClassifyIds []int
  132. for _, v := range classifies {
  133. switch v.Level {
  134. case 1:
  135. firstClassifyIds = append(firstClassifyIds, v.Id)
  136. case 2:
  137. secondClassifyIds = append(secondClassifyIds, v.Id)
  138. case 3:
  139. thirdClassifyIds = append(thirdClassifyIds, v.Id)
  140. }
  141. }
  142. if len(firstClassifyIds) > 0 {
  143. cond += fmt.Sprintf(` AND %s IN (%s)`, recordOb.Cols().ClassifyIdFirst, utils.GetOrmInReplace(len(firstClassifyIds)))
  144. pars = append(pars, firstClassifyIds)
  145. }
  146. if len(secondClassifyIds) > 0 {
  147. cond += fmt.Sprintf(` AND %s IN (%s)`, recordOb.Cols().ClassifyIdSecond, utils.GetOrmInReplace(len(secondClassifyIds)))
  148. pars = append(pars, secondClassifyIds)
  149. }
  150. if len(thirdClassifyIds) > 0 {
  151. cond += fmt.Sprintf(` AND %s IN (%s)`, recordOb.Cols().ClassifyIdThird, utils.GetOrmInReplace(len(thirdClassifyIds)))
  152. pars = append(pars, thirdClassifyIds)
  153. }
  154. }
  155. total, e := recordOb.GetCountByCondition(cond, pars)
  156. if e != nil {
  157. br.Msg = "获取失败"
  158. br.ErrMsg = fmt.Sprintf("获取阅读记录计数失败, %v", e)
  159. return
  160. }
  161. list, e := recordOb.GetPageItemsByCondition(cond, pars, []string{}, "", startSize, params.PageSize)
  162. if e != nil {
  163. br.Msg = "获取失败"
  164. br.ErrMsg = fmt.Sprintf("获取阅读记录列表失败, %v", e)
  165. return
  166. }
  167. for _, v := range list {
  168. respList = append(respList, v.Format2Item())
  169. }
  170. page := paging.GetPaging(params.CurrentIndex, params.PageSize, total)
  171. resp.Paging = page
  172. resp.List = respList
  173. br.Data = resp
  174. br.Ret = 200
  175. br.Msg = "获取成功"
  176. br.Success = true
  177. }