You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
3.2 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package BaseMenuDao
import (
"dsBaseRpc/RpcService/BaseMenu/BaseMenuProto"
"dsBaseRpc/Utils/CommonUtil"
"dsBaseRpc/Utils/DbUtil"
"dsBaseRpc/Utils/SqlKit"
"dsBaseRpc/models"
"errors"
"github.com/xormplus/builder"
"strings"
)
var db = DbUtil.Engine
//批量增加
func AddBaseMenu(ms models.TBaseMenu) (int64, error) {
return db.Insert(ms)
}
//检查一个菜单编码是不是已存在
func ExistMenuCode(menuCode string) (bool, error) {
sql := `select count(1) as c from t_base_menu where b_use=1 and menu_code=?`
list, err := db.SQL(sql, menuCode).Query().List()
if err != nil {
return false, err
}
c := list[0]["c"].(int64)
if c == 0 {
return false, nil
} else {
return true, nil
}
}
//更新
func UpdateBaseMenu(model models.TBaseMenu, ForceUpdateFields []string) (int64, error) {
//1、清除Redis缓存
var ids = []string{model.MenuId}
var selector = SqlKit.GetBean("t_base_menu")
SqlKit.DeleteCacheByIds(ids, selector)
//2、计算本次操作有哪些字段需要更新
NeedUpdateFields := CommonUtil.GetNeedUpdateFields(model)
//3、合并强制更新的字段
ForceUpdateFields = append(ForceUpdateFields, NeedUpdateFields...)
//4、去重
ForceUpdateFields = CommonUtil.RemoveDuplicatesAndEmpty(ForceUpdateFields)
//5、更新操作,强制更新这些列
affected, err := db.ID(model.MenuId).Cols(ForceUpdateFields...).Update(model)
return affected, err
}
//分页查询
func PageBaseMenu(in *BaseMenuProto.QueryArg) ([]map[string]interface{}, int32, error) {
//接收传入参数
var myBuilder = builder.Dialect(builder.MYSQL).Select("t1.*").From("t_base_menu as t1")
myBuilder.And(builder.Eq{"t1.app_id": in.AppId})
myBuilder.And(builder.Eq{"t1.identity_id": in.IdentityId})
//获取拼接完成的SQL语句
sql, err := myBuilder.OrderBy("t1.sort_id").ToBoundSQL()
if err != nil {
return nil, 0, err
}
//调用多查询字段通用方法
list, count, err := SqlKit.Query(sql)
return list, count, err
}
//调整菜单的排序号direction=1向上drection=2向下
func ChangeMenuOrder(menuId string, direction int32) (bool, error) {
if direction != 1 && direction != 2 {
return false, errors.New("传入调整方向有误向上1向下2传入值不正确")
}
//查找当前menu的sort_id
list := SqlKit.QueryByIds([]string{menuId}, "t_base_role_menu")
if len(list) == 0 {
return false, errors.New("没有找到对应的menu_id!")
}
parentId := list[0]["parent_id"].(string)
sortId := list[0]["sort_id"].(int32)
sql := `select menu_id,sort_id from t_base_menu where parent_id=? and sort_id#? order sort_id limit 1`
if direction == 1 {
sql = strings.Replace(sql, "#", "<", -1)
} else {
sql = strings.Replace(sql, "#", ">", -1)
}
list2, err := db.SQL(sql, parentId, sortId).Query().List()
if err != nil {
return false, err
}
if len(list2) == 0 {
return true, nil
}
//目标替换对象
menuId2 := list2[0]["menu_id"].(string)
sortId2 := list2[0]["sort_id"].(int32)
//更新
//1、清除Redis缓存
var ids = []string{menuId, menuId2}
var selector = SqlKit.GetBean("t_base_menu")
SqlKit.DeleteCacheByIds(ids, selector)
sql = `update t_base_menu set sort_id=? where menu_id=?`
db.SQL(sql, sortId2, menuId).Execute()
db.SQL(sql, sortId, menuId2).Execute()
return true, nil
}