research_report.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. ResearchReportTypeList []*company_report_permission.ResearchReportTypeList `json:"research_report_type_list"`
  22. HasMenu int `json:"has_menu"`
  23. ResearchReportTypeContentList []*research_report.ResearchReportTypeContent `description:"报告详情"`
  24. LikeNum int64 `description:"点赞总数" json:"like_num"`
  25. LikeEnabled int8 `description:"是否已点赞: 0-未点赞 1-已点赞" json:"like_enabled"`
  26. }
  27. // GetResearchReportInfo 获取报告详情
  28. func GetResearchReportInfo(researchReportId, userId uint64) (result ResearchReportInfo, hasPermission bool, err error) {
  29. //获取报告详情
  30. reportInfo, err := research_report.GetByResearchReportId(researchReportId)
  31. if err != nil {
  32. return
  33. }
  34. reportType := reportInfo.Type
  35. //这些个报告需要做权限校验
  36. if utils.InArray(reportInfo.Type, []string{"month", "two_week", "other"}) {
  37. list, tmpErr := company_report_permission.GetReportVarietyList(userId, reportType)
  38. if tmpErr != nil {
  39. err = tmpErr
  40. return
  41. }
  42. for _, v := range list {
  43. if reportInfo.ResearchReportID == v.ReportChapterTypeId {
  44. hasPermission = true
  45. break
  46. }
  47. }
  48. if !hasPermission {
  49. //permissionName, tmpErr := company_report_permission.GetPermissionNameByReportId(reportInfo.ResearchReportID, reportType)
  50. //if tmpErr != nil {
  51. // err = tmpErr
  52. // return
  53. //}
  54. return
  55. }
  56. } else {
  57. hasPermission = true
  58. }
  59. researchReportTypeList := make([]*company_report_permission.ResearchReportTypeList, 0)
  60. tmpResearchReportTypeList, err := company_report_permission.GetResearchReportType(reportInfo.ResearchReportID, userId, reportInfo.Type)
  61. if err != nil {
  62. return
  63. }
  64. reportDate := reportInfo.ResearchReportDate
  65. for _, v := range tmpResearchReportTypeList {
  66. if reportDate.Before(v.PauseStartTime) || reportDate.After(v.PauseEndTime) {
  67. researchReportTypeList = append(researchReportTypeList, v)
  68. }
  69. }
  70. // 联系人信息
  71. strInt64 := strconv.FormatUint(userId, 10)
  72. id, _ := strconv.Atoi(strInt64)
  73. wxUserInfo, err := wx_user.GetByUserId(id)
  74. if err != nil {
  75. fmt.Println("GetByUserId:", err.Error())
  76. return
  77. }
  78. companyInfo, tmpErr := company.GetByCompanyId(wxUserInfo.CompanyID)
  79. if tmpErr != nil {
  80. err = tmpErr
  81. if tmpErr == utils.ErrNoRow {
  82. err = errors.New("找不到该客户")
  83. return
  84. }
  85. return
  86. }
  87. //查询是否读过这篇报告,如果未读过则阅读人数+1
  88. _, err = user_view_history.GetReportByUserId(userId, reportInfo.ResearchReportID)
  89. if err != nil {
  90. err = reportInfo.UpdateViewers()
  91. if err != nil {
  92. fmt.Println("UpdateViewers err:", err.Error())
  93. }
  94. }
  95. //新增userViewHistory记录
  96. userViewHistory := &user_view_history.UserViewHistory{
  97. ViewHistoryID: 0,
  98. UserID: userId,
  99. Mobile: wxUserInfo.Mobile,
  100. Email: wxUserInfo.Email,
  101. RealName: wxUserInfo.RealName,
  102. CompanyName: companyInfo.CompanyName,
  103. ViewTitle: "",
  104. ViewPage: "",
  105. ReportChapterModule: "",
  106. CreatedTime: time.Now(),
  107. LastUpdatedTime: time.Now(),
  108. Type: "weekly_report",
  109. ResearchReportID: reportInfo.ResearchReportID,
  110. ResearchReportTypeID: 0,
  111. }
  112. err = userViewHistory.AddUserViewHistory()
  113. if err != nil {
  114. fmt.Println("AddUserViewHistory err", err.Error())
  115. }
  116. //添加阅读日志的数据加入到redis
  117. go PushViewRecordNewRedisData(userViewHistory, int(wxUserInfo.CompanyID))
  118. //查询点赞数
  119. likeNum,likeEnabled, _ := services.GetReportLikeByReportIdOldReportId(wxUserInfo.UserID, 0, 0, int(researchReportId),0)
  120. result = ResearchReportInfo{
  121. ResearchReportInfo: reportInfo,
  122. ResearchReportTypeList: researchReportTypeList,
  123. HasMenu: 1,
  124. LikeNum: likeNum,
  125. LikeEnabled: likeEnabled,
  126. }
  127. if len(researchReportTypeList) <= 0 {
  128. } else if len(researchReportTypeList) == 1 {
  129. //只有一个章节,即没有目录的时候,需要直接返回章节详情
  130. result.HasMenu = 0
  131. researchReportTypeContent, tmpErr := research_report.GetResearchReportTypeContentList(researchReportTypeList[0].ResearchReportTypeId)
  132. if tmpErr != nil {
  133. return
  134. }
  135. result.ResearchReportTypeContentList = researchReportTypeContent
  136. }
  137. return
  138. }
  139. type ResearchReportTypeContentInfo struct {
  140. ResearchReportTypeInfo *research_report_type.ResearchReportTypeInfo `json:"research_report_type_info"`
  141. Add int `json:"add"`
  142. ResearchReportTypeContentList []*research_report.ResearchReportTypeContent `description:"报告详情" json:"research_report_type_content_list"`
  143. LikeNum int64 `description:"点赞总数" json:"like_num"`
  144. LikeEnabled int8 `description:"是否已点赞: 0-未点赞 1-已点赞" json:"like_enabled"`
  145. }
  146. // GetResearchReportTypeContentInfo 获取报告章节详情
  147. func GetResearchReportTypeContentInfo(researchReportTypeId, userId uint64) (result ResearchReportTypeContentInfo, hasPermission bool, err error) {
  148. //获取章节详情
  149. researchReportTypeContentList, err := research_report.GetResearchReportTypeContentList(researchReportTypeId)
  150. if err != nil {
  151. return
  152. }
  153. researchReportTypeInfo, err := research_report_type.GetResearchReportTypeInfo(researchReportTypeId)
  154. //获取报告详情
  155. reportInfo, err := research_report.GetByResearchReportId(researchReportTypeInfo.ResearchReportID)
  156. if err != nil {
  157. return
  158. }
  159. reportType := reportInfo.Type
  160. //这些个报告需要做权限校验
  161. if utils.InArray(reportInfo.Type, []string{"week", "month", "two_week", "other"}) {
  162. list, tmpErr := company_report_permission.GetReportVarietyList(userId, reportType)
  163. if tmpErr != nil {
  164. err = tmpErr
  165. return
  166. }
  167. if reportInfo.Type == "week" {
  168. //周报校验章节是否在权限内
  169. for _, v := range list {
  170. if researchReportTypeInfo.ReportChapterTypeId == v.ReportChapterTypeId {
  171. hasPermission = true
  172. break
  173. }
  174. }
  175. } else {
  176. //双周报和月报校验 类型是否在权限内
  177. for _, v := range list {
  178. if reportInfo.ResearchReportID == v.ReportChapterTypeId {
  179. hasPermission = true
  180. break
  181. }
  182. }
  183. }
  184. if !hasPermission {
  185. //permissionName, tmpErr := company_report_permission.GetPermissionNameByReportId(reportInfo.ResearchReportID, reportType)
  186. //if tmpErr != nil {
  187. // err = tmpErr
  188. // return
  189. //}
  190. return
  191. }
  192. } else {
  193. hasPermission = true
  194. }
  195. add := 1
  196. if len(researchReportTypeContentList) > 0 {
  197. add = 0
  198. }
  199. // 联系人信息
  200. strInt64 := strconv.FormatUint(userId, 10)
  201. id, _ := strconv.Atoi(strInt64)
  202. wxUserInfo, err := wx_user.GetByUserId(id)
  203. if err != nil {
  204. fmt.Println("GetByUserId:", err.Error())
  205. return
  206. }
  207. companyInfo, tmpErr := company.GetByCompanyId(wxUserInfo.CompanyID)
  208. if tmpErr != nil {
  209. err = tmpErr
  210. if tmpErr == utils.ErrNoRow {
  211. err = errors.New("找不到该客户")
  212. return
  213. }
  214. return
  215. }
  216. //新增userViewHistory记录
  217. userViewHistory := &user_view_history.UserViewHistory{
  218. ViewHistoryID: 0,
  219. UserID: userId,
  220. Mobile: wxUserInfo.Mobile,
  221. Email: wxUserInfo.Email,
  222. RealName: wxUserInfo.RealName,
  223. CompanyName: companyInfo.CompanyName,
  224. ViewTitle: "",
  225. ViewPage: "",
  226. ReportChapterModule: "",
  227. CreatedTime: time.Now(),
  228. LastUpdatedTime: time.Now(),
  229. Type: "weekly_report",
  230. ResearchReportID: reportInfo.ResearchReportID,
  231. ResearchReportTypeID: researchReportTypeId,
  232. }
  233. err = userViewHistory.AddUserViewHistory()
  234. if err != nil {
  235. fmt.Println("AddUserViewHistory err", err.Error())
  236. }
  237. //添加阅读日志的数据加入到redis
  238. go PushViewRecordNewRedisData(userViewHistory, int(wxUserInfo.CompanyID))
  239. //查询点赞数
  240. likeNum,likeEnabled, _ := services.GetReportLikeByReportIdOldReportId(wxUserInfo.UserID, 0, 0, int(researchReportTypeInfo.ResearchReportID), int(researchReportTypeInfo.ResearchReportTypeID))
  241. result = ResearchReportTypeContentInfo{
  242. ResearchReportTypeContentList: researchReportTypeContentList,
  243. ResearchReportTypeInfo: researchReportTypeInfo,
  244. Add: add,
  245. LikeNum: likeNum,
  246. LikeEnabled: likeEnabled,
  247. }
  248. return
  249. }
  250. // UserViewRedisData 阅读数据
  251. type UserViewRedisData struct {
  252. Mobile string `json:"mobile"`
  253. Email string `json:"email"`
  254. RealName string `json:"real_name"`
  255. CompanyName string `json:"company_name"`
  256. ViewTime string `json:"view_time" description:"阅读时间,格式:2022-02-17 13:06:13"`
  257. ProductId int `json:"product_id" description:"报告所属产品,ficc:1,权益:2"`
  258. CompanyId int `json:"company_id" description:"客户id"`
  259. }
  260. // PushViewRecordNewRedisData 阅读数据加入到redis
  261. func PushViewRecordNewRedisData(userViewHistory *user_view_history.UserViewHistory, companyId int) bool {
  262. data := &UserViewRedisData{
  263. Mobile: userViewHistory.Mobile,
  264. Email: userViewHistory.Email,
  265. RealName: userViewHistory.RealName,
  266. CompanyName: userViewHistory.CompanyName,
  267. ViewTime: userViewHistory.CreatedTime.Format(utils.FormatDateTime),
  268. ProductId: 1,
  269. CompanyId: companyId,
  270. }
  271. if global.Redis != nil {
  272. dataStr, _ := json.Marshal(data)
  273. _, err := global.Redis.LPush(context.TODO(), utils.CACHE_KEY_USER_VIEW, dataStr).Result()
  274. if err != nil {
  275. fmt.Println("PushViewRecordNewRedisData LPush Err:" + err.Error())
  276. }
  277. return true
  278. }
  279. return false
  280. }