cutego/modules/core/dao/user_role_dao.go

78 lines
1.6 KiB
Go
Raw Normal View History

2022-03-01 13:50:13 +08:00
package dao
import (
2023-01-18 15:40:27 +08:00
"cutego/modules/core/api/v1/request"
2023-01-18 17:09:49 +08:00
"cutego/modules/core/entity"
2022-03-01 13:50:13 +08:00
"cutego/pkg/common"
2023-01-18 17:09:49 +08:00
"cutego/refs"
2022-03-01 13:50:13 +08:00
)
type UserRoleDao struct {
}
// BatchInsert 批量新增用户角色信息
2023-01-18 17:09:49 +08:00
func (d UserRoleDao) BatchInsert(roles []entity.SysUserRole) {
session := refs.SqlDB.NewSession()
2022-03-01 13:50:13 +08:00
session.Begin()
2023-01-18 17:09:49 +08:00
_, err := session.Table(entity.SysUserRole{}.TableName()).Insert(&roles)
2022-03-01 13:50:13 +08:00
if err != nil {
common.ErrorLog(err)
session.Rollback()
return
}
session.Commit()
}
// Delete 删除用户和角色关系
func (d UserRoleDao) Delete(id int64) {
2023-01-18 17:09:49 +08:00
role := entity.SysUserRole{
2022-03-01 13:50:13 +08:00
UserId: id,
}
2023-01-18 17:09:49 +08:00
session := refs.SqlDB.NewSession()
2022-03-01 13:50:13 +08:00
session.Begin()
_, err := session.Delete(&role)
if err != nil {
common.ErrorLog(err)
session.Rollback()
return
}
session.Commit()
}
// DeleteAuthUser 取消用户授权
2023-01-18 17:09:49 +08:00
func (d UserRoleDao) DeleteAuthUser(role entity.SysUserRole) int64 {
session := refs.SqlDB.NewSession()
2022-03-01 13:50:13 +08:00
session.Begin()
i, err := session.Delete(&role)
if err != nil {
common.ErrorLog(err)
session.Rollback()
return 0
}
session.Commit()
return i
}
// InsertAuthUsers 批量选择用户授权
func (d UserRoleDao) InsertAuthUsers(body request.UserRoleBody) int64 {
ids := body.UserIds
2023-01-18 17:09:49 +08:00
roles := make([]entity.SysUserRole, 0)
2022-03-01 13:50:13 +08:00
for i := 0; i < len(ids); i++ {
2023-01-18 17:09:49 +08:00
role := entity.SysUserRole{
2022-03-01 13:50:13 +08:00
RoleId: body.RoleId,
UserId: ids[i],
}
roles = append(roles, role)
}
2023-01-18 17:09:49 +08:00
session := refs.SqlDB.NewSession()
2022-03-01 13:50:13 +08:00
session.Begin()
insert, err := session.Insert(&roles)
if err != nil {
common.ErrorLog(err)
session.Rollback()
return 0
}
session.Commit()
return insert
}