report_view.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. } else if v.TypeName == `双周报` {
  182. sheet, err := file.AddSheet(v.TypeName + "研报阅读统计")
  183. if err != nil {
  184. return err
  185. }
  186. //标头
  187. rowTitle := sheet.AddRow()
  188. cellA := rowTitle.AddCell()
  189. cellA.Value = "用户名称"
  190. cellB := rowTitle.AddCell()
  191. cellB.Value = "公司名称"
  192. cellC := rowTitle.AddCell()
  193. cellC.Value = "访问时间"
  194. cellD := rowTitle.AddCell()
  195. cellD.Value = "访问标题"
  196. cellE := rowTitle.AddCell()
  197. cellE.Value = "访问页面"
  198. cellF := rowTitle.AddCell()
  199. cellF.Value = "报告类型"
  200. items, err := models.GetResearchReportViewersDetail(startTime, endTime, v.TypeValue)
  201. if err != nil {
  202. return err
  203. }
  204. for _, item := range items {
  205. row := sheet.AddRow()
  206. cellA := row.AddCell()
  207. cellA.Value = item.RealName
  208. cellB := row.AddCell()
  209. cellB.Value = item.CompanyName
  210. cellC := row.AddCell()
  211. cellC.Value = item.CreatedTime
  212. cellD := row.AddCell()
  213. cellD.Value = item.ResearchReportName
  214. cellE := row.AddCell()
  215. cellE.Value = item.ReportVariety
  216. cellF := row.AddCell()
  217. cellF.Value = v.TypeName
  218. }
  219. //新双周报的数据
  220. weekItems, err := models.GetRddpTwoWeekReportViewersDetail(startTime, endTime)
  221. if err != nil {
  222. return err
  223. }
  224. for _, item := range weekItems {
  225. row := sheet.AddRow()
  226. cellA := row.AddCell()
  227. cellA.Value = item.RealName
  228. cellB := row.AddCell()
  229. cellB.Value = item.CompanyName
  230. cellC := row.AddCell()
  231. cellC.Value = item.CreatedTime
  232. cellD := row.AddCell()
  233. cellD.Value = item.ResearchReportName + "(" + item.ReportCreateDate + ")"
  234. cellE := row.AddCell()
  235. cellE.Value = item.ReportVariety
  236. cellF := row.AddCell()
  237. cellF.Value = v.TypeName
  238. }
  239. continue
  240. }
  241. if v.TypeValue == "rddp" {
  242. sheet, err := file.AddSheet(v.TypeName + "阅读统计")
  243. if err != nil {
  244. return err
  245. }
  246. //标头
  247. rowTitle := sheet.AddRow()
  248. cellA := rowTitle.AddCell()
  249. cellA.Value = "用户名称"
  250. cellB := rowTitle.AddCell()
  251. cellB.Value = "公司名称"
  252. cellC := rowTitle.AddCell()
  253. cellC.Value = "访问时间"
  254. cellD := rowTitle.AddCell()
  255. cellD.Value = "访问标题"
  256. cellF := rowTitle.AddCell()
  257. cellF.Value = "报告类型"
  258. items, err := models.GetRddpReportViewersDetail(startTime, endTime)
  259. if err != nil {
  260. return err
  261. }
  262. for _, item := range items {
  263. row := sheet.AddRow()
  264. cellA := row.AddCell()
  265. cellA.Value = item.RealName
  266. cellB := row.AddCell()
  267. cellB.Value = item.CompanyName
  268. cellC := row.AddCell()
  269. cellC.Value = item.CreatedTime
  270. cellD := row.AddCell()
  271. cellD.Value = item.ResearchReportName + "(" + item.ReportCreateDate + ")"
  272. cellE := row.AddCell()
  273. cellE.Value = v.TypeName
  274. }
  275. } else if v.TypeValue == "advisory" {
  276. sheet, err := file.AddSheet(v.TypeName + "研报阅读统计")
  277. if err != nil {
  278. return err
  279. }
  280. //标头
  281. rowTitle := sheet.AddRow()
  282. cellA := rowTitle.AddCell()
  283. cellA.Value = "用户名称"
  284. cellB := rowTitle.AddCell()
  285. cellB.Value = "公司名称"
  286. cellC := rowTitle.AddCell()
  287. cellC.Value = "访问时间"
  288. cellD := rowTitle.AddCell()
  289. cellD.Value = "访问标题"
  290. cellE := rowTitle.AddCell()
  291. cellE.Value = "报告类型"
  292. items, err := models.GetAdvisoryViewersDetail(startTime, endTime)
  293. if err != nil {
  294. return err
  295. }
  296. for _, item := range items {
  297. row := sheet.AddRow()
  298. cellA := row.AddCell()
  299. cellA.Value = item.RealName
  300. cellB := row.AddCell()
  301. cellB.Value = item.CompanyName
  302. cellC := row.AddCell()
  303. cellC.Value = item.CreatedTime
  304. cellD := row.AddCell()
  305. cellD.Value = item.ResearchReportName
  306. cellE := row.AddCell()
  307. cellE.Value = v.TypeName
  308. }
  309. } else {
  310. sheet, err := file.AddSheet(v.TypeName + "研报阅读统计")
  311. if err != nil {
  312. return err
  313. }
  314. //标头
  315. rowTitle := sheet.AddRow()
  316. cellA := rowTitle.AddCell()
  317. cellA.Value = "用户名称"
  318. cellB := rowTitle.AddCell()
  319. cellB.Value = "公司名称"
  320. cellC := rowTitle.AddCell()
  321. cellC.Value = "访问时间"
  322. cellD := rowTitle.AddCell()
  323. cellD.Value = "访问标题"
  324. cellE := rowTitle.AddCell()
  325. cellE.Value = "访问页面"
  326. cellF := rowTitle.AddCell()
  327. cellF.Value = "报告类型"
  328. items, err := models.GetResearchReportViewersDetail(startTime, endTime, v.TypeValue)
  329. if err != nil {
  330. return err
  331. }
  332. for _, item := range items {
  333. row := sheet.AddRow()
  334. cellA := row.AddCell()
  335. cellA.Value = item.RealName
  336. cellB := row.AddCell()
  337. cellB.Value = item.CompanyName
  338. cellC := row.AddCell()
  339. cellC.Value = item.CreatedTime
  340. cellD := row.AddCell()
  341. cellD.Value = item.ResearchReportName
  342. cellE := row.AddCell()
  343. cellE.Value = item.ReportVariety
  344. cellF := row.AddCell()
  345. cellF.Value = v.TypeName
  346. }
  347. }
  348. }
  349. title := "研报阅读统计报表"
  350. savePath := "report_view_detail" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  351. err = file.Save("./" + savePath)
  352. fmt.Println(err)
  353. if err != nil {
  354. return
  355. }
  356. //发送邮件
  357. fmt.Println("start send email")
  358. sendResult := utils.SendEmailByHongze(title, "你好,上周研报阅读统计见附件。", utils.EmailSendToHzUsers, savePath, title+".xlsx")
  359. //sendResult:=utils.SendEmailByHongze(title,"你好,上周研报阅读统计见附件。",utils.EmailSendToMe,savePath)
  360. if sendResult {
  361. os.Remove(savePath)
  362. }
  363. //fmt.Println("send result:", sendResult)
  364. //fmt.Println("end send email")
  365. return nil
  366. }