report_view.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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="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="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. //创建excel
  148. file:=xlsx.NewFile()
  149. for _, v := range typeList {
  150. fmt.Println(v.TypeName, v.TypeValue)
  151. if v.TypeValue == "rddp" {
  152. sheet,err:=file.AddSheet(v.TypeName+"阅读统计")
  153. if err!=nil {
  154. return err
  155. }
  156. //标头
  157. rowTitle:=sheet.AddRow()
  158. cellA:=rowTitle.AddCell()
  159. cellA.Value="用户名称"
  160. cellB:=rowTitle.AddCell()
  161. cellB.Value="公司名称"
  162. cellC:=rowTitle.AddCell()
  163. cellC.Value="访问时间"
  164. cellD:=rowTitle.AddCell()
  165. cellD.Value="访问标题"
  166. cellF:=rowTitle.AddCell()
  167. cellF.Value="报告类型"
  168. items, err := models.GetRddpReportViewersDetail(startTime)
  169. if err != nil {
  170. return err
  171. }
  172. for _, item := range items {
  173. row := sheet.AddRow()
  174. cellA:=row.AddCell()
  175. cellA.Value=item.RealName
  176. cellB:=row.AddCell()
  177. cellB.Value=item.CompanyName
  178. cellC:=row.AddCell()
  179. cellC.Value=item.CreatedTime
  180. cellD:=row.AddCell()
  181. cellD.Value=item.ResearchReportName+"("+item.ReportCreateDate+")"
  182. cellE:=row.AddCell()
  183. cellE.Value=v.TypeName
  184. }
  185. } else {
  186. sheet,err:=file.AddSheet(v.TypeName+"研报阅读统计")
  187. if err!=nil {
  188. return err
  189. }
  190. //标头
  191. rowTitle:=sheet.AddRow()
  192. cellA:=rowTitle.AddCell()
  193. cellA.Value="用户名称"
  194. cellB:=rowTitle.AddCell()
  195. cellB.Value="公司名称"
  196. cellC:=rowTitle.AddCell()
  197. cellC.Value="访问时间"
  198. cellD:=rowTitle.AddCell()
  199. cellD.Value="访问标题"
  200. cellE:=rowTitle.AddCell()
  201. cellE.Value="访问页面"
  202. cellF:=rowTitle.AddCell()
  203. cellF.Value="报告类型"
  204. items, err := models.GetResearchReportViewersDetail(startTime,v.TypeValue)
  205. if err != nil {
  206. return err
  207. }
  208. for _, item := range items {
  209. row := sheet.AddRow()
  210. cellA:=row.AddCell()
  211. cellA.Value=item.RealName
  212. cellB:=row.AddCell()
  213. cellB.Value=item.CompanyName
  214. cellC:=row.AddCell()
  215. cellC.Value=item.CreatedTime
  216. cellD:=row.AddCell()
  217. cellD.Value=item.ResearchReportName
  218. cellE:=row.AddCell()
  219. cellE.Value=item.ReportVariety
  220. cellF:=row.AddCell()
  221. cellF.Value=v.TypeName
  222. }
  223. }
  224. }
  225. savePath:="report_viewer_details"+time.Now().Format(utils.FormatDateTimeUnSpace)+".xlsx"
  226. err=file.Save("./"+savePath)
  227. if err!=nil {
  228. return
  229. }
  230. //发送邮件
  231. title:="研报阅读统计报表"
  232. fmt.Println("start send email")
  233. sendResult:=utils.SendEmailByHongze(title,"你好,上周研报阅读统计见附件。",utils.EmailSendToHzUsers,savePath)
  234. if sendResult {
  235. os.Remove(savePath)
  236. }
  237. fmt.Println("send result:",sendResult)
  238. fmt.Println("end send email")
  239. return nil
  240. }