增加字典

This commit is contained in:
2025-11-07 17:38:59 +08:00
parent b159d71a77
commit 8b8b0c54a1
10 changed files with 1892 additions and 97 deletions
+511
View File
@@ -0,0 +1,511 @@
package controllers
import (
"encoding/json"
"server/models"
"server/services"
"strconv"
"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
}
}
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
}
dictTypes, err := services.GetDictTypes(tenantId, 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": dictTypes,
}
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
}
}
// 设置租户ID和创建人
dictType.TenantId = tenantId
dictType.CreateBy = username
if dictType.Status == 0 {
dictType.Status = 1 // 默认启用
}
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()
}
// 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
}
}
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()
}
// 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
tenantIdData := c.Ctx.Input.GetData("tenantId")
tenantId := 0
if tenantIdData != nil {
if tid, ok := tenantIdData.(int); ok {
tenantId = tid
}
}
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()
}
// 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()
}
// 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()
}
// 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()
}
// 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()
}