report_view.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. "strconv"
  9. "time"
  10. )
  11. //报告历史访问次数
  12. func ReportViewTimes() (err error) {
  13. defer func() {
  14. if err!=nil {
  15. fmt.Println("crete ReportViewTimes err:",err.Error())
  16. utils.FileLog.Info("crete ReportViewTimes err: %s",err.Error())
  17. }
  18. }()
  19. typeList:=make([]*models.ReportType,0)
  20. day:=new(models.ReportType)
  21. day.TypeName="晨报"
  22. day.TypeValue="day"
  23. typeList=append(typeList,day)
  24. week:=new(models.ReportType)
  25. week.TypeName="周报"
  26. week.TypeValue="week"
  27. typeList=append(typeList,week)
  28. two_week:=new(models.ReportType)
  29. two_week.TypeName="双周报"
  30. two_week.TypeValue="two_week"
  31. typeList=append(typeList,two_week)
  32. month:=new(models.ReportType)
  33. month.TypeName="月报"
  34. month.TypeValue="month"
  35. typeList=append(typeList,month)
  36. other:=new(models.ReportType)
  37. other.TypeName="数据点评"
  38. other.TypeValue="other"
  39. typeList=append(typeList,other)
  40. rddp:=new(models.ReportType)
  41. rddp.TypeName="日度点评"
  42. rddp.TypeValue="rddp"
  43. typeList=append(typeList,rddp)
  44. //创建excel
  45. file:=xlsx.NewFile()
  46. for _, v := range typeList {
  47. fmt.Println(v.TypeValue,v.TypeName)
  48. sheet,err:=file.AddSheet(v.TypeName+"阅读人次数")
  49. if err!=nil {
  50. return err
  51. }
  52. //标头
  53. rowTitle:=sheet.AddRow()
  54. cellA:=rowTitle.AddCell()
  55. cellA.Value="访问标题"
  56. cellB:=rowTitle.AddCell()
  57. cellB.Value="报告类型"
  58. cellC:=rowTitle.AddCell()
  59. cellC.Value="历史访问次数"
  60. cellD:=rowTitle.AddCell()
  61. cellD.Value="历史访问人数"
  62. fmt.Println(v.TypeName, v.TypeValue)
  63. if v.TypeValue == "rddp" {
  64. items, err := models.GetRddpReportViewers()
  65. if err != nil {
  66. return err
  67. }
  68. for _, item := range items {
  69. row := sheet.AddRow()
  70. cellA:=row.AddCell()
  71. cellA.Value=item.ResearchReportName+"("+item.CreateDate+")"
  72. cellB:=row.AddCell()
  73. cellB.Value=v.TypeName
  74. cellC:=row.AddCell()
  75. cellC.Value=strconv.Itoa(item.Count)
  76. cellD:=row.AddCell()
  77. cellD.Value=strconv.Itoa(item.UserCount)
  78. }
  79. } else {
  80. items, err := models.GetResearchReportViewers(v.TypeValue)
  81. if err != nil {
  82. return err
  83. }
  84. for _, item := range items {
  85. row := sheet.AddRow()
  86. cellA:=row.AddCell()
  87. cellA.Value=item.ResearchReportName
  88. cellB:=row.AddCell()
  89. cellB.Value=v.TypeName
  90. cellC:=row.AddCell()
  91. cellC.Value=strconv.Itoa(item.Count)
  92. cellD:=row.AddCell()
  93. cellD.Value=strconv.Itoa(item.UserCount)
  94. }
  95. }
  96. }
  97. savePath:="report_view_times"+time.Now().Format(utils.FormatDateTimeUnSpace)+".xlsx"
  98. err=file.Save("./"+savePath)
  99. if err!=nil {
  100. return
  101. }
  102. //发送邮件
  103. title:="用户访问次数',\"你好,上周用户访问次数见附件。"
  104. fmt.Println("start send email")
  105. sendResult:=utils.SendEmailByHongze(title,"",utils.EmailSendToHzUsers,savePath)
  106. if sendResult {
  107. os.Remove(savePath)
  108. }
  109. fmt.Println("send result:",sendResult)
  110. fmt.Println("end send email")
  111. return nil
  112. }
  113. //报告访问详情
  114. func ReportViewDetail() (err error) {
  115. defer func() {
  116. if err!=nil {
  117. fmt.Println("crete ReportViewDetail err:",err.Error())
  118. utils.FileLog.Info("crete ReportViewDetail err: %s",err.Error())
  119. }
  120. }()
  121. typeList:=make([]*models.ReportType,0)
  122. day:=new(models.ReportType)
  123. day.TypeName="晨报"
  124. day.TypeValue="day"
  125. typeList=append(typeList,day)
  126. week:=new(models.ReportType)
  127. week.TypeName="周报"
  128. week.TypeValue="week"
  129. typeList=append(typeList,week)
  130. two_week:=new(models.ReportType)
  131. two_week.TypeName="双周报"
  132. two_week.TypeValue="two_week"
  133. typeList=append(typeList,two_week)
  134. month:=new(models.ReportType)
  135. month.TypeName="月报"
  136. month.TypeValue="month"
  137. typeList=append(typeList,month)
  138. other:=new(models.ReportType)
  139. other.TypeName="数据点评"
  140. other.TypeValue="other"
  141. typeList=append(typeList,other)
  142. rddp:=new(models.ReportType)
  143. rddp.TypeName="日度点评"
  144. rddp.TypeValue="rddp"
  145. typeList=append(typeList,rddp)
  146. startTime:=time.Now().AddDate(0,0,-7).Format(utils.FormatDateTime)
  147. endTime:=time.Now().Format(utils.FormatDateTime)
  148. //创建excel
  149. file:=xlsx.NewFile()
  150. for _, v := range typeList {
  151. fmt.Println(v.TypeName, v.TypeValue)
  152. if v.TypeValue == "rddp" {
  153. sheet,err:=file.AddSheet(v.TypeName+"阅读统计")
  154. if err!=nil {
  155. return err
  156. }
  157. //标头
  158. rowTitle:=sheet.AddRow()
  159. cellA:=rowTitle.AddCell()
  160. cellA.Value="用户名称"
  161. cellB:=rowTitle.AddCell()
  162. cellB.Value="公司名称"
  163. cellC:=rowTitle.AddCell()
  164. cellC.Value="访问时间"
  165. cellD:=rowTitle.AddCell()
  166. cellD.Value="访问标题"
  167. cellF:=rowTitle.AddCell()
  168. cellF.Value="报告类型"
  169. items, err := models.GetRddpReportViewersDetail(startTime,endTime)
  170. if err != nil {
  171. return err
  172. }
  173. for _, item := range items {
  174. row := sheet.AddRow()
  175. cellA:=row.AddCell()
  176. cellA.Value=item.RealName
  177. cellB:=row.AddCell()
  178. cellB.Value=item.CompanyName
  179. cellC:=row.AddCell()
  180. cellC.Value=item.CreatedTime
  181. cellD:=row.AddCell()
  182. cellD.Value=item.ResearchReportName+"("+item.ReportCreateDate+")"
  183. cellE:=row.AddCell()
  184. cellE.Value=v.TypeName
  185. }
  186. } else {
  187. sheet,err:=file.AddSheet(v.TypeName+"研报阅读统计")
  188. if err!=nil {
  189. return err
  190. }
  191. //标头
  192. rowTitle:=sheet.AddRow()
  193. cellA:=rowTitle.AddCell()
  194. cellA.Value="用户名称"
  195. cellB:=rowTitle.AddCell()
  196. cellB.Value="公司名称"
  197. cellC:=rowTitle.AddCell()
  198. cellC.Value="访问时间"
  199. cellD:=rowTitle.AddCell()
  200. cellD.Value="访问标题"
  201. cellE:=rowTitle.AddCell()
  202. cellE.Value="访问页面"
  203. cellF:=rowTitle.AddCell()
  204. cellF.Value="报告类型"
  205. items, err := models.GetResearchReportViewersDetail(startTime,endTime,v.TypeValue)
  206. if err != nil {
  207. return err
  208. }
  209. for _, item := range items {
  210. row := sheet.AddRow()
  211. cellA:=row.AddCell()
  212. cellA.Value=item.RealName
  213. cellB:=row.AddCell()
  214. cellB.Value=item.CompanyName
  215. cellC:=row.AddCell()
  216. cellC.Value=item.CreatedTime
  217. cellD:=row.AddCell()
  218. cellD.Value=item.ResearchReportName
  219. cellE:=row.AddCell()
  220. cellE.Value=item.ReportVariety
  221. cellF:=row.AddCell()
  222. cellF.Value=v.TypeName
  223. }
  224. }
  225. }
  226. savePath:="report_viewer_details"+time.Now().Format(utils.FormatDateTimeUnSpace)+".xlsx"
  227. err=file.Save("./"+savePath)
  228. if err!=nil {
  229. return
  230. }
  231. //发送邮件
  232. title:="研报阅读统计报表"
  233. fmt.Println("start send email")
  234. sendResult:=utils.SendEmailByHongze(title,"你好,上周研报阅读统计见附件。",utils.EmailSendToHzUsers,savePath)
  235. //sendResult:=utils.SendEmailByHongze(title,"你好,上周研报阅读统计见附件。",utils.EmailSendToMe,savePath)
  236. if sendResult {
  237. os.Remove(savePath)
  238. }
  239. fmt.Println("send result:",sendResult)
  240. fmt.Println("end send email")
  241. return nil
  242. }