// Copyright (c) Shenyang Leading Edge Intelligent Technology Co., Ltd. All rights reserved.

package common

import (
	"LAPP_LF_MOM_BACKEND/utils"
	"LAPP_LF_MOM_BACKEND/web/middleware/jwts"
	"LAPP_LF_MOM_BACKEND/web/models"
	"LAPP_LF_MOM_BACKEND/web/supports"
	"fmt"
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/core/router"
	"strings"
)

/******************************************************************************
 *
 * @Function Name            : RegisterUploadMustPic
 *-----------------------------------------------------------------------------
 *
 * @Description              : 为一个通过主键处理多条RegisterUploadMustPic的方法注册路由
 *
 * @Function Parameters      : 路由分组
 *
 * @Function Parameters      : HTTP方法
 *
 * @Function Parameters      : 路径
 *
 * @Function Parameters      : 实际处理请求的方法
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-18 22:49:12
 *
 ******************************************************************************/
func RegisterUploadMustPic(party router.Party, httpMethod string, path string, method func(*models.Usertab) error) {

	party.Handle(httpMethod, path, func(ctx iris.Context) {
		user, ok := jwts.ParseToken(ctx)
		if !ok {
			supports.Error(ctx, iris.StatusBadRequest, supports.ParseParamsFailur, nil)
			return
		}

		var err error = nil
		//获取通过iris.WithPostMaxMemory获取的最大上传值大小。
		maxSize := ctx.Application().ConfigurationReadOnly().GetPostMaxMemory()
		err = ctx.Request().ParseMultipartForm(maxSize)
		if err != nil {
			ctx.StatusCode(iris.StatusInternalServerError)
			ctx.WriteString(err.Error())
			return
		}
		form := ctx.Request().MultipartForm
		files := form.File["upload"]
		var uploadfiles  []string
		for _, file := range files {
			if !utils.ValueIsEmpty(file.Filename) {
				filename := strings.Split(file.Filename,".")
				filetype := filename[1]
				guid := utils.MakeOrderSn(user.Userid)
				filPath := "public/uploadfile/" + guid + "." +filetype
				savePath := "web/public/uploadfile/" + guid + "." +filetype
				// 上传文件至指定目录
				err := utils.SaveUploadedFile(file, savePath)
				if err != nil {
					supports.Error(ctx, iris.StatusBadRequest, err.Error(), nil)
					return
				}
				fmt.Println(filPath)
				uploadfiles = append(uploadfiles,filPath)
			}
		}
		//err = method(user)
		//if err != nil {
		//	supports.Error(ctx, iris.StatusBadRequest, err.Error(), nil)
		//	return
		//}
		picPath := strings.Join(uploadfiles,",")
		supports.Ok(ctx, supports.OptionSuccess, picPath)
	})
}

/******************************************************************************
 *
 * @Function Name            : RegisterUploadPic
 *-----------------------------------------------------------------------------
 *
 * @Description              : 上传图片的方法注册路由
 *
 * @Function Parameters      : 路由分组
 *
 * @Function Parameters      : 路径
 *
 * @Function Parameters      : 实际处理请求的方法
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-18 22:49:12
 *
 ******************************************************************************/
func RegisterUploadPic(party router.Party, path string, method func(*models.Usertab) error) {
	RegisterUploadMustPic(party, "POST", path, method)
}
