package hdl

import (
	"applet/app/cfg"
	"applet/app/e"
	"applet/app/svc"
	"applet/app/utils"
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"strings"
)

// ServiceFileUpload 上传文件
// @Summary      基本配置-上传文件
// @Tags         基本配置
// @Description  基本配置-上传文件
// @Accept       json
// @Produce      json
// @Param        req 	body               true   "请求参数 表单提交  file文件流  type类型  微信p12文件-wechat_apiclient_cert 支付宝应用公钥证书 payAliNewAppCertSn 支付宝公钥证书 payAliNewAlipayrsaPublicKey 支付宝根证书 payAliNewAlipayRootCertSn"
// @Success      200   {string} 				""
// @Failure      400   {object}   md.Response              	"具体错误"
// @Router       /api/v1/communityTeam/agent/service/file/upload [POST]

func ServiceFileUpload(c *gin.Context) {
	// 单文件上传
	file, err := c.FormFile("file")
	if err != nil {
		c.String(http.StatusBadRequest, fmt.Sprintf("upload error: %s", err.Error()))
		return
	}
	user := svc.GetUser(c)
	ext := strings.Split(file.Filename, ".")
	newName := c.GetString("mid") + "_" + utils.IntToStr(user.Info.Uid) + "_" + c.PostForm("type") + "." + ext[1]
	path := "./static/"
	if cfg.Prd {
		path = "/etc/zyos-admin/wx_check_file/"
	}
	if err := c.SaveUploadedFile(file, path+newName); err != nil {
		e.OutErr(c, 400, err.Error())
		return
	}
	e.OutSuc(c, "success", nil)
	return
}
