report_view.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  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.GetRddpDayReportViewersDetail(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.GetRddpWeekReportViewersDetail(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. } else if v.TypeName == `双周报` {
  241. sheet, err := file.AddSheet(v.TypeName + "研报阅读统计")
  242. if err != nil {
  243. return err
  244. }
  245. //标头
  246. rowTitle := sheet.AddRow()
  247. cellA := rowTitle.AddCell()
  248. cellA.Value = "用户名称"
  249. cellB := rowTitle.AddCell()
  250. cellB.Value = "公司名称"
  251. cellC := rowTitle.AddCell()
  252. cellC.Value = "访问时间"
  253. cellD := rowTitle.AddCell()
  254. cellD.Value = "访问标题"
  255. cellE := rowTitle.AddCell()
  256. cellE.Value = "访问页面"
  257. cellF := rowTitle.AddCell()
  258. cellF.Value = "报告类型"
  259. items, err := models.GetResearchReportViewersDetail(startTime, endTime, v.TypeValue)
  260. if err != nil {
  261. return err
  262. }
  263. for _, item := range items {
  264. row := sheet.AddRow()
  265. cellA := row.AddCell()
  266. cellA.Value = item.RealName
  267. cellB := row.AddCell()
  268. cellB.Value = item.CompanyName
  269. cellC := row.AddCell()
  270. cellC.Value = item.CreatedTime
  271. cellD := row.AddCell()
  272. cellD.Value = item.ResearchReportName
  273. cellE := row.AddCell()
  274. cellE.Value = item.ReportVariety
  275. cellF := row.AddCell()
  276. cellF.Value = v.TypeName
  277. }
  278. //新双周报的数据
  279. weekItems, err := models.GetRddpTwoWeekReportViewersDetail(startTime, endTime)
  280. if err != nil {
  281. return err
  282. }
  283. for _, item := range weekItems {
  284. row := sheet.AddRow()
  285. cellA := row.AddCell()
  286. cellA.Value = item.RealName
  287. cellB := row.AddCell()
  288. cellB.Value = item.CompanyName
  289. cellC := row.AddCell()
  290. cellC.Value = item.CreatedTime
  291. cellD := row.AddCell()
  292. cellD.Value = `【弘则` + item.ClassifyNameSecond + `】` + item.ResearchReportName
  293. cellE := row.AddCell()
  294. cellE.Value = item.ClassifyNameSecond
  295. cellF := row.AddCell()
  296. cellF.Value = `大宗商品`
  297. }
  298. continue
  299. } else if v.TypeName == `月报` {
  300. sheet, err := file.AddSheet(v.TypeName + "研报阅读统计")
  301. if err != nil {
  302. return err
  303. }
  304. //标头
  305. rowTitle := sheet.AddRow()
  306. cellA := rowTitle.AddCell()
  307. cellA.Value = "用户名称"
  308. cellB := rowTitle.AddCell()
  309. cellB.Value = "公司名称"
  310. cellC := rowTitle.AddCell()
  311. cellC.Value = "访问时间"
  312. cellD := rowTitle.AddCell()
  313. cellD.Value = "访问标题"
  314. cellE := rowTitle.AddCell()
  315. cellE.Value = "访问页面"
  316. cellF := rowTitle.AddCell()
  317. cellF.Value = "报告类型"
  318. items, err := models.GetResearchReportViewersDetail(startTime, endTime, v.TypeValue)
  319. if err != nil {
  320. return err
  321. }
  322. for _, item := range items {
  323. row := sheet.AddRow()
  324. cellA := row.AddCell()
  325. cellA.Value = item.RealName
  326. cellB := row.AddCell()
  327. cellB.Value = item.CompanyName
  328. cellC := row.AddCell()
  329. cellC.Value = item.CreatedTime
  330. cellD := row.AddCell()
  331. cellD.Value = item.ResearchReportName
  332. cellE := row.AddCell()
  333. cellE.Value = item.ReportVariety
  334. cellF := row.AddCell()
  335. cellF.Value = v.TypeName
  336. }
  337. //新月报的数据
  338. monthItems, err := models.GetRddpMonthReportViewersDetail(startTime, endTime)
  339. if err != nil {
  340. return err
  341. }
  342. for _, item := range monthItems {
  343. row := sheet.AddRow()
  344. cellA := row.AddCell()
  345. cellA.Value = item.RealName
  346. cellB := row.AddCell()
  347. cellB.Value = item.CompanyName
  348. cellC := row.AddCell()
  349. cellC.Value = item.CreatedTime
  350. cellD := row.AddCell()
  351. cellD.Value = `【弘则` + item.ClassifyNameSecond + `】` + item.ResearchReportName
  352. cellE := row.AddCell()
  353. cellE.Value = item.ClassifyNameSecond
  354. cellF := row.AddCell()
  355. cellF.Value = `月报`
  356. }
  357. continue
  358. } else if v.TypeName == `数据点评` {
  359. sheet, err := file.AddSheet(v.TypeName + "研报阅读统计")
  360. if err != nil {
  361. return err
  362. }
  363. //标头
  364. rowTitle := sheet.AddRow()
  365. cellA := rowTitle.AddCell()
  366. cellA.Value = "用户名称"
  367. cellB := rowTitle.AddCell()
  368. cellB.Value = "公司名称"
  369. cellC := rowTitle.AddCell()
  370. cellC.Value = "访问时间"
  371. cellD := rowTitle.AddCell()
  372. cellD.Value = "访问标题"
  373. cellE := rowTitle.AddCell()
  374. cellE.Value = "访问页面"
  375. cellF := rowTitle.AddCell()
  376. cellF.Value = "报告类型"
  377. items, err := models.GetResearchReportViewersDetail(startTime, endTime, v.TypeValue)
  378. if err != nil {
  379. return err
  380. }
  381. for _, item := range items {
  382. row := sheet.AddRow()
  383. cellA := row.AddCell()
  384. cellA.Value = item.RealName
  385. cellB := row.AddCell()
  386. cellB.Value = item.CompanyName
  387. cellC := row.AddCell()
  388. cellC.Value = item.CreatedTime
  389. cellD := row.AddCell()
  390. cellD.Value = item.ResearchReportName
  391. cellE := row.AddCell()
  392. cellE.Value = item.ReportVariety
  393. cellF := row.AddCell()
  394. cellF.Value = v.TypeName
  395. }
  396. //新数据点评的数据
  397. monthItems, err := models.GetRddpDataReviewReportViewersDetail(startTime, endTime)
  398. if err != nil {
  399. return err
  400. }
  401. for _, item := range monthItems {
  402. row := sheet.AddRow()
  403. cellA := row.AddCell()
  404. cellA.Value = item.RealName
  405. cellB := row.AddCell()
  406. cellB.Value = item.CompanyName
  407. cellC := row.AddCell()
  408. cellC.Value = item.CreatedTime
  409. cellD := row.AddCell()
  410. cellD.Value = `【弘则` + item.ClassifyNameSecond + `】` + item.ResearchReportName
  411. cellE := row.AddCell()
  412. cellE.Value = item.ClassifyNameSecond
  413. cellF := row.AddCell()
  414. cellF.Value = item.ClassifyNameFirst
  415. }
  416. continue
  417. }
  418. if v.TypeValue == "rddp" {
  419. sheet, err := file.AddSheet(v.TypeName + "阅读统计")
  420. if err != nil {
  421. return err
  422. }
  423. //标头
  424. rowTitle := sheet.AddRow()
  425. cellA := rowTitle.AddCell()
  426. cellA.Value = "用户名称"
  427. cellB := rowTitle.AddCell()
  428. cellB.Value = "公司名称"
  429. cellC := rowTitle.AddCell()
  430. cellC.Value = "访问时间"
  431. cellD := rowTitle.AddCell()
  432. cellD.Value = "访问标题"
  433. cellF := rowTitle.AddCell()
  434. cellF.Value = "报告类型"
  435. items, err := models.GetRddpReportViewersDetail(startTime, endTime)
  436. if err != nil {
  437. return err
  438. }
  439. for _, item := range items {
  440. row := sheet.AddRow()
  441. cellA := row.AddCell()
  442. cellA.Value = item.RealName
  443. cellB := row.AddCell()
  444. cellB.Value = item.CompanyName
  445. cellC := row.AddCell()
  446. cellC.Value = item.CreatedTime
  447. cellD := row.AddCell()
  448. cellD.Value = item.ResearchReportName + "(" + item.ReportCreateDate + ")"
  449. cellE := row.AddCell()
  450. cellE.Value = v.TypeName
  451. }
  452. } else if v.TypeValue == "advisory" {
  453. sheet, err := file.AddSheet(v.TypeName + "研报阅读统计")
  454. if err != nil {
  455. return err
  456. }
  457. //标头
  458. rowTitle := sheet.AddRow()
  459. cellA := rowTitle.AddCell()
  460. cellA.Value = "用户名称"
  461. cellB := rowTitle.AddCell()
  462. cellB.Value = "公司名称"
  463. cellC := rowTitle.AddCell()
  464. cellC.Value = "访问时间"
  465. cellD := rowTitle.AddCell()
  466. cellD.Value = "访问标题"
  467. cellE := rowTitle.AddCell()
  468. cellE.Value = "报告类型"
  469. items, err := models.GetAdvisoryViewersDetail(startTime, endTime)
  470. if err != nil {
  471. return err
  472. }
  473. for _, item := range items {
  474. row := sheet.AddRow()
  475. cellA := row.AddCell()
  476. cellA.Value = item.RealName
  477. cellB := row.AddCell()
  478. cellB.Value = item.CompanyName
  479. cellC := row.AddCell()
  480. cellC.Value = item.CreatedTime
  481. cellD := row.AddCell()
  482. cellD.Value = item.ResearchReportName
  483. cellE := row.AddCell()
  484. cellE.Value = v.TypeName
  485. }
  486. } else {
  487. sheet, err := file.AddSheet(v.TypeName + "研报阅读统计")
  488. if err != nil {
  489. return err
  490. }
  491. //标头
  492. rowTitle := sheet.AddRow()
  493. cellA := rowTitle.AddCell()
  494. cellA.Value = "用户名称"
  495. cellB := rowTitle.AddCell()
  496. cellB.Value = "公司名称"
  497. cellC := rowTitle.AddCell()
  498. cellC.Value = "访问时间"
  499. cellD := rowTitle.AddCell()
  500. cellD.Value = "访问标题"
  501. cellE := rowTitle.AddCell()
  502. cellE.Value = "访问页面"
  503. cellF := rowTitle.AddCell()
  504. cellF.Value = "报告类型"
  505. items, err := models.GetResearchReportViewersDetail(startTime, endTime, v.TypeValue)
  506. if err != nil {
  507. return err
  508. }
  509. for _, item := range items {
  510. row := sheet.AddRow()
  511. cellA := row.AddCell()
  512. cellA.Value = item.RealName
  513. cellB := row.AddCell()
  514. cellB.Value = item.CompanyName
  515. cellC := row.AddCell()
  516. cellC.Value = item.CreatedTime
  517. cellD := row.AddCell()
  518. cellD.Value = item.ResearchReportName
  519. cellE := row.AddCell()
  520. cellE.Value = item.ReportVariety
  521. cellF := row.AddCell()
  522. cellF.Value = v.TypeName
  523. }
  524. }
  525. }
  526. title := "研报阅读统计报表"
  527. savePath := "report_view_detail" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  528. err = file.Save("./" + savePath)
  529. fmt.Println(err)
  530. if err != nil {
  531. return
  532. }
  533. //发送邮件
  534. fmt.Println("start send email")
  535. sendResult := utils.SendEmailByHongze(title, "你好,上周研报阅读统计见附件。", utils.EmailSendToHzUsers, savePath, title+".xlsx")
  536. //sendResult:=utils.SendEmailByHongze(title,"你好,上周研报阅读统计见附件。",utils.EmailSendToMe,savePath)
  537. if sendResult {
  538. os.Remove(savePath)
  539. }
  540. //fmt.Println("send result:", sendResult)
  541. //fmt.Println("end send email")
  542. return nil
  543. }