hotgo/server/internal/logic/admin/post.go

215 lines
4.9 KiB
Go
Raw Normal View History

2022-11-24 23:37:34 +08:00
// Package admin
// @Link https://github.com/bufanyun/hotgo
// @Copyright Copyright (c) 2023 HotGo CLI
2022-11-24 23:37:34 +08:00
// @Author Ms <133814250@qq.com>
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
package admin
2022-02-25 17:11:17 +08:00
import (
"context"
"github.com/gogf/gf/v2/errors/gerror"
2022-11-24 23:37:34 +08:00
"hotgo/internal/consts"
"hotgo/internal/dao"
"hotgo/internal/model/input/adminin"
"hotgo/internal/model/input/form"
2022-11-24 23:37:34 +08:00
"hotgo/internal/service"
"hotgo/utility/validate"
2022-02-25 17:11:17 +08:00
)
2022-11-24 23:37:34 +08:00
type sAdminPost struct{}
2022-02-25 17:11:17 +08:00
2022-11-24 23:37:34 +08:00
func NewAdminPost() *sAdminPost {
return &sAdminPost{}
}
2022-02-25 17:11:17 +08:00
2022-11-24 23:37:34 +08:00
func init() {
service.RegisterAdminPost(NewAdminPost())
}
2022-02-25 17:11:17 +08:00
2022-11-24 23:37:34 +08:00
// Delete 删除
func (s *sAdminPost) Delete(ctx context.Context, in adminin.PostDeleteInp) (err error) {
2022-02-25 17:11:17 +08:00
exist, err := dao.AdminMemberPost.Ctx(ctx).Where("post_id", in.Id).One()
if err != nil {
err = gerror.Wrap(err, consts.ErrorORM)
return err
}
2022-02-25 17:11:17 +08:00
if !exist.IsEmpty() {
return gerror.New("请先解除该岗位下所有已关联用户关联关系!")
}
_, err = dao.AdminPost.Ctx(ctx).Where("id", in.Id).Delete()
return
2022-02-25 17:11:17 +08:00
}
2022-11-24 23:37:34 +08:00
// Edit 修改/新增
func (s *sAdminPost) Edit(ctx context.Context, in adminin.PostEditInp) (err error) {
2022-02-25 17:11:17 +08:00
if in.Name == "" {
err = gerror.New("名称不能为空")
return
2022-02-25 17:11:17 +08:00
}
if in.Code == "" {
err = gerror.New("编码不能为空")
return
2022-02-25 17:11:17 +08:00
}
uniqueName, err := dao.AdminPost.IsUniqueName(ctx, in.Id, in.Name)
if err != nil {
err = gerror.Wrap(err, consts.ErrorORM)
return
2022-02-25 17:11:17 +08:00
}
if !uniqueName {
err = gerror.New("名称已存在")
return
2022-02-25 17:11:17 +08:00
}
uniqueCode, err := dao.AdminPost.IsUniqueCode(ctx, in.Id, in.Code)
if err != nil {
err = gerror.Wrap(err, consts.ErrorORM)
return
2022-02-25 17:11:17 +08:00
}
if !uniqueCode {
err = gerror.New("编码已存在")
return
2022-02-25 17:11:17 +08:00
}
// 修改
if in.Id > 0 {
_, err = dao.AdminPost.Ctx(ctx).Where("id", in.Id).Data(in).Update()
return
2022-02-25 17:11:17 +08:00
}
// 新增
_, err = dao.AdminPost.Ctx(ctx).Data(in).Insert()
return
2022-02-25 17:11:17 +08:00
}
2022-11-24 23:37:34 +08:00
// MaxSort 最大排序
func (s *sAdminPost) MaxSort(ctx context.Context, in adminin.PostMaxSortInp) (res *adminin.PostMaxSortModel, err error) {
2022-02-25 17:11:17 +08:00
if in.Id > 0 {
if err = dao.AdminMenu.Ctx(ctx).Where("id", in.Id).Order("sort desc").Scan(&res); err != nil {
2022-02-25 17:11:17 +08:00
err = gerror.Wrap(err, consts.ErrorORM)
return
2022-02-25 17:11:17 +08:00
}
}
if res == nil {
res = new(adminin.PostMaxSortModel)
}
res.Sort = form.DefaultMaxSort(ctx, res.Sort)
return
2022-02-25 17:11:17 +08:00
}
2022-11-24 23:37:34 +08:00
// NameUnique 菜单名称是否唯一
func (s *sAdminPost) NameUnique(ctx context.Context, in adminin.PostNameUniqueInp) (res *adminin.PostNameUniqueModel, err error) {
2022-02-25 17:11:17 +08:00
isUnique, err := dao.AdminPost.IsUniqueName(ctx, in.Id, in.Name)
if err != nil {
err = gerror.Wrap(err, consts.ErrorORM)
return
2022-02-25 17:11:17 +08:00
}
res = new(adminin.PostNameUniqueModel)
2022-02-25 17:11:17 +08:00
res.IsUnique = isUnique
return
2022-02-25 17:11:17 +08:00
}
2022-11-24 23:37:34 +08:00
// CodeUnique 编码是否唯一
func (s *sAdminPost) CodeUnique(ctx context.Context, in adminin.PostCodeUniqueInp) (res *adminin.PostCodeUniqueModel, err error) {
2022-02-25 17:11:17 +08:00
isUnique, err := dao.AdminPost.IsUniqueCode(ctx, in.Id, in.Code)
if err != nil {
err = gerror.Wrap(err, consts.ErrorORM)
return
2022-02-25 17:11:17 +08:00
}
res = new(adminin.PostCodeUniqueModel)
2022-02-25 17:11:17 +08:00
res.IsUnique = isUnique
return
2022-02-25 17:11:17 +08:00
}
2022-11-24 23:37:34 +08:00
// View 获取指定字典类型信息
func (s *sAdminPost) View(ctx context.Context, in adminin.PostViewInp) (res *adminin.PostViewModel, err error) {
err = dao.AdminPost.Ctx(ctx).Where("id", in.Id).Scan(&res)
return
2022-02-25 17:11:17 +08:00
}
2022-11-24 23:37:34 +08:00
// List 获取列表
func (s *sAdminPost) List(ctx context.Context, in adminin.PostListInp) (list []*adminin.PostListModel, totalCount int, err error) {
2022-02-25 17:11:17 +08:00
mod := dao.AdminPost.Ctx(ctx)
// 访问路径
if in.Name != "" {
mod = mod.WhereLike("name", "%"+in.Name+"%")
}
// 模块
if in.Code != "" {
mod = mod.Where("code", in.Code)
}
// 请求方式
if in.Status > 0 {
mod = mod.Where("status", in.Status)
}
totalCount, err = mod.Count()
if err != nil {
err = gerror.Wrap(err, consts.ErrorORM)
return
2022-02-25 17:11:17 +08:00
}
2022-11-24 23:37:34 +08:00
if totalCount == 0 {
return list, totalCount, nil
}
err = mod.Page(in.Page, in.PerPage).Order("id asc").Scan(&list)
return
2022-02-25 17:11:17 +08:00
}
2022-11-24 23:37:34 +08:00
// GetMemberByStartName 获取指定用户的第一岗位
func (s *sAdminPost) GetMemberByStartName(ctx context.Context, memberId int64) (name string, err error) {
// 默认取第一岗位
2022-02-25 17:11:17 +08:00
postId, err := dao.AdminMemberPost.Ctx(ctx).
Fields("post_id").
Where("member_id", memberId).
Limit(1).
Value()
if err != nil {
err = gerror.Wrap(err, consts.ErrorORM)
return
2022-02-25 17:11:17 +08:00
}
val, err := dao.AdminPost.Ctx(ctx).
Fields("name").
Where("id", postId.Int()).
Order("id desc").
Value()
if err != nil {
err = gerror.Wrap(err, consts.ErrorORM)
return
2022-02-25 17:11:17 +08:00
}
return val.String(), nil
}
2022-11-24 23:37:34 +08:00
// Status 更新状态
func (s *sAdminPost) Status(ctx context.Context, in adminin.PostStatusInp) (err error) {
if in.Id <= 0 {
err = gerror.New("ID不能为空")
return
2022-11-24 23:37:34 +08:00
}
if in.Status <= 0 {
err = gerror.New("状态不能为空")
return
2022-11-24 23:37:34 +08:00
}
if !validate.InSliceInt(consts.StatusSlice, in.Status) {
2022-11-24 23:37:34 +08:00
err = gerror.New("状态不正确")
return
2022-11-24 23:37:34 +08:00
}
// 修改
_, err = dao.AdminPost.Ctx(ctx).Where("id", in.Id).Data("status", in.Status).Update()
return
2022-11-24 23:37:34 +08:00
}