package test_data

import (
	"github.com/gin-gonic/gin"
	"os"
	"path/filepath"
	"strings"
	"testData/model"
	"testData/model/response"
	test_data "testData/repository/test.data"
	"time"
)

type UploadService struct {
}

func InitUpload() *UploadService {
	return &UploadService{}
}

// @Tags 数据分析平台
// @Summary 上传没录入文件
// @Security ApiKeyAuth
// @accept multipart/form-data
// @Param file formData file true "file"
// @Param value formData file true "product_name"
// @Param value formData file true "lot"
// @Param value formData file true "sub_batch"
// -// @Param data body request.SelectionLimit true "参数极限"
// @Produce application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"操作成功"}"
// @Router /testData/upload [post]
func (D *UploadService) UploadAnswerFile(c *gin.Context) {
	form, err := c.MultipartForm()
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	files := form.File["file"]
	if len(form.Value["product_name"]) < 1 {
		response.FailWithMessage("缺少产品名称", c)
		return
	}
	productName := form.Value["product_name"][0]
	if len(form.Value["lot"]) < 1 {
		response.FailWithMessage("缺少批次", c)
		return
	}
	lot := form.Value["lot"][0]
	if len(form.Value["sub_batch"]) < 1 {
		response.FailWithMessage("缺少子批次", c)
		return
	}
	subBatch := form.Value["sub_batch"][0]
	if len(form.Value["step"]) < 1 {
		response.FailWithMessage("缺少工序", c)
		return
	}
	step := form.Value["step"][0]
	if len(form.Value["machine"]) < 1 {
		response.FailWithMessage("缺少机台类型", c)
		return
	}
	machine := form.Value["machine"][0]
	if (lot == "" && subBatch == "") || productName == "" || step == "" || machine == "" {
		response.FailWithMessage("信息不全", c)
		return
	}
	fileTexts := make([]*model.FileText, 0)
	for _, file := range files {
		suffix := file.Filename[strings.LastIndex(file.Filename, "."):]
		fileName := time.Now().Format("20060102150405") + suffix
		os.MkdirAll("testData/siyuanUpload", os.ModePerm)
		filePath := filepath.Join("testData/siyuanUpload", fileName)
		if err = c.SaveUploadedFile(file, filePath); err != nil {
			response.FailWithMessage(err.Error(), c)
			return
		}
		fileTexts = append(fileTexts, &model.FileText{
			Name:        fileName,
			Path:        filePath,
			Factory:     "",
			ProductName: productName,
			Lot:         lot,
			SubBatch:    subBatch,
		})
	}
	//for _, file := range files {
	//	os.MkdirAll("testData/siyuanUpload", os.ModePerm)
	//	filePath := filepath.Join("testData/siyuanUpload", file.Filename)
	//	if err = c.SaveUploadedFile(file, filePath); err != nil {
	//		response.FailWithMessage(err.Error(), c)
	//		return
	//	}
	//	archive, err := zip.OpenReader(filePath)
	//	if err != nil {
	//		response.FailWithMessage(err.Error(), c)
	//		return
	//	}
	//	for _, archiveFile := range archive.File {
	//		archiveFilePath := filepath.Join("testData/siyuanUpload", archiveFile.Name)
	//		dstFile, err := os.OpenFile(archiveFilePath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, archiveFile.Mode())
	//		if err != nil {
	//			log.Println("创建文件" + archiveFilePath + "失败")
	//		}
	//		inArchiveFile, err := archiveFile.Open()
	//		if err != nil {
	//			log.Println("打开压缩包内文件" + archiveFile.Name + "失败")
	//		}
	//		if _, err := io.Copy(dstFile, inArchiveFile); err != nil {
	//			log.Println("拷贝压缩包文件至" + archiveFilePath + "失败")
	//		}
	//		dstFile.Close()
	//		inArchiveFile.Close()
	//		fileTexts = append(fileTexts, &model.FileText{
	//			Name:        archiveFile.Name,
	//			Path:        archiveFilePath,
	//			Factory:     "",
	//			ProductName: productName,
	//			Lot:         lot,
	//			SubBatch:    subBatch,
	//		})
	//	}
	//	archive.Close()
	//}

	test_data.HandleUploadFiles(fileTexts, step, machine)
	response.Ok(c)
}
