report_view.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. typeList := make([]*models.ReportType, 0)
  93. advisory := new(models.ReportType)
  94. advisory.TypeName = "商品晨报"
  95. advisory.TypeValue = "advisory"
  96. typeList = append(typeList, advisory)
  97. week := new(models.ReportType)
  98. week.TypeName = "周报"
  99. week.TypeValue = "week"
  100. typeList = append(typeList, week)
  101. two_week := new(models.ReportType)
  102. two_week.TypeName = "双周报"
  103. two_week.TypeValue = "two_week"
  104. typeList = append(typeList, two_week)
  105. month := new(models.ReportType)
  106. month.TypeName = "月报"
  107. month.TypeValue = "month"
  108. typeList = append(typeList, month)
  109. other := new(models.ReportType)
  110. other.TypeName = "数据点评"
  111. other.TypeValue = "other"
  112. typeList = append(typeList, other)
  113. rddp := new(models.ReportType)
  114. rddp.TypeName = "日度点评"
  115. rddp.TypeValue = "rddp"
  116. typeList = append(typeList, rddp)
  117. startTime := time.Now().AddDate(0, 0, -7).Format(utils.FormatDateTime)
  118. endTime := time.Now().Format(utils.FormatDateTime)
  119. //创建excel
  120. file := xlsx.NewFile()
  121. for _, v := range typeList {
  122. fmt.Println(v.TypeName, v.TypeValue)
  123. if v.TypeValue == "rddp" {
  124. sheet, err := file.AddSheet(v.TypeName + "阅读统计")
  125. if err != nil {
  126. return err
  127. }
  128. //标头
  129. rowTitle := sheet.AddRow()
  130. cellA := rowTitle.AddCell()
  131. cellA.Value = "用户名称"
  132. cellB := rowTitle.AddCell()
  133. cellB.Value = "公司名称"
  134. cellC := rowTitle.AddCell()
  135. cellC.Value = "访问时间"
  136. cellD := rowTitle.AddCell()
  137. cellD.Value = "访问标题"
  138. cellF := rowTitle.AddCell()
  139. cellF.Value = "报告类型"
  140. items, err := models.GetRddpReportViewersDetail(startTime, endTime)
  141. if err != nil {
  142. return err
  143. }
  144. for _, item := range items {
  145. row := sheet.AddRow()
  146. cellA := row.AddCell()
  147. cellA.Value = item.RealName
  148. cellB := row.AddCell()
  149. cellB.Value = item.CompanyName
  150. cellC := row.AddCell()
  151. cellC.Value = item.CreatedTime
  152. cellD := row.AddCell()
  153. cellD.Value = item.ResearchReportName + "(" + item.ReportCreateDate + ")"
  154. cellE := row.AddCell()
  155. cellE.Value = v.TypeName
  156. }
  157. } else if v.TypeValue == "advisory" {
  158. sheet, err := file.AddSheet(v.TypeName + "研报阅读统计")
  159. if err != nil {
  160. return err
  161. }
  162. //标头
  163. rowTitle := sheet.AddRow()
  164. cellA := rowTitle.AddCell()
  165. cellA.Value = "用户名称"
  166. cellB := rowTitle.AddCell()
  167. cellB.Value = "公司名称"
  168. cellC := rowTitle.AddCell()
  169. cellC.Value = "访问时间"
  170. cellD := rowTitle.AddCell()
  171. cellD.Value = "访问标题"
  172. cellE := rowTitle.AddCell()
  173. cellE.Value = "报告类型"
  174. items, err := models.GetAdvisoryViewersDetail(startTime, endTime)
  175. if err != nil {
  176. return err
  177. }
  178. for _, item := range items {
  179. row := sheet.AddRow()
  180. cellA := row.AddCell()
  181. cellA.Value = item.RealName
  182. cellB := row.AddCell()
  183. cellB.Value = item.CompanyName
  184. cellC := row.AddCell()
  185. cellC.Value = item.CreatedTime
  186. cellD := row.AddCell()
  187. cellD.Value = item.ResearchReportName
  188. cellE := row.AddCell()
  189. cellE.Value = v.TypeName
  190. }
  191. } else {
  192. sheet, err := file.AddSheet(v.TypeName + "研报阅读统计")
  193. if err != nil {
  194. return err
  195. }
  196. //标头
  197. rowTitle := sheet.AddRow()
  198. cellA := rowTitle.AddCell()
  199. cellA.Value = "用户名称"
  200. cellB := rowTitle.AddCell()
  201. cellB.Value = "公司名称"
  202. cellC := rowTitle.AddCell()
  203. cellC.Value = "访问时间"
  204. cellD := rowTitle.AddCell()
  205. cellD.Value = "访问标题"
  206. cellE := rowTitle.AddCell()
  207. cellE.Value = "访问页面"
  208. cellF := rowTitle.AddCell()
  209. cellF.Value = "报告类型"
  210. items, err := models.GetResearchReportViewersDetail(startTime, endTime, v.TypeValue)
  211. if err != nil {
  212. return err
  213. }
  214. for _, item := range items {
  215. row := sheet.AddRow()
  216. cellA := row.AddCell()
  217. cellA.Value = item.RealName
  218. cellB := row.AddCell()
  219. cellB.Value = item.CompanyName
  220. cellC := row.AddCell()
  221. cellC.Value = item.CreatedTime
  222. cellD := row.AddCell()
  223. cellD.Value = item.ResearchReportName
  224. cellE := row.AddCell()
  225. cellE.Value = item.ReportVariety
  226. cellF := row.AddCell()
  227. cellF.Value = v.TypeName
  228. }
  229. }
  230. }
  231. title := "研报阅读统计报表"
  232. savePath := "report_view_detail"+ time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  233. err = file.Save("./" + savePath)
  234. fmt.Println(err)
  235. if err != nil {
  236. return
  237. }
  238. //发送邮件
  239. fmt.Println("start send email")
  240. sendResult := utils.SendEmailByHongze(title, "你好,上周研报阅读统计见附件。", utils.EmailSendToHzUsers, savePath,title+".xlsx")
  241. //sendResult:=utils.SendEmailByHongze(title,"你好,上周研报阅读统计见附件。",utils.EmailSendToMe,savePath)
  242. if sendResult {
  243. os.Remove(savePath)
  244. }
  245. //fmt.Println("send result:", sendResult)
  246. //fmt.Println("end send email")
  247. return nil
  248. }