like.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. package yb
  2. import (
  3. "github.com/rdlucklib/rdluck_tools/paging"
  4. "hongze/hz_crm_api/controllers"
  5. "hongze/hz_crm_api/models"
  6. "hongze/hz_crm_api/models/company"
  7. "hongze/hz_crm_api/models/yb"
  8. ybResp "hongze/hz_crm_api/models/yb/response"
  9. ybService "hongze/hz_crm_api/services/yb"
  10. "hongze/hz_crm_api/utils"
  11. "strconv"
  12. "strings"
  13. "time"
  14. )
  15. type LikeController struct {
  16. controllers.BaseAuthController
  17. }
  18. // ReportList
  19. // @Title 获取已被点赞的报告列表
  20. // @Description 获取已被点赞的报告列表
  21. // @Param SortParam query string false "排序关键词:LikeNum"
  22. // @Param SortType query string false "排序顺序:asc、desc"
  23. // @Param PageSize query string false "每页展示的记录数"
  24. // @param CurrentIndex query string false "页码"
  25. // @Success 200 {object} ybResp.LikeReportListResp
  26. // @router /like/report/list [get]
  27. func (this *LikeController) ReportList() {
  28. br := new(models.BaseResponse).Init()
  29. defer func() {
  30. this.Data["json"] = br
  31. this.ServeJSON()
  32. }()
  33. sysUser := this.SysUser
  34. if sysUser == nil {
  35. br.Msg = "请登录"
  36. br.ErrMsg = "请登录,SysUser is Empty"
  37. br.Ret = 408
  38. return
  39. }
  40. pageSize, _ := this.GetInt("PageSize")
  41. currentIndex, _ := this.GetInt("CurrentIndex")
  42. var startSize int
  43. if pageSize <= 0 {
  44. pageSize = utils.PageSize20
  45. }
  46. if currentIndex <= 0 {
  47. currentIndex = 1
  48. }
  49. startSize = paging.StartIndex(currentIndex, pageSize)
  50. //todo 判断查询权限
  51. brMsg, brErrMsg := ybService.CheckCommentPermission(sysUser.AdminId)
  52. if brMsg != "" {
  53. br.Msg = brMsg
  54. br.ErrMsg = brErrMsg
  55. return
  56. }
  57. orderStr := ""
  58. orderType := ""
  59. reqSortParam := this.GetString("SortParam", "")
  60. if reqSortParam != "" {
  61. }
  62. reqSortType := this.GetString("SortType")
  63. if reqSortType != "" && reqSortParam == "LikeNum" {
  64. orderType = reqSortType
  65. }
  66. likeList, err := yb.GetLikeReportIds(orderType)
  67. if err != nil {
  68. br.Msg = "获取报告ID失败!"
  69. br.ErrMsg = "获取报告ID失败,Err:" + err.Error()
  70. return
  71. }
  72. if len(likeList) == 0 {
  73. page := paging.GetPaging(currentIndex, pageSize, 0)
  74. data := &ybResp.LikeReportListResp{
  75. Paging: page,
  76. }
  77. br.Ret = 200
  78. br.Success = true
  79. br.Msg = "操作成功"
  80. br.Data = data
  81. return
  82. }
  83. reportIds := "0"
  84. reportChapterIds := "0"
  85. reportChapterConcat := ""
  86. LikeNumMap := make(map[string]int)
  87. LikeTimeMap := make(map[string]time.Time)
  88. dayWeekReportIds := ""
  89. for _, v := range likeList {
  90. reportIdStr := strconv.Itoa(v.ReportId)
  91. reportChapterIdStr := strconv.Itoa(v.ReportChapterId)
  92. // 过滤晨报和周报报告ID
  93. if v.ReportChapterId == 0 {
  94. reportIds += "," + reportIdStr
  95. }
  96. // 过滤章节ID为0的值
  97. if reportChapterIdStr != "0" {
  98. dayWeekReportIds += "," + reportIdStr
  99. reportChapterIds += "," + reportChapterIdStr
  100. }
  101. concat := reportIdStr + "-" + reportChapterIdStr
  102. reportChapterConcat += `,"` + concat + `"`
  103. LikeNumMap[concat] = v.LikeNum
  104. LikeTimeMap[concat] = v.ModifyTime
  105. }
  106. if reportChapterConcat != "" {
  107. reportChapterConcat = strings.Trim(reportChapterConcat, ",")
  108. }
  109. //根据入参判断是否需要按照点赞数排序
  110. if reqSortParam == "LikeNum" {
  111. orderStr = reportChapterConcat
  112. }
  113. reportList, err := models.GetLikeReportByReportIdReportChapterId(reportIds, reportChapterIds, orderStr, startSize, pageSize)
  114. if err != nil {
  115. br.Msg = "获取报告失败!"
  116. br.ErrMsg = "获取报告失败,Err:" + err.Error()
  117. return
  118. }
  119. //查询晨报周报的总标题
  120. dayWeekTitleMap := make(map[int]string)
  121. if dayWeekReportIds != "" {
  122. dayWeekReportIds = strings.Trim(dayWeekReportIds, ",")
  123. dayWeekReports, tErr := models.GetSimpleReportByIds(dayWeekReportIds)
  124. if tErr != nil {
  125. br.Msg = "获取报告失败!"
  126. br.ErrMsg = "获取报告失败,Err:" + tErr.Error()
  127. return
  128. }
  129. for _, v := range dayWeekReports {
  130. dayWeekTitleMap[v.Id] = v.Title
  131. }
  132. }
  133. var list []*ybResp.LikeReportListItem
  134. for _, v := range reportList {
  135. tmp := new(ybResp.LikeReportListItem)
  136. concat := strconv.Itoa(v.ReportId) + "-" + strconv.Itoa(v.ReportChapterId)
  137. tmp.ReportId = v.ReportId
  138. tmp.ReportChapterId = v.ReportChapterId
  139. tmp.ClassifyIdFirst = v.ClassifyIdFirst
  140. tmp.ClassifyNameFirst = v.ClassifyNameFirst
  141. tmp.PublishTime = v.PublishTime.Format(utils.FormatDateTime)
  142. // todo 晨周报需要展示大标题
  143. if tmp.ReportChapterId > 0 {
  144. if t, ok := dayWeekTitleMap[v.ReportId]; ok {
  145. tmp.Title = t
  146. }
  147. } else {
  148. tmp.Title = v.Title
  149. }
  150. tmp.ReportChapterTypeName = v.ReportChapterTypeName
  151. if t, ok := LikeTimeMap[concat]; ok {
  152. tmp.ModifyTime = t.Format(utils.FormatDateTime)
  153. }
  154. if n, ok := LikeNumMap[concat]; ok {
  155. tmp.LikeNum = n
  156. }
  157. list = append(list, tmp)
  158. }
  159. // 获取列表分页总数
  160. total, err := models.GetLikeReportTotalByReportIdReportChapterId(reportIds, reportChapterIds)
  161. if err != nil {
  162. br.Msg = "获取留言列表总数失败!"
  163. br.ErrMsg = "获取留言列表总数失败,Err:" + err.Error()
  164. return
  165. }
  166. page := paging.GetPaging(currentIndex, pageSize, int(total))
  167. data := &ybResp.LikeReportListResp{
  168. List: list,
  169. Paging: page,
  170. }
  171. br.Ret = 200
  172. br.Success = true
  173. br.Msg = "操作成功"
  174. br.Data = data
  175. return
  176. }
  177. // List
  178. // @Title 获取报告的点赞列表
  179. // @Description 获取报告的点赞列表
  180. // @Param ReportId query string true "报告ID"
  181. // @Param ReportChapterId query string true "报告章节ID"
  182. // @Param PageSize query string false "页码"
  183. // @param CurrentIndex query string false "每页展示的记录数"
  184. // @Success 200 {object} ybResp.LikeListResp
  185. // @router /like/list [get]
  186. func (this *LikeController) List() {
  187. br := new(models.BaseResponse).Init()
  188. defer func() {
  189. this.Data["json"] = br
  190. this.ServeJSON()
  191. }()
  192. sysUser := this.SysUser
  193. if sysUser == nil {
  194. br.Msg = "请登录"
  195. br.ErrMsg = "请登录,SysUser is Empty"
  196. br.Ret = 408
  197. return
  198. }
  199. pageSize, _ := this.GetInt("PageSize")
  200. currentIndex, _ := this.GetInt("CurrentIndex")
  201. var startSize int
  202. if pageSize <= 0 {
  203. pageSize = utils.PageSize20
  204. }
  205. if currentIndex <= 0 {
  206. currentIndex = 1
  207. }
  208. startSize = paging.StartIndex(currentIndex, pageSize)
  209. //todo 判断查询权限
  210. brMsg, brErrMsg := ybService.CheckCommentPermission(sysUser.AdminId)
  211. if brMsg != "" {
  212. br.Msg = brMsg
  213. br.ErrMsg = brErrMsg
  214. return
  215. }
  216. //根据搜索条件查询留言列表
  217. var (
  218. err error
  219. reportId int
  220. reportChapterId int
  221. )
  222. reqReportId := this.GetString("ReportId", "")
  223. if reqReportId == "" {
  224. br.Msg = "请输入报告ID"
  225. br.ErrMsg = "请输入报告ID"
  226. return
  227. }
  228. reportId, err = strconv.Atoi(reqReportId)
  229. if err != nil {
  230. br.Msg = "请输入正确的报告ID"
  231. br.ErrMsg = "请输入正确的报告ID:" + err.Error()
  232. return
  233. }
  234. reqReportChapterId := this.GetString("ReportChapterId", "")
  235. if reqReportId == "" {
  236. br.Msg = "请输入报告ID"
  237. br.ErrMsg = "请输入报告ID"
  238. return
  239. }
  240. if reqReportChapterId != "" {
  241. reportChapterId, err = strconv.Atoi(reqReportChapterId)
  242. if err != nil {
  243. br.Msg = "请输入正确的章节ID"
  244. br.ErrMsg = "请输入正确的章节ID:" + err.Error()
  245. return
  246. }
  247. }
  248. likeList, err := yb.GetLikeListByReportId(reportId, reportChapterId, startSize, pageSize)
  249. if err != nil {
  250. br.Msg = "获取点赞列表失败!"
  251. br.ErrMsg = "获取点赞列表失败,Err:" + err.Error()
  252. return
  253. }
  254. var userIds string
  255. for _, v := range likeList {
  256. userIds += "," + strconv.Itoa(int(v.UserId))
  257. }
  258. // 查询点赞相关的用户
  259. var userOthers []*models.WxUser
  260. if len(userIds) > 0 {
  261. userOthers, err = models.GetWxUserListByUserIds(strings.Trim(userIds, ","))
  262. if err != nil {
  263. br.Msg = "查询点赞用户出错"
  264. br.ErrMsg = "查询点赞用户出错,Err:" + err.Error()
  265. return
  266. }
  267. }
  268. usersMap := make(map[uint64]*models.WxUser)
  269. companyIdArr := make([]int, 0)
  270. for _, v := range userOthers {
  271. usersMap[uint64(v.UserId)] = v
  272. companyIdArr = append(companyIdArr, v.CompanyId)
  273. }
  274. // 客户名称、状态
  275. companyIdNameMap := make(map[int]string, 0)
  276. companyIdStatusMap := make(map[int]string, 0)
  277. if len(companyIdArr) > 0 {
  278. companies, e := company.GetCompanyProductsByCompanyIdsAndProductId(companyIdArr, 1)
  279. if e != nil {
  280. br.Msg = "获取点赞客户信息失败"
  281. br.ErrMsg = "查询客户信息出错,Err:" + e.Error()
  282. return
  283. }
  284. companyLen := len(companies)
  285. for i := 0; i < companyLen; i++ {
  286. companyIdNameMap[companies[i].CompanyId] = companies[i].CompanyName
  287. companyIdStatusMap[companies[i].CompanyId] = companies[i].Status
  288. }
  289. }
  290. var list []*ybResp.LikeListItem
  291. for _, v := range likeList {
  292. tmp := new(ybResp.LikeListItem)
  293. tmp.LikeId = v.LikeId
  294. tmp.UserId = v.UserId
  295. if info, ok := usersMap[v.UserId]; ok {
  296. tmp.NickName = info.NickName
  297. tmp.RealName = info.RealName
  298. tmp.CompanyName = companyIdNameMap[info.CompanyId]
  299. tmp.CompanyStatus = companyIdStatusMap[info.CompanyId]
  300. }
  301. tmp.SourceAgent = v.SourceAgent
  302. tmp.ModifyTime = v.ModifyTime.Format(utils.FormatDateTime)
  303. list = append(list, tmp)
  304. }
  305. // 获取列表分页总数
  306. total, err := yb.GetLikeListTotalByReportId(reportId, reportChapterId)
  307. if err != nil {
  308. br.Msg = "获取点赞列表总数失败!"
  309. br.ErrMsg = "获取点赞列表总数失败,Err:" + err.Error()
  310. return
  311. }
  312. page := paging.GetPaging(currentIndex, pageSize, int(total))
  313. resp := ybResp.LikeListResp{
  314. List: list,
  315. Paging: page,
  316. }
  317. br.Ret = 200
  318. br.Success = true
  319. br.Msg = "获取成功"
  320. br.Data = resp
  321. return
  322. }