78 lines
1.6 KiB
Go
78 lines
1.6 KiB
Go
|
package menu
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"wechat-robot/internal/database"
|
||
|
"wechat-robot/internal/orm"
|
||
|
"wechat-robot/model/entity"
|
||
|
menuParam "wechat-robot/model/param/menu"
|
||
|
)
|
||
|
|
||
|
// Save
|
||
|
// @description: 保存菜单
|
||
|
// @param param menuParam.Save 菜单数据
|
||
|
// @return err error 错误信息
|
||
|
func Save(param menuParam.Save) (err error) {
|
||
|
tx := database.Client.Begin()
|
||
|
defer func() {
|
||
|
if err != nil {
|
||
|
tx.Rollback()
|
||
|
return
|
||
|
}
|
||
|
tx.Commit()
|
||
|
}()
|
||
|
|
||
|
// 提前梳理出参数
|
||
|
paramSql := "parent_id IS NULL"
|
||
|
if param.ParentId != "" {
|
||
|
paramSql = fmt.Sprintf("parent_id = '%s'", param.ParentId)
|
||
|
}
|
||
|
|
||
|
if param.Id == "" {
|
||
|
// 新增
|
||
|
var m entity.Menu
|
||
|
// 预处理排序
|
||
|
if err = orm.CreateSortBefore(tx, m.TableName(), param.Sort, paramSql); err != nil {
|
||
|
return
|
||
|
}
|
||
|
// 组装参数
|
||
|
m.Type = param.Type
|
||
|
m.Name = param.Name
|
||
|
m.Path = param.Path
|
||
|
m.Title = param.Title
|
||
|
m.Icon = param.Icon
|
||
|
m.Sort = param.Sort
|
||
|
if param.ParentId != "" {
|
||
|
m.ParentId = ¶m.ParentId
|
||
|
}
|
||
|
err = tx.Create(&m).Error
|
||
|
} else {
|
||
|
// 修改
|
||
|
// 预处理排序
|
||
|
if err = orm.UpdateSortBefore(tx, entity.Menu{}.TableName(), param.Id, param.Sort, paramSql); err != nil {
|
||
|
return
|
||
|
}
|
||
|
// 组装参数
|
||
|
var m = make(map[string]any)
|
||
|
m["type"] = param.Type
|
||
|
m["name"] = param.Name
|
||
|
m["path"] = param.Path
|
||
|
m["title"] = param.Title
|
||
|
m["icon"] = param.Icon
|
||
|
m["sort"] = param.Sort
|
||
|
if param.ParentId != "" {
|
||
|
m["parent_id"] = ¶m.ParentId
|
||
|
}
|
||
|
err = tx.Model(&entity.Menu{}).Where("id = ?", param.Id).Updates(m).Error
|
||
|
}
|
||
|
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
// 处理排序
|
||
|
err = orm.DealSortAfter(tx, entity.Menu{}.TableName(), paramSql)
|
||
|
|
||
|
return
|
||
|
}
|