327 lines
9.0 KiB
Go
327 lines
9.0 KiB
Go
package controller
|
|
|
|
import (
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"ems.agt/src/framework/i18n"
|
|
"ems.agt/src/framework/utils/ctx"
|
|
"ems.agt/src/framework/utils/file"
|
|
"ems.agt/src/framework/utils/parse"
|
|
"ems.agt/src/framework/vo/result"
|
|
"ems.agt/src/modules/system/model"
|
|
"ems.agt/src/modules/system/service"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/gin-gonic/gin/binding"
|
|
)
|
|
|
|
// 实例化控制层 SysConfigController 结构体
|
|
var NewSysConfig = &SysConfigController{
|
|
sysConfigService: service.NewSysConfigImpl,
|
|
}
|
|
|
|
// 参数配置信息
|
|
//
|
|
// PATH /system/config
|
|
type SysConfigController struct {
|
|
// 参数配置服务
|
|
sysConfigService service.ISysConfig
|
|
}
|
|
|
|
// 参数配置列表
|
|
//
|
|
// GET /list
|
|
func (s *SysConfigController) List(c *gin.Context) {
|
|
querys := ctx.QueryMap(c)
|
|
data := s.sysConfigService.SelectConfigPage(querys)
|
|
|
|
rows := data["rows"].([]model.SysConfig)
|
|
// 闭包函数处理多语言
|
|
language := ctx.AcceptLanguage(c)
|
|
converI18n := func(language string, arr *[]model.SysConfig) {
|
|
for i := range *arr {
|
|
(*arr)[i].ConfigName = i18n.TKey(language, (*arr)[i].ConfigName)
|
|
(*arr)[i].ConfigValue = i18n.TKey(language, (*arr)[i].ConfigValue)
|
|
(*arr)[i].Remark = i18n.TKey(language, (*arr)[i].Remark)
|
|
}
|
|
}
|
|
converI18n(language, &rows)
|
|
|
|
c.JSON(200, result.Ok(data))
|
|
}
|
|
|
|
// 参数配置信息
|
|
//
|
|
// 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")))
|
|
return
|
|
}
|
|
|
|
data := s.sysConfigService.SelectConfigById(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))
|
|
return
|
|
}
|
|
c.JSON(200, result.Err(nil))
|
|
}
|
|
|
|
// 参数配置新增
|
|
//
|
|
// POST /
|
|
func (s *SysConfigController) Add(c *gin.Context) {
|
|
language := ctx.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")))
|
|
return
|
|
}
|
|
|
|
// 检查属性值唯一
|
|
uniqueConfigKey := s.sysConfigService.CheckUniqueConfigKey(body.ConfigKey, "")
|
|
if !uniqueConfigKey {
|
|
// 参数配置新增【%s】失败,参数键名已存在
|
|
msg := i18n.TTemplate(language, "config.errKeyExists", map[string]any{"name": body.ConfigKey})
|
|
c.JSON(200, result.ErrMsg(msg))
|
|
return
|
|
}
|
|
|
|
body.CreateBy = ctx.LoginUserToUserName(c)
|
|
insertId := s.sysConfigService.InsertConfig(body)
|
|
if insertId != "" {
|
|
c.JSON(200, result.Ok(nil))
|
|
return
|
|
}
|
|
c.JSON(200, result.Err(nil))
|
|
}
|
|
|
|
// 参数配置修改
|
|
//
|
|
// PUT /
|
|
func (s *SysConfigController) Edit(c *gin.Context) {
|
|
language := ctx.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")))
|
|
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))
|
|
return
|
|
}
|
|
|
|
// 检查是否存在
|
|
configInfo := s.sysConfigService.SelectConfigById(body.ConfigID)
|
|
if configInfo.ConfigID != body.ConfigID {
|
|
// 没有可访问参数配置数据!
|
|
c.JSON(200, result.ErrMsg(i18n.TKey(language, "config.noData")))
|
|
return
|
|
}
|
|
|
|
// 多语言非原始值
|
|
i18nValue := i18n.TKey(language, configInfo.ConfigName)
|
|
if i18nValue != configInfo.ConfigName {
|
|
i18n.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)
|
|
body.ConfigValue = configInfo.ConfigValue
|
|
}
|
|
// 多语言非原始值
|
|
i18nValue3 := i18n.TKey(language, configInfo.Remark)
|
|
if i18nValue3 != configInfo.Remark {
|
|
i18n.UpdateKeyValue(language, configInfo.Remark, body.Remark)
|
|
body.Remark = configInfo.Remark
|
|
}
|
|
|
|
body.UpdateBy = ctx.LoginUserToUserName(c)
|
|
rows := s.sysConfigService.UpdateConfig(body)
|
|
if rows > 0 {
|
|
c.JSON(200, result.Ok(nil))
|
|
return
|
|
}
|
|
c.JSON(200, result.Err(nil))
|
|
}
|
|
|
|
// 参数配置删除
|
|
//
|
|
// 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")))
|
|
return
|
|
}
|
|
// 处理字符转id数组后去重
|
|
ids := strings.Split(configIds, ",")
|
|
uniqueIDs := parse.RemoveDuplicates(ids)
|
|
if len(uniqueIDs) <= 0 {
|
|
c.JSON(200, result.Err(nil))
|
|
return
|
|
}
|
|
rows, err := s.sysConfigService.DeleteConfigByIds(uniqueIDs)
|
|
if err != nil {
|
|
c.JSON(200, result.ErrMsg(i18n.TKey(language, err.Error())))
|
|
return
|
|
}
|
|
msg := i18n.TTemplate(language, "app.common.deleteSuccess", map[string]any{"num": rows})
|
|
c.JSON(200, result.OkMsg(msg))
|
|
}
|
|
|
|
// 参数配置刷新缓存
|
|
//
|
|
// PUT /refreshCache
|
|
func (s *SysConfigController) RefreshCache(c *gin.Context) {
|
|
s.sysConfigService.ResetConfigCache()
|
|
c.JSON(200, result.Ok(nil))
|
|
}
|
|
|
|
// 参数配置根据参数键名
|
|
//
|
|
// GET /configKey/:configKey
|
|
func (s *SysConfigController) ConfigKey(c *gin.Context) {
|
|
language := ctx.AcceptLanguage(c)
|
|
configKey := c.Param("configKey")
|
|
if configKey == "" {
|
|
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
|
return
|
|
}
|
|
key := s.sysConfigService.SelectConfigValueByKey(configKey)
|
|
if key != "" {
|
|
c.JSON(200, result.OkData(key))
|
|
return
|
|
}
|
|
c.JSON(200, result.Err(nil))
|
|
}
|
|
|
|
// 导出参数配置信息
|
|
//
|
|
// POST /export
|
|
func (s *SysConfigController) Export(c *gin.Context) {
|
|
language := ctx.AcceptLanguage(c)
|
|
// 查询结果,根据查询条件结果,单页最大值限制
|
|
querys := ctx.BodyJSONMap(c)
|
|
data := s.sysConfigService.SelectConfigPage(querys)
|
|
if data["total"].(int64) == 0 {
|
|
// 导出数据记录为空
|
|
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.exportEmpty")))
|
|
return
|
|
}
|
|
rows := data["rows"].([]model.SysConfig)
|
|
|
|
// 闭包函数处理多语言
|
|
converI18n := func(language string, arr *[]model.SysConfig) {
|
|
for i := range *arr {
|
|
(*arr)[i].ConfigName = i18n.TKey(language, (*arr)[i].ConfigName)
|
|
(*arr)[i].ConfigValue = i18n.TKey(language, (*arr)[i].ConfigValue)
|
|
(*arr)[i].Remark = i18n.TKey(language, (*arr)[i].Remark)
|
|
}
|
|
}
|
|
converI18n(language, &rows)
|
|
|
|
// 导出文件名称
|
|
fileName := fmt.Sprintf("config_export_%d_%d.xlsx", len(rows), time.Now().UnixMilli())
|
|
// 第一行表头标题
|
|
headerCells := map[string]string{
|
|
"A1": i18n.TKey(language, "config..export.id"),
|
|
"B1": i18n.TKey(language, "config..export.name"),
|
|
"C1": i18n.TKey(language, "config..export.key"),
|
|
"D1": i18n.TKey(language, "config..export.value"),
|
|
"E1": i18n.TKey(language, "config..export.type"),
|
|
"F1": i18n.TKey(language, "config..export.remark"),
|
|
}
|
|
// 从第二行开始的数据
|
|
dataCells := make([]map[string]any, 0)
|
|
for i, row := range rows {
|
|
idx := strconv.Itoa(i + 2)
|
|
typeValue := i18n.TKey(language, "dictData.no")
|
|
if row.ConfigType == "Y" {
|
|
typeValue = i18n.TKey(language, "dictData.yes")
|
|
}
|
|
dataCells = append(dataCells, map[string]any{
|
|
"A" + idx: row.ConfigID,
|
|
"B" + idx: row.ConfigName,
|
|
"C" + idx: row.ConfigKey,
|
|
"D" + idx: row.ConfigValue,
|
|
"E" + idx: typeValue,
|
|
"F" + idx: row.Remark,
|
|
})
|
|
}
|
|
|
|
// 导出数据表格
|
|
saveFilePath, err := file.WriteSheet(headerCells, dataCells, fileName, "")
|
|
if err != nil {
|
|
c.JSON(200, result.ErrMsg(err.Error()))
|
|
return
|
|
}
|
|
|
|
c.FileAttachment(saveFilePath, fileName)
|
|
}
|
|
|
|
// 参数配置修改配置参数
|
|
//
|
|
// PUT /changeValue
|
|
func (s *SysConfigController) ConfigValue(c *gin.Context) {
|
|
language := ctx.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")))
|
|
return
|
|
}
|
|
|
|
// 检查是否存在
|
|
info := s.sysConfigService.SelectConfigByKey(body.Key)
|
|
if info.ConfigKey != body.Key {
|
|
// 无效 key
|
|
c.JSON(200, result.ErrMsg(i18n.TKey(language, "config.errKey")))
|
|
return
|
|
}
|
|
|
|
// 与旧值相等 不变更
|
|
i18nValue := i18n.TKey(language, info.ConfigValue)
|
|
if i18nValue == body.Value {
|
|
// 变更状态与旧值相等!
|
|
c.JSON(200, result.ErrMsg(i18n.TKey(language, "config.errValueEq")))
|
|
return
|
|
}
|
|
|
|
// 多语言非原始值
|
|
if i18nValue != info.ConfigValue {
|
|
i18n.UpdateKeyValue(language, info.ConfigValue, body.Value)
|
|
} else {
|
|
info.ConfigValue = body.Value
|
|
}
|
|
|
|
info.UpdateBy = ctx.LoginUserToUserName(c)
|
|
rows := s.sysConfigService.UpdateConfig(info)
|
|
if rows > 0 {
|
|
c.JSON(200, result.Ok(nil))
|
|
return
|
|
}
|
|
c.JSON(200, result.Err(nil))
|
|
}
|