参数配置信息接口添加到路由
This commit is contained in:
321
features/sys_config/service/repo_sys_config.go
Normal file
321
features/sys_config/service/repo_sys_config.go
Normal file
@@ -0,0 +1,321 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"ems.agt/features/sys_config/model"
|
||||
"ems.agt/lib/core/datasource"
|
||||
"ems.agt/lib/core/utils/date"
|
||||
"ems.agt/lib/core/utils/parse"
|
||||
"ems.agt/lib/log"
|
||||
)
|
||||
|
||||
// 实例化数据层 RepoSysConfig 结构体
|
||||
var NewRepoSysConfig = &RepoSysConfig{
|
||||
selectSql: `select
|
||||
config_id, config_name, config_key, config_value, config_type, create_by, create_time, update_by, update_time, remark
|
||||
from sys_config`,
|
||||
|
||||
resultMap: map[string]string{
|
||||
"config_id": "ConfigID",
|
||||
"config_name": "ConfigName",
|
||||
"config_key": "ConfigKey",
|
||||
"config_value": "ConfigValue",
|
||||
"config_type": "ConfigType",
|
||||
"remark": "Remark",
|
||||
"create_by": "CreateBy",
|
||||
"create_time": "CreateTime",
|
||||
"update_by": "UpdateBy",
|
||||
"update_time": "UpdateTime",
|
||||
},
|
||||
}
|
||||
|
||||
// RepoSysConfig 参数配置表 数据层处理
|
||||
type RepoSysConfig struct {
|
||||
// 查询视图对象SQL
|
||||
selectSql string
|
||||
// 结果字段与实体映射
|
||||
resultMap map[string]string
|
||||
}
|
||||
|
||||
// convertResultRows 将结果记录转实体结果组
|
||||
func (r *RepoSysConfig) convertResultRows(rows []map[string]any) []model.SysConfig {
|
||||
arr := make([]model.SysConfig, 0)
|
||||
for _, row := range rows {
|
||||
sysConfig := model.SysConfig{}
|
||||
for key, value := range row {
|
||||
if keyMapper, ok := r.resultMap[key]; ok {
|
||||
datasource.SetFieldValue(&sysConfig, keyMapper, value)
|
||||
}
|
||||
}
|
||||
arr = append(arr, sysConfig)
|
||||
}
|
||||
return arr
|
||||
}
|
||||
|
||||
// SelectDictDataPage 分页查询参数配置列表数据
|
||||
func (r *RepoSysConfig) SelectConfigPage(query map[string]any) map[string]any {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if v, ok := query["configName"]; ok && v != "" {
|
||||
conditions = append(conditions, "config_name like concat(?, '%')")
|
||||
params = append(params, v)
|
||||
}
|
||||
if v, ok := query["configType"]; ok && v != "" {
|
||||
conditions = append(conditions, "config_type = ?")
|
||||
params = append(params, v)
|
||||
}
|
||||
if v, ok := query["configKey"]; ok && v != "" {
|
||||
conditions = append(conditions, "config_key like concat(?, '%')")
|
||||
params = append(params, v)
|
||||
}
|
||||
beginTime, ok := query["beginTime"]
|
||||
if !ok {
|
||||
beginTime, ok = query["params[beginTime]"]
|
||||
}
|
||||
if ok && beginTime != "" {
|
||||
conditions = append(conditions, "create_time >= ?")
|
||||
beginDate := date.ParseStrToDate(beginTime.(string), date.YYYY_MM_DD)
|
||||
params = append(params, beginDate.UnixMilli())
|
||||
}
|
||||
endTime, ok := query["endTime"]
|
||||
if !ok {
|
||||
endTime, ok = query["params[endTime]"]
|
||||
}
|
||||
if ok && endTime != "" {
|
||||
conditions = append(conditions, "create_time <= ?")
|
||||
endDate := date.ParseStrToDate(endTime.(string), date.YYYY_MM_DD)
|
||||
params = append(params, endDate.UnixMilli())
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
}
|
||||
|
||||
// 查询数量 长度为0直接返回
|
||||
totalSql := "select count(1) as 'total' from sys_config"
|
||||
totalRows, err := datasource.RawDB("", totalSql+whereSql, params)
|
||||
if err != nil {
|
||||
log.Errorf("total err => %v", err)
|
||||
}
|
||||
total := parse.Number(totalRows[0]["total"])
|
||||
if total == 0 {
|
||||
return map[string]any{
|
||||
"total": total,
|
||||
"rows": []model.SysConfig{},
|
||||
}
|
||||
}
|
||||
|
||||
// 分页
|
||||
pageNum, pageSize := datasource.PageNumSize(query["pageNum"], query["pageSize"])
|
||||
pageSql := " limit ?,? "
|
||||
params = append(params, pageNum*pageSize)
|
||||
params = append(params, pageSize)
|
||||
|
||||
// 查询数据
|
||||
querySql := r.selectSql + whereSql + pageSql
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
log.Errorf("query err => %v", err)
|
||||
}
|
||||
|
||||
// 转换实体
|
||||
rows := r.convertResultRows(results)
|
||||
return map[string]any{
|
||||
"total": total,
|
||||
"rows": rows,
|
||||
}
|
||||
}
|
||||
|
||||
// SelectConfigList 查询参数配置列表
|
||||
func (r *RepoSysConfig) SelectConfigList(sysConfig model.SysConfig) []model.SysConfig {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if sysConfig.ConfigName != "" {
|
||||
conditions = append(conditions, "config_name like concat(?, '%')")
|
||||
params = append(params, sysConfig.ConfigName)
|
||||
}
|
||||
if sysConfig.ConfigType != "" {
|
||||
conditions = append(conditions, "config_type = ?")
|
||||
params = append(params, sysConfig.ConfigType)
|
||||
}
|
||||
if sysConfig.ConfigKey != "" {
|
||||
conditions = append(conditions, "config_key like concat(?, '%')")
|
||||
params = append(params, sysConfig.ConfigKey)
|
||||
}
|
||||
if sysConfig.CreateTime > 0 {
|
||||
conditions = append(conditions, "create_time >= ?")
|
||||
params = append(params, sysConfig.CreateTime)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
querySql := r.selectSql + whereSql
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
log.Errorf("query err => %v", err)
|
||||
return []model.SysConfig{}
|
||||
}
|
||||
|
||||
// 转换实体
|
||||
return r.convertResultRows(results)
|
||||
}
|
||||
|
||||
// SelectConfigValueByKey 通过参数键名查询参数键值
|
||||
func (r *RepoSysConfig) SelectConfigValueByKey(configKey string) string {
|
||||
querySql := "select config_value as 'str' from sys_config where config_key = ?"
|
||||
results, err := datasource.RawDB("", querySql, []any{configKey})
|
||||
if err != nil {
|
||||
log.Errorf("query err => %v", err)
|
||||
return ""
|
||||
}
|
||||
if len(results) > 0 {
|
||||
return fmt.Sprintf("%v", results[0]["str"])
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// SelectConfigByIds 通过配置ID查询参数配置信息
|
||||
func (r *RepoSysConfig) SelectConfigByIds(configIds []string) []model.SysConfig {
|
||||
placeholder := datasource.KeyPlaceholderByQuery(len(configIds))
|
||||
querySql := r.selectSql + " where config_id in (" + placeholder + ")"
|
||||
parameters := datasource.ConvertIdsSlice(configIds)
|
||||
results, err := datasource.RawDB("", querySql, parameters)
|
||||
if err != nil {
|
||||
log.Errorf("query err => %v", err)
|
||||
return []model.SysConfig{}
|
||||
}
|
||||
// 转换实体
|
||||
return r.convertResultRows(results)
|
||||
}
|
||||
|
||||
// CheckUniqueConfig 校验配置参数是否唯一
|
||||
func (r *RepoSysConfig) CheckUniqueConfig(sysConfig model.SysConfig) string {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if sysConfig.ConfigKey != "" {
|
||||
conditions = append(conditions, "config_key = ?")
|
||||
params = append(params, sysConfig.ConfigKey)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
} else {
|
||||
return ""
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
querySql := "select config_id as 'str' from sys_config " + whereSql + " limit 1"
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
log.Errorf("query err %v", err)
|
||||
return ""
|
||||
}
|
||||
if len(results) > 0 {
|
||||
return fmt.Sprintf("%v", results[0]["str"])
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// InsertConfig 新增参数配置
|
||||
func (r *RepoSysConfig) InsertConfig(sysConfig model.SysConfig) string {
|
||||
// 参数拼接
|
||||
params := make(map[string]any)
|
||||
if sysConfig.ConfigName != "" {
|
||||
params["config_name"] = sysConfig.ConfigName
|
||||
}
|
||||
if sysConfig.ConfigKey != "" {
|
||||
params["config_key"] = sysConfig.ConfigKey
|
||||
}
|
||||
if sysConfig.ConfigValue != "" {
|
||||
params["config_value"] = sysConfig.ConfigValue
|
||||
}
|
||||
if sysConfig.ConfigType != "" {
|
||||
params["config_type"] = sysConfig.ConfigType
|
||||
}
|
||||
if sysConfig.Remark != "" {
|
||||
params["remark"] = sysConfig.Remark
|
||||
}
|
||||
if sysConfig.CreateBy != "" {
|
||||
params["create_by"] = sysConfig.CreateBy
|
||||
params["create_time"] = time.Now().UnixMilli()
|
||||
}
|
||||
|
||||
// 构建执行语句
|
||||
keys, placeholder, values := datasource.KeyPlaceholderValueByInsert(params)
|
||||
sql := "insert into sys_config (" + strings.Join(keys, ",") + ")values(" + placeholder + ")"
|
||||
|
||||
// 执行插入
|
||||
rows, err := datasource.ExecDB("", sql, values)
|
||||
if err != nil {
|
||||
log.Errorf("insert row : %v", err.Error())
|
||||
return ""
|
||||
}
|
||||
|
||||
return fmt.Sprint(rows)
|
||||
}
|
||||
|
||||
// UpdateConfig 修改参数配置
|
||||
func (r *RepoSysConfig) UpdateConfig(sysConfig model.SysConfig) int64 {
|
||||
// 参数拼接
|
||||
params := make(map[string]any)
|
||||
if sysConfig.ConfigName != "" {
|
||||
params["config_name"] = sysConfig.ConfigName
|
||||
}
|
||||
if sysConfig.ConfigKey != "" {
|
||||
params["config_key"] = sysConfig.ConfigKey
|
||||
}
|
||||
if sysConfig.ConfigValue != "" {
|
||||
params["config_value"] = sysConfig.ConfigValue
|
||||
}
|
||||
if sysConfig.ConfigType != "" {
|
||||
params["config_type"] = sysConfig.ConfigType
|
||||
}
|
||||
if sysConfig.Remark != "" {
|
||||
params["remark"] = sysConfig.Remark
|
||||
}
|
||||
if sysConfig.UpdateBy != "" {
|
||||
params["update_by"] = sysConfig.UpdateBy
|
||||
params["update_time"] = time.Now().UnixMilli()
|
||||
}
|
||||
|
||||
// 构建执行语句
|
||||
keys, values := datasource.KeyValueByUpdate(params)
|
||||
sql := "update sys_config set " + strings.Join(keys, ",") + " where config_id = ?"
|
||||
|
||||
// 执行更新
|
||||
values = append(values, sysConfig.ConfigID)
|
||||
rows, err := datasource.ExecDB("", sql, values)
|
||||
if err != nil {
|
||||
log.Errorf("update row : %v", err.Error())
|
||||
return 0
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
// DeleteConfigByIds 批量删除参数配置信息
|
||||
func (r *RepoSysConfig) DeleteConfigByIds(configIds []string) int64 {
|
||||
placeholder := datasource.KeyPlaceholderByQuery(len(configIds))
|
||||
sql := "delete from sys_config where config_id in (" + placeholder + ")"
|
||||
parameters := datasource.ConvertIdsSlice(configIds)
|
||||
results, err := datasource.ExecDB("", sql, parameters)
|
||||
if err != nil {
|
||||
log.Errorf("delete err => %v", err)
|
||||
return 0
|
||||
}
|
||||
return results
|
||||
}
|
||||
155
features/sys_config/service/service_sys_config.go
Normal file
155
features/sys_config/service/service_sys_config.go
Normal file
@@ -0,0 +1,155 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"ems.agt/features/sys_config/model"
|
||||
"ems.agt/lib/core/cache"
|
||||
"ems.agt/lib/core/constants/cachekey"
|
||||
)
|
||||
|
||||
// 实例化服务层 ServiceSysConfig 结构体
|
||||
var NewServiceSysConfig = &ServiceSysConfig{
|
||||
sysConfigRepository: NewRepoSysConfig,
|
||||
}
|
||||
|
||||
// ServiceSysConfig 参数配置 服务层处理
|
||||
type ServiceSysConfig struct {
|
||||
// 参数配置表
|
||||
sysConfigRepository *RepoSysConfig
|
||||
}
|
||||
|
||||
// SelectDictDataPage 分页查询参数配置列表数据
|
||||
func (r *ServiceSysConfig) SelectConfigPage(query map[string]any) map[string]any {
|
||||
return r.sysConfigRepository.SelectConfigPage(query)
|
||||
}
|
||||
|
||||
// SelectConfigList 查询参数配置列表
|
||||
func (r *ServiceSysConfig) SelectConfigList(sysConfig model.SysConfig) []model.SysConfig {
|
||||
return r.sysConfigRepository.SelectConfigList(sysConfig)
|
||||
}
|
||||
|
||||
// SelectConfigValueByKey 通过参数键名查询参数键值
|
||||
func (r *ServiceSysConfig) SelectConfigValueByKey(configKey string) string {
|
||||
cacheKey := r.getCacheKey(configKey)
|
||||
// 从缓存中读取
|
||||
cacheValue, ok := cache.GetLocal(cacheKey)
|
||||
if cacheValue != nil && ok {
|
||||
return cacheValue.(string)
|
||||
}
|
||||
// 无缓存时读取数据放入缓存中
|
||||
configValue := r.sysConfigRepository.SelectConfigValueByKey(configKey)
|
||||
if configValue != "" {
|
||||
cache.SetLocal(cacheKey, configValue)
|
||||
return configValue
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// SelectConfigById 通过配置ID查询参数配置信息
|
||||
func (r *ServiceSysConfig) SelectConfigById(configId string) model.SysConfig {
|
||||
if configId == "" {
|
||||
return model.SysConfig{}
|
||||
}
|
||||
configs := r.sysConfigRepository.SelectConfigByIds([]string{configId})
|
||||
if len(configs) > 0 {
|
||||
return configs[0]
|
||||
}
|
||||
return model.SysConfig{}
|
||||
}
|
||||
|
||||
// CheckUniqueConfigKey 校验参数键名是否唯一
|
||||
func (r *ServiceSysConfig) CheckUniqueConfigKey(configKey, configId string) bool {
|
||||
uniqueId := r.sysConfigRepository.CheckUniqueConfig(model.SysConfig{
|
||||
ConfigKey: configKey,
|
||||
})
|
||||
if uniqueId == configId {
|
||||
return true
|
||||
}
|
||||
return uniqueId == ""
|
||||
}
|
||||
|
||||
// InsertConfig 新增参数配置
|
||||
func (r *ServiceSysConfig) InsertConfig(sysConfig model.SysConfig) string {
|
||||
configId := r.sysConfigRepository.InsertConfig(sysConfig)
|
||||
if configId != "" {
|
||||
r.loadingConfigCache(sysConfig.ConfigKey)
|
||||
}
|
||||
return configId
|
||||
}
|
||||
|
||||
// UpdateConfig 修改参数配置
|
||||
func (r *ServiceSysConfig) UpdateConfig(sysConfig model.SysConfig) int64 {
|
||||
rows := r.sysConfigRepository.UpdateConfig(sysConfig)
|
||||
if rows > 0 {
|
||||
r.loadingConfigCache(sysConfig.ConfigKey)
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
// DeleteConfigByIds 批量删除参数配置信息
|
||||
func (r *ServiceSysConfig) DeleteConfigByIds(configIds []string) (int64, error) {
|
||||
// 检查是否存在
|
||||
configs := r.sysConfigRepository.SelectConfigByIds(configIds)
|
||||
if len(configs) <= 0 {
|
||||
return 0, errors.New("没有权限访问参数配置数据!")
|
||||
}
|
||||
for _, config := range configs {
|
||||
// 检查是否为内置参数
|
||||
if config.ConfigType == "Y" {
|
||||
return 0, errors.New(config.ConfigID + " 配置参数属于内置参数,禁止删除!")
|
||||
}
|
||||
// 清除缓存
|
||||
r.clearConfigCache(config.ConfigKey)
|
||||
}
|
||||
if len(configs) == len(configIds) {
|
||||
rows := r.sysConfigRepository.DeleteConfigByIds(configIds)
|
||||
return rows, nil
|
||||
}
|
||||
return 0, errors.New("删除参数配置信息失败!")
|
||||
}
|
||||
|
||||
// ResetConfigCache 重置参数缓存数据
|
||||
func (r *ServiceSysConfig) ResetConfigCache() {
|
||||
r.clearConfigCache("*")
|
||||
r.loadingConfigCache("*")
|
||||
}
|
||||
|
||||
// getCacheKey 组装缓存key
|
||||
func (r *ServiceSysConfig) getCacheKey(configKey string) string {
|
||||
return cachekey.SYS_CONFIG_KEY + configKey
|
||||
}
|
||||
|
||||
// loadingConfigCache 加载参数缓存数据
|
||||
func (r *ServiceSysConfig) loadingConfigCache(configKey string) {
|
||||
// 查询全部参数
|
||||
if configKey == "*" {
|
||||
sysConfigs := r.SelectConfigList(model.SysConfig{})
|
||||
for _, v := range sysConfigs {
|
||||
key := r.getCacheKey(v.ConfigKey)
|
||||
cache.DeleteLocal(key)
|
||||
cache.SetLocal(key, v.ConfigValue)
|
||||
}
|
||||
return
|
||||
}
|
||||
// 指定参数
|
||||
if configKey != "" {
|
||||
cacheValue := r.sysConfigRepository.SelectConfigValueByKey(configKey)
|
||||
if cacheValue != "" {
|
||||
key := r.getCacheKey(configKey)
|
||||
cache.DeleteLocal(key)
|
||||
cache.SetLocal(key, cacheValue)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// clearConfigCache 清空参数缓存数据
|
||||
func (r *ServiceSysConfig) clearConfigCache(configKey string) bool {
|
||||
key := r.getCacheKey(configKey)
|
||||
keys := cache.GetLocalKeys(key)
|
||||
for _, v := range keys {
|
||||
cache.DeleteLocal(v)
|
||||
}
|
||||
return len(keys) > 0
|
||||
}
|
||||
Reference in New Issue
Block a user