report_view.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. package services
  2. import (
  3. "fmt"
  4. "github.com/tealeg/xlsx"
  5. "hongze/hongze_task/models"
  6. "hongze/hongze_task/utils"
  7. "os"
  8. "time"
  9. )
  10. //报告历史访问次数
  11. func ReportViewTimes() (err error) {
  12. defer func() {
  13. if err != nil {
  14. fmt.Println("crete ReportViewTimes err:", err.Error())
  15. utils.FileLog.Info("crete ReportViewTimes err: %s", err.Error())
  16. }
  17. }()
  18. //创建excel
  19. file := xlsx.NewFile()
  20. sheet, err := file.AddSheet("用户访问次数")
  21. if err != nil {
  22. return err
  23. }
  24. //标头
  25. rowTitle := sheet.AddRow()
  26. cellA := rowTitle.AddCell()
  27. cellA.Value = "用户名称"
  28. cellB := rowTitle.AddCell()
  29. cellB.Value = "公司名称"
  30. cellC := rowTitle.AddCell()
  31. cellC.Value = "历史访问次数"
  32. cellD := rowTitle.AddCell()
  33. cellD.Value = "最近一次访问日期"
  34. rddpItems, err := models.GetRddpHistoryViewTimes()
  35. items, err := models.GetHistoryViewTimes()
  36. if err != nil {
  37. return err
  38. }
  39. for _, item := range items {
  40. row := sheet.AddRow()
  41. cellA := row.AddCell()
  42. cellA.Value = item.RealName
  43. cellB := row.AddCell()
  44. cellB.Value = item.CompanyName
  45. cellC := row.AddCell()
  46. cellC.SetInt(item.ViewCount)
  47. cellD := row.AddCell()
  48. cellD.Value = item.CreateTime
  49. }
  50. for _, item := range rddpItems {
  51. row := sheet.AddRow()
  52. cellA := row.AddCell()
  53. cellA.Value = item.RealName
  54. cellB := row.AddCell()
  55. cellB.Value = item.CompanyName
  56. cellC := row.AddCell()
  57. cellC.SetInt(item.ViewCount)
  58. cellD := row.AddCell()
  59. cellD.Value = item.CreateTime
  60. }
  61. title := "用户访问次数"
  62. savePath := "report_view_times" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  63. err = file.Save("./" + savePath)
  64. if err != nil {
  65. return
  66. }
  67. //发送邮件
  68. content := "你好,上周用户访问次数见附件。"
  69. fmt.Println("start send email")
  70. sendResult := utils.SendEmailByHongze(title, content, utils.EmailSendToHzUsers, savePath, title+".xlsx")
  71. //sendResult:=utils.SendEmailByHongze(title,content,utils.EmailSendToMe,savePath)
  72. if sendResult {
  73. os.Remove(savePath)
  74. }
  75. fmt.Println("send result:", sendResult)
  76. fmt.Println("end send email")
  77. return nil
  78. }
  79. //报告访问详情
  80. func ReportViewDetail() (err error) {
  81. defer func() {
  82. if err != nil {
  83. fmt.Println("crete ReportViewDetail err:", err.Error())
  84. utils.FileLog.Info("crete ReportViewDetail err: %s", err.Error())
  85. }
  86. }()
  87. typeList := make([]*models.ReportType, 0)
  88. day := new(models.ReportType)
  89. day.TypeName = "晨报"
  90. day.TypeValue = "day"
  91. typeList = append(typeList, day)
  92. advisory := new(models.ReportType)
  93. advisory.TypeName = "每日商品聚焦"
  94. advisory.TypeValue = "advisory"
  95. typeList = append(typeList, advisory)
  96. week := new(models.ReportType)
  97. week.TypeName = "周报"
  98. week.TypeValue = "week"
  99. typeList = append(typeList, week)
  100. two_week := new(models.ReportType)
  101. two_week.TypeName = "双周报"
  102. two_week.TypeValue = "two_week"
  103. typeList = append(typeList, two_week)
  104. month := new(models.ReportType)
  105. month.TypeName = "月报"
  106. month.TypeValue = "month"
  107. typeList = append(typeList, month)
  108. other := new(models.ReportType)
  109. other.TypeName = "数据点评"
  110. other.TypeValue = "other"
  111. typeList = append(typeList, other)
  112. rddp := new(models.ReportType)
  113. rddp.TypeName = "日度点评"
  114. rddp.TypeValue = "rddp"
  115. typeList = append(typeList, rddp)
  116. startTime := time.Now().AddDate(0, 0, -7).Format(utils.FormatDateTime)
  117. endTime := time.Now().Format(utils.FormatDateTime)
  118. //创建excel
  119. file := xlsx.NewFile()
  120. for _, v := range typeList {
  121. fmt.Println(v.TypeName, v.TypeValue)
  122. if v.TypeName == `周报` {
  123. sheet, err := file.AddSheet(v.TypeName + "研报阅读统计")
  124. if err != nil {
  125. return err
  126. }
  127. //标头
  128. rowTitle := sheet.AddRow()
  129. cellA := rowTitle.AddCell()
  130. cellA.Value = "用户名称"
  131. cellB := rowTitle.AddCell()
  132. cellB.Value = "公司名称"
  133. cellC := rowTitle.AddCell()
  134. cellC.Value = "访问时间"
  135. cellD := rowTitle.AddCell()
  136. cellD.Value = "访问标题"
  137. cellE := rowTitle.AddCell()
  138. cellE.Value = "访问页面"
  139. cellF := rowTitle.AddCell()
  140. cellF.Value = "报告类型"
  141. items, err := models.GetResearchReportViewersDetail(startTime, endTime, v.TypeValue)
  142. if err != nil {
  143. return err
  144. }
  145. for _, item := range items {
  146. row := sheet.AddRow()
  147. cellA := row.AddCell()
  148. cellA.Value = item.RealName
  149. cellB := row.AddCell()
  150. cellB.Value = item.CompanyName
  151. cellC := row.AddCell()
  152. cellC.Value = item.CreatedTime
  153. cellD := row.AddCell()
  154. cellD.Value = item.ResearchReportName
  155. cellE := row.AddCell()
  156. cellE.Value = item.ReportVariety
  157. cellF := row.AddCell()
  158. cellF.Value = v.TypeName
  159. }
  160. //新周报的数据
  161. weekItems, err := models.GetRddpWeekReportViewersDetail(startTime, endTime)
  162. if err != nil {
  163. return err
  164. }
  165. for _, item := range weekItems {
  166. row := sheet.AddRow()
  167. cellA := row.AddCell()
  168. cellA.Value = item.RealName
  169. cellB := row.AddCell()
  170. cellB.Value = item.CompanyName
  171. cellC := row.AddCell()
  172. cellC.Value = item.CreatedTime
  173. cellD := row.AddCell()
  174. cellD.Value = item.ResearchReportName + "(" + item.ReportCreateDate + ")"
  175. cellE := row.AddCell()
  176. cellE.Value = item.ReportVariety
  177. cellF := row.AddCell()
  178. cellF.Value = v.TypeName
  179. }
  180. continue
  181. }
  182. if v.TypeValue == "rddp" {
  183. sheet, err := file.AddSheet(v.TypeName + "阅读统计")
  184. if err != nil {
  185. return err
  186. }
  187. //标头
  188. rowTitle := sheet.AddRow()
  189. cellA := rowTitle.AddCell()
  190. cellA.Value = "用户名称"
  191. cellB := rowTitle.AddCell()
  192. cellB.Value = "公司名称"
  193. cellC := rowTitle.AddCell()
  194. cellC.Value = "访问时间"
  195. cellD := rowTitle.AddCell()
  196. cellD.Value = "访问标题"
  197. cellF := rowTitle.AddCell()
  198. cellF.Value = "报告类型"
  199. items, err := models.GetRddpReportViewersDetail(startTime, endTime)
  200. if err != nil {
  201. return err
  202. }
  203. for _, item := range items {
  204. row := sheet.AddRow()
  205. cellA := row.AddCell()
  206. cellA.Value = item.RealName
  207. cellB := row.AddCell()
  208. cellB.Value = item.CompanyName
  209. cellC := row.AddCell()
  210. cellC.Value = item.CreatedTime
  211. cellD := row.AddCell()
  212. cellD.Value = item.ResearchReportName + "(" + item.ReportCreateDate + ")"
  213. cellE := row.AddCell()
  214. cellE.Value = v.TypeName
  215. }
  216. } else if v.TypeValue == "advisory" {
  217. sheet, err := file.AddSheet(v.TypeName + "研报阅读统计")
  218. if err != nil {
  219. return err
  220. }
  221. //标头
  222. rowTitle := sheet.AddRow()
  223. cellA := rowTitle.AddCell()
  224. cellA.Value = "用户名称"
  225. cellB := rowTitle.AddCell()
  226. cellB.Value = "公司名称"
  227. cellC := rowTitle.AddCell()
  228. cellC.Value = "访问时间"
  229. cellD := rowTitle.AddCell()
  230. cellD.Value = "访问标题"
  231. cellE := rowTitle.AddCell()
  232. cellE.Value = "报告类型"
  233. items, err := models.GetAdvisoryViewersDetail(startTime, endTime)
  234. if err != nil {
  235. return err
  236. }
  237. for _, item := range items {
  238. row := sheet.AddRow()
  239. cellA := row.AddCell()
  240. cellA.Value = item.RealName
  241. cellB := row.AddCell()
  242. cellB.Value = item.CompanyName
  243. cellC := row.AddCell()
  244. cellC.Value = item.CreatedTime
  245. cellD := row.AddCell()
  246. cellD.Value = item.ResearchReportName
  247. cellE := row.AddCell()
  248. cellE.Value = v.TypeName
  249. }
  250. } else {
  251. sheet, err := file.AddSheet(v.TypeName + "研报阅读统计")
  252. if err != nil {
  253. return err
  254. }
  255. //标头
  256. rowTitle := sheet.AddRow()
  257. cellA := rowTitle.AddCell()
  258. cellA.Value = "用户名称"
  259. cellB := rowTitle.AddCell()
  260. cellB.Value = "公司名称"
  261. cellC := rowTitle.AddCell()
  262. cellC.Value = "访问时间"
  263. cellD := rowTitle.AddCell()
  264. cellD.Value = "访问标题"
  265. cellE := rowTitle.AddCell()
  266. cellE.Value = "访问页面"
  267. cellF := rowTitle.AddCell()
  268. cellF.Value = "报告类型"
  269. items, err := models.GetResearchReportViewersDetail(startTime, endTime, v.TypeValue)
  270. if err != nil {
  271. return err
  272. }
  273. for _, item := range items {
  274. row := sheet.AddRow()
  275. cellA := row.AddCell()
  276. cellA.Value = item.RealName
  277. cellB := row.AddCell()
  278. cellB.Value = item.CompanyName
  279. cellC := row.AddCell()
  280. cellC.Value = item.CreatedTime
  281. cellD := row.AddCell()
  282. cellD.Value = item.ResearchReportName
  283. cellE := row.AddCell()
  284. cellE.Value = item.ReportVariety
  285. cellF := row.AddCell()
  286. cellF.Value = v.TypeName
  287. }
  288. }
  289. }
  290. title := "研报阅读统计报表"
  291. savePath := "report_view_detail" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  292. err = file.Save("./" + savePath)
  293. fmt.Println(err)
  294. if err != nil {
  295. return
  296. }
  297. //发送邮件
  298. fmt.Println("start send email")
  299. sendResult := utils.SendEmailByHongze(title, "你好,上周研报阅读统计见附件。", utils.EmailSendToHzUsers, savePath, title+".xlsx")
  300. //sendResult:=utils.SendEmailByHongze(title,"你好,上周研报阅读统计见附件。",utils.EmailSendToMe,savePath)
  301. if sendResult {
  302. os.Remove(savePath)
  303. }
  304. //fmt.Println("send result:", sendResult)
  305. //fmt.Println("end send email")
  306. return nil
  307. }