user_read_record.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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 "禁用":
  107. condition += " AND u.status=? "
  108. pars = append(pars, 0)
  109. case "潜在":
  110. condition += " AND u.status=? "
  111. pars = append(pars, 1)
  112. case "正式":
  113. condition += " AND u.status=? "
  114. pars = append(pars, 2)
  115. case "":
  116. condition += " AND (u.status=? OR u.status=?) "
  117. pars = append(pars, 0, 2)
  118. }
  119. switch IsRegistered {
  120. case "是":
  121. condition += " AND u.is_registered=? "
  122. pars = append(pars, true)
  123. case "否":
  124. condition += " AND u.is_registered=? "
  125. pars = append(pars, false)
  126. }
  127. switch IsSubscribed {
  128. case "是":
  129. condition += " AND u.is_subscribed=? "
  130. pars = append(pars, true)
  131. case "否":
  132. condition += " AND u.is_subscribed=? "
  133. pars = append(pars, false)
  134. }
  135. if registerStartDate != "" {
  136. registerStartTime, er := time.Parse("2006-01-02 15:04:05", registerStartDate)
  137. if er != nil {
  138. br.Msg = "日期格式有误"
  139. return
  140. }
  141. condition += " AND u.register_time>? "
  142. pars = append(pars, registerStartTime)
  143. }
  144. if registerEndDate != "" {
  145. registerEndTime, er := time.Parse("2006-01-02 15:04:05", registerEndDate)
  146. if er != nil {
  147. br.Msg = "日期格式有误"
  148. return
  149. }
  150. condition += " AND u.register_time<? "
  151. pars = append(pars, registerEndTime)
  152. }
  153. if createStartDate != "" {
  154. createStartTime, er := time.Parse("2006-01-02 15:04:05", createStartDate)
  155. if er != nil {
  156. br.Msg = "日期格式有误"
  157. return
  158. }
  159. condition += " AND u.create_time>? "
  160. pars = append(pars, createStartTime)
  161. }
  162. if createEndDate != "" {
  163. createEndTime, er := time.Parse("2006-01-02 15:04:05", createEndDate)
  164. if er != nil {
  165. br.Msg = "日期格式有误"
  166. return
  167. }
  168. condition += " AND u.create_time<? "
  169. pars = append(pars, createEndTime)
  170. }
  171. if pageSize <= 0 {
  172. pageSize = utils.PageSize20
  173. } else if pageSize > utils.PageSize100 {
  174. pageSize = utils.PageSize100
  175. }
  176. if currentIndex <= 0 {
  177. currentIndex = 1
  178. }
  179. startSize := utils.StartIndex(currentIndex, pageSize)
  180. total, err := models.GetUserReadCount(condition, pars)
  181. if err != nil {
  182. br.Msg = "获取失败"
  183. br.ErrMsg = "获取失败,Err:" + err.Error()
  184. return
  185. }
  186. userList, err := models.GetUserReadList(condition, sortCondition, pars, startSize, pageSize)
  187. if err != nil {
  188. br.Msg = "查询用户失败"
  189. br.Msg = "查询用户失败,系统错误,Err:" + err.Error()
  190. return
  191. }
  192. page := paging.GetPaging(currentIndex, pageSize, total)
  193. resp := new(response.UserListResp)
  194. resp.Paging = page
  195. resp.List = userList
  196. br.Data = resp
  197. br.Ret = 200
  198. br.Success = true
  199. br.Msg = "获取成功"
  200. }
  201. // Detail
  202. // @Title 用户阅读记录详情
  203. // @Description 用户阅读记录详情信息
  204. // @Param PageSize query int true "每页数据条数"
  205. // @Param CurrentIndex query int true "当前页页码,从1开始"
  206. // @Param ChartPermissionIds query string true "品种列表"
  207. // @Param ClassifyIds query string true "品种列表"
  208. // @Success 200 {object} models.LoginResp
  209. // @router /detail [get]
  210. func (this *UserReadRecordController) Detail() {
  211. br := new(models.BaseResponse).Init()
  212. defer func() {
  213. this.Data["json"] = br
  214. this.ServeJSON()
  215. }()
  216. UserId, _ := this.GetInt("UserId")
  217. pageSize, _ := this.GetInt("PageSize")
  218. currentIndex, _ := this.GetInt("CurrentIndex")
  219. chartPermissionids := this.GetString("ChartPermissionIds")
  220. classifyIds := this.GetString("ClassifyIds")
  221. if UserId <= 0 {
  222. br.Msg = "查询用户不存在"
  223. return
  224. }
  225. if pageSize <= 0 {
  226. pageSize = utils.PageSize20
  227. } else if pageSize > utils.PageSize100 {
  228. pageSize = utils.PageSize100
  229. }
  230. if currentIndex <= 0 {
  231. currentIndex = 1
  232. }
  233. startSize := utils.StartIndex(currentIndex, pageSize)
  234. user, err := models.GetUserById(UserId)
  235. if err != nil {
  236. if err == orm.ErrNoRows {
  237. br.Msg = "用户不存在或已删除,请刷新页面"
  238. return
  239. }
  240. br.Msg = "查询用户失败"
  241. br.ErrMsg = "查询用户失败,系统错误,Err:" + err.Error()
  242. return
  243. }
  244. if user == nil {
  245. br.Msg = "用户不存在或已删除,请刷新页面"
  246. return
  247. }
  248. var condition string
  249. var pars []interface{}
  250. if chartPermissionids != "" {
  251. ids := strings.Split(chartPermissionids, ",")
  252. if len(ids) != 0 {
  253. condition += ` AND ( `
  254. for i, id := range ids {
  255. if i == 0 {
  256. condition += ` urp2.chart_permission_id = ? `
  257. pars = append(pars, id)
  258. } else {
  259. condition += ` OR urp2.chart_permission_id = ? `
  260. pars = append(pars, id)
  261. }
  262. }
  263. condition += `) `
  264. }
  265. }
  266. if classifyIds != "" {
  267. ids := strings.Split(classifyIds, ",")
  268. if len(ids) != 0 {
  269. condition += ` AND ( `
  270. for i, id := range ids {
  271. if i == 0 {
  272. condition += ` classify_id2 = ? `
  273. pars = append(pars, id)
  274. } else {
  275. condition += ` OR classify_id2 = ? `
  276. pars = append(pars, id)
  277. }
  278. }
  279. condition += `) `
  280. }
  281. }
  282. total, err := models.GetUserReadRecordCountByUserId(UserId, condition, pars)
  283. if err != nil {
  284. br.Msg = "查询阅读记录失败"
  285. br.ErrMsg = "查询阅读记录失败,Err:" + err.Error()
  286. return
  287. }
  288. readList, err := models.GetUserReadRecordByUserId(UserId, condition, pars, startSize, pageSize)
  289. if err != nil {
  290. br.Msg = "查询阅读记录失败"
  291. br.ErrMsg = "查询阅读记录失败,系统错误,Err:" + err.Error()
  292. return
  293. }
  294. page := paging.GetPaging(currentIndex, pageSize, total)
  295. resp := new(response.UserReadRecordListResp)
  296. resp.Paging = page
  297. resp.List = readList
  298. br.Msg = "获取成功"
  299. br.Data = resp
  300. br.Success = true
  301. br.Ret = 200
  302. }
  303. // ReadCntChart
  304. // @Title 用户阅读量统计图信息
  305. // @Description 用户阅读量统计图信息
  306. // @Param ChartPermissionIds query string true "品种列表"
  307. // @Param ClassifyIds query string true "品种列表"
  308. // @Param StartDate query string true "开始时间"
  309. // @Param EndDate query string true "结束时间"
  310. // @Param UserId query int true "用户id"
  311. // @Success 200 {object} models.LoginResp
  312. // @router /readCntChart [get]
  313. func (this *UserReadRecordController) ReadCntChart() {
  314. br := new(models.BaseResponse).Init()
  315. defer func() {
  316. this.Data["json"] = br
  317. this.ServeJSON()
  318. }()
  319. userId, _ := this.GetInt("UserId")
  320. startDate := this.GetString("StartDate")
  321. endDate := this.GetString("EndDate")
  322. chartPermissionIds := this.GetString("ChartPermissionIds")
  323. classifyIds := this.GetString("ClassifyIds")
  324. var condition string
  325. var pars []interface{}
  326. if chartPermissionIds != "" {
  327. ids := strings.Split(chartPermissionIds, ",")
  328. if len(ids) != 0 {
  329. condition += ` AND ( `
  330. for i, id := range ids {
  331. if i == 0 {
  332. condition += ` urp.chart_permission_id = ? `
  333. pars = append(pars, id)
  334. } else {
  335. condition += ` OR urp.chart_permission_id = ? `
  336. pars = append(pars, id)
  337. }
  338. }
  339. condition += `) `
  340. }
  341. }
  342. if classifyIds != "" {
  343. ids := strings.Split(classifyIds, ",")
  344. if len(ids) != 0 {
  345. condition += ` AND ( `
  346. for i, id := range ids {
  347. if i == 0 {
  348. condition += ` classify_id2 = ? `
  349. pars = append(pars, id)
  350. } else {
  351. condition += ` OR classify_id2 = ? `
  352. pars = append(pars, id)
  353. }
  354. }
  355. condition += `) `
  356. }
  357. }
  358. if startDate == "" {
  359. startDate = time.Now().AddDate(-1, 0, 0).Format(utils.FormatDate)
  360. }
  361. if endDate == "" {
  362. endDate = time.Now().AddDate(0, 0, 1).Format(utils.FormatDate)
  363. }
  364. if userId > 0 {
  365. condition += ` AND ur.user_id = ? `
  366. pars = append(pars, userId)
  367. }
  368. readCnts, err := models.GetStaticReadCnt(condition, pars, startDate, endDate)
  369. if err != nil {
  370. br.Msg = "获取阅读统计失败"
  371. br.ErrMsg = "获取阅读统计失败,系统错误,Err:" + err.Error()
  372. return
  373. }
  374. br.Msg = "查询成功"
  375. br.Data = readCnts
  376. br.Ret = 200
  377. br.Success = true
  378. }
  379. // readPermissionChart
  380. // @Title 用户阅读品种图信息
  381. // @Description 用户阅读品种图信息
  382. // @Param UserId query string true "用户id"
  383. // @Success 200 {object} models.LoginResp
  384. // @router /readPermissionChart [get]
  385. func (this *UserReadRecordController) ReadPermissionChart() {
  386. br := new(models.BaseResponse).Init()
  387. defer func() {
  388. this.Data["json"] = br
  389. this.ServeJSON()
  390. }()
  391. userId, _ := this.GetInt("UserId")
  392. var condition string
  393. var pars []interface{}
  394. if userId > 0 {
  395. condition += ` AND ur.user_id = ? `
  396. pars = append(pars, userId)
  397. }
  398. permissionCnts, err := models.GetStaticPermissionCnt(condition, pars)
  399. if err != nil {
  400. br.Msg = "获取品种阅读统计失败"
  401. br.ErrMsg = "获取品种阅读统计失败,系统错误,Err:" + err.Error()
  402. return
  403. }
  404. var sum int
  405. for _, v := range permissionCnts {
  406. sum += v.Count
  407. }
  408. for _, v := range permissionCnts {
  409. percent := float64(v.Count) / float64(sum) * 100
  410. percentStr := fmt.Sprintf("%.0f", percent)
  411. parsedFloat, _ := strconv.ParseFloat(percentStr, 64)
  412. v.Percent = parsedFloat
  413. }
  414. br.Msg = "查询成功"
  415. br.Data = permissionCnts
  416. br.Ret = 200
  417. br.Success = true
  418. }