This commit is contained in:
2025-07-07 20:11:59 +08:00
parent ab0fdbc447
commit 06e3aa2eb3
2009 changed files with 193082 additions and 0 deletions

View File

@ -0,0 +1,221 @@
// ==========================================================================
// GFast自动生成logic操作代码。
// 生成日期2024-05-28 15:11:37
// 生成路径: internal/app/system/logic/app_user_roles.go
// 生成人gfast
// desc:app用户角色关联
// company:云南奇讯科技有限公司
// ==========================================================================
package logic
import (
"context"
"fmt"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/util/gconv"
"github.com/tiger1103/gfast/v3/api/v1/system"
"github.com/tiger1103/gfast/v3/internal/app/system/consts"
"github.com/tiger1103/gfast/v3/internal/app/system/dao"
"github.com/tiger1103/gfast/v3/internal/app/system/model"
"github.com/tiger1103/gfast/v3/internal/app/system/model/do"
"github.com/tiger1103/gfast/v3/internal/app/system/service"
"github.com/tiger1103/gfast/v3/library/liberr"
)
func init() {
service.RegisterAppUserRoles(New())
}
func New() *sAppUserRoles {
return &sAppUserRoles{}
}
type sAppUserRoles struct{}
func (s *sAppUserRoles) List(ctx context.Context, req *system.AppUserRolesSearchReq) (listRes *system.AppUserRolesSearchRes, err error) {
listRes = new(system.AppUserRolesSearchRes)
err = g.Try(ctx, func(ctx context.Context) {
m := dao.AppUserRoles.Ctx(ctx).WithAll()
if req.Id != "" {
m = m.Where(dao.AppUserRoles.Columns().Id+" = ?", req.Id)
}
if req.UserId != "" {
m = m.Where(dao.AppUserRoles.Columns().UserId+" = ?", gconv.Int(req.UserId))
}
if req.RoleId != "" {
m = m.Where(dao.AppUserRoles.Columns().RoleId+" = ?", gconv.Int(req.RoleId))
}
if len(req.DateRange) != 0 {
m = m.Where(dao.AppUserRoles.Columns().CreatedAt+" >=? AND "+dao.AppUserRoles.Columns().CreatedAt+" <=?", req.DateRange[0], req.DateRange[1])
}
listRes.Total, err = m.Count()
liberr.ErrIsNil(ctx, err, "获取总行数失败")
if req.PageNum == 0 {
req.PageNum = 1
}
listRes.CurrentPage = req.PageNum
if req.PageSize == 0 {
req.PageSize = consts.PageSize
}
order := "id asc"
if req.OrderBy != "" {
order = req.OrderBy
}
var res []*model.AppUserRolesInfoRes
err = m.Fields(system.AppUserRolesSearchRes{}).Page(req.PageNum, req.PageSize).Order(order).Scan(&res)
liberr.ErrIsNil(ctx, err, "获取数据失败")
listRes.List = make([]*model.AppUserRolesListRes, len(res))
for k, v := range res {
listRes.List[k] = &model.AppUserRolesListRes{
Id: v.Id,
UserId: v.UserId,
RoleId: v.RoleId,
CreatedAt: v.CreatedAt,
}
}
})
return
}
func (s *sAppUserRoles) GetById(ctx context.Context, id uint) (res *model.AppUserRolesInfoRes, err error) {
err = g.Try(ctx, func(ctx context.Context) {
err = dao.AppUserRoles.Ctx(ctx).WithAll().Where(dao.AppUserRoles.Columns().Id, id).Scan(&res)
liberr.ErrIsNil(ctx, err, "获取信息失败")
})
return
}
func (s *sAppUserRoles) Add(ctx context.Context, req *system.AppUserRolesAddReq) (err error) {
err = g.Try(ctx, func(ctx context.Context) {
_, err = dao.AppUserRoles.Ctx(ctx).Insert(do.AppUserRoles{
UserId: req.UserId,
RoleId: req.RoleId,
})
liberr.ErrIsNil(ctx, err, "添加失败")
})
return
}
func (s *sAppUserRoles) Edit(ctx context.Context, req *system.AppUserRolesEditReq) (err error) {
err = g.Try(ctx, func(ctx context.Context) {
_, err = dao.AppUserRoles.Ctx(ctx).WherePri(req.Id).Update(do.AppUserRoles{
UserId: req.UserId,
RoleId: req.RoleId,
})
liberr.ErrIsNil(ctx, err, "修改失败")
})
return
}
func (s *sAppUserRoles) Delete(ctx context.Context, ids []uint) (err error) {
err = g.Try(ctx, func(ctx context.Context) {
_, err = dao.AppUserRoles.Ctx(ctx).Delete(dao.AppUserRoles.Columns().Id+" in (?)", ids)
liberr.ErrIsNil(ctx, err, "删除失败")
})
return
}
func (s *sAppUserRoles) BindRole(ctx context.Context, req *system.AppUserRolesBindReq) error {
return g.Try(ctx, func(ctx context.Context) {
bindOrDeleteRoles := func(roles []int, majorRole int) error {
// 删除用户当前绑定的所有指定类型的角色
_, err := dao.AppUserRoles.Ctx(ctx).Where(dao.AppUserRoles.Columns().UserId, req.UserId).
Where(dao.AppUserRoles.Columns().MajorRole, majorRole).Delete()
if err != nil {
return err
}
if len(roles) > 0 {
data := make([]do.AppUserRoles, 0, len(roles))
for _, v := range roles {
data = append(data, do.AppUserRoles{
UserId: req.UserId,
RoleId: v,
MajorRole: majorRole,
})
}
_, err = dao.AppUserRoles.Ctx(ctx).Data(data).Insert()
if err != nil {
return err
}
}
return nil
}
if len(req.ConstructionRole) == 0 && len(req.AdminRole) == 0 {
liberr.ErrIsNil(ctx, fmt.Errorf("至少需要绑定一个角色"))
}
// select sptm.post_id
// from sys_user as su
// left join bus_construction_user bcu on su.mobile = bcu.phone
// left join sys_project_team_member sptm on sptm.openid = bcu.openid
// where su.id = 145 ;
//var user struct {
// PostId int `orm:"post_id"`
//}
//err := dao.SysUser.Ctx(ctx).As("su").
// LeftJoin("bus_construction_user bcu", "su.mobile = bcu.phone").
// LeftJoin("sys_project_team_member sptm", "sptm.openid = bcu.openid").
// Fields("sptm.post_id").
// Where("su.id", req.UserId).Scan(&user)
//liberr.ErrIsNil(ctx, err, "该用户没有加入班组")
//
//// 如果用户的 postID != 10 且本次绑定的角色为班组长,则报错
//if user.PostId != 10 && lo.Contains(req.ConstructionRole, 1) {
// liberr.ErrIsNil(ctx, fmt.Errorf("该用户不是班组长,不能为其绑定班组长角色菜单"))
//}
// 绑定管理员角色
err := bindOrDeleteRoles(req.AdminRole, 0)
liberr.ErrIsNil(ctx, err, "绑定或删除管理员角色失败")
// 绑定施工人员角色
err = bindOrDeleteRoles(req.ConstructionRole, 1)
liberr.ErrIsNil(ctx, err, "绑定或删除施工人员角色失败")
result, err := g.Redis().Do(ctx, "KEYS", fmt.Sprintf("gfToken:%d-*", req.UserId))
liberr.ErrIsNil(ctx, err, "获取Token失败")
for _, key := range result.Array() {
g.Redis().Do(ctx, "DEL", key)
}
})
}
// wrapProjectTeamMemberEdit 项目组成员
// func wrapProjectTeamMemberEdit(ctx context.Context, userID int) error {
// // OpenID
// openID := lo.Must(dao.BusConstructionUser.Ctx(ctx).As("bcu").
// LeftJoin("sys_user su", "bcu.phone = su.mobile").
// Fields("bcu.openid").
// Where("su.id", userID).Value()).String()
// // TeamID
// teamID := lo.Must(sdao.SysProjectTeamMember.Ctx(ctx).As("sptm").
// Fields("sptm.team_id").
// Where("sptm.openid", openID).Value()).Int()
// }
func (s *sAppUserRoles) GetUserRoles(ctx context.Context, userID int) (roles model.AppUserRoleDetails, err error) {
err = g.Try(ctx, func(ctx context.Context) {
// TODO: 分为两大类,一类是施工人员,一类是管理人员
// 施工人员:班组长、普通人员
// 管理人员:项目管理员、公司管理员
// select ar.role_id,ar.role_name,aur.user_id
// from app_user_roles aur
// join app_roles ar on aur.role_id = ar.role_id
// where aur.user_id = 144;
err = dao.AppUserRoles.Ctx(ctx).As("aur").
InnerJoin("app_roles ar", "aur.role_id = ar.role_id").
Fields("ar.role_id,ar.role_name,aur.user_id").
Where("aur.user_id", userID).
Scan(&roles.Rules)
liberr.ErrIsNil(ctx, err, "获取用户角色失败")
})
return
}