Browse Source

fix import

Dasflugen 5 years ago
parent
commit
d3dfb35a2d
2 changed files with 92 additions and 27 deletions
  1. 65 0
      api/studyPlanImport.go
  2. 27 27
      schedule/xlsx.go

+ 65 - 0
api/studyPlanImport.go

@@ -0,0 +1,65 @@
+package api
+
+import(
+	"net/http"
+	"encoding/json"
+	"os"
+	"mime/multipart"
+	"crypto/rand"
+	xls "../schedule"
+)
+
+func ImportPlanRoute(w http.ResponseWriter, r *http.Request){
+	input, _, err := r.FormFile("uploadfile")
+	if err != nil {
+		json.NewEncoder(w).Encode(struct{Error string}{Error: "Error read upload file"})
+		return
+	}
+	tempname := readFile(input)
+	result := xls.ImportXLSX(tempname)
+	json.NewEncoder(w).Encode(result)
+}
+
+func readFile(input multipart.File) string {
+	var (
+		size   = uint64(0)
+		buffer = make([]byte, 2 << 20) // 1MiB
+	)
+
+	tempname := GenerateRandomString(16) + ".xlsx"
+
+	output, err := os.OpenFile(
+		tempname,
+		os.O_WRONLY|os.O_CREATE,
+		0666,
+	)
+	if err != nil {
+		printError(err)
+		return "error"
+	}
+
+	for {
+		length, err := input.Read(buffer)
+		if err != nil {
+			printError(err)
+			break
+		}
+		size += uint64(length)
+		output.Write(buffer[:length])
+	}
+
+	input.Close()
+	output.Close()
+	return tempname
+}
+
+func GenerateRandomString(max int) string {
+    var slice []byte = make([]byte, max)
+    _, err := rand.Read(slice)
+    if err != nil {
+    	printError(err)
+        return ""
+    }
+
+    return string(slice)
+}

+ 27 - 27
schedule/xlsx.go

@@ -17,35 +17,35 @@ func ImportXLSX(filename string) []*PlanXLSX {
     }
     for _, sheet := range xlFile.Sheets {
         var plan = new(PlanXLSX)
-        plan.ProgramCode   = sheet.Row[5].Col[1].String()
-        plan.NameCode      = strings.Split(sheet.Row[5].Col[6].String(), " ")[0]
-        plan.PeriodOfStudy = sheet.Row[7].Col[6].String()
-        splited := strings.Split(sheet.Row[9].Col[1].String(), " ")
+        plan.ProgramCode   = sheet.Rows[5].Cells[1].String()
+        plan.NameCode      = strings.Split(sheet.Rows[5].Cells[6].String(), " ")[0]
+        plan.PeriodOfStudy = sheet.Rows[7].Cells[6].String()
+        splited := strings.Split(sheet.Rows[9].Cells[1].String(), " ")
         plan.GroupNumber   = strings.Split(splited[len(splited)-1], "-")[0]
 
-        for i, row := range sheet.Row {
+        for i, row := range sheet.Rows {
             if i < 20 {
                 continue
             }
-            if len(row.Col) < 20 {
+            if len(row.Cells) < 20 {
                 continue
             }
-            if row.Col[2].String() == "" || row.Col[2].String() == "ИТОГО" {
+            if row.Cells[2].String() == "" || row.Cells[2].String() == "ИТОГО" {
                 continue
             }
 
-            if strings.HasPrefix(row.Col[1].String(), "Консультации") || strings.HasPrefix(row.Col[1].String(), "Государственная") {
+            if strings.HasPrefix(row.Cells[1].String(), "Консультации") || strings.HasPrefix(row.Cells[1].String(), "Государственная") {
                 continue
             }
 
-            splited := strings.Split(row.Col[1].String(), ".")
+            splited := strings.Split(row.Cells[1].String(), ".")
             if len(splited) != 2 {
                 continue
             }
 
             typerow := TypeRow(IsSubTR)
             switch {
-            case strings.Contains(row.Col[2].String(), "цикл"):
+            case strings.Contains(row.Cells[2].String(), "цикл"):
                 typerow = IsCycleTR
             case splited[1] == "00":
                 typerow = IsStartTR
@@ -53,23 +53,23 @@ func ImportXLSX(filename string) []*PlanXLSX {
                 typerow = IsPmTR
             }
 
-            max, _       := row.Col[4].Int()
-            selfstudy, _ := row.Col[5].Int()
-            allstudy, _  := row.Col[6].Int()
-            lectures, _  := row.Col[7].Int()
-            labs, _      := row.Col[8].Int()
-            projects, _  := row.Col[9].Int()
-            c1s1, _      := row.Col[12].Int()
-            c1s2, _      := row.Col[13].Int()
-            c2s1, _      := row.Col[14].Int()
-            c2s2, _      := row.Col[15].Int()
-            c3s1, _      := row.Col[16].Int()
-            c3s2, _      := row.Col[17].Int()
-            c4s1, _      := row.Col[18].Int()
-            c4s2, _      := row.Col[19].Int()
+            max, _       := row.Cells[4].Int()
+            selfstudy, _ := row.Cells[5].Int()
+            allstudy, _  := row.Cells[6].Int()
+            lectures, _  := row.Cells[7].Int()
+            labs, _      := row.Cells[8].Int()
+            projects, _  := row.Cells[9].Int()
+            c1s1, _      := row.Cells[12].Int()
+            c1s2, _      := row.Cells[13].Int()
+            c2s1, _      := row.Cells[14].Int()
+            c2s2, _      := row.Cells[15].Int()
+            c3s1, _      := row.Cells[16].Int()
+            c3s2, _      := row.Cells[17].Int()
+            c4s1, _      := row.Cells[18].Int()
+            c4s2, _      := row.Cells[19].Int()
 
             certform := ""
-            splited = strings.Split(row.Col[3].String(), ",")
+            splited = strings.Split(row.Cells[3].String(), ",")
             for _, v := range splited {
                 if v != "-" {
                     certform = v
@@ -78,8 +78,8 @@ func ImportXLSX(filename string) []*PlanXLSX {
             }
 
             plan.Lines = append(plan.Lines, LineXLSX{
-                ID:        row.Col[1].String(),
-                Name:      row.Col[2].String(),
+                ID:        row.Cells[1].String(),
+                Name:      row.Cells[2].String(),
                 CertForm:  certform,
                 StudyLoad: StudyLoadXLSX{
                     Max:       max,