yb_common.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. package services
  2. import (
  3. "errors"
  4. "gorm.io/gorm"
  5. "hongze/hongze_yb/models/tables/company_product"
  6. "hongze/hongze_yb/models/tables/rddp/report"
  7. "hongze/hongze_yb/models/tables/rddp/report_chapter"
  8. "hongze/hongze_yb/models/tables/research_report_type"
  9. "hongze/hongze_yb/models/tables/voice_broadcast_statistics"
  10. "hongze/hongze_yb/models/tables/yb_community_audio_listen_log"
  11. "hongze/hongze_yb/models/tables/yb_community_video_play_log"
  12. "hongze/hongze_yb/models/tables/yb_like"
  13. "hongze/hongze_yb/services/user"
  14. "hongze/hongze_yb/utils"
  15. "strings"
  16. )
  17. // 访问日志来源
  18. const (
  19. ViewLogSourceQuestion = iota + 1 // 问答社区-1
  20. ViewLogSourceVoiceBroadcast // 语音播报-2
  21. ViewLogSourceVideo // 视频社区-3
  22. ViewLogSourceRoadVideo // 路演视频-4
  23. )
  24. // GetReportIdReportChapterIdByOldReportId 根据老报告的ID查询对应的新报告ID
  25. func GetReportIdReportChapterIdByOldReportId(oldReportId, oldReportChapterId uint64) (reportId int, reportChapterId int, err error, errMsg string) {
  26. var reportNew *report.Report
  27. if oldReportChapterId > 0 {
  28. //查询章节详情,根据章节类型ID和老报告ID,根据老报告ID,查询新报告ID,根据新报告ID和type_id 找到新的章节ID
  29. var oldReportChapter *research_report_type.ResearchReportTypeInfo
  30. var reportChapterNew *report_chapter.ReportChapter
  31. oldReportChapter, err = research_report_type.GetResearchReportTypeInfo(oldReportChapterId)
  32. if err != nil {
  33. errMsg = err.Error()
  34. err = errors.New("找不到报告章节")
  35. return
  36. }
  37. if oldReportChapter == nil {
  38. err = errors.New("找不到报告章节")
  39. return
  40. }
  41. if oldReportId != oldReportChapter.ResearchReportID {
  42. err = errors.New("报告ID和章节ID不一致")
  43. return
  44. }
  45. //判断是否是晨报或者周报的章节ID,双周报的章节ID不记录
  46. if oldReportChapter.TypeID == 0 {
  47. err = errors.New("只允许传晨报和周报的章节ID")
  48. return
  49. }
  50. reportNew, err = report.GetReportByOldReportId(oldReportChapter.ResearchReportID)
  51. if err != nil {
  52. errMsg = err.Error()
  53. //err = errors.New("找不到新版报告")
  54. return
  55. }
  56. if reportNew.Id <= 0 {
  57. //err = errors.New("找不到新版报告")
  58. return
  59. }
  60. reportChapterNew, err = report_chapter.GetChapterByReportIdTypeId(reportNew.Id, oldReportChapter.TypeID)
  61. if err != nil {
  62. errMsg = err.Error()
  63. return
  64. }
  65. if reportChapterNew.ReportChapterId <= 0 {
  66. //err = errors.New("找不到新版章节")
  67. return
  68. }
  69. reportId = reportNew.Id
  70. reportChapterId = reportChapterNew.ReportChapterId
  71. return
  72. } else if oldReportId > 0 {
  73. // 查询新报告ID
  74. reportNew, err = report.GetReportByOldReportId(oldReportId)
  75. if err != nil {
  76. errMsg = err.Error()
  77. //err = errors.New("找不到新版报告")
  78. return
  79. }
  80. reportId = reportNew.Id
  81. return
  82. }
  83. return
  84. }
  85. // CheckReportExistByReportIdReportChapterId 评论和点赞时,校验传入的报告ID是否正确
  86. func CheckReportExistByReportIdReportChapterId(reportId, reportChapterId int) (err error, errMsg string) {
  87. reportInfo, err := report.GetByReportId(reportId)
  88. if err != nil {
  89. errMsg = err.Error()
  90. err = errors.New("查询报告出错")
  91. return
  92. }
  93. if reportInfo.Id <= 0 {
  94. err = errors.New("报告不存在")
  95. return
  96. }
  97. //if reportInfo.HasChapter == 1 && reportChapterId <= 0 {
  98. // err = errors.New("请输入报告章节ID")
  99. // return
  100. //}
  101. if reportChapterId > 0 {
  102. reportChapterInfo, tErr := report_chapter.GetTypeIdById(reportChapterId)
  103. if tErr != nil {
  104. errMsg = tErr.Error()
  105. err = errors.New("查询章节失败")
  106. return
  107. }
  108. if reportChapterInfo.ReportChapterId == 0 {
  109. err = errors.New("章节不存在或者未发布")
  110. return
  111. }
  112. if reportChapterInfo.ReportId != reportId {
  113. err = errors.New("章节ID和报告ID不匹配")
  114. return
  115. }
  116. }
  117. return
  118. }
  119. // CheckSimpleCompanyProduct 校验用户是否FICC产品的已购或者试用状态
  120. func CheckSimpleCompanyProduct(userinfo user.UserInfo) (err error, errMsg string) {
  121. // 判断用户状态是否是正常和永续
  122. var productAuthOk bool
  123. companyProduct, err := company_product.GetByCompany2ProductId(userinfo.CompanyID, 1)
  124. if err == utils.ErrNoRow {
  125. err = nil
  126. }
  127. if err != nil {
  128. errMsg = err.Error()
  129. err = errors.New("查询用户购买产品出错")
  130. return
  131. }
  132. if companyProduct != nil {
  133. // 无FICC权限的客户不可见
  134. if companyProduct.CompanyProductID > 0 {
  135. // 已购或者试用用户可见
  136. if strings.Contains("永续,正式", companyProduct.Status) || (companyProduct.Status == "试用" && companyProduct.IsSuspend != 1) {
  137. productAuthOk = true
  138. }
  139. }
  140. }
  141. if !productAuthOk {
  142. err = errors.New("无权操作")
  143. return
  144. }
  145. return
  146. }
  147. func GetReportLikeByReportIdOldReportId(userId uint64, reportId, reportChapterId int, oldReportId, oldReportChapterId int) (likeNum int64, likeEnabled int8, err error) {
  148. // 根据老报告找到新报告的ID
  149. if reportId == 0 && oldReportId > 0 {
  150. reportId, reportChapterId, err, _ = GetReportIdReportChapterIdByOldReportId(uint64(oldReportId), uint64(oldReportChapterId))
  151. }
  152. //查询总的点赞数
  153. if reportId > 0 {
  154. likeNum, err = yb_like.GetLikeNumByReportId(reportId, reportChapterId)
  155. } else if oldReportId > 0 {
  156. likeNum, err = yb_like.GetLikeNumByOldReportId(oldReportId, oldReportChapterId)
  157. }
  158. if err != nil {
  159. err = errors.New("查询点赞数出错")
  160. return
  161. }
  162. //查询用户对研报的点赞状态
  163. var likeItem *yb_like.YbLike
  164. if reportId > 0 {
  165. likeItem, err = yb_like.GetLikeByUserIdAndReportId(userId, reportId, reportChapterId)
  166. } else {
  167. likeItem, err = yb_like.GetLikeByUserIdAndOldReportId(userId, oldReportId, oldReportChapterId)
  168. }
  169. if err != nil {
  170. err = errors.New("查询用户点赞记录出错")
  171. return
  172. }
  173. likeEnabled = likeItem.Enabled
  174. return
  175. }
  176. // UpdateViewLogBySource 更新各模块访问日志
  177. // Source: 来源:1-问答社区; 2-语音播报; 3-视频社区; 4-路演视频...
  178. func UpdateViewLogBySource(userId, viewLogId, stopSeconds, source int) (err error) {
  179. if viewLogId <= 0 || source <= 0 {
  180. return
  181. }
  182. switch source {
  183. case ViewLogSourceQuestion:
  184. viewLog := new(yb_community_audio_listen_log.YbCommunityAudioListenLog)
  185. viewLog.Id = viewLogId
  186. item, e := viewLog.GetById()
  187. if e != nil {
  188. if e == gorm.ErrRecordNotFound {
  189. return
  190. }
  191. err = errors.New("获取访问日志失败, Err: " + e.Error())
  192. return
  193. }
  194. if item.UserID != userId {
  195. err = errors.New("用户不一致, 更新日志失败")
  196. return
  197. }
  198. item.StopSeconds = stopSeconds
  199. err = item.Update([]string{"stop_seconds"})
  200. case ViewLogSourceVoiceBroadcast:
  201. viewLog := new(voice_broadcast_statistics.VoiceBroadcastStatistics)
  202. viewLog.Id = viewLogId
  203. item, e := viewLog.GetById()
  204. if e != nil {
  205. if e == gorm.ErrRecordNotFound {
  206. return
  207. }
  208. err = errors.New("获取访问日志失败, Err: " + e.Error())
  209. return
  210. }
  211. if item.UserId != userId {
  212. err = errors.New("用户不一致, 更新日志失败")
  213. return
  214. }
  215. item.StopSeconds = stopSeconds
  216. err = item.Update([]string{"stop_seconds"})
  217. case ViewLogSourceVideo, ViewLogSourceRoadVideo:
  218. viewLog := new(yb_community_video_play_log.YbCommunityVideoPlayLog)
  219. viewLog.ID = viewLogId
  220. item, e := viewLog.GetById()
  221. if e != nil {
  222. if e == gorm.ErrRecordNotFound {
  223. return
  224. }
  225. err = errors.New("获取访问日志失败, Err: " + e.Error())
  226. return
  227. }
  228. if item.UserID != userId {
  229. err = errors.New("用户不一致, 更新日志失败")
  230. return
  231. }
  232. item.StopSeconds = stopSeconds
  233. err = item.Update([]string{"stop_seconds"})
  234. default:
  235. err = errors.New("来源有误")
  236. return
  237. }
  238. return
  239. }