research_report.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. package report
  2. import (
  3. "errors"
  4. "fmt"
  5. "hongze/hongze_yb/models/tables/company"
  6. "hongze/hongze_yb/models/tables/company_report_permission"
  7. "hongze/hongze_yb/models/tables/research_report"
  8. "hongze/hongze_yb/models/tables/research_report_type"
  9. "hongze/hongze_yb/models/tables/user_view_history"
  10. "hongze/hongze_yb/models/tables/wx_user"
  11. "hongze/hongze_yb/utils"
  12. "strconv"
  13. "time"
  14. )
  15. type ResearchReportInfo struct {
  16. ResearchReportInfo *research_report.ResearchReport `json:"research_report_info"`
  17. ResearchReportTypeList []*company_report_permission.ResearchReportTypeList `json:"research_report_type_list"`
  18. HasMenu int `json:"has_menu"`
  19. ResearchReportTypeContentList []*research_report.ResearchReportTypeContent `description:"报告详情"`
  20. }
  21. // GetResearchReportInfo 获取报告详情
  22. func GetResearchReportInfo(researchReportId, userId uint64) (result ResearchReportInfo, hasPermission bool, err error) {
  23. //获取报告详情
  24. reportInfo, err := research_report.GetByResearchReportId(researchReportId)
  25. if err != nil {
  26. return
  27. }
  28. reportType := reportInfo.Type
  29. //这些个报告需要做权限校验
  30. if utils.InArray(reportInfo.Type, []string{"month", "two_week", "other"}) {
  31. list, tmpErr := company_report_permission.GetReportVarietyList(userId, reportType)
  32. if tmpErr != nil {
  33. err = tmpErr
  34. return
  35. }
  36. for _, v := range list {
  37. if reportInfo.ResearchReportID == v.ReportChapterTypeId {
  38. hasPermission = true
  39. break
  40. }
  41. }
  42. if !hasPermission {
  43. //permissionName, tmpErr := company_report_permission.GetPermissionNameByReportId(reportInfo.ResearchReportID, reportType)
  44. //if tmpErr != nil {
  45. // err = tmpErr
  46. // return
  47. //}
  48. return
  49. }
  50. } else {
  51. hasPermission = true
  52. }
  53. researchReportTypeList := make([]*company_report_permission.ResearchReportTypeList, 0)
  54. tmpResearchReportTypeList, err := company_report_permission.GetResearchReportType(reportInfo.ResearchReportID, userId, reportInfo.Type)
  55. if err != nil {
  56. return
  57. }
  58. reportDate := reportInfo.ResearchReportDate
  59. for _, v := range tmpResearchReportTypeList {
  60. if reportDate.Before(v.PauseStartTime) || reportDate.After(v.PauseEndTime) {
  61. researchReportTypeList = append(researchReportTypeList, v)
  62. }
  63. }
  64. // 联系人信息
  65. strInt64 := strconv.FormatUint(userId, 10)
  66. id, _ := strconv.Atoi(strInt64)
  67. wxUserInfo, err := wx_user.GetByUserId(id)
  68. if err != nil {
  69. fmt.Println("GetByUserId:", err.Error())
  70. return
  71. }
  72. companyInfo, tmpErr := company.GetByCompanyId(wxUserInfo.CompanyID)
  73. if tmpErr != nil {
  74. err = tmpErr
  75. if tmpErr == utils.ErrNoRow {
  76. err = errors.New("找不到该客户")
  77. return
  78. }
  79. return
  80. }
  81. //查询是否读过这篇报告,如果未读过则阅读人数+1
  82. _, err = user_view_history.GetReportByUserId(userId, reportInfo.ResearchReportID)
  83. if err != nil {
  84. err = reportInfo.UpdateViewers()
  85. if err != nil {
  86. fmt.Println("UpdateViewers err:", err.Error())
  87. }
  88. }
  89. //新增userViewHistory记录
  90. userViewHistory := &user_view_history.UserViewHistory{
  91. ViewHistoryID: 0,
  92. UserID: userId,
  93. Mobile: wxUserInfo.Mobile,
  94. Email: wxUserInfo.Email,
  95. RealName: wxUserInfo.RealName,
  96. CompanyName: companyInfo.CompanyName,
  97. ViewTitle: "",
  98. ViewPage: "",
  99. ReportChapterModule: "",
  100. CreatedTime: time.Now(),
  101. LastUpdatedTime: time.Now(),
  102. Type: "weekly_report",
  103. ResearchReportID: reportInfo.ResearchReportID,
  104. ResearchReportTypeID: 0,
  105. }
  106. err = userViewHistory.AddUserViewHistory()
  107. if err != nil {
  108. fmt.Println("AddUserViewHistory err", err.Error())
  109. }
  110. result = ResearchReportInfo{
  111. ResearchReportInfo: reportInfo,
  112. ResearchReportTypeList: researchReportTypeList,
  113. HasMenu: 1,
  114. }
  115. if len(researchReportTypeList) <= 0 {
  116. } else if len(researchReportTypeList) == 1 {
  117. //只有一个章节,即没有目录的时候,需要直接返回章节详情
  118. result.HasMenu = 0
  119. researchReportTypeContent, tmpErr := research_report.GetResearchReportTypeContentList(researchReportTypeList[0].ResearchReportTypeId)
  120. if tmpErr != nil {
  121. return
  122. }
  123. result.ResearchReportTypeContentList = researchReportTypeContent
  124. }
  125. return
  126. }
  127. type ResearchReportTypeContentInfo struct {
  128. ResearchReportTypeInfo *research_report_type.ResearchReportTypeInfo `json:"research_report_type_info"`
  129. Add int `json:"add"`
  130. ResearchReportTypeContentList []*research_report.ResearchReportTypeContent `description:"报告详情" json:"research_report_type_content_list"`
  131. }
  132. // GetResearchReportTypeContentInfo 获取报告章节详情
  133. func GetResearchReportTypeContentInfo(researchReportTypeId, userId uint64) (result ResearchReportTypeContentInfo, hasPermission bool, err error) {
  134. //获取章节详情
  135. researchReportTypeContentList, err := research_report.GetResearchReportTypeContentList(researchReportTypeId)
  136. if err != nil {
  137. return
  138. }
  139. researchReportTypeInfo, err := research_report_type.GetResearchReportTypeInfo(researchReportTypeId)
  140. //获取报告详情
  141. reportInfo, err := research_report.GetByResearchReportId(researchReportTypeInfo.ResearchReportID)
  142. if err != nil {
  143. return
  144. }
  145. reportType := reportInfo.Type
  146. //这些个报告需要做权限校验
  147. if utils.InArray(reportInfo.Type, []string{"week", "month", "two_week", "other"}) {
  148. list, tmpErr := company_report_permission.GetReportVarietyList(userId, reportType)
  149. if tmpErr != nil {
  150. err = tmpErr
  151. return
  152. }
  153. if reportInfo.Type == "week" {
  154. //周报校验章节是否在权限内
  155. for _, v := range list {
  156. if researchReportTypeInfo.ReportChapterTypeId == v.ReportChapterTypeId {
  157. hasPermission = true
  158. break
  159. }
  160. }
  161. } else {
  162. //双周报和月报校验 类型是否在权限内
  163. for _, v := range list {
  164. if reportInfo.ResearchReportID == v.ReportChapterTypeId {
  165. hasPermission = true
  166. break
  167. }
  168. }
  169. }
  170. if !hasPermission {
  171. //permissionName, tmpErr := company_report_permission.GetPermissionNameByReportId(reportInfo.ResearchReportID, reportType)
  172. //if tmpErr != nil {
  173. // err = tmpErr
  174. // return
  175. //}
  176. return
  177. }
  178. } else {
  179. hasPermission = true
  180. }
  181. add := 1
  182. if len(researchReportTypeContentList) > 0 {
  183. add = 0
  184. }
  185. // 联系人信息
  186. strInt64 := strconv.FormatUint(userId, 10)
  187. id, _ := strconv.Atoi(strInt64)
  188. wxUserInfo, err := wx_user.GetByUserId(id)
  189. if err != nil {
  190. fmt.Println("GetByUserId:", err.Error())
  191. return
  192. }
  193. companyInfo, tmpErr := company.GetByCompanyId(wxUserInfo.CompanyID)
  194. if tmpErr != nil {
  195. err = tmpErr
  196. if tmpErr == utils.ErrNoRow {
  197. err = errors.New("找不到该客户")
  198. return
  199. }
  200. return
  201. }
  202. //查询是否读过这篇章节,如果未读过则阅读人数+1
  203. _, err = user_view_history.GetReportTypeByUserId(userId, researchReportTypeId)
  204. if err != nil {
  205. err = reportInfo.UpdateViewers()
  206. if err != nil {
  207. fmt.Println("UpdateViewers err:", err.Error())
  208. }
  209. }
  210. //新增userViewHistory记录
  211. userViewHistory := &user_view_history.UserViewHistory{
  212. ViewHistoryID: 0,
  213. UserID: userId,
  214. Mobile: wxUserInfo.Mobile,
  215. Email: wxUserInfo.Email,
  216. RealName: wxUserInfo.RealName,
  217. CompanyName: companyInfo.CompanyName,
  218. ViewTitle: "",
  219. ViewPage: "",
  220. ReportChapterModule: "",
  221. CreatedTime: time.Now(),
  222. LastUpdatedTime: time.Now(),
  223. Type: "weekly_report",
  224. ResearchReportID: reportInfo.ResearchReportID,
  225. ResearchReportTypeID: researchReportTypeId,
  226. }
  227. err = userViewHistory.AddUserViewHistory()
  228. if err != nil {
  229. fmt.Println("AddUserViewHistory err", err.Error())
  230. }
  231. result = ResearchReportTypeContentInfo{
  232. ResearchReportTypeContentList: researchReportTypeContentList,
  233. ResearchReportTypeInfo: researchReportTypeInfo,
  234. Add: add,
  235. }
  236. return
  237. }