user_read_record.go 11 KB

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