2021-11-30 17:50:21 +08:00
|
|
|
// Package manager ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 11:46 下午 2021/8/9
|
|
|
|
package manager
|
|
|
|
|
|
|
|
import (
|
2021-12-01 14:15:05 +08:00
|
|
|
"encoding/json"
|
2021-11-30 17:50:21 +08:00
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
2021-12-01 14:15:05 +08:00
|
|
|
// Delete 删除配置
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 12:16 下午 2021/12/1
|
|
|
|
func (c *config) Delete(dataForm *form.DeleteConfig) error {
|
|
|
|
var (
|
|
|
|
cfgInfo *model.Config
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
// 读取key的配置
|
|
|
|
if cfgInfo, err = c.GetConfigInfo(dataForm.NamespaceID, dataForm.Key); nil != err {
|
|
|
|
return errors.New("要删除的key查询失败 : " + err.Error())
|
|
|
|
}
|
|
|
|
if err = dao.Config.Delete(dao.DBInstance, "config", cfgInfo.ID, model.Config{}); nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
byteData, _ := json.Marshal(cfgInfo)
|
|
|
|
logData := c.GetLogData(cfgInfo.NamespaceID, cfgInfo.ID, dataForm.OperateUserID, model.LogTypeDeleteConfig, "删除配置-"+dataForm.Description)
|
|
|
|
logData.BeforeValue = string(byteData)
|
|
|
|
_ = dao.Log.CreateNewData(dao.DBInstance, logData, &dao.Option{DBTable: "log"})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-30 17:50:21 +08:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2021-12-01 14:15:05 +08:00
|
|
|
// ValidateDeleteConfigForm 删除配置表单
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 2:12 下午 2021/12/1
|
|
|
|
func (c *config) ValidateDeleteConfigForm(ctx *gin.Context) (*form.DeleteConfig, error) {
|
|
|
|
var (
|
|
|
|
dataForm form.DeleteConfig
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
if err = c.ParsePostForm(ctx, &dataForm); nil != err {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if dataForm.NamespaceID == 0 {
|
|
|
|
dataForm.NamespaceID = Namespace.GetNamespaceIDByNamespace(dataForm.Namespace)
|
|
|
|
}
|
|
|
|
if dataForm.NamespaceID == 0 {
|
|
|
|
return nil, errors.New("namespace or namespace_id must exist")
|
|
|
|
}
|
|
|
|
if len(dataForm.Key) == 0 {
|
|
|
|
return nil, errors.New("delete is not allowed empty")
|
|
|
|
}
|
|
|
|
return &dataForm, nil
|
|
|
|
}
|
|
|
|
|
2021-11-30 17:50:21 +08:00
|
|
|
// 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
|
|
|
|
}
|