user_read_record.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. if classifyIds != "" {
  271. ids := strings.Split(classifyIds, ",")
  272. if len(ids) != 0 {
  273. condition += ` AND ( `
  274. for i, id := range ids {
  275. if i == 0 {
  276. condition += ` classify_id2 = ? `
  277. pars = append(pars, id)
  278. } else {
  279. condition += ` OR classify_id2 = ? `
  280. pars = append(pars, id)
  281. }
  282. }
  283. condition += `) `
  284. }
  285. }
  286. total, err := models.GetUserReadRecordCountByUserId(UserId, condition, pars)
  287. if err != nil {
  288. br.Msg = "查询阅读记录失败"
  289. br.ErrMsg = "查询阅读记录失败,Err:" + err.Error()
  290. return
  291. }
  292. readList, err := models.GetUserReadRecordByUserId(UserId, condition, pars, startSize, pageSize)
  293. if err != nil {
  294. br.Msg = "查询阅读记录失败"
  295. br.ErrMsg = "查询阅读记录失败,系统错误,Err:" + err.Error()
  296. return
  297. }
  298. page := paging.GetPaging(currentIndex, pageSize, total)
  299. resp := new(response.UserReadRecordListResp)
  300. resp.Paging = page
  301. resp.List = readList
  302. br.Msg = "获取成功"
  303. br.Data = resp
  304. br.Success = true
  305. br.Ret = 200
  306. }
  307. // ReadCntChart
  308. // @Title 用户阅读量统计图信息
  309. // @Description 用户阅读量统计图信息
  310. // @Param ChartPermissionIds query string true "品种列表"
  311. // @Param ClassifyIds query string true "品种列表"
  312. // @Param StartDate query string true "开始时间"
  313. // @Param EndDate query string true "结束时间"
  314. // @Param UserId query int true "用户id"
  315. // @Success 200 {object} models.LoginResp
  316. // @router /readCntChart [get]
  317. func (this *UserReadRecordController) ReadCntChart() {
  318. br := new(models.BaseResponse).Init()
  319. defer func() {
  320. this.Data["json"] = br
  321. this.ServeJSON()
  322. }()
  323. userId, _ := this.GetInt("UserId")
  324. startDate := this.GetString("StartDate")
  325. endDate := this.GetString("EndDate")
  326. chartPermissionIds := this.GetString("ChartPermissionIds")
  327. classifyIds := this.GetString("ClassifyIds")
  328. var condition string
  329. var pars []interface{}
  330. if chartPermissionIds != "" {
  331. ids := strings.Split(chartPermissionIds, ",")
  332. if len(ids) != 0 {
  333. condition += ` AND ( `
  334. for i, id := range ids {
  335. if i == 0 {
  336. condition += ` urp.chart_permission_id = ? `
  337. pars = append(pars, id)
  338. } else {
  339. condition += ` OR urp.chart_permission_id = ? `
  340. pars = append(pars, id)
  341. }
  342. }
  343. condition += `) `
  344. }
  345. }
  346. if classifyIds != "" {
  347. ids := strings.Split(classifyIds, ",")
  348. if len(ids) != 0 {
  349. condition += ` AND ( `
  350. for i, id := range ids {
  351. if i == 0 {
  352. condition += ` classify_id2 = ? `
  353. pars = append(pars, id)
  354. } else {
  355. condition += ` OR classify_id2 = ? `
  356. pars = append(pars, id)
  357. }
  358. }
  359. condition += `) `
  360. }
  361. }
  362. if startDate == "" {
  363. startDate = time.Now().AddDate(-1, 0, 0).Format(utils.FormatDate)
  364. }
  365. if endDate == "" {
  366. endDate = time.Now().AddDate(0, 0, 1).Format(utils.FormatDate)
  367. }
  368. if userId > 0 {
  369. condition += ` AND ur.user_id = ? `
  370. pars = append(pars, userId)
  371. }
  372. readCnts, err := models.GetStaticReadCnt(condition, pars, startDate, endDate)
  373. if err != nil {
  374. br.Msg = "获取阅读统计失败"
  375. br.ErrMsg = "获取阅读统计失败,系统错误,Err:" + err.Error()
  376. return
  377. }
  378. br.Msg = "查询成功"
  379. br.Data = readCnts
  380. br.Ret = 200
  381. br.Success = true
  382. }
  383. // readPermissionChart
  384. // @Title 用户阅读品种图信息
  385. // @Description 用户阅读品种图信息
  386. // @Param UserId query string true "用户id"
  387. // @Success 200 {object} models.LoginResp
  388. // @router /readPermissionChart [get]
  389. func (this *UserReadRecordController) ReadPermissionChart() {
  390. br := new(models.BaseResponse).Init()
  391. defer func() {
  392. this.Data["json"] = br
  393. this.ServeJSON()
  394. }()
  395. userId, _ := this.GetInt("UserId")
  396. var condition string
  397. var pars []interface{}
  398. if userId > 0 {
  399. condition += ` AND ur.user_id = ? `
  400. pars = append(pars, userId)
  401. }
  402. permissionCnts, err := models.GetStaticPermissionCnt(condition, pars)
  403. if err != nil {
  404. br.Msg = "获取品种阅读统计失败"
  405. br.ErrMsg = "获取品种阅读统计失败,系统错误,Err:" + err.Error()
  406. return
  407. }
  408. var sum int
  409. for _, v := range permissionCnts {
  410. sum += v.Count
  411. }
  412. for _, v := range permissionCnts {
  413. percent := float64(v.Count) / float64(sum) * 100
  414. percentStr := fmt.Sprintf("%.0f", percent)
  415. parsedFloat, _ := strconv.ParseFloat(percentStr, 64)
  416. v.Percent = parsedFloat
  417. }
  418. br.Msg = "查询成功"
  419. br.Data = permissionCnts
  420. br.Ret = 200
  421. br.Success = true
  422. }