user.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. package controllers
  2. import (
  3. "eta/eta_mini_crm_ht/models"
  4. "eta/eta_mini_crm_ht/models/response"
  5. "eta/eta_mini_crm_ht/services"
  6. "eta/eta_mini_crm_ht/utils"
  7. "fmt"
  8. "github.com/rdlucklib/rdluck_tools/paging"
  9. "strconv"
  10. "strings"
  11. "time"
  12. )
  13. type UserController struct {
  14. BaseAuthController
  15. }
  16. // TemplateList
  17. // @Title 临时用户列表
  18. // @Description 临时用户列表
  19. // @Param PageSize query int true "每页数据条数"
  20. // @Param CurrentIndex query int true "当前页页码,从1开始"
  21. // @Param Keyword query string false "手机号"
  22. // @Param SortParam query string false "排序字段参数,用来排序的字段, 枚举值:0:注册时间,1:阅读数,2:最近一次阅读时间"
  23. // @Param SortType query string true "如何排序,是正序还是倒序,0:倒序,1:正序"
  24. // @Success 200 {object} response.TemplateUserListResp
  25. // @router /temporary/list [get]
  26. func (this *UserController) TemplateList() {
  27. br := new(models.BaseResponse).Init()
  28. defer func() {
  29. this.Data["json"] = br
  30. this.ServeJSON()
  31. }()
  32. pageSize, _ := this.GetInt("PageSize")
  33. currentIndex, _ := this.GetInt("CurrentIndex")
  34. keyword := this.GetString("Keyword")
  35. if pageSize <= 0 {
  36. pageSize = utils.PageSize20
  37. } else if pageSize > utils.PageSize100 {
  38. pageSize = utils.PageSize100
  39. }
  40. if currentIndex <= 0 {
  41. currentIndex = 1
  42. }
  43. if pageSize <= 0 {
  44. pageSize = utils.PageSize20
  45. } else if pageSize > utils.PageSize100 {
  46. pageSize = utils.PageSize100
  47. }
  48. if currentIndex <= 0 {
  49. currentIndex = 1
  50. }
  51. startSize := utils.StartIndex(currentIndex, pageSize)
  52. sortParamInt, _ := this.GetInt("SortParam", 0)
  53. sortTypeInt, _ := this.GetInt("SortType", 0)
  54. var sortStr = ``
  55. var condition string
  56. var pars []interface{}
  57. sortParamMap := map[int]string{0: "created_time", 1: "read_count", 2: "last_read_time"}
  58. sortTypeMap := map[int]string{0: "desc", 1: "asc"}
  59. sortParam, ok := sortParamMap[sortParamInt]
  60. if !ok {
  61. br.Msg = "错误的排序字段参数"
  62. br.ErrMsg = fmt.Sprint("错误的排序字段:", sortParamInt)
  63. return
  64. }
  65. sortType, ok := sortTypeMap[sortTypeInt]
  66. if !ok {
  67. br.Msg = "错误的排序字段"
  68. br.ErrMsg = fmt.Sprint("错误的排序字段:", sortTypeInt)
  69. return
  70. }
  71. sortStr = fmt.Sprintf("%s %s,updated_time desc ", sortParam, sortType)
  72. if keyword != "" {
  73. condition += ` AND mobile LIKE ? `
  74. pars = utils.GetLikeKeywordPars(pars, keyword, 1)
  75. }
  76. resp := new(response.TemplateUserListResp)
  77. total, userList, err := models.GetPageTemplateUserList(condition, pars, sortStr, startSize, pageSize)
  78. if err != nil {
  79. br.Msg = "查询用户失败"
  80. br.Msg = "查询用户失败,系统错误,Err:" + err.Error()
  81. return
  82. }
  83. list := make([]models.TemplateUsersItem, 0)
  84. for _, v := range userList {
  85. list = append(list, v.ToItem())
  86. }
  87. page := paging.GetPaging(currentIndex, pageSize, total)
  88. resp.Paging = page
  89. resp.List = list
  90. br.Data = resp
  91. br.Ret = 200
  92. br.Success = true
  93. br.Msg = "获取成功"
  94. }
  95. // OfficialList
  96. // @Title 正式用户列表
  97. // @Description 正式用户列表
  98. // @Param PageSize query int true "每页数据条数"
  99. // @Param CurrentIndex query int true "当前页页码,从1开始"
  100. // @Param Keyword query string false "手机号"
  101. // @Param SortParam query string false "排序字段参数,用来排序的字段, 枚举值:0:注册时间,1:阅读数,2:最近一次阅读时间"
  102. // @Param SortType query string true "如何排序,是正序还是倒序,0:倒序,1:正序"
  103. // @Success 200 {object} response.TemplateUserListResp
  104. // @router /official/list [get]
  105. func (this *UserController) OfficialList() {
  106. br := new(models.BaseResponse).Init()
  107. defer func() {
  108. this.Data["json"] = br
  109. this.ServeJSON()
  110. }()
  111. pageSize, _ := this.GetInt("PageSize")
  112. currentIndex, _ := this.GetInt("CurrentIndex")
  113. keyword := this.GetString("Keyword")
  114. FollowingGzh := this.GetString("FollowingGzh")
  115. RegisterBeginDate := this.GetString("RegisterBeginDate")
  116. RegisterEndDate := this.GetString("RegisterEndDate")
  117. if pageSize <= 0 {
  118. pageSize = utils.PageSize20
  119. } else if pageSize > utils.PageSize100 {
  120. pageSize = utils.PageSize100
  121. }
  122. if currentIndex <= 0 {
  123. currentIndex = 1
  124. }
  125. if pageSize <= 0 {
  126. pageSize = utils.PageSize20
  127. } else if pageSize > utils.PageSize100 {
  128. pageSize = utils.PageSize100
  129. }
  130. if currentIndex <= 0 {
  131. currentIndex = 1
  132. }
  133. startSize := utils.StartIndex(currentIndex, pageSize)
  134. sortParamInt, _ := this.GetInt("SortParam", 0)
  135. sortTypeInt, _ := this.GetInt("SortType", 0)
  136. var sortStr = ``
  137. var condition string
  138. var pars []interface{}
  139. sortParamMap := map[int]string{0: "created_time", 1: "read_count", 2: "last_read_time"}
  140. sortTypeMap := map[int]string{0: "desc", 1: "asc"}
  141. sortParam, ok := sortParamMap[sortParamInt]
  142. if !ok {
  143. br.Msg = "错误的排序字段参数"
  144. br.ErrMsg = fmt.Sprint("错误的排序字段:", sortParamInt)
  145. return
  146. }
  147. sortType, ok := sortTypeMap[sortTypeInt]
  148. if !ok {
  149. br.Msg = "错误的排序字段"
  150. br.ErrMsg = fmt.Sprintf("错误的排序字段:%v", sortTypeInt)
  151. return
  152. }
  153. sortStr = fmt.Sprintf("%s %s,updated_time desc ", sortParam, sortType)
  154. if FollowingGzh != "" {
  155. switch FollowingGzh {
  156. case "true":
  157. condition += ` AND following_gzh=1 `
  158. case "false":
  159. condition += ` AND following_gzh=0 `
  160. default:
  161. br.Msg = "关注公众号字段非法字段"
  162. br.ErrMsg = fmt.Sprintf("错误的关注公众号字段:%s", FollowingGzh)
  163. return
  164. }
  165. }
  166. if RegisterBeginDate != "" || RegisterEndDate != "" {
  167. var beginDate, endDate time.Time
  168. var parseErr error
  169. if RegisterBeginDate != "" {
  170. beginDate, parseErr = time.Parse(time.DateOnly, RegisterBeginDate)
  171. if parseErr != nil {
  172. br.Msg = "注册时间开始字段非法字段"
  173. br.ErrMsg = fmt.Sprintf("错误的注册时间开始字段:%s", RegisterBeginDate)
  174. return
  175. }
  176. }
  177. if RegisterEndDate != "" {
  178. endDate, parseErr = time.Parse(time.DateOnly, RegisterEndDate)
  179. if parseErr != nil {
  180. br.Msg = "注册时间结束字段非法字段"
  181. br.ErrMsg = fmt.Sprintf("错误的注册时间结束字段:%s", RegisterEndDate)
  182. return
  183. }
  184. }
  185. if RegisterBeginDate != "" {
  186. if RegisterEndDate != "" {
  187. if beginDate.After(endDate) {
  188. br.Msg = "结束时间不能早于开始时间"
  189. br.ErrMsg = fmt.Sprintf("错误的注册时间结束字段:开始时间:%s,结束时间:%s", RegisterBeginDate, RegisterEndDate)
  190. return
  191. }
  192. condition += ` AND DATE_FORMAT(created_time,'%Y-%m-%d') BETWEEN ? AND ?`
  193. pars = append(pars, RegisterBeginDate, RegisterEndDate)
  194. } else {
  195. condition += ` AND DATE_FORMAT(created_time,'%Y-%m-%d') >= ?`
  196. pars = append(pars, RegisterBeginDate)
  197. }
  198. } else {
  199. condition += ` AND DATE_FORMAT(created_time,'%Y-%m-%d') <= ?`
  200. pars = append(pars, RegisterEndDate)
  201. }
  202. }
  203. if keyword != "" {
  204. condition += ` AND ( mobile LIKE ? or real_name like ?)`
  205. pars = utils.GetLikeKeywordPars(pars, keyword, 2)
  206. }
  207. resp := new(response.UserListResp)
  208. total, userList, err := models.GetPageOfficialUserList(condition, pars, sortStr, startSize, pageSize)
  209. if err != nil {
  210. br.Msg = "查询用户失败"
  211. br.Msg = "查询用户失败,系统错误,Err:" + err.Error()
  212. return
  213. }
  214. //list := make([]*models.UserView, 0)
  215. //var wg sync.WaitGroup
  216. //wg.Add(len(userList))
  217. //for _, v := range userList {
  218. // go func(v *models.User) {
  219. // defer wg.Done()
  220. // tempUser, _ := models.GetTemplateUser(v.TemplateUserId)
  221. // userView := v.FillUserInfo(tempUser)
  222. // list = append(list, &userView)
  223. // }(&v)
  224. //}
  225. //wg.Wait()
  226. page := paging.GetPaging(currentIndex, pageSize, total)
  227. resp.Paging = page
  228. resp.List = userList
  229. br.Data = resp
  230. br.Ret = 200
  231. br.Success = true
  232. br.Msg = "获取成功"
  233. }
  234. // readRecordList
  235. // @Title 用户阅读记录
  236. // @Description 用户阅读记录
  237. // @Param PageSize query int true "每页数据条数"
  238. // @Param CurrentIndex query int true "当前页页码,从1开始"
  239. // @Param ChartPermissionIds query string true "品种列表"
  240. // @Param ClassifyIds query string true "品种列表"
  241. // @Success 200 {object} models.LoginResp
  242. // @router /readRecordList [get]
  243. func (this *UserController) ReadRecordList() {
  244. br := new(models.BaseResponse).Init()
  245. defer func() {
  246. this.Data["json"] = br
  247. this.ServeJSON()
  248. }()
  249. UserId, _ := this.GetInt("UserId")
  250. pageSize, _ := this.GetInt("PageSize")
  251. currentIndex, _ := this.GetInt("CurrentIndex")
  252. permissionIds := this.GetString("PermissionIds")
  253. productType := this.GetString("ProductType")
  254. if UserId <= 0 {
  255. br.Msg = "查询用户失败,用户id不合法"
  256. return
  257. }
  258. if pageSize <= 0 {
  259. pageSize = utils.PageSize20
  260. } else if pageSize > utils.PageSize100 {
  261. pageSize = utils.PageSize100
  262. }
  263. if currentIndex <= 0 {
  264. currentIndex = 1
  265. }
  266. startSize := utils.StartIndex(currentIndex, pageSize)
  267. user, err := models.GetTemplateUser(UserId)
  268. if err != nil {
  269. if err.Error() == utils.ErrNoRow() {
  270. br.Msg = "用户不存在或已删除,请刷新页面"
  271. return
  272. }
  273. br.Msg = "查询用户失败"
  274. br.ErrMsg = "查询用户失败,系统错误,Err:" + err.Error()
  275. return
  276. }
  277. if user == nil {
  278. br.Msg = "用户不存在或已删除,请刷新页面"
  279. return
  280. }
  281. var condition string
  282. var pars []interface{}
  283. var sourceType models.SourceType
  284. if productType != "" {
  285. sourceType = transSourceType(productType)
  286. if sourceType == "" {
  287. br.Msg = "错误的产品类型"
  288. br.ErrMsg = "错误的产品类型:" + productType
  289. return
  290. }
  291. condition += `and source_type= ?`
  292. pars = append(pars, sourceType)
  293. }
  294. var permissionIdList []int
  295. if permissionIds != "" {
  296. permissionIdStrList := strings.Split(permissionIds, ",")
  297. for _, permissionIdStr := range permissionIdStrList {
  298. permissionId, parseErr := strconv.Atoi(permissionIdStr)
  299. if parseErr != nil {
  300. continue
  301. }
  302. permissionIdList = append(permissionIdList, permissionId)
  303. }
  304. }
  305. total, condition, pars, err := services.GetUserSourceClickFlowListCountByUserId(UserId, condition, pars, permissionIdList, sourceType)
  306. if err != nil {
  307. br.Msg = "查询阅读记录失败"
  308. br.ErrMsg = "查询阅读记录失败,Err:" + err.Error()
  309. return
  310. }
  311. if total <= 0 {
  312. br.Msg = "暂无数据"
  313. br.Data = new(response.UserSourceClickFlowResp)
  314. br.Success = true
  315. br.Ret = 200
  316. return
  317. }
  318. readList, err := services.GetUserSourceClickFlowListByUserId(UserId, condition, pars, startSize, pageSize)
  319. if err != nil {
  320. br.Msg = "查询阅读记录失败"
  321. br.ErrMsg = "查询阅读记录失败,系统错误,Err:" + err.Error()
  322. return
  323. }
  324. page := paging.GetPaging(currentIndex, pageSize, total)
  325. resp := new(response.UserSourceClickFlowResp)
  326. resp.Paging = page
  327. resp.List = readList
  328. br.Msg = "获取成功"
  329. br.Data = resp
  330. br.Success = true
  331. br.Ret = 200
  332. }
  333. func transSourceType(productType string) (sourceType models.SourceType) {
  334. if productType == "" {
  335. return ""
  336. }
  337. switch productType {
  338. case Report:
  339. sourceType = models.ReportSourceType
  340. return
  341. case Video:
  342. sourceType = models.VideoSourceType
  343. return
  344. case Audio:
  345. sourceType = models.AudioSourceType
  346. return
  347. default:
  348. return ""
  349. }
  350. }