package admin import ( "fmt" "strings" "time" "zhiyuan/models/match" "zhiyuan/pkg/app" "zhiyuan/pkg/db" "zhiyuan/pkg/utils" "github.com/gin-gonic/gin" "github.com/tealeg/xlsx/v3" ) type MatchCell struct { Value string Half bool } func isMatchTrue(value string) bool { value = strings.Trim(value, " ") if value == "是" || value == "✔" || value == "√" { return true } b := true m := false for _, s := range []rune(value) { if s == '是' { b = true m = true } else if s == '否' { b = false m = false } else if s == '✔' || s == '√' { return b } } return m } func MyMatch(c *gin.Context) { now := time.Now() offset := int(time.Monday - now.Weekday()) if offset > 0 { offset -= 7 } monday := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, offset) info, _ := db.GetOneModelMap(db.Type(match.MatchOrder{}), map[string]interface{}{ "adminId": c.GetInt("adminID"), "created_at >=": monday.Unix(), "order by": "`created_at` DESC", }, nil) app.Success(c, info) } func MatchImport(c *gin.Context) { file, err := c.FormFile("file") if err != nil { app.Error(c, fmt.Sprintf("get form err: %s", err.Error())) return } fileExt := utils.FileExt(file.Filename) attachKey := utils.RandomStr() + fileExt file.Filename = "uploads/" + attachKey if err := c.SaveUploadedFile(file, file.Filename); err != nil { app.Error(c, fmt.Sprintf("上传文件失败%s", err.Error())) return } wb, err := xlsx.OpenFile(file.Filename) if err != nil { app.Error(c, err.Error()) return } halfs := 0 rows := make([][]MatchCell, 0) for _, sheet := range wb.Sheet { err = sheet.ForEachRow(func(r *xlsx.Row) error { if r.GetCoordinate() < 4 || r.GetCell(0).HMerge > 0 { return nil } cells := make([]MatchCell, 0) for i := 0; i < 61; i++ { cells = append(cells, MatchCell{ Value: r.GetCell(i).String(), Half: false, }) } if cells[0].Value == "" && cells[1].Value == "" && cells[2].Value == "" && cells[3].Value == "" { return nil } for _, i := range []int{0, 1, 2, 3, 58, 59, 60} { if cells[i].Value == "" { cells[i].Half = true } } if cells[10].Value != "" || cells[11].Value != "" { for _, i := range []int{12, 14, 16, 18} { if !isMatchTrue(cells[i].Value) { if cells[i+1].Value == "" { cells[i+1].Half = true } } } if !isMatchTrue(cells[20].Value) { if cells[25].Value == "" { cells[25].Half = true } } } cause := false for i := 27; i < 37; i++ { if cells[i].Value != "" { cause = true } } if !cause && cells[37].Value == "" { cells[37].Half = true } visit := false for _, i := range []int{9, 10, 11} { if cells[i].Value != "" { visit = true } } if visit { for _, i := range []int{38, 39, 40} { if cells[i].Value == "" { cells[i].Half = true } } } for _, i := range []int{49, 51, 53, 55} { if isMatchTrue(cells[i].Value) { if cells[i+1].Value == "" { cells[i+1].Half = true } if i == 55 && cells[i+2].Value == "" { cells[i+2].Half = true } } } for _, c := range cells { if c.Half { halfs++ } } rows = append(rows, cells) return nil }) } data := map[string]interface{}{ "adminId": int64(c.GetInt("adminID")), "halfs": halfs, "content": utils.JsonEncode(rows), "path": file.Filename, } id, err := db.InsertModel(db.Type(match.MatchOrder{}), data) if err != nil { app.Error(c, err.Error()) return } app.Success(c, gin.H{"id": id}) }