statistics.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package gamelogic
  2. import (
  3. "context"
  4. "fmt"
  5. "path/filepath"
  6. "time"
  7. excelize "github.com/xuri/excelize/v2"
  8. )
  9. var cacheBuffer [][]string
  10. var excelFile *excelize.File
  11. type Statistics struct {
  12. betAmount int
  13. winOrLossAmount int
  14. betArea []int
  15. waterpool int
  16. }
  17. type StatisticsList struct {
  18. lists []Statistics
  19. lastLists []Statistics
  20. tableId int
  21. }
  22. func (sl *StatisticsList) initData(tableId int) {
  23. sl.lists = []Statistics{}
  24. sl.lastLists = []Statistics{}
  25. sl.tableId = tableId
  26. go sl.updateValue()
  27. }
  28. func (sl *StatisticsList) updateValue() {
  29. for {
  30. c, cancel := context.WithTimeout(context.Background(), 60*time.Second)
  31. select {
  32. case <-c.Done():
  33. sl.lastLists = []Statistics{}
  34. sl.lastLists = append(sl.lastLists, sl.lists...)
  35. sl.lists = []Statistics{}
  36. sl.storeToExcel()
  37. sl.lastLists = []Statistics{}
  38. cancel()
  39. }
  40. }
  41. }
  42. func (sl *StatisticsList) storeToExcel() {
  43. if len(sl.lastLists) == 0 {
  44. return
  45. }
  46. for n := 0; n < len(sl.lastLists); n++ {
  47. str := []string{fmt.Sprintf("%d", sl.lastLists[n].betAmount),
  48. fmt.Sprintf("%d", sl.lastLists[n].winOrLossAmount),
  49. fmt.Sprintf("%v", sl.lastLists[n].betArea),
  50. fmt.Sprintf("%d", sl.lastLists[n].waterpool),
  51. }
  52. cacheBuffer = append(cacheBuffer, str)
  53. }
  54. sl.saveFile()
  55. }
  56. func (sl *StatisticsList) saveFile() {
  57. var err error
  58. sheetName := "Sheet1"
  59. dirName := "./"
  60. fileName := fmt.Sprintf("masharie_table%d.xlsx", sl.tableId)
  61. excelFile = excelize.NewFile()
  62. excelFile.NewSheet(sheetName)
  63. // 设置列宽
  64. excelFile.SetColWidth(sheetName, "A", "A", 20)
  65. excelFile.SetColWidth(sheetName, "B", "B", 20)
  66. excelFile.SetColWidth(sheetName, "C", "C", 20)
  67. excelFile.SetColWidth(sheetName, "D", "D", 20)
  68. style, _ := excelFile.NewStyle(&excelize.Style{
  69. Alignment: &excelize.Alignment{
  70. Horizontal: "center",
  71. Vertical: "center",
  72. },
  73. })
  74. excelFile.SetCellStyle(sheetName, "A1", "D1", style)
  75. // 设置工作表的单元格值
  76. excelFile.SetCellValue(sheetName, "A1", "下注金额")
  77. excelFile.SetCellValue(sheetName, "B1", "盈利金额")
  78. excelFile.SetCellValue(sheetName, "C1", "投注区域")
  79. excelFile.SetCellValue(sheetName, "D1", "当前水池")
  80. // 获取已有数据的行数
  81. rows, err := excelFile.GetRows(sheetName)
  82. if err != nil {
  83. return
  84. }
  85. rowIndex := len(rows)
  86. for _, row := range cacheBuffer {
  87. for colIndex, col := range row {
  88. cellName, _ := excelize.CoordinatesToCellName(colIndex+1, rowIndex+1)
  89. excelFile.SetCellValue(sheetName, cellName, col)
  90. excelFile.SetCellStyle(sheetName, cellName, cellName, style)
  91. }
  92. rowIndex++
  93. }
  94. excelFile.SaveAs(filepath.Join(dirName, fileName))
  95. }