report.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 := fmt.Sprintf(` AND %s = ?`, recordOb.Cols().UserId)
  102. pars := make([]interface{}, 0)
  103. pars = append(pars, params.UserId)
  104. if params.ClassifyIds != "" {
  105. idArr := strings.Split(params.ClassifyIds, ",")
  106. var ids []int
  107. for _, v := range idArr {
  108. id, _ := strconv.Atoi(v)
  109. ids = append(ids, id)
  110. }
  111. if len(ids) == 0 {
  112. page := paging.GetPaging(params.CurrentIndex, params.PageSize, 0)
  113. resp.Paging = page
  114. resp.List = respList
  115. br.Data = resp
  116. br.Ret = 200
  117. br.Msg = "获取成功"
  118. br.Success = true
  119. return
  120. }
  121. classifyOb := new(models.Classify)
  122. classifyCond := fmt.Sprintf(` AND %s IN (%s)`, classifyOb.Cols().PrimaryId, utils.GetOrmInReplace(len(ids)))
  123. classifyPars := make([]interface{}, 0)
  124. classifyPars = append(classifyPars, ids)
  125. classifies, e := classifyOb.GetItemsByCondition(classifyCond, classifyPars, []string{}, "")
  126. if e != nil {
  127. br.Msg = "获取失败"
  128. br.ErrMsg = fmt.Sprintf("获取分类列表失败, %v", e)
  129. return
  130. }
  131. // 根据层级
  132. var firstClassifyIds, secondClassifyIds, thirdClassifyIds []int
  133. for _, v := range classifies {
  134. switch v.Level {
  135. case 1:
  136. firstClassifyIds = append(firstClassifyIds, v.Id)
  137. case 2:
  138. secondClassifyIds = append(secondClassifyIds, v.Id)
  139. case 3:
  140. thirdClassifyIds = append(thirdClassifyIds, v.Id)
  141. }
  142. }
  143. var classifyIdsCond string
  144. if len(firstClassifyIds) > 0 {
  145. classifyIdsCond += fmt.Sprintf(` %s IN (%s)`, recordOb.Cols().ClassifyIdFirst, utils.GetOrmInReplace(len(firstClassifyIds)))
  146. pars = append(pars, firstClassifyIds)
  147. }
  148. if len(secondClassifyIds) > 0 {
  149. if classifyIdsCond != "" {
  150. classifyIdsCond += fmt.Sprintf(` OR %s IN (%s)`, recordOb.Cols().ClassifyIdSecond, utils.GetOrmInReplace(len(secondClassifyIds)))
  151. } else {
  152. classifyIdsCond += fmt.Sprintf(` %s IN (%s)`, recordOb.Cols().ClassifyIdSecond, utils.GetOrmInReplace(len(secondClassifyIds)))
  153. }
  154. pars = append(pars, secondClassifyIds)
  155. }
  156. if len(thirdClassifyIds) > 0 {
  157. if classifyIdsCond != "" {
  158. classifyIdsCond += fmt.Sprintf(` OR %s IN (%s)`, recordOb.Cols().ClassifyIdThird, utils.GetOrmInReplace(len(thirdClassifyIds)))
  159. } else {
  160. classifyIdsCond += fmt.Sprintf(` %s IN (%s)`, recordOb.Cols().ClassifyIdThird, utils.GetOrmInReplace(len(thirdClassifyIds)))
  161. }
  162. pars = append(pars, thirdClassifyIds)
  163. }
  164. if classifyIdsCond != "" {
  165. cond += fmt.Sprintf(` AND (%s)`, classifyIdsCond)
  166. }
  167. }
  168. total, e := recordOb.GetCountByCondition(cond, pars)
  169. if e != nil {
  170. br.Msg = "获取失败"
  171. br.ErrMsg = fmt.Sprintf("获取阅读记录计数失败, %v", e)
  172. return
  173. }
  174. list, e := recordOb.GetPageItemsByCondition(cond, pars, []string{}, "", startSize, params.PageSize)
  175. if e != nil {
  176. br.Msg = "获取失败"
  177. br.ErrMsg = fmt.Sprintf("获取阅读记录列表失败, %v", e)
  178. return
  179. }
  180. for _, v := range list {
  181. respList = append(respList, v.Format2Item())
  182. }
  183. page := paging.GetPaging(params.CurrentIndex, params.PageSize, total)
  184. resp.Paging = page
  185. resp.List = respList
  186. br.Data = resp
  187. br.Ret = 200
  188. br.Msg = "获取成功"
  189. br.Success = true
  190. }