parse.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package xlsx
  2. import (
  3. "database/sql"
  4. "errors"
  5. "fmt"
  6. "log"
  7. "mime/multipart"
  8. "strings"
  9. "xugu_license/internal/models"
  10. "xugu_license/internal/module/email"
  11. "github.com/xuri/excelize/v2"
  12. )
  13. func parseExcel(file *multipart.File) []models.LicenseApplication {
  14. f, err := excelize.OpenReader(*file)
  15. if err != nil {
  16. log.Fatalf("无法读取 Excel 文件: %v", err)
  17. }
  18. // 获取第一个工作表的名称
  19. sheetName := f.GetSheetName(f.GetActiveSheetIndex())
  20. // 获取所有单元格数据
  21. rows, err := f.GetRows(sheetName)
  22. if err != nil {
  23. log.Fatalf("无法获取行数据: %v", err)
  24. }
  25. var applications []models.LicenseApplication
  26. for _, row := range rows[1:] { // 跳过表头行
  27. if len(row) == 0 {
  28. continue // 跳过空行
  29. }
  30. app := models.LicenseApplication{
  31. Creator: getValue(row, 0),
  32. ApplicationDate: getValue(row, 1),
  33. AssociatedProject: getValue(row, 2),
  34. SalesPerson: getValue(row, 3),
  35. SalesEmail: getValue(row, 4),
  36. SupportPerson: getValue(row, 5),
  37. SupportEmail: getValue(row, 6),
  38. TotalNodes: getValue(row, 7),
  39. Company: getValue(row, 8),
  40. ProductName: getValue(row, 9),
  41. Version: getValue(row, 10),
  42. NodeCount: getValue(row, 11),
  43. Processor: getValue(row, 12),
  44. OperatingSystem: getValue(row, 13),
  45. MasterMacAddress: getValue(row, 14),
  46. SecondaryMasterMacAddress: sql.NullString{
  47. String: getValue(row, 15),
  48. Valid: getValue(row, 15) != "", // 如果字符串不为空,则认为它是有效的
  49. },
  50. }
  51. applications = append(applications, app)
  52. }
  53. // 处理 applications 切片
  54. for _, app := range applications {
  55. // 打印结构体的值
  56. fmt.Printf("Creator: %s\n", app.Creator)
  57. fmt.Printf("ApplicationDate: %s\n", app.ApplicationDate)
  58. fmt.Printf("AssociatedProject: %s\n", app.AssociatedProject)
  59. fmt.Printf("SalesPerson: %s\n", app.SalesPerson)
  60. fmt.Printf("SalesEmail: %s\n", app.SalesEmail)
  61. fmt.Printf("SupportPerson: %s\n", app.SupportPerson)
  62. fmt.Printf("SupportEmail: %s\n", app.SupportEmail)
  63. fmt.Printf("Company: %s\n", app.Company)
  64. fmt.Printf("TotalNodes: %s\n", app.TotalNodes)
  65. fmt.Printf("ProductName: %s\n", app.ProductName)
  66. fmt.Printf("Version: %s\n", app.Version)
  67. fmt.Printf("NodeCount: %s\n", app.NodeCount)
  68. fmt.Printf("Processor: %s\n", app.Processor)
  69. fmt.Printf("OperatingSystem: %s\n", app.OperatingSystem)
  70. fmt.Printf("MasterMacAddress: %s\n", app.MasterMacAddress)
  71. fmt.Printf("SecondaryMasterMacAddress: %s\n", app.SecondaryMasterMacAddress)
  72. }
  73. return applications
  74. }
  75. func getValue(row []string, index int) string {
  76. if index < len(row) && row[index] != "" {
  77. return row[index]
  78. }
  79. return ""
  80. }
  81. func XlsxController(ass interface{}) ([]models.LicenseApplication, error) {
  82. if ass != nil && ass.(*multipart.File) != nil {
  83. fmt.Println("xlsx")
  84. licenseProcess := parseExcel(ass.(*multipart.File))
  85. return licenseProcess, nil
  86. } else {
  87. return nil, errors.New("xlsx file not found")
  88. }
  89. }
  90. func ExcelToMail(lic models.LicenseApplication, licStr []byte, licStr2 []byte) {
  91. // insertQuery := `
  92. // INSERT INTO LicenseApplication (
  93. // UserId,UpUser,UpTime,LicenseFlage ,Creator, ApplicationDate,
  94. // AssociatedProject, SalesPerson, SalesEmail, SupportPerson, SupportEmail,
  95. // TotalNodes, ProductName, Version, NodeCount,Processor,
  96. // OperatingSystem, MasterMacAddress, SecondaryMasterMacAddress
  97. // ) VALUES (?, ?, ?, ?, ?,?,
  98. // ?, ?, ?, ?, ?,
  99. // ?, ?, ?,?,?,
  100. // ?,?,?)
  101. // `
  102. // fmt.Printf("lic 为 %#v", lic)
  103. // _, err := global.XuguDB.Exec(insertQuery,
  104. // "10", "gttes", time.Now(), "未生成", lic.Creator, lic.ApplicationDate, lic.AssociatedProject, lic.SalesPerson, lic.SalesEmail,
  105. // lic.TotalNodes, lic.SupportPerson, lic.SupportEmail,
  106. // lic.ProductName, lic.Version, lic.NodeCount, lic.Processor, lic.OperatingSystem, lic.MasterMacAddress, lic.SecondaryMasterMacAddress,
  107. // )
  108. // if err != nil {
  109. // fmt.Println("插入数据失败: %v", err)
  110. // }
  111. CheckEmailFormat([]*string{&lic.SalesEmail, &lic.SupportEmail})
  112. body := fmt.Sprintf(" 来自%s 使用%s %s,该项目销售:%s, 技术支持:%s", lic.AssociatedProject, lic.ProductName, lic.SalesEmail, lic.SalesPerson, lic.SupportPerson)
  113. email.NewEmail("gt@xugudb.com", "zI7cKadNHv7XedV5", []string{lic.SalesEmail, lic.SupportEmail}, "smtp.sparkspace.huaweicloud.com",
  114. "465", "来自license消息分发", body, licStr, licStr2)
  115. // email.SendEmail(em)
  116. }
  117. // CheckEmailFormat 检查邮箱格式
  118. func CheckEmailFormat(emails []*string) {
  119. for _, email := range emails {
  120. if !strings.Contains(*email, "@") {
  121. *email = ""
  122. } else {
  123. fmt.Println("邮箱格式正确")
  124. }
  125. }
  126. }