user_read_record.go 13 KB

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