package aliyun

import (
	"applet/app/db"
	"applet/app/e"
	md "applet/app/md/setCenter/oss/aliyun"
	"applet/app/utils/cache"
	"code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
	enum2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/enum"
	"github.com/gin-gonic/gin"
)

// GetBasic
// @Summary      设置中心-对象存储-对象存储设置(获取)
// @Tags         对象存储
// @Description  对象存储设置(获取)
// @Accept       json
// @Produce      json
// @param Authorization header string true "验证参数Bearer和token空格拼接"
// @Success      200   {object}  	md.GetBasicResp "设置列表"
// @Failure      400   {object}   md.Response              	"具体错误"
// @Router       /api/comm/oss/getBasic [get]
func GetBasic(c *gin.Context) {
	redisConn := cache.GetPool().Get()
	sysCfgDb := implement.NewSysCfgDb(db.Db, redisConn)
	sysCfg, err := sysCfgDb.SysCfgGetAll()
	if err != nil {
		e.OutErr(c, e.ERR_DB_ORM, err.Error())
		return
	}
	cfgMap := make(map[string]string, len(*sysCfg))
	for _, cfg := range *sysCfg {
		cfgMap[cfg.Key] = cfg.Val
	}
	endpoint, ok := cfgMap[enum2.AliyunOss(enum2.AliyunOssEndpoint).String()]
	if !ok {
		sysCfgDb.SysCfgInsert(enum2.AliyunOss(enum2.AliyunOssEndpoint).String(), "", "oss地域节点")
		endpoint = ""
	}
	bucketName, ok := cfgMap[enum2.AliyunOss(enum2.AliyunOssBucketName).String()]
	if !ok {
		sysCfgDb.SysCfgInsert(enum2.AliyunOss(enum2.AliyunOssBucketName).String(), "", "oss存储桶名称")
		bucketName = ""
	}
	bucketScheme, ok := cfgMap[enum2.AliyunOss(enum2.AliyunOssBucketScheme).String()]
	if !ok {
		sysCfgDb.SysCfgInsert(enum2.AliyunOss(enum2.AliyunOssBucketScheme).String(), "", "oss上传方式")
		bucketScheme = ""
	}
	accessKeyID, ok := cfgMap[enum2.AliyunOss(enum2.AliyunOssAccessKeyID).String()]
	if !ok {
		sysCfgDb.SysCfgInsert(enum2.AliyunOss(enum2.AliyunOssAccessKeyID).String(), "", "oss访问秘钥id")
		accessKeyID = ""
	}
	accessKeySecret, ok := cfgMap[enum2.AliyunOss(enum2.AliyunOssAccessKeySecret).String()]
	if !ok {
		sysCfgDb.SysCfgInsert(enum2.AliyunOss(enum2.AliyunOssAccessKeySecret).String(), "", "oss访问秘钥")
		accessKeySecret = ""
	}
	domain, ok := cfgMap[enum2.AliyunOss(enum2.AliyunOssDomain).String()]
	if !ok {
		sysCfgDb.SysCfgInsert(enum2.AliyunOss(enum2.AliyunOssDomain).String(), "", "oss域名")
		domain = ""
	}

	resp := md.GetBasicResp{
		OssEndpoint:        endpoint,
		OssBucketName:      bucketName,
		OssBucketScheme:    bucketScheme,
		OssAccessKeyId:     accessKeyID,
		OssAccessKeySecret: accessKeySecret,
		OssObjectDomain:    domain,
	}

	e.OutSuc(c, resp, nil)
}

// SetBasic
// @Summary      设置中心-对象存储-对象存储设置(更新)
// @Tags         对象存储
// @Description  对象存储设置(更新)
// @Accept       json
// @Produce      json
// @param Authorization header string true "验证参数Bearer和token空格拼接"
// @param req body  md.SetBasicReq true "上传需要修改的信息"
// @Success      200   {string}  	 "success"
// @Failure      400   {object}   md.Response              	"具体错误"
// @Router       /api/comm/oss/setBasic [post]
func SetBasic(c *gin.Context) {
	var req *md.SetBasicReq
	if err1 := c.ShouldBindJSON(&req); err1 != nil {
		e.OutErr(c, e.ERR_INVALID_ARGS, err1.Error())
		return
	}

	redisConn := cache.GetPool().Get()
	cfgDb := implement.NewSysCfgDb(db.Db, redisConn)

	if req.OssAccessKeyId != "" {
		cfgDb.SysCfgUpdate(enum2.AliyunOss(enum2.AliyunOssAccessKeyID).String(), req.OssAccessKeyId)
	}
	if req.OssAccessKeySecret != "" {
		cfgDb.SysCfgUpdate(enum2.AliyunOss(enum2.AliyunOssAccessKeySecret).String(), req.OssAccessKeySecret)
	}
	if req.OssBucketScheme != "" {
		cfgDb.SysCfgUpdate(enum2.AliyunOss(enum2.AliyunOssBucketScheme).String(), req.OssBucketScheme)
	}
	if req.OssObjectDomain != "" {
		cfgDb.SysCfgUpdate(enum2.AliyunOss(enum2.AliyunOssDomain).String(), req.OssObjectDomain)
	}
	if req.OssBucketName != "" {
		cfgDb.SysCfgUpdate(enum2.AliyunOss(enum2.AliyunOssBucketName).String(), req.OssBucketName)
	}
	if req.OssEndpoint != "" {
		cfgDb.SysCfgUpdate(enum2.AliyunOss(enum2.AliyunOssEndpoint).String(), req.OssEndpoint)
	}
	e.OutSuc(c, "success", nil)
}