123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- package xlsx
- import (
- "database/sql"
- "errors"
- "fmt"
- "log"
- "mime/multipart"
- "strings"
- "xugu_license/internal/models"
- "xugu_license/internal/module/email"
- "github.com/xuri/excelize/v2"
- )
- func parseExcel(file *multipart.File) []models.LicenseApplication {
- f, err := excelize.OpenReader(*file)
- if err != nil {
- log.Fatalf("无法读取 Excel 文件: %v", err)
- }
- // 获取第一个工作表的名称
- sheetName := f.GetSheetName(f.GetActiveSheetIndex())
- // 获取所有单元格数据
- rows, err := f.GetRows(sheetName)
- if err != nil {
- log.Fatalf("无法获取行数据: %v", err)
- }
- var applications []models.LicenseApplication
- for _, row := range rows[1:] { // 跳过表头行
- if len(row) == 0 {
- continue // 跳过空行
- }
- app := models.LicenseApplication{
- Creator: getValue(row, 0),
- ApplicationDate: getValue(row, 1),
- AssociatedProject: getValue(row, 2),
- SalesPerson: getValue(row, 3),
- SalesEmail: getValue(row, 4),
- SupportPerson: getValue(row, 5),
- SupportEmail: getValue(row, 6),
- TotalNodes: getValue(row, 7),
- Company: getValue(row, 8),
- ProductName: getValue(row, 9),
- Version: getValue(row, 10),
- NodeCount: getValue(row, 11),
- Processor: getValue(row, 12),
- OperatingSystem: getValue(row, 13),
- MasterMacAddress: getValue(row, 14),
- SecondaryMasterMacAddress: sql.NullString{
- String: getValue(row, 15),
- Valid: getValue(row, 15) != "", // 如果字符串不为空,则认为它是有效的
- },
- }
- applications = append(applications, app)
- }
- // 处理 applications 切片
- for _, app := range applications {
- // 打印结构体的值
- fmt.Printf("Creator: %s\n", app.Creator)
- fmt.Printf("ApplicationDate: %s\n", app.ApplicationDate)
- fmt.Printf("AssociatedProject: %s\n", app.AssociatedProject)
- fmt.Printf("SalesPerson: %s\n", app.SalesPerson)
- fmt.Printf("SalesEmail: %s\n", app.SalesEmail)
- fmt.Printf("SupportPerson: %s\n", app.SupportPerson)
- fmt.Printf("SupportEmail: %s\n", app.SupportEmail)
- fmt.Printf("Company: %s\n", app.Company)
- fmt.Printf("TotalNodes: %s\n", app.TotalNodes)
- fmt.Printf("ProductName: %s\n", app.ProductName)
- fmt.Printf("Version: %s\n", app.Version)
- fmt.Printf("NodeCount: %s\n", app.NodeCount)
- fmt.Printf("Processor: %s\n", app.Processor)
- fmt.Printf("OperatingSystem: %s\n", app.OperatingSystem)
- fmt.Printf("MasterMacAddress: %s\n", app.MasterMacAddress)
- fmt.Printf("SecondaryMasterMacAddress: %s\n", app.SecondaryMasterMacAddress)
- }
- return applications
- }
- func getValue(row []string, index int) string {
- if index < len(row) && row[index] != "" {
- return row[index]
- }
- return ""
- }
- func XlsxController(ass interface{}) ([]models.LicenseApplication, error) {
- if ass != nil && ass.(*multipart.File) != nil {
- fmt.Println("xlsx")
- licenseProcess := parseExcel(ass.(*multipart.File))
- return licenseProcess, nil
- } else {
- return nil, errors.New("xlsx file not found")
- }
- }
- func ExcelToMail(lic models.LicenseApplication, licStr []byte, licStr2 []byte) {
- // insertQuery := `
- // INSERT INTO LicenseApplication (
- // UserId,UpUser,UpTime,LicenseFlage ,Creator, ApplicationDate,
- // AssociatedProject, SalesPerson, SalesEmail, SupportPerson, SupportEmail,
- // TotalNodes, ProductName, Version, NodeCount,Processor,
- // OperatingSystem, MasterMacAddress, SecondaryMasterMacAddress
- // ) VALUES (?, ?, ?, ?, ?,?,
- // ?, ?, ?, ?, ?,
- // ?, ?, ?,?,?,
- // ?,?,?)
- // `
- // fmt.Printf("lic 为 %#v", lic)
- // _, err := global.XuguDB.Exec(insertQuery,
- // "10", "gttes", time.Now(), "未生成", lic.Creator, lic.ApplicationDate, lic.AssociatedProject, lic.SalesPerson, lic.SalesEmail,
- // lic.TotalNodes, lic.SupportPerson, lic.SupportEmail,
- // lic.ProductName, lic.Version, lic.NodeCount, lic.Processor, lic.OperatingSystem, lic.MasterMacAddress, lic.SecondaryMasterMacAddress,
- // )
- // if err != nil {
- // fmt.Println("插入数据失败: %v", err)
- // }
- CheckEmailFormat([]*string{&lic.SalesEmail, &lic.SupportEmail})
- body := fmt.Sprintf(" 来自%s 使用%s %s,该项目销售:%s, 技术支持:%s", lic.AssociatedProject, lic.ProductName, lic.SalesEmail, lic.SalesPerson, lic.SupportPerson)
- email.NewEmail("gt@xugudb.com", "zI7cKadNHv7XedV5", []string{lic.SalesEmail, lic.SupportEmail}, "smtp.sparkspace.huaweicloud.com",
- "465", "来自license消息分发", body, licStr, licStr2)
- // email.SendEmail(em)
- }
- // CheckEmailFormat 检查邮箱格式
- func CheckEmailFormat(emails []*string) {
- for _, email := range emails {
- if !strings.Contains(*email, "@") {
- *email = ""
- } else {
- fmt.Println("邮箱格式正确")
- }
- }
- }
|