user_read_record.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. package controllers
  2. import (
  3. "eta/eta_mini_crm/models"
  4. "eta/eta_mini_crm/models/response"
  5. "eta/eta_mini_crm/utils"
  6. "fmt"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "github.com/beego/beego/v2/client/orm"
  11. "github.com/rdlucklib/rdluck_tools/paging"
  12. )
  13. type UserReadRecordController struct {
  14. BaseAuthController
  15. }
  16. // List
  17. // @Title 用户阅读统计列表
  18. // @Description 用户阅读统计列表
  19. // @Param PageSize query int true "每页数据条数"
  20. // @Param CurrentIndex query int true "当前页页码,从1开始"
  21. // @Param SellerId query int true "销售id"
  22. // @Param Status query string true "用户状态"
  23. // @Param KeyWord query string true "手机号/邮箱/姓名"
  24. // @Param IsRegistered query string true "是否注册"
  25. // @Param IsSubscribed query string true "是否关注"
  26. // @Param RegisterStartDate query string true "注册开始时间"
  27. // @Param RegisterEndDate query string true "注册结束时间"
  28. // @Param CreateStartDate query string true "创建开始时间"
  29. // @Param CreateEndDate query string true "创建结束时间"
  30. // @Param SortParam query string true "排序字段"
  31. // @Param SortType query string true "排序方式"
  32. // @Success 200 {object} response.UserListResp
  33. // @router /list [get]
  34. func (this *UserReadRecordController) List() {
  35. br := new(models.BaseResponse).Init()
  36. defer func() {
  37. this.Data["json"] = br
  38. this.ServeJSON()
  39. }()
  40. pageSize, _ := this.GetInt("PageSize")
  41. currentIndex, _ := this.GetInt("CurrentIndex")
  42. sellerIdStr := this.GetString("SellerId")
  43. status := this.GetString("Status")
  44. keyWord := this.GetString("KeyWord")
  45. IsRegistered := this.GetString("IsRegisterd")
  46. IsSubscribed := this.GetString("IsSubscribed")
  47. registerStartDate := this.GetString("RegisterStartDate")
  48. registerEndDate := this.GetString("RegisterEndDate")
  49. createStartDate := this.GetString("CreateStartDate")
  50. createEndDate := this.GetString("CreateEndDate")
  51. sortParma := this.GetString("SortParam")
  52. sortType := this.GetString("SortType")
  53. var condition string
  54. var sortCondition string
  55. var pars []interface{}
  56. if keyWord != "" {
  57. condition += ` AND (u.real_name LIKE ? OR u.phone LIKE ? OR u.email LIKE ? OR u.company LIKE ?) `
  58. pars = utils.GetLikeKeywordPars(pars, keyWord, 4)
  59. }
  60. if pageSize <= 0 {
  61. pageSize = utils.PageSize20
  62. } else if pageSize > utils.PageSize100 {
  63. pageSize = utils.PageSize100
  64. }
  65. if currentIndex <= 0 {
  66. currentIndex = 1
  67. }
  68. if sellerIdStr != "" {
  69. sellerIds := strings.Split(sellerIdStr, ",")
  70. if len(sellerIds) != 0 {
  71. condition += ` AND ( `
  72. for i, id := range sellerIds {
  73. if i == 0 {
  74. condition += ` u.seller_id = ? `
  75. pars = append(pars, id)
  76. } else {
  77. condition += ` OR u.seller_id = ? `
  78. pars = append(pars, id)
  79. }
  80. }
  81. condition += `) `
  82. }
  83. }
  84. if sortParma != "" && sortType != "" {
  85. sortCondition = " ORDER BY "
  86. var param, sort string
  87. switch sortParma {
  88. case "LastUpdateTime":
  89. param = "last_update_time"
  90. case "ReadCnt":
  91. param = "read_cnt"
  92. }
  93. switch sortType {
  94. case "asc":
  95. sort = " ASC "
  96. case "desc":
  97. sort = " DESC "
  98. }
  99. if param != "" && sort != "" {
  100. sortCondition += param + " " + sort
  101. } else {
  102. sortCondition = ""
  103. }
  104. }
  105. switch status {
  106. case fmt.Sprint(utils.UserStatusNo):
  107. condition += " AND u.status=? "
  108. pars = append(pars, 0)
  109. case fmt.Sprint(1):
  110. condition += " AND u.status=? "
  111. pars = append(pars, 2)
  112. default:
  113. condition += " AND (u.status=? OR u.status=?) "
  114. pars = append(pars, 0, 2)
  115. }
  116. switch IsRegistered {
  117. case "是":
  118. condition += " AND u.is_registered=? "
  119. pars = append(pars, true)
  120. case "否":
  121. condition += " AND u.is_registered=? "
  122. pars = append(pars, false)
  123. }
  124. switch IsSubscribed {
  125. case "是":
  126. condition += " AND u.is_subscribed=? "
  127. pars = append(pars, true)
  128. case "否":
  129. condition += " AND u.is_subscribed=? "
  130. pars = append(pars, false)
  131. }
  132. if registerStartDate != "" {
  133. condition += " AND u.register_time>? "
  134. pars = append(pars, registerStartDate)
  135. }
  136. if registerEndDate != "" {
  137. condition += " AND u.register_time<? "
  138. pars = append(pars, registerEndDate)
  139. }
  140. if createStartDate != "" {
  141. condition += " AND u.create_time>? "
  142. pars = append(pars, createStartDate)
  143. }
  144. if createEndDate != "" {
  145. condition += " AND u.create_time<? "
  146. pars = append(pars, createEndDate)
  147. }
  148. if pageSize <= 0 {
  149. pageSize = utils.PageSize20
  150. } else if pageSize > utils.PageSize100 {
  151. pageSize = utils.PageSize100
  152. }
  153. if currentIndex <= 0 {
  154. currentIndex = 1
  155. }
  156. startSize := utils.StartIndex(currentIndex, pageSize)
  157. total, err := models.GetUserReadCount(condition, pars)
  158. if err != nil {
  159. br.Msg = "获取失败"
  160. br.ErrMsg = "获取失败,Err:" + err.Error()
  161. return
  162. }
  163. userList, err := models.GetUserReadList(condition, sortCondition, pars, startSize, pageSize)
  164. if err != nil {
  165. br.Msg = "查询用户失败"
  166. br.Msg = "查询用户失败,系统错误,Err:" + err.Error()
  167. return
  168. }
  169. page := paging.GetPaging(currentIndex, pageSize, total)
  170. resp := new(response.UserListResp)
  171. resp.Paging = page
  172. resp.List = userList
  173. br.Data = resp
  174. br.Ret = 200
  175. br.Success = true
  176. br.Msg = "获取成功"
  177. }
  178. // Detail
  179. // @Title 用户阅读记录详情
  180. // @Description 用户阅读记录详情信息
  181. // @Param PageSize query int true "每页数据条数"
  182. // @Param CurrentIndex query int true "当前页页码,从1开始"
  183. // @Param ChartPermissionIds query string true "品种列表"
  184. // @Param ClassifyIds query string true "品种列表"
  185. // @Success 200 {object} models.LoginResp
  186. // @router /detail [get]
  187. func (this *UserReadRecordController) Detail() {
  188. br := new(models.BaseResponse).Init()
  189. defer func() {
  190. this.Data["json"] = br
  191. this.ServeJSON()
  192. }()
  193. UserId, _ := this.GetInt("UserId")
  194. pageSize, _ := this.GetInt("PageSize")
  195. currentIndex, _ := this.GetInt("CurrentIndex")
  196. chartPermissionids := this.GetString("ChartPermissionIds")
  197. classifyIds := this.GetString("ClassifyIds")
  198. if UserId <= 0 {
  199. br.Msg = "查询用户不存在"
  200. return
  201. }
  202. if pageSize <= 0 {
  203. pageSize = utils.PageSize20
  204. } else if pageSize > utils.PageSize100 {
  205. pageSize = utils.PageSize100
  206. }
  207. if currentIndex <= 0 {
  208. currentIndex = 1
  209. }
  210. startSize := utils.StartIndex(currentIndex, pageSize)
  211. user, err := models.GetUserById(UserId)
  212. if err != nil {
  213. if err == orm.ErrNoRows {
  214. br.Msg = "用户不存在或已删除,请刷新页面"
  215. return
  216. }
  217. br.Msg = "查询用户失败"
  218. br.ErrMsg = "查询用户失败,系统错误,Err:" + err.Error()
  219. return
  220. }
  221. if user == nil {
  222. br.Msg = "用户不存在或已删除,请刷新页面"
  223. return
  224. }
  225. var condition string
  226. var pars []interface{}
  227. if chartPermissionids != "" {
  228. ids := strings.Split(chartPermissionids, ",")
  229. if len(ids) != 0 {
  230. condition += ` AND ( `
  231. for i, id := range ids {
  232. if i == 0 {
  233. condition += ` urp2.chart_permission_id = ? `
  234. pars = append(pars, id)
  235. } else {
  236. condition += ` OR urp2.chart_permission_id = ? `
  237. pars = append(pars, id)
  238. }
  239. }
  240. condition += `) `
  241. }
  242. }
  243. if classifyIds != "" {
  244. ids := strings.Split(classifyIds, ",")
  245. if len(ids) != 0 {
  246. condition += ` AND ( `
  247. for i, id := range ids {
  248. if i == 0 {
  249. condition += ` classify_id2 = ? `
  250. pars = append(pars, id)
  251. } else {
  252. condition += ` OR classify_id2 = ? `
  253. pars = append(pars, id)
  254. }
  255. }
  256. condition += `) `
  257. }
  258. }
  259. total, err := models.GetUserReadRecordCountByUserId(UserId, condition, pars)
  260. if err != nil {
  261. br.Msg = "查询阅读记录失败"
  262. br.ErrMsg = "查询阅读记录失败,Err:" + err.Error()
  263. return
  264. }
  265. readList, err := models.GetUserReadRecordByUserId(UserId, condition, pars, startSize, pageSize)
  266. if err != nil {
  267. br.Msg = "查询阅读记录失败"
  268. br.ErrMsg = "查询阅读记录失败,系统错误,Err:" + err.Error()
  269. return
  270. }
  271. page := paging.GetPaging(currentIndex, pageSize, total)
  272. resp := new(response.UserReadRecordListResp)
  273. resp.Paging = page
  274. resp.List = readList
  275. br.Msg = "获取成功"
  276. br.Data = resp
  277. br.Success = true
  278. br.Ret = 200
  279. }
  280. // ReadCntChart
  281. // @Title 用户阅读量统计图信息
  282. // @Description 用户阅读量统计图信息
  283. // @Param ChartPermissionIds query string true "品种列表"
  284. // @Param ClassifyIds query string true "品种列表"
  285. // @Param StartDate query string true "开始时间"
  286. // @Param EndDate query string true "结束时间"
  287. // @Param UserId query int true "用户id"
  288. // @Success 200 {object} models.LoginResp
  289. // @router /readCntChart [get]
  290. func (this *UserReadRecordController) ReadCntChart() {
  291. br := new(models.BaseResponse).Init()
  292. defer func() {
  293. this.Data["json"] = br
  294. this.ServeJSON()
  295. }()
  296. userId, _ := this.GetInt("UserId")
  297. startDate := this.GetString("StartDate")
  298. endDate := this.GetString("EndDate")
  299. chartPermissionIds := this.GetString("ChartPermissionIds")
  300. classifyIds := this.GetString("ClassifyIds")
  301. var condition string
  302. var pars []interface{}
  303. if chartPermissionIds != "" {
  304. ids := strings.Split(chartPermissionIds, ",")
  305. if len(ids) != 0 {
  306. condition += ` AND ( `
  307. for i, id := range ids {
  308. if i == 0 {
  309. condition += ` urp.chart_permission_id = ? `
  310. pars = append(pars, id)
  311. } else {
  312. condition += ` OR urp.chart_permission_id = ? `
  313. pars = append(pars, id)
  314. }
  315. }
  316. condition += `) `
  317. }
  318. }
  319. if classifyIds != "" {
  320. ids := strings.Split(classifyIds, ",")
  321. if len(ids) != 0 {
  322. condition += ` AND ( `
  323. for i, id := range ids {
  324. if i == 0 {
  325. condition += ` classify_id2 = ? `
  326. pars = append(pars, id)
  327. } else {
  328. condition += ` OR classify_id2 = ? `
  329. pars = append(pars, id)
  330. }
  331. }
  332. condition += `) `
  333. }
  334. }
  335. if startDate == "" {
  336. startDate = time.Now().AddDate(-1, 0, 0).Format(utils.FormatDate)
  337. }
  338. if endDate == "" {
  339. endDate = time.Now().AddDate(0, 0, 1).Format(utils.FormatDate)
  340. }
  341. if userId > 0 {
  342. condition += ` AND ur.user_id = ? `
  343. pars = append(pars, userId)
  344. }
  345. readCnts, err := models.GetStaticReadCnt(condition, pars, startDate, endDate)
  346. if err != nil {
  347. br.Msg = "获取阅读统计失败"
  348. br.ErrMsg = "获取阅读统计失败,系统错误,Err:" + err.Error()
  349. return
  350. }
  351. br.Msg = "查询成功"
  352. br.Data = readCnts
  353. br.Ret = 200
  354. br.Success = true
  355. }
  356. // readPermissionChart
  357. // @Title 用户阅读品种图信息
  358. // @Description 用户阅读品种图信息
  359. // @Param UserId query string true "用户id"
  360. // @Success 200 {object} models.LoginResp
  361. // @router /readPermissionChart [get]
  362. func (this *UserReadRecordController) ReadPermissionChart() {
  363. br := new(models.BaseResponse).Init()
  364. defer func() {
  365. this.Data["json"] = br
  366. this.ServeJSON()
  367. }()
  368. userId, _ := this.GetInt("UserId")
  369. var condition string
  370. var pars []interface{}
  371. if userId > 0 {
  372. condition += ` AND ur.user_id = ? `
  373. pars = append(pars, userId)
  374. }
  375. permissionCnts, err := models.GetStaticPermissionCnt(condition, pars)
  376. if err != nil {
  377. br.Msg = "获取品种阅读统计失败"
  378. br.ErrMsg = "获取品种阅读统计失败,系统错误,Err:" + err.Error()
  379. return
  380. }
  381. var sum int
  382. for _, v := range permissionCnts {
  383. sum += v.Count
  384. }
  385. for _, v := range permissionCnts {
  386. percent := float64(v.Count) / float64(sum) * 100
  387. percentStr := fmt.Sprintf("%.0f", percent)
  388. parsedFloat, _ := strconv.ParseFloat(percentStr, 64)
  389. v.Percent = parsedFloat
  390. }
  391. br.Msg = "查询成功"
  392. br.Data = permissionCnts
  393. br.Ret = 200
  394. br.Success = true
  395. }