center-config/manager/config.go

223 lines
6.0 KiB
Go

// Package manager ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:46 下午 2021/8/9
package manager
import (
"errors"
"fmt"
"time"
"git.zhangdeman.cn/zhangdeman/center-config/dao"
"git.zhangdeman.cn/zhangdeman/center-config/define/model"
"git.zhangdeman.cn/zhangdeman/center-config/define/form"
"github.com/gin-gonic/gin"
)
type config struct {
base
}
// CreateOrUpdateConfig 创建或更新配置
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:50 下午 2021/8/9
func (c *config) CreateOrUpdateConfig(dataForm *form.CreateOrUpdateConfig) error {
var (
err error
namespaceInfo *model.Namespace
configInfo *model.Config
)
if namespaceInfo, err = Namespace.GetNamespaceDetail(dataForm.NamespaceID, dataForm.Namespace); nil != err {
return errors.New("namespace is not found : " + err.Error())
}
if err = dao.Config.Detail(dao.DBInstance, &model.Config{NamespaceID: namespaceInfo.ID, Field: dataForm.Key}, configInfo, nil); nil != err {
// 没查到,创建
return c.Create(dataForm, namespaceInfo)
} else {
// 查到更新
return c.Update(dataForm, namespaceInfo, configInfo)
}
}
// Create 创建配置
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:10 上午 2021/8/10
func (c *config) Create(dataForm *form.CreateOrUpdateConfig, namespaceInfo *model.Namespace) error {
var (
err error
configInfo *model.Config
)
if nil == namespaceInfo {
if namespaceInfo, err = Namespace.GetNamespaceDetail(dataForm.NamespaceID, dataForm.Namespace); nil != err {
return errors.New("namespace is not found :" + err.Error())
}
}
// 创建配置
tx := dao.Config.Begin()
if nil != tx.Error {
return err
}
// 创建配置
configInfo = &model.Config{
NamespaceID: namespaceInfo.ID,
Field: dataForm.Key,
Value: dataForm.Value,
Description: dataForm.Description,
CreateUserID: dataForm.OperateUserID,
ModifyUserID: dataForm.OperateUserID,
CreateTime: time.Now().Format("2006-01-02 15:04:05"),
ModifyTime: time.Now().Format("2006-01-02 15:04:05"),
}
if err = dao.Config.CreateNewData(tx, configInfo, nil); nil != err {
_ = dao.Config.Rollback(tx)
return err
}
// 记录日志
logData := c.GetLogData(namespaceInfo.ID, configInfo.ID, dataForm.OperateUserID, model.LogTypeCreateConfig, "新建配置")
logData.AfterValue = fmt.Sprintf(`{"field":"%s", "value":"%s"}`, dataForm.Key, dataForm.Value)
if err = dao.Log.CreateNewData(tx, logData, nil); nil != err {
_ = dao.Config.Rollback(tx)
return err
}
if err = dao.Config.Commit(tx); nil != err {
return err
}
return nil
}
// Update 更新配置
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:34 上午 2021/8/10
func (c *config) Update(dataForm *form.CreateOrUpdateConfig, namespaceInfo *model.Namespace, configInfo *model.Config) error {
var (
err error
)
if nil == namespaceInfo {
if namespaceInfo, err = Namespace.GetNamespaceDetail(dataForm.NamespaceID, dataForm.Namespace); nil != err {
return errors.New("namespace is not found :" + err.Error())
}
}
if nil == configInfo {
if configInfo, err = c.GetConfigInfo(namespaceInfo.ID, dataForm.Key); nil != err {
return err
}
}
// 更新配置
tx := dao.Config.Begin()
if nil != tx.Error {
return err
}
if err = dao.Config.UpdateData(tx, &model.Config{Value: dataForm.Value}, &model.Config{NamespaceID: namespaceInfo.ID, Field: dataForm.Key, Value: configInfo.Value}, nil); nil != err {
_ = dao.Config.Rollback(tx)
return err
}
// 记录日志
logData := c.GetLogData(namespaceInfo.ID, configInfo.ID, dataForm.OperateUserID, model.LogTypeUpdateConfig, "更新配置 : "+dataForm.Description)
logData.BeforeValue = fmt.Sprintf(`{"field":"%s", "value":"%s"}`, dataForm.Key, configInfo.Value)
logData.AfterValue = fmt.Sprintf(`{"field":"%s", "value":"%s"}`, dataForm.Key, dataForm.Value)
if err = dao.Log.CreateNewData(tx, logData, nil); nil != err {
_ = dao.Config.Rollback(tx)
return err
}
if err = dao.Config.Commit(tx); nil != err {
return err
}
return nil
}
// ValidateCreateOrUpdateConfigForm ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:49 下午 2021/8/9
func (c *config) ValidateCreateOrUpdateConfigForm(ctx *gin.Context) (*form.CreateOrUpdateConfig, error) {
var (
dataForm form.CreateOrUpdateConfig
err error
)
if err = c.ParsePostForm(ctx, &dataForm); nil != err {
return nil, err
}
if dataForm.NamespaceID <= 0 && len(dataForm.Namespace) == 0 {
return nil, errors.New("namespace_id or namespace is required")
}
if len(dataForm.Key) == 0 {
return nil, errors.New("key is required")
}
if len(dataForm.OperateUserID) == 0 {
return nil, errors.New("operate_user_id is required")
}
return &dataForm, nil
}
// GetConfigInfo 读取配置
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:59 上午 2021/8/10
func (c *config) GetConfigInfo(namespaceID int64, configKey string) (*model.Config, error) {
var (
err error
configInfo model.Config
)
if err = dao.Config.Detail(dao.DBInstance, &model.Config{NamespaceID: namespaceID, Field: configKey}, &configInfo, nil); nil != err {
return nil, errors.New("config is not found :" + err.Error())
}
return &configInfo, err
}
// GetAllConfig 获取全部配置
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 9:57 下午 2021/8/10
func (c *config) GetAllConfig() ([]*model.Config, error) {
var (
list []*model.Config
err error
)
if err = dao.Config.Select(dao.DBInstance, &model.Config{}, &list, nil); nil != err {
return make([]*model.Config, 0), err
}
return list, nil
}
// GetNamespaceConfig 获取命名空间的配置
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:45 上午 2021/8/11
func (c *config) GetNamespaceConfig(namespaceID int64) ([]*model.Config, error) {
var (
list []*model.Config
err error
)
if err = dao.Config.Select(dao.DBInstance, &model.Config{NamespaceID: namespaceID}, &list, nil); nil != err {
return make([]*model.Config, 0), err
}
return list, nil
}