feat: 更新多个模块以支持新的数据结构和日志格式
This commit is contained in:
@@ -3,24 +3,22 @@ package controller
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"be.ems/src/framework/i18n"
|
||||
"be.ems/src/framework/utils/ctx"
|
||||
"be.ems/src/framework/reqctx"
|
||||
"be.ems/src/framework/resp"
|
||||
"be.ems/src/framework/utils/file"
|
||||
"be.ems/src/framework/utils/parse"
|
||||
"be.ems/src/framework/vo/result"
|
||||
"be.ems/src/modules/system/model"
|
||||
"be.ems/src/modules/system/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gin-gonic/gin/binding"
|
||||
)
|
||||
|
||||
// 实例化控制层 SysConfigController 结构体
|
||||
var NewSysConfig = &SysConfigController{
|
||||
sysConfigService: service.NewSysConfigImpl,
|
||||
sysConfigService: service.NewSysConfig,
|
||||
}
|
||||
|
||||
// 参数配置信息
|
||||
@@ -28,7 +26,7 @@ var NewSysConfig = &SysConfigController{
|
||||
// PATH /system/config
|
||||
type SysConfigController struct {
|
||||
// 参数配置服务
|
||||
sysConfigService service.ISysConfig
|
||||
sysConfigService *service.SysConfig // 参数配置服务
|
||||
}
|
||||
|
||||
// 参数配置列表
|
||||
@@ -47,15 +45,14 @@ type SysConfigController struct {
|
||||
// @Description Config Information List
|
||||
// @Router /system/config/list [get]
|
||||
func (s *SysConfigController) List(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
querys := ctx.QueryMap(c)
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
query := reqctx.QueryMap(c)
|
||||
// 多语言值转key查询
|
||||
if v, ok := querys["configName"]; ok && v != "" {
|
||||
querys["configName"] = i18n.TFindKeyPrefix(language, "config", v.(string))
|
||||
if v, ok := query["configName"]; ok && v != "" {
|
||||
query["configName"] = i18n.TFindKeyPrefix(language, "config", v)
|
||||
}
|
||||
|
||||
data := s.sysConfigService.SelectConfigPage(querys)
|
||||
rows := data["rows"].([]model.SysConfig)
|
||||
rows, total := s.sysConfigService.FindByPage(query)
|
||||
|
||||
// 闭包函数处理多语言
|
||||
converI18n := func(language string, arr *[]model.SysConfig) {
|
||||
@@ -67,187 +64,200 @@ func (s *SysConfigController) List(c *gin.Context) {
|
||||
}
|
||||
converI18n(language, &rows)
|
||||
|
||||
c.JSON(200, result.Ok(data))
|
||||
c.JSON(200, resp.OkData(map[string]any{"rows": rows, "total": total}))
|
||||
}
|
||||
|
||||
// 参数配置信息
|
||||
//
|
||||
// GET /:configId
|
||||
func (s *SysConfigController) Info(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
configId := c.Param("configId")
|
||||
if configId == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
configId := parse.Number(c.Param("configId"))
|
||||
if configId <= 0 {
|
||||
c.JSON(400, resp.CodeMsg(40010, "bind err: configId is empty"))
|
||||
return
|
||||
}
|
||||
|
||||
data := s.sysConfigService.SelectConfigById(configId)
|
||||
if data.ConfigID == configId {
|
||||
data := s.sysConfigService.FindById(configId)
|
||||
if data.ConfigId == configId {
|
||||
// 处理多语言
|
||||
data.ConfigName = i18n.TKey(language, data.ConfigName)
|
||||
data.ConfigValue = i18n.TKey(language, data.ConfigValue)
|
||||
data.Remark = i18n.TKey(language, data.Remark)
|
||||
c.JSON(200, result.OkData(data))
|
||||
c.JSON(200, resp.OkData(data))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
c.JSON(200, resp.Err(nil))
|
||||
}
|
||||
|
||||
// 参数配置新增
|
||||
//
|
||||
// POST /
|
||||
func (s *SysConfigController) Add(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
var body model.SysConfig
|
||||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||||
if err != nil || body.ConfigID != "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
if err := c.ShouldBindBodyWithJSON(&body); err != nil {
|
||||
errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
|
||||
c.JSON(422, resp.CodeMsg(40422, errMsgs))
|
||||
return
|
||||
}
|
||||
if body.ConfigId > 0 {
|
||||
c.JSON(400, resp.CodeMsg(40010, "bind err: configId not is empty"))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查属性值唯一
|
||||
uniqueConfigKey := s.sysConfigService.CheckUniqueConfigKey(body.ConfigKey, "")
|
||||
uniqueConfigKey := s.sysConfigService.CheckUniqueByKey(body.ConfigKey, 0)
|
||||
if !uniqueConfigKey {
|
||||
// 参数配置新增【%s】失败,参数键名已存在
|
||||
// msg := fmt.Sprintf("参数配置新增【%s】失败,参数键名已存在", body.ConfigKey)
|
||||
msg := i18n.TTemplate(language, "config.errKeyExists", map[string]any{"name": body.ConfigKey})
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
c.JSON(200, resp.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
|
||||
body.CreateBy = ctx.LoginUserToUserName(c)
|
||||
insertId := s.sysConfigService.InsertConfig(body)
|
||||
if insertId != "" {
|
||||
c.JSON(200, result.Ok(nil))
|
||||
body.CreateBy = reqctx.LoginUserToUserName(c)
|
||||
insertId := s.sysConfigService.Insert(body)
|
||||
if insertId > 0 {
|
||||
c.JSON(200, resp.OkData(insertId))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
c.JSON(200, resp.Err(nil))
|
||||
}
|
||||
|
||||
// 参数配置修改
|
||||
//
|
||||
// PUT /
|
||||
func (s *SysConfigController) Edit(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
var body model.SysConfig
|
||||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||||
if err != nil || body.ConfigID == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
if err := c.ShouldBindBodyWithJSON(&body); err != nil {
|
||||
errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
|
||||
c.JSON(422, resp.CodeMsg(40422, errMsgs))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查属性值唯一
|
||||
uniqueConfigKey := s.sysConfigService.CheckUniqueConfigKey(body.ConfigKey, body.ConfigID)
|
||||
if !uniqueConfigKey {
|
||||
// 参数配置修改【%s】失败,参数键名已存在
|
||||
msg := i18n.TTemplate(language, "config.errKeyExists", map[string]any{"name": body.ConfigKey})
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
if body.ConfigId <= 0 {
|
||||
c.JSON(400, resp.CodeMsg(40010, "bind err: configId is empty"))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查是否存在
|
||||
configInfo := s.sysConfigService.SelectConfigById(body.ConfigID)
|
||||
if configInfo.ConfigID != body.ConfigID {
|
||||
// 没有可访问参数配置数据!
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "config.noData")))
|
||||
configInfo := s.sysConfigService.FindById(body.ConfigId)
|
||||
if configInfo.ConfigId != body.ConfigId {
|
||||
// c.JSON(200, resp.ErrMsg("没有权限访问参数配置数据!"))
|
||||
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "config.noData")))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查属性值唯一
|
||||
uniqueConfigKey := s.sysConfigService.CheckUniqueByKey(body.ConfigKey, body.ConfigId)
|
||||
if !uniqueConfigKey {
|
||||
// msg := fmt.Sprintf("参数配置修改【%s】失败,参数键名已存在", body.ConfigKey)
|
||||
msg := i18n.TTemplate(language, "config.errKeyExists", map[string]any{"name": body.ConfigKey})
|
||||
c.JSON(200, resp.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
|
||||
// 多语言非原始值
|
||||
i18nValue := i18n.TKey(language, configInfo.ConfigName)
|
||||
if i18nValue != configInfo.ConfigName {
|
||||
i18n.UpdateKeyValue(language, configInfo.ConfigName, body.ConfigName)
|
||||
service.NewSysI18n.UpdateKeyValue(language, configInfo.ConfigName, body.ConfigName)
|
||||
body.ConfigName = configInfo.ConfigName
|
||||
}
|
||||
// 多语言非原始值
|
||||
i18nValue2 := i18n.TKey(language, configInfo.ConfigValue)
|
||||
if i18nValue2 != configInfo.ConfigValue {
|
||||
i18n.UpdateKeyValue(language, configInfo.ConfigValue, body.ConfigValue)
|
||||
service.NewSysI18n.UpdateKeyValue(language, configInfo.ConfigValue, body.ConfigValue)
|
||||
body.ConfigValue = configInfo.ConfigValue
|
||||
}
|
||||
// 多语言非原始值
|
||||
i18nValue3 := i18n.TKey(language, configInfo.Remark)
|
||||
if i18nValue3 != configInfo.Remark {
|
||||
i18n.UpdateKeyValue(language, configInfo.Remark, body.Remark)
|
||||
service.NewSysI18n.UpdateKeyValue(language, configInfo.Remark, body.Remark)
|
||||
body.Remark = configInfo.Remark
|
||||
}
|
||||
|
||||
body.UpdateBy = ctx.LoginUserToUserName(c)
|
||||
rows := s.sysConfigService.UpdateConfig(body)
|
||||
configInfo.ConfigType = body.ConfigType
|
||||
configInfo.ConfigName = body.ConfigName
|
||||
configInfo.ConfigKey = body.ConfigKey
|
||||
configInfo.ConfigValue = body.ConfigValue
|
||||
configInfo.Remark = body.Remark
|
||||
configInfo.UpdateBy = reqctx.LoginUserToUserName(c)
|
||||
rows := s.sysConfigService.Update(configInfo)
|
||||
if rows > 0 {
|
||||
c.JSON(200, result.Ok(nil))
|
||||
c.JSON(200, resp.Ok(nil))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
c.JSON(200, resp.Err(nil))
|
||||
}
|
||||
|
||||
// 参数配置删除
|
||||
// Remove 参数配置删除
|
||||
//
|
||||
// DELETE /:configIds
|
||||
func (s *SysConfigController) Remove(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
configIds := c.Param("configIds")
|
||||
if configIds == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
// DELETE /:configId
|
||||
func (s SysConfigController) Remove(c *gin.Context) {
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
configId := c.Param("configId")
|
||||
if configId == "" {
|
||||
c.JSON(400, resp.CodeMsg(40010, "bind err: configId is empty"))
|
||||
return
|
||||
}
|
||||
|
||||
// 处理字符转id数组后去重
|
||||
ids := strings.Split(configIds, ",")
|
||||
uniqueIDs := parse.RemoveDuplicates(ids)
|
||||
if len(uniqueIDs) <= 0 {
|
||||
c.JSON(200, result.Err(nil))
|
||||
return
|
||||
uniqueIDs := parse.RemoveDuplicatesToArray(configId, ",")
|
||||
// 转换成int64数组类型
|
||||
ids := make([]int64, 0)
|
||||
for _, v := range uniqueIDs {
|
||||
ids = append(ids, parse.Number(v))
|
||||
}
|
||||
rows, err := s.sysConfigService.DeleteConfigByIds(uniqueIDs)
|
||||
|
||||
rows, err := s.sysConfigService.DeleteByIds(ids)
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, err.Error())))
|
||||
c.JSON(200, resp.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
// msg := fmt.Sprintf("删除成功:%d", rows)
|
||||
msg := i18n.TTemplate(language, "app.common.deleteSuccess", map[string]any{"num": rows})
|
||||
c.JSON(200, result.OkMsg(msg))
|
||||
c.JSON(200, resp.OkMsg(msg))
|
||||
}
|
||||
|
||||
// 参数配置刷新缓存
|
||||
// Refresh 参数配置刷新缓存
|
||||
//
|
||||
// PUT /refreshCache
|
||||
func (s *SysConfigController) RefreshCache(c *gin.Context) {
|
||||
s.sysConfigService.ResetConfigCache()
|
||||
c.JSON(200, result.Ok(nil))
|
||||
// PUT /refresh
|
||||
func (s SysConfigController) Refresh(c *gin.Context) {
|
||||
s.sysConfigService.CacheClean("*")
|
||||
s.sysConfigService.CacheLoad("*")
|
||||
c.JSON(200, resp.Ok(nil))
|
||||
}
|
||||
|
||||
// 参数配置根据参数键名
|
||||
// ConfigKey 参数配置根据参数键名
|
||||
//
|
||||
// GET /configKey/:configKey
|
||||
func (s *SysConfigController) ConfigKey(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
// GET /config-key/:configKey
|
||||
func (s SysConfigController) ConfigKey(c *gin.Context) {
|
||||
configKey := c.Param("configKey")
|
||||
if configKey == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
c.JSON(400, resp.CodeMsg(40010, "bind err: configKey is empty"))
|
||||
return
|
||||
}
|
||||
key := s.sysConfigService.SelectConfigValueByKey(configKey)
|
||||
key := s.sysConfigService.FindValueByKey(configKey)
|
||||
if key != "" {
|
||||
c.JSON(200, result.OkData(i18n.TKey(language, key)))
|
||||
c.JSON(200, resp.OkData(key))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
c.JSON(200, resp.Err(nil))
|
||||
}
|
||||
|
||||
// 导出参数配置信息
|
||||
// Export 导出参数配置信息
|
||||
//
|
||||
// POST /export
|
||||
func (s *SysConfigController) Export(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
// GET /export
|
||||
func (s SysConfigController) Export(c *gin.Context) {
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
// 查询结果,根据查询条件结果,单页最大值限制
|
||||
querys := ctx.BodyJSONMap(c)
|
||||
querys["pageNum"] = 1
|
||||
querys["pageSize"] = 10000
|
||||
data := s.sysConfigService.SelectConfigPage(querys)
|
||||
if parse.Number(data["total"]) == 0 {
|
||||
// 导出数据记录为空
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.exportEmpty")))
|
||||
query := reqctx.QueryMap(c)
|
||||
rows, total := s.sysConfigService.FindByPage(query)
|
||||
if total == 0 {
|
||||
// c.JSON(200, resp.CodeMsg(40016, "export data record as empty"))
|
||||
c.JSON(200, resp.CodeMsg(40016, i18n.TKey(language, "app.common.exportEmpty")))
|
||||
return
|
||||
}
|
||||
rows := data["rows"].([]model.SysConfig)
|
||||
|
||||
// 闭包函数处理多语言
|
||||
converI18n := func(language string, arr *[]model.SysConfig) {
|
||||
@@ -279,7 +289,7 @@ func (s *SysConfigController) Export(c *gin.Context) {
|
||||
typeValue = i18n.TKey(language, "dictData.yes")
|
||||
}
|
||||
dataCells = append(dataCells, map[string]any{
|
||||
"A" + idx: row.ConfigID,
|
||||
"A" + idx: row.ConfigId,
|
||||
"B" + idx: row.ConfigName,
|
||||
"C" + idx: row.ConfigKey,
|
||||
"D" + idx: row.ConfigValue,
|
||||
@@ -291,7 +301,7 @@ func (s *SysConfigController) Export(c *gin.Context) {
|
||||
// 导出数据表格
|
||||
saveFilePath, err := file.WriteSheet(headerCells, dataCells, fileName, "")
|
||||
if err != nil {
|
||||
c.JSON(200, result.ErrMsg(err.Error()))
|
||||
c.JSON(200, resp.ErrMsg(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -302,21 +312,22 @@ func (s *SysConfigController) Export(c *gin.Context) {
|
||||
//
|
||||
// PUT /changeValue
|
||||
func (s *SysConfigController) ConfigValue(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
var body struct {
|
||||
Key string `json:"key" binding:"required"`
|
||||
Value string `json:"value" binding:"required"`
|
||||
}
|
||||
if err := c.ShouldBindBodyWith(&body, binding.JSON); err != nil {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
if err := c.ShouldBindBodyWithJSON(&body); err != nil {
|
||||
errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
|
||||
c.JSON(422, resp.CodeMsg(40422, errMsgs))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查是否存在
|
||||
info := s.sysConfigService.SelectConfigByKey(body.Key)
|
||||
info := s.sysConfigService.FindByKey(body.Key)
|
||||
if info.ConfigKey != body.Key {
|
||||
// 无效 key
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "config.errKey")))
|
||||
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "config.errKey")))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -324,22 +335,22 @@ func (s *SysConfigController) ConfigValue(c *gin.Context) {
|
||||
i18nValue := i18n.TKey(language, info.ConfigValue)
|
||||
if i18nValue == body.Value {
|
||||
// 变更状态与旧值相等!
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "config.errValueEq")))
|
||||
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "config.errValueEq")))
|
||||
return
|
||||
}
|
||||
|
||||
// 多语言非原始值
|
||||
if i18nValue != info.ConfigValue {
|
||||
i18n.UpdateKeyValue(language, info.ConfigValue, body.Value)
|
||||
service.NewSysI18n.UpdateKeyValue(language, info.ConfigValue, body.Value)
|
||||
} else {
|
||||
info.ConfigValue = body.Value
|
||||
}
|
||||
|
||||
info.UpdateBy = ctx.LoginUserToUserName(c)
|
||||
rows := s.sysConfigService.UpdateConfig(info)
|
||||
info.UpdateBy = reqctx.LoginUserToUserName(c)
|
||||
rows := s.sysConfigService.Update(info)
|
||||
if rows > 0 {
|
||||
c.JSON(200, result.Ok(nil))
|
||||
c.JSON(200, resp.Ok(nil))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
c.JSON(200, resp.Err(nil))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user