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:21:12 +08:00
"cutego/modules/core/dataobject"
2023-01-18 17:46:25 +08:00
"cutego/pkg/logging"
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:21:12 +08:00
func (d UserRoleDao) BatchInsert(roles []dataobject.SysUserRole) {
2023-01-18 17:09:49 +08:00
session := refs.SqlDB.NewSession()
2022-03-01 13:50:13 +08:00
session.Begin()
2023-01-18 17:21:12 +08:00
_, err := session.Table(dataobject.SysUserRole{}.TableName()).Insert(&roles)
2022-03-01 13:50:13 +08:00
if err != nil {
2023-01-18 17:46:25 +08:00
logging.ErrorLog(err)
2022-03-01 13:50:13 +08:00
session.Rollback()
return
}
session.Commit()
}
// Delete 删除用户和角色关系
func (d UserRoleDao) Delete(id int64) {
2023-01-18 17:21:12 +08:00
role := dataobject.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 {
2023-01-18 17:46:25 +08:00
logging.ErrorLog(err)
2022-03-01 13:50:13 +08:00
session.Rollback()
return
}
session.Commit()
}
// DeleteAuthUser 取消用户授权
2023-01-18 17:21:12 +08:00
func (d UserRoleDao) DeleteAuthUser(role dataobject.SysUserRole) int64 {
2023-01-18 17:09:49 +08:00
session := refs.SqlDB.NewSession()
2022-03-01 13:50:13 +08:00
session.Begin()
i, err := session.Delete(&role)
if err != nil {
2023-01-18 17:46:25 +08:00
logging.ErrorLog(err)
2022-03-01 13:50:13 +08:00
session.Rollback()
return 0
}
session.Commit()
return i
}
// InsertAuthUsers 批量选择用户授权
func (d UserRoleDao) InsertAuthUsers(body request.UserRoleBody) int64 {
ids := body.UserIds
2023-01-18 17:21:12 +08:00
roles := make([]dataobject.SysUserRole, 0)
2022-03-01 13:50:13 +08:00
for i := 0; i < len(ids); i++ {
2023-01-18 17:21:12 +08:00
role := dataobject.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 {
2023-01-18 17:46:25 +08:00
logging.ErrorLog(err)
2022-03-01 13:50:13 +08:00
session.Rollback()
return 0
}
session.Commit()
return insert
}