yunzer_go/server/controllers/dict.go
2025-11-17 15:33:57 +08:00

765 lines
17 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package controllers
import (
"encoding/json"
"server/models"
"server/services"
"strconv"
"time"
"github.com/beego/beego/v2/server/web"
)
// DictController 字典管理控制器
type DictController struct {
web.Controller
}
// GetDictTypes 获取字典类型列表
func (c *DictController) GetDictTypes() {
// 获取租户ID
tenantIdData := c.Ctx.Input.GetData("tenantId")
tenantId := 0
if tenantIdData != nil {
if tid, ok := tenantIdData.(int); ok {
tenantId = tid
}
}
// 获取用户ID
userIdData := c.Ctx.Input.GetData("userId")
userId := 0
if userIdData != nil {
if uid, ok := userIdData.(int); ok {
userId = uid
}
}
parentId, _ := c.GetInt("parent_id", -1)
statusStr := c.GetString("status")
var status *int8
if statusStr != "" {
s, _ := strconv.ParseInt(statusStr, 10, 8)
statusVal := int8(s)
status = &statusVal
}
// 分页参数
page, _ := c.GetInt("page", 1)
pageSize, _ := c.GetInt("pageSize", 20)
dictTypes, total, err := services.GetDictTypes(tenantId, userId, parentId, status, page, pageSize)
if err != nil {
c.Data["json"] = map[string]interface{}{
"success": false,
"message": "查询字典类型失败: " + err.Error(),
}
c.ServeJSON()
return
}
c.Data["json"] = map[string]interface{}{
"success": true,
"data": map[string]interface{}{
"list": dictTypes,
"total": total,
"page": page,
"pageSize": pageSize,
},
}
c.ServeJSON()
}
// GetDictTypeById 根据ID获取字典类型
func (c *DictController) GetDictTypeById() {
idStr := c.Ctx.Input.Param(":id")
id, err := strconv.Atoi(idStr)
if err != nil || id <= 0 {
c.Data["json"] = map[string]interface{}{
"success": false,
"message": "参数错误",
}
c.ServeJSON()
return
}
dictType, err := services.GetDictTypeById(id)
if err != nil {
c.Data["json"] = map[string]interface{}{
"success": false,
"message": "字典类型不存在",
}
c.ServeJSON()
return
}
c.Data["json"] = map[string]interface{}{
"success": true,
"data": dictType,
}
c.ServeJSON()
}
// AddDictType 添加字典类型
func (c *DictController) AddDictType() {
var dictType models.DictType
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &dictType); err != nil {
c.Data["json"] = map[string]interface{}{
"success": false,
"message": "参数解析失败: " + err.Error(),
}
c.ServeJSON()
return
}
// 获取租户ID和用户名
tenantIdData := c.Ctx.Input.GetData("tenantId")
tenantId := 0
if tenantIdData != nil {
if tid, ok := tenantIdData.(int); ok {
tenantId = tid
}
}
usernameData := c.Ctx.Input.GetData("username")
username := ""
if usernameData != nil {
if u, ok := usernameData.(string); ok {
username = u
}
}
userIdData := c.Ctx.Input.GetData("userId")
userId := 0
if userIdData != nil {
if uid, ok := userIdData.(int); ok {
userId = uid
}
}
// 设置租户ID和创建人
dictType.TenantId = tenantId
dictType.CreateBy = username
if dictType.Status == 0 {
dictType.Status = 1 // 默认启用
}
// 根据用户角色设置是否全局展示
// system_admin 和 admin 角色默认是全局,其他角色默认是租户私有
dictType.IsGlobal = 0 // 默认为租户私有
if userId > 0 {
// 获取用户的角色信息
roleCode, err := services.GetUserRoleCode(userId)
if err == nil && (roleCode == "system_admin" || roleCode == "admin") {
dictType.IsGlobal = 1 // system_admin 和 admin 角色默认全局
}
}
id, err := services.AddDictType(&dictType)
if err != nil {
c.Data["json"] = map[string]interface{}{
"success": false,
"message": err.Error(),
}
c.ServeJSON()
return
}
c.Data["json"] = map[string]interface{}{
"success": true,
"message": "添加成功",
"data": map[string]interface{}{"id": id},
}
c.ServeJSON()
// 记录操作日志 - 创建字典类型
go func() {
// 异步写日志,避免阻塞请求响应
rid := int(id)
newVal, _ := json.Marshal(dictType)
log := &models.OperationLog{
TenantId: dictType.TenantId,
UserId: userId,
Username: username,
Module: "dict",
ResourceType: "dict_type",
ResourceId: &rid,
Operation: "CREATE",
Description: "创建字典类型",
NewValue: string(newVal),
IpAddress: c.Ctx.Input.IP(),
UserAgent: c.Ctx.Input.Header("User-Agent"),
RequestMethod: c.Ctx.Input.Method(),
RequestUrl: c.Ctx.Input.URL(),
Status: 1,
Duration: 0,
CreateTime: time.Now(),
}
_ = services.AddOperationLog(log)
}()
}
// UpdateDictType 更新字典类型
func (c *DictController) UpdateDictType() {
idStr := c.Ctx.Input.Param(":id")
id, err := strconv.Atoi(idStr)
if err != nil || id <= 0 {
c.Data["json"] = map[string]interface{}{
"success": false,
"message": "参数错误",
}
c.ServeJSON()
return
}
var dictType models.DictType
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &dictType); err != nil {
c.Data["json"] = map[string]interface{}{
"success": false,
"message": "参数解析失败: " + err.Error(),
}
c.ServeJSON()
return
}
// 获取用户名
usernameData := c.Ctx.Input.GetData("username")
username := ""
if usernameData != nil {
if u, ok := usernameData.(string); ok {
username = u
}
}
// 获取用户ID
userIdData := c.Ctx.Input.GetData("userId")
userId := 0
if userIdData != nil {
if uid, ok := userIdData.(int); ok {
userId = uid
}
}
dictType.Id = id
dictType.UpdateBy = username
err = services.UpdateDictType(&dictType)
if err != nil {
c.Data["json"] = map[string]interface{}{
"success": false,
"message": err.Error(),
}
c.ServeJSON()
return
}
c.Data["json"] = map[string]interface{}{
"success": true,
"message": "更新成功",
}
c.ServeJSON()
// 记录操作日志 - 更新字典类型
go func() {
newVal, _ := json.Marshal(dictType)
// 旧值可以从服务层获取,如果需要更详细的旧值,可以在 UpdateDictType 前查询并传入
log := &models.OperationLog{
TenantId: dictType.TenantId,
UserId: userId,
Username: username,
Module: "dict",
ResourceType: "dict_type",
ResourceId: &dictType.Id,
Operation: "UPDATE",
Description: "更新字典类型",
NewValue: string(newVal),
IpAddress: c.Ctx.Input.IP(),
UserAgent: c.Ctx.Input.Header("User-Agent"),
RequestMethod: c.Ctx.Input.Method(),
RequestUrl: c.Ctx.Input.URL(),
Status: 1,
Duration: 0,
CreateTime: time.Now(),
}
_ = services.AddOperationLog(log)
}()
}
// DeleteDictType 删除字典类型
func (c *DictController) DeleteDictType() {
idStr := c.Ctx.Input.Param(":id")
id, err := strconv.Atoi(idStr)
if err != nil || id <= 0 {
c.Data["json"] = map[string]interface{}{
"success": false,
"message": "参数错误",
}
c.ServeJSON()
return
}
// 获取租户ID、用户名和用户ID 用于记录日志
tenantIdData := c.Ctx.Input.GetData("tenantId")
tenantId := 0
if tenantIdData != nil {
if tid, ok := tenantIdData.(int); ok {
tenantId = tid
}
}
usernameData := c.Ctx.Input.GetData("username")
username := ""
if usernameData != nil {
if u, ok := usernameData.(string); ok {
username = u
}
}
userIdData := c.Ctx.Input.GetData("userId")
userId := 0
if userIdData != nil {
if uid, ok := userIdData.(int); ok {
userId = uid
}
}
err = services.DeleteDictType(id, tenantId)
if err != nil {
c.Data["json"] = map[string]interface{}{
"success": false,
"message": err.Error(),
}
c.ServeJSON()
return
}
c.Data["json"] = map[string]interface{}{
"success": true,
"message": "删除成功",
}
c.ServeJSON()
// 记录操作日志 - 删除字典类型
go func() {
rid := id
log := &models.OperationLog{
TenantId: tenantId,
UserId: userId,
Username: username,
Module: "dict",
ResourceType: "dict_type",
ResourceId: &rid,
Operation: "DELETE",
Description: "删除字典类型",
IpAddress: c.Ctx.Input.IP(),
UserAgent: c.Ctx.Input.Header("User-Agent"),
RequestMethod: c.Ctx.Input.Method(),
RequestUrl: c.Ctx.Input.URL(),
Status: 1,
Duration: 0,
CreateTime: time.Now(),
}
_ = services.AddOperationLog(log)
}()
}
// GetDictItems 获取字典项列表
func (c *DictController) GetDictItems() {
dictTypeId, _ := c.GetInt("dict_type_id", 0)
if dictTypeId <= 0 {
c.Data["json"] = map[string]interface{}{
"success": false,
"message": "参数错误dict_type_id 必填",
}
c.ServeJSON()
return
}
parentIdStr := c.GetString("parent_id")
statusStr := c.GetString("status")
var parentId *int
if parentIdStr != "" {
pid, _ := strconv.Atoi(parentIdStr)
parentId = &pid
}
var status *int8
if statusStr != "" {
s, _ := strconv.ParseInt(statusStr, 10, 8)
statusVal := int8(s)
status = &statusVal
}
dictItems, err := services.GetDictItems(dictTypeId, parentId, status)
if err != nil {
c.Data["json"] = map[string]interface{}{
"success": false,
"message": "查询字典项失败: " + err.Error(),
}
c.ServeJSON()
return
}
c.Data["json"] = map[string]interface{}{
"success": true,
"data": dictItems,
}
c.ServeJSON()
}
// GetDictItemById 根据ID获取字典项
func (c *DictController) GetDictItemById() {
idStr := c.Ctx.Input.Param(":id")
id, err := strconv.Atoi(idStr)
if err != nil || id <= 0 {
c.Data["json"] = map[string]interface{}{
"success": false,
"message": "参数错误",
}
c.ServeJSON()
return
}
dictItem, err := services.GetDictItemById(id)
if err != nil {
c.Data["json"] = map[string]interface{}{
"success": false,
"message": "字典项不存在",
}
c.ServeJSON()
return
}
c.Data["json"] = map[string]interface{}{
"success": true,
"data": dictItem,
}
c.ServeJSON()
}
// AddDictItem 添加字典项
func (c *DictController) AddDictItem() {
var dictItem models.DictItem
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &dictItem); err != nil {
c.Data["json"] = map[string]interface{}{
"success": false,
"message": "参数解析失败: " + err.Error(),
}
c.ServeJSON()
return
}
if dictItem.DictTypeId <= 0 {
c.Data["json"] = map[string]interface{}{
"success": false,
"message": "参数错误dict_type_id 必填",
}
c.ServeJSON()
return
}
// 获取用户名
usernameData := c.Ctx.Input.GetData("username")
username := ""
if usernameData != nil {
if u, ok := usernameData.(string); ok {
username = u
}
}
// 设置创建人
dictItem.CreateBy = username
if dictItem.Status == 0 {
dictItem.Status = 1 // 默认启用
}
id, err := services.AddDictItem(&dictItem)
if err != nil {
c.Data["json"] = map[string]interface{}{
"success": false,
"message": err.Error(),
}
c.ServeJSON()
return
}
c.Data["json"] = map[string]interface{}{
"success": true,
"message": "添加成功",
"data": map[string]interface{}{"id": id},
}
c.ServeJSON()
// 记录操作日志 - 创建字典项
go func() {
// 获取用户ID和租户ID
userIdData := c.Ctx.Input.GetData("userId")
userId := 0
if userIdData != nil {
if uid, ok := userIdData.(int); ok {
userId = uid
}
}
tenantIdData := c.Ctx.Input.GetData("tenantId")
tenantId := 0
if tenantIdData != nil {
if tid, ok := tenantIdData.(int); ok {
tenantId = tid
}
}
rid := int(id)
newVal, _ := json.Marshal(dictItem)
log := &models.OperationLog{
TenantId: tenantId,
UserId: userId,
Username: username,
Module: "dict",
ResourceType: "dict_item",
ResourceId: &rid,
Operation: "CREATE",
Description: "创建字典项",
NewValue: string(newVal),
IpAddress: c.Ctx.Input.IP(),
UserAgent: c.Ctx.Input.Header("User-Agent"),
RequestMethod: c.Ctx.Input.Method(),
RequestUrl: c.Ctx.Input.URL(),
Status: 1,
Duration: 0,
CreateTime: time.Now(),
}
_ = services.AddOperationLog(log)
}()
}
// UpdateDictItem 更新字典项
func (c *DictController) UpdateDictItem() {
idStr := c.Ctx.Input.Param(":id")
id, err := strconv.Atoi(idStr)
if err != nil || id <= 0 {
c.Data["json"] = map[string]interface{}{
"success": false,
"message": "参数错误",
}
c.ServeJSON()
return
}
var dictItem models.DictItem
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &dictItem); err != nil {
c.Data["json"] = map[string]interface{}{
"success": false,
"message": "参数解析失败: " + err.Error(),
}
c.ServeJSON()
return
}
// 获取用户名
usernameData := c.Ctx.Input.GetData("username")
username := ""
if usernameData != nil {
if u, ok := usernameData.(string); ok {
username = u
}
}
dictItem.Id = id
dictItem.UpdateBy = username
err = services.UpdateDictItem(&dictItem)
if err != nil {
c.Data["json"] = map[string]interface{}{
"success": false,
"message": err.Error(),
}
c.ServeJSON()
return
}
c.Data["json"] = map[string]interface{}{
"success": true,
"message": "更新成功",
}
c.ServeJSON()
// 记录操作日志 - 更新字典项
go func() {
// 获取用户ID和租户ID
userIdData := c.Ctx.Input.GetData("userId")
userId := 0
if userIdData != nil {
if uid, ok := userIdData.(int); ok {
userId = uid
}
}
tenantId := 0
// 通过字典项id尝试获取所属字典类型并推断租户可选
// 这里先不查询,留 0 或者可从前端传入
newVal, _ := json.Marshal(dictItem)
log := &models.OperationLog{
TenantId: tenantId,
UserId: userId,
Username: username,
Module: "dict",
ResourceType: "dict_item",
ResourceId: &dictItem.Id,
Operation: "UPDATE",
Description: "更新字典项",
NewValue: string(newVal),
IpAddress: c.Ctx.Input.IP(),
UserAgent: c.Ctx.Input.Header("User-Agent"),
RequestMethod: c.Ctx.Input.Method(),
RequestUrl: c.Ctx.Input.URL(),
Status: 1,
Duration: 0,
CreateTime: time.Now(),
}
_ = services.AddOperationLog(log)
}()
}
// DeleteDictItem 删除字典项
func (c *DictController) DeleteDictItem() {
idStr := c.Ctx.Input.Param(":id")
id, err := strconv.Atoi(idStr)
if err != nil || id <= 0 {
c.Data["json"] = map[string]interface{}{
"success": false,
"message": "参数错误",
}
c.ServeJSON()
return
}
err = services.DeleteDictItem(id)
if err != nil {
c.Data["json"] = map[string]interface{}{
"success": false,
"message": err.Error(),
}
c.ServeJSON()
return
}
c.Data["json"] = map[string]interface{}{
"success": true,
"message": "删除成功",
}
c.ServeJSON()
// 记录操作日志 - 删除字典项
go func() {
// 获取用户ID和租户ID
userIdData := c.Ctx.Input.GetData("userId")
userId := 0
if userIdData != nil {
if uid, ok := userIdData.(int); ok {
userId = uid
}
}
tenantIdData := c.Ctx.Input.GetData("tenantId")
tenantId := 0
if tenantIdData != nil {
if tid, ok := tenantIdData.(int); ok {
tenantId = tid
}
}
usernameData := c.Ctx.Input.GetData("username")
username := ""
if usernameData != nil {
if u, ok := usernameData.(string); ok {
username = u
}
}
rid := id
log := &models.OperationLog{
TenantId: tenantId,
UserId: userId,
Username: username,
Module: "dict",
ResourceType: "dict_item",
ResourceId: &rid,
Operation: "DELETE",
Description: "删除字典项",
IpAddress: c.Ctx.Input.IP(),
UserAgent: c.Ctx.Input.Header("User-Agent"),
RequestMethod: c.Ctx.Input.Method(),
RequestUrl: c.Ctx.Input.URL(),
Status: 1,
Duration: 0,
CreateTime: time.Now(),
}
_ = services.AddOperationLog(log)
}()
}
// GetDictItemsByCode 根据字典编码获取字典项(用于业务查询)
func (c *DictController) GetDictItemsByCode() {
dictCode := c.Ctx.Input.Param(":code")
if dictCode == "" {
c.Data["json"] = map[string]interface{}{
"success": false,
"message": "参数错误code 必填",
}
c.ServeJSON()
return
}
// 获取租户ID
tenantIdData := c.Ctx.Input.GetData("tenantId")
tenantId := 0
if tenantIdData != nil {
if tid, ok := tenantIdData.(int); ok {
tenantId = tid
}
}
includeDisabled := c.GetString("include_disabled") == "1"
dictItems, err := services.GetDictItemsByCode(dictCode, tenantId, includeDisabled)
if err != nil {
c.Data["json"] = map[string]interface{}{
"success": false,
"message": "查询字典项失败: " + err.Error(),
}
c.ServeJSON()
return
}
c.Data["json"] = map[string]interface{}{
"success": true,
"data": dictItems,
}
c.ServeJSON()
}
// BatchUpdateDictItemSort 批量更新字典项排序
func (c *DictController) BatchUpdateDictItemSort() {
var items []struct {
Id int `json:"id"`
Sort int `json:"sort"`
}
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &items); err != nil {
c.Data["json"] = map[string]interface{}{
"success": false,
"message": "参数解析失败: " + err.Error(),
}
c.ServeJSON()
return
}
err := services.BatchUpdateDictItemSort(items)
if err != nil {
c.Data["json"] = map[string]interface{}{
"success": false,
"message": err.Error(),
}
c.ServeJSON()
return
}
c.Data["json"] = map[string]interface{}{
"success": true,
"message": "更新成功",
}
c.ServeJSON()
}