research_report.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. package report
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "hongze/hongze_yb/global"
  8. "hongze/hongze_yb/models/tables/company"
  9. "hongze/hongze_yb/models/tables/company_report_permission"
  10. "hongze/hongze_yb/models/tables/research_report"
  11. "hongze/hongze_yb/models/tables/research_report_type"
  12. "hongze/hongze_yb/models/tables/user_view_history"
  13. "hongze/hongze_yb/models/tables/wx_user"
  14. "hongze/hongze_yb/services"
  15. "hongze/hongze_yb/utils"
  16. "strconv"
  17. "time"
  18. )
  19. type ResearchReportInfo struct {
  20. //ResearchReportInfo *research_report.ResearchReport `json:"research_report_info"`
  21. ResearchReportInfo *ResearchReportShow `json:"research_report_info"`
  22. ResearchReportTypeList []*company_report_permission.ResearchReportTypeList `json:"research_report_type_list"`
  23. HasMenu int `json:"has_menu"`
  24. ResearchReportTypeContentList []*research_report.ResearchReportTypeContent `description:"报告详情"`
  25. LikeNum int64 `description:"点赞总数" json:"like_num"`
  26. LikeEnabled int8 `description:"是否已点赞: 0-未点赞 1-已点赞" json:"like_enabled"`
  27. }
  28. type ResearchReportShow struct {
  29. ResearchReportID uint64 `json:"researchReportId"` // 研究报告id
  30. ResearchReportName string `json:"researchReportName"` // 研究报告名称
  31. ResearchReportTitle string `json:"researchReportTitle"` // 研究报告标题
  32. ResearchReportImg string `json:"researchReportImg"` // 报告缩略图URL
  33. ResearchReportDate string `json:"researchReportDate"` // 报告日期
  34. Type string `json:"type"` // day 晨报 week 周报 twoweek双周报 month 月报
  35. Author string `json:"author"` // 报告作者
  36. ReportVariety string `json:"reportVariety"` // 研究报告的品种,双周报和月报有标识
  37. IsHasMenu int8 `json:"isHasMenu"` // 报告是否含有目录
  38. IsSendedMsg int8 `json:"isSendedMsg"` // 是否发送过模板消息
  39. Periods int `json:"periods"` // 期数
  40. Status string `json:"status"` // 状态,draft:草稿,
  41. Enabled int8 `json:"enabled"` // 报告状态
  42. CreatedTime string `json:"createdTime"` // 创建时间
  43. LastUpdatedTime string `json:"lastUpdatedTime"` // 最后更新时间
  44. Viewers int `json:"viewers"` // H5观看用户数
  45. }
  46. // GetResearchReportInfo 获取报告详情
  47. func GetResearchReportInfo(researchReportId, userId uint64) (result ResearchReportInfo, hasPermission bool, err error) {
  48. //获取报告详情
  49. reportInfo, err := research_report.GetByResearchReportId(researchReportId)
  50. if err != nil {
  51. return
  52. }
  53. reportType := reportInfo.Type
  54. //这些个报告需要做权限校验
  55. if utils.InArray(reportInfo.Type, []string{"month", "two_week", "other"}) {
  56. list, tmpErr := company_report_permission.GetReportVarietyList(userId, reportType)
  57. if tmpErr != nil {
  58. err = tmpErr
  59. return
  60. }
  61. for _, v := range list {
  62. if reportInfo.ResearchReportID == v.ReportChapterTypeId {
  63. hasPermission = true
  64. break
  65. }
  66. }
  67. if !hasPermission {
  68. //permissionName, tmpErr := company_report_permission.GetPermissionNameByReportId(reportInfo.ResearchReportID, reportType)
  69. //if tmpErr != nil {
  70. // err = tmpErr
  71. // return
  72. //}
  73. return
  74. }
  75. } else {
  76. hasPermission = true
  77. }
  78. researchReportTypeList := make([]*company_report_permission.ResearchReportTypeList, 0)
  79. tmpResearchReportTypeList, err := company_report_permission.GetResearchReportType(reportInfo.ResearchReportID, userId, reportInfo.Type)
  80. if err != nil {
  81. return
  82. }
  83. reportDate := reportInfo.ResearchReportDate
  84. for _, v := range tmpResearchReportTypeList {
  85. if reportDate.Before(v.PauseStartTime) || reportDate.After(v.PauseEndTime) {
  86. researchReportTypeList = append(researchReportTypeList, v)
  87. }
  88. }
  89. // 联系人信息
  90. strInt64 := strconv.FormatUint(userId, 10)
  91. id, _ := strconv.Atoi(strInt64)
  92. wxUserInfo, err := wx_user.GetByUserId(id)
  93. if err != nil {
  94. fmt.Println("GetByUserId:", err.Error())
  95. return
  96. }
  97. companyInfo, tmpErr := company.GetByCompanyId(wxUserInfo.CompanyID)
  98. if tmpErr != nil {
  99. err = tmpErr
  100. if tmpErr == utils.ErrNoRow {
  101. err = errors.New("找不到该客户")
  102. return
  103. }
  104. return
  105. }
  106. //查询是否读过这篇报告,如果未读过则阅读人数+1
  107. _, err = user_view_history.GetReportByUserId(userId, reportInfo.ResearchReportID)
  108. if err != nil {
  109. err = reportInfo.UpdateViewers()
  110. if err != nil {
  111. fmt.Println("UpdateViewers err:", err.Error())
  112. }
  113. }
  114. //新增userViewHistory记录
  115. userViewHistory := &user_view_history.UserViewHistory{
  116. ViewHistoryID: 0,
  117. UserID: userId,
  118. Mobile: wxUserInfo.Mobile,
  119. Email: wxUserInfo.Email,
  120. RealName: wxUserInfo.RealName,
  121. CompanyName: companyInfo.CompanyName,
  122. ViewTitle: "",
  123. ViewPage: "",
  124. ReportChapterModule: "",
  125. CreatedTime: time.Now(),
  126. LastUpdatedTime: time.Now(),
  127. Type: "weekly_report",
  128. ResearchReportID: reportInfo.ResearchReportID,
  129. ResearchReportTypeID: 0,
  130. }
  131. err = userViewHistory.AddUserViewHistory()
  132. if err != nil {
  133. fmt.Println("AddUserViewHistory err", err.Error())
  134. }
  135. //添加阅读日志的数据加入到redis
  136. go PushViewRecordNewRedisData(userViewHistory, int(wxUserInfo.CompanyID))
  137. //查询点赞数
  138. likeNum, likeEnabled, _ := services.GetReportLikeByReportIdOldReportId(wxUserInfo.UserID, 0, 0, int(researchReportId), 0)
  139. reportInfoShow := &ResearchReportShow{
  140. ResearchReportID: reportInfo.ResearchReportID,
  141. ResearchReportName: reportInfo.ResearchReportName,
  142. ResearchReportTitle: reportInfo.ResearchReportTitle,
  143. ResearchReportImg: reportInfo.ResearchReportImg,
  144. ResearchReportDate: reportInfo.ResearchReportDate.Format(utils.FormatDate),
  145. Type: reportInfo.Type,
  146. Author: reportInfo.Author,
  147. ReportVariety: reportInfo.ReportVariety,
  148. IsHasMenu: reportInfo.IsHasMenu,
  149. IsSendedMsg: reportInfo.IsSendedMsg,
  150. Periods: reportInfo.Periods,
  151. Status: reportInfo.Status,
  152. Enabled: reportInfo.Enabled,
  153. CreatedTime: reportInfo.CreatedTime.Format(utils.FormatDateTime),
  154. LastUpdatedTime: reportInfo.LastUpdatedTime.Format(utils.FormatDateTime),
  155. Viewers: reportInfo.Viewers,
  156. }
  157. result = ResearchReportInfo{
  158. ResearchReportInfo: reportInfoShow,
  159. ResearchReportTypeList: researchReportTypeList,
  160. HasMenu: 1,
  161. LikeNum: likeNum,
  162. LikeEnabled: likeEnabled,
  163. }
  164. if len(researchReportTypeList) <= 0 {
  165. } else if len(researchReportTypeList) == 1 {
  166. //只有一个章节,即没有目录的时候,需要直接返回章节详情
  167. result.HasMenu = 0
  168. researchReportTypeContent, tmpErr := research_report.GetResearchReportTypeContentList(researchReportTypeList[0].ResearchReportTypeId)
  169. if tmpErr != nil {
  170. return
  171. }
  172. result.ResearchReportTypeContentList = researchReportTypeContent
  173. }
  174. return
  175. }
  176. type ResearchReportTypeContentInfo struct {
  177. ResearchReportTypeInfo *research_report_type.ResearchReportTypeInfo `json:"research_report_type_info"`
  178. Add int `json:"add"`
  179. ResearchReportTypeContentList []*research_report.ResearchReportTypeContent `description:"报告详情" json:"research_report_type_content_list"`
  180. LikeNum int64 `description:"点赞总数" json:"like_num"`
  181. LikeEnabled int8 `description:"是否已点赞: 0-未点赞 1-已点赞" json:"like_enabled"`
  182. }
  183. // GetResearchReportTypeContentInfo 获取报告章节详情
  184. func GetResearchReportTypeContentInfo(researchReportTypeId, userId uint64) (result ResearchReportTypeContentInfo, hasPermission bool, err error) {
  185. //获取章节详情
  186. researchReportTypeContentList, err := research_report.GetResearchReportTypeContentList(researchReportTypeId)
  187. if err != nil {
  188. return
  189. }
  190. researchReportTypeInfo, err := research_report_type.GetResearchReportTypeInfo(researchReportTypeId)
  191. //获取报告详情
  192. reportInfo, err := research_report.GetByResearchReportId(researchReportTypeInfo.ResearchReportID)
  193. if err != nil {
  194. return
  195. }
  196. reportType := reportInfo.Type
  197. //这些个报告需要做权限校验
  198. if utils.InArray(reportInfo.Type, []string{"week", "month", "two_week", "other"}) {
  199. list, tmpErr := company_report_permission.GetReportVarietyList(userId, reportType)
  200. if tmpErr != nil {
  201. err = tmpErr
  202. return
  203. }
  204. if reportInfo.Type == "week" {
  205. //周报校验章节是否在权限内
  206. for _, v := range list {
  207. if researchReportTypeInfo.ReportChapterTypeId == v.ReportChapterTypeId {
  208. hasPermission = true
  209. break
  210. }
  211. }
  212. } else {
  213. //双周报和月报校验 类型是否在权限内
  214. for _, v := range list {
  215. if reportInfo.ResearchReportID == v.ReportChapterTypeId {
  216. hasPermission = true
  217. break
  218. }
  219. }
  220. }
  221. if !hasPermission {
  222. //permissionName, tmpErr := company_report_permission.GetPermissionNameByReportId(reportInfo.ResearchReportID, reportType)
  223. //if tmpErr != nil {
  224. // err = tmpErr
  225. // return
  226. //}
  227. return
  228. }
  229. } else {
  230. hasPermission = true
  231. }
  232. add := 1
  233. if len(researchReportTypeContentList) > 0 {
  234. add = 0
  235. }
  236. // 联系人信息
  237. strInt64 := strconv.FormatUint(userId, 10)
  238. id, _ := strconv.Atoi(strInt64)
  239. wxUserInfo, err := wx_user.GetByUserId(id)
  240. if err != nil {
  241. fmt.Println("GetByUserId:", err.Error())
  242. return
  243. }
  244. companyInfo, tmpErr := company.GetByCompanyId(wxUserInfo.CompanyID)
  245. if tmpErr != nil {
  246. err = tmpErr
  247. if tmpErr == utils.ErrNoRow {
  248. err = errors.New("找不到该客户")
  249. return
  250. }
  251. return
  252. }
  253. //新增userViewHistory记录
  254. userViewHistory := &user_view_history.UserViewHistory{
  255. ViewHistoryID: 0,
  256. UserID: userId,
  257. Mobile: wxUserInfo.Mobile,
  258. Email: wxUserInfo.Email,
  259. RealName: wxUserInfo.RealName,
  260. CompanyName: companyInfo.CompanyName,
  261. ViewTitle: "",
  262. ViewPage: "",
  263. ReportChapterModule: "",
  264. CreatedTime: time.Now(),
  265. LastUpdatedTime: time.Now(),
  266. Type: "weekly_report",
  267. ResearchReportID: reportInfo.ResearchReportID,
  268. ResearchReportTypeID: researchReportTypeId,
  269. }
  270. err = userViewHistory.AddUserViewHistory()
  271. if err != nil {
  272. fmt.Println("AddUserViewHistory err", err.Error())
  273. }
  274. //添加阅读日志的数据加入到redis
  275. go PushViewRecordNewRedisData(userViewHistory, int(wxUserInfo.CompanyID))
  276. //查询点赞数
  277. likeNum, likeEnabled, _ := services.GetReportLikeByReportIdOldReportId(wxUserInfo.UserID, 0, 0, int(researchReportTypeInfo.ResearchReportID), int(researchReportTypeInfo.ResearchReportTypeID))
  278. result = ResearchReportTypeContentInfo{
  279. ResearchReportTypeContentList: researchReportTypeContentList,
  280. ResearchReportTypeInfo: researchReportTypeInfo,
  281. Add: add,
  282. LikeNum: likeNum,
  283. LikeEnabled: likeEnabled,
  284. }
  285. return
  286. }
  287. // UserViewRedisData 阅读数据
  288. type UserViewRedisData struct {
  289. Mobile string `json:"mobile"`
  290. Email string `json:"email"`
  291. RealName string `json:"real_name"`
  292. CompanyName string `json:"company_name"`
  293. ViewTime string `json:"view_time" description:"阅读时间,格式:2022-02-17 13:06:13"`
  294. ProductId int `json:"product_id" description:"报告所属产品,ficc:1,权益:2"`
  295. CompanyId int `json:"company_id" description:"客户id"`
  296. UserId int `json:"user_id" description:"用户id"`
  297. ReportId int `json:"report_id" description:"报告ID"`
  298. ReportChapterId int `json:"report_chapter_id" description:"章节ID"`
  299. StopTime int `json:"stop_time" description:"停留时间"`
  300. OutId int `json:"out_id" description:"章节ID"`
  301. }
  302. // PushViewRecordNewRedisData 阅读数据加入到redis
  303. func PushViewRecordNewRedisData(userViewHistory *user_view_history.UserViewHistory, companyId int) bool {
  304. data := &UserViewRedisData{
  305. Mobile: userViewHistory.Mobile,
  306. Email: userViewHistory.Email,
  307. RealName: userViewHistory.RealName,
  308. CompanyName: userViewHistory.CompanyName,
  309. ViewTime: userViewHistory.CreatedTime.Format(utils.FormatDateTime),
  310. ProductId: 1,
  311. CompanyId: companyId,
  312. UserId: int(userViewHistory.UserID),
  313. ReportId: int(userViewHistory.ResearchReportID),
  314. ReportChapterId: int(userViewHistory.ResearchReportTypeID),
  315. OutId: int(userViewHistory.ViewHistoryID),
  316. }
  317. if global.Redis != nil {
  318. dataStr, _ := json.Marshal(data)
  319. _, err := global.Redis.LPush(context.TODO(), utils.CACHE_KEY_USER_VIEW, dataStr).Result()
  320. if err != nil {
  321. fmt.Println("PushViewRecordNewRedisData LPush Err:" + err.Error())
  322. }
  323. return true
  324. }
  325. return false
  326. }