meeting_probabilities.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package services
  2. import (
  3. "context"
  4. "encoding/json"
  5. "eta/eta_crawler/models"
  6. "eta/eta_crawler/services/alarm_msg"
  7. "eta/eta_crawler/utils"
  8. "fmt"
  9. "github.com/xuri/excelize/v2"
  10. "time"
  11. )
  12. func RefreshMeetingProbabilities(cont context.Context) (err error) {
  13. defer func() {
  14. if err != nil {
  15. fmt.Println("RefreshMeetingProbabilities Err:" + err.Error())
  16. msg := "失败提醒" + "RefreshMeetingProbabilities ErrMsg:" + err.Error()
  17. go alarm_msg.SendAlarmMsg(msg, 3)
  18. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "RefreshDataFromCoalJsm ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  19. }
  20. }()
  21. templatePath := "./static/%s.xlsx"
  22. //templatePath := "/Users/xi/Desktop/%s.xlsx"
  23. templatePath = fmt.Sprintf(templatePath, time.Now().Format(utils.FormatDate))
  24. fmt.Println(templatePath)
  25. f, tmpErr := excelize.OpenFile(templatePath)
  26. if tmpErr != nil {
  27. fmt.Println("打开文件失败,Err:" + tmpErr.Error())
  28. err = tmpErr
  29. return
  30. }
  31. defer func() {
  32. // 关闭工作簿
  33. if err := f.Close(); err != nil {
  34. fmt.Println(err)
  35. }
  36. }()
  37. // 获取工作表中指定单元格的值
  38. cell, err := f.GetCellValue("MEETING PROBABILITIES", "B5")
  39. if err != nil {
  40. fmt.Println("GetCellValue err:" +err.Error())
  41. return
  42. }
  43. fmt.Println(cell)
  44. // 获取 Sheet1 上所有单元格
  45. rows, err := f.GetRows("MEETING PROBABILITIES")
  46. if err != nil {
  47. fmt.Println("GetRows err:" +err.Error())
  48. fmt.Println(err)
  49. return
  50. }
  51. minCowLength := 60
  52. minRowlength := 84
  53. realCowLength := minCowLength
  54. realRowLength := minRowlength
  55. data := make([][]interface{}, 0)
  56. var null []struct{}
  57. cellData := make([]models.ISheetCellData, 0)
  58. var tmpCt models.ISheetDataCt
  59. tmpCt.Fa = "General"
  60. tmpCt.T = "g"
  61. for r, row := range rows {
  62. lenCow := len(row)
  63. var dataRow []interface{}
  64. for c, colCell := range row {
  65. if colCell == "" {
  66. dataRow = append(dataRow, null)
  67. } else {
  68. var tmp models.ISheetData
  69. tmp.M = colCell
  70. tmp.Ct = tmpCt
  71. tmp.V = colCell
  72. dataRow = append(dataRow, tmp)
  73. var tmpS models.ISheetCellData
  74. tmpS.R = r
  75. tmpS.C = c
  76. tmpS.V = tmp
  77. cellData = append(cellData, tmpS)
  78. }
  79. fmt.Print(colCell, "\t")
  80. }
  81. if lenCow < minCowLength {
  82. lastCow := minCowLength - lenCow
  83. for i := 0; i < lastCow; i++ {
  84. dataRow = append(dataRow, null)
  85. }
  86. }
  87. realCowLength = len(dataRow)
  88. data = append(data, dataRow)
  89. }
  90. lenRow := len(rows)
  91. //补充剩余的单元格
  92. if lenRow < minRowlength {
  93. lastRow := minRowlength - lenRow
  94. for i := 0; i < lastRow; i++ {
  95. var dataRow []interface{}
  96. for j := 0; j < minCowLength; j++ {
  97. dataRow = append(dataRow, null)
  98. }
  99. data = append(data, dataRow)
  100. }
  101. }
  102. realRowLength = len(data)
  103. //visibleDataRow
  104. var visibleDataRow []int
  105. tmpVdr := 0
  106. for i := 1; i <= realRowLength; i++ {
  107. tmpVdr = i * 20
  108. visibleDataRow = append(visibleDataRow, tmpVdr)
  109. }
  110. var visibleDataColumn []int
  111. tmpVdr = 0
  112. for i := 1; i <= realCowLength; i++ {
  113. tmpVdr = i * 74
  114. visibleDataColumn = append(visibleDataColumn, tmpVdr)
  115. }
  116. sheet := new(models.ISheet)
  117. sheet.ScrollTop = 0
  118. sheet.ScrollLeft = 0
  119. sheet.Index = "Sheet_pekSifat0Kci_1694244624851"
  120. sheet.Status = 1
  121. sheet.JfGirdSelectSave = []struct{}{}
  122. sheet.LuckySheetSelectSave = []struct{}{}
  123. sheet.Data = data
  124. sheet.Config = make(map[string]struct{}, 0)
  125. sheet.VisibleDataRow = visibleDataRow
  126. sheet.VisibleDataColumn = visibleDataColumn
  127. sheet.ChWidth = 4560
  128. sheet.RhHeight = 1760
  129. sheet.LuckySheetSelectionRange = make([]int, 0)
  130. sheet.ZoomRatio = 1
  131. sheet.CellData = cellData
  132. var content []byte
  133. content, err = json.Marshal(sheet)
  134. if err != nil {
  135. return
  136. }
  137. var req models.AddExcelInfoReq
  138. excelInfo := &models.MeetingProbabilities{
  139. //MeetingInfoId: 0,
  140. Content: string(content),
  141. ExcelImage: req.ExcelImage,
  142. DateTime: time.Now().Format(utils.FormatDate),
  143. IsDelete: 0,
  144. ModifyTime: time.Now(),
  145. CreateTime: time.Now(),
  146. }
  147. err = models.AddMeetingProbabilities(excelInfo)
  148. return
  149. }