实现了门店员工、员工角色的接口开发
This commit is contained in:
@ -2,6 +2,7 @@ package storeAdmin
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"server/internal/consts"
|
||||
"server/internal/dao"
|
||||
"server/internal/model"
|
||||
@ -92,3 +93,132 @@ func (s *sStoreAdmin) Info(ctx context.Context, in *model.StoreAdminInfoIn) (out
|
||||
}
|
||||
return
|
||||
}
|
||||
func (s *sStoreAdmin) List(ctx context.Context, in *model.StoreAdminListIn) (out *model.StoreAdminListOut, err error) {
|
||||
list := make([]model.StoreAdmin, 0)
|
||||
var total int
|
||||
if err = dao.StoreAdmins.Ctx(ctx).LeftJoin(
|
||||
dao.StoreRoles.Table(),
|
||||
fmt.Sprintf("%s.%s = %s.%s", dao.StoreAdmins.Table(), dao.StoreAdmins.Columns().StoreRoleId, dao.StoreRoles.Table(), dao.StoreRoles.Columns().Id),
|
||||
).Fields(
|
||||
fmt.Sprintf("%s.*, %s.name as store_role_name", dao.StoreAdmins.Table(), dao.StoreRoles.Table()),
|
||||
).Page(in.Page, in.Size).Where(do.StoreAdmins{StoreId: in.StoreId}).ScanAndCount(&list, &total, false); err != nil {
|
||||
return nil, ecode.Fail.Sub("查询门店管理员失败")
|
||||
}
|
||||
return &model.StoreAdminListOut{
|
||||
List: list,
|
||||
Total: total,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *sStoreAdmin) Create(ctx context.Context, in *model.StoreAdminCreateIn) (out *model.CreateOut, err error) {
|
||||
exist, err := dao.StoreAdmins.Ctx(ctx).Where(do.StoreAdmins{Username: in.Username}).Exist()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("查询商户管理员失败")
|
||||
}
|
||||
if exist {
|
||||
return nil, ecode.Fail.Sub("该用户名已被注册")
|
||||
}
|
||||
passwordHash, err := encrypt.EncryptPassword(in.Password)
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("密码加密失败")
|
||||
}
|
||||
count, err := dao.StoreRoles.Ctx(ctx).Where(do.StoreRoles{Id: in.StoreRoleId}).Count()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("查询角色失败")
|
||||
}
|
||||
if count == 0 {
|
||||
return nil, ecode.Params.Sub("角色不存在")
|
||||
}
|
||||
value, err := dao.Roles.Ctx(ctx).Where(do.Roles{Code: consts.StoreRoleCode}).Fields(dao.Roles.Columns().Id).Value()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("查询角色失败")
|
||||
}
|
||||
id, err := dao.StoreAdmins.Ctx(ctx).Data(do.StoreAdmins{
|
||||
Username: in.Username,
|
||||
RealName: in.RealName,
|
||||
PasswordHash: passwordHash,
|
||||
StoreId: in.StoreId,
|
||||
Phone: in.Phone,
|
||||
StoreRoleId: in.StoreRoleId,
|
||||
Status: consts.StoreAdminEnable,
|
||||
RoleId: value.Int(),
|
||||
IsPrimary: false,
|
||||
}).InsertAndGetId()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("插入商户管理员数据失败")
|
||||
}
|
||||
return &model.CreateOut{
|
||||
Id: id,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *sStoreAdmin) Update(ctx context.Context, in *model.StoreAdminUpdateIn) (out *model.UpdateOut, err error) {
|
||||
exist, err := dao.StoreAdmins.Ctx(ctx).WherePri(in.StoreAdminId).Exist()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("查询商户管理员失败")
|
||||
}
|
||||
if !exist {
|
||||
return nil, ecode.Params.Sub("该用户不存在")
|
||||
}
|
||||
if _, err = dao.StoreAdmins.Ctx(ctx).WherePri(in.StoreAdminId).Data(do.StoreAdmins{
|
||||
RealName: in.RealName,
|
||||
Phone: in.Phone,
|
||||
StoreRoleId: in.StoreRoleId,
|
||||
}).Update(); err != nil {
|
||||
return nil, ecode.Fail.Sub("更新商户管理员数据失败")
|
||||
}
|
||||
return &model.UpdateOut{
|
||||
Success: true,
|
||||
}, nil
|
||||
}
|
||||
func (s *sStoreAdmin) EditPassword(ctx context.Context, in *model.StoreAdminEditPasswordIn) (out *model.UpdateOut, err error) {
|
||||
exist, err := dao.StoreAdmins.Ctx(ctx).WherePri(in.StoreAdminId).Exist()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("查询商户管理员失败")
|
||||
}
|
||||
if !exist {
|
||||
return nil, ecode.Params.Sub("该用户不存在")
|
||||
}
|
||||
value, err := dao.StoreAdmins.Ctx(ctx).WherePri(in.StoreAdminId).Fields(dao.StoreAdmins.Columns().PasswordHash).Value()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("查询商户管理员失败")
|
||||
}
|
||||
if !encrypt.ComparePassword(value.String(), in.Password) {
|
||||
return nil, ecode.Params.Sub("密码错误")
|
||||
}
|
||||
newPassHash, err := encrypt.EncryptPassword(in.Password2)
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("密码加密失败")
|
||||
}
|
||||
if _, err = dao.StoreAdmins.Ctx(ctx).WherePri(in.StoreAdminId).Data(do.StoreAdmins{
|
||||
PasswordHash: newPassHash,
|
||||
}).Update(); err != nil {
|
||||
return nil, ecode.Fail.Sub("更新商户管理员数据失败")
|
||||
}
|
||||
return &model.UpdateOut{
|
||||
Success: true,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *sStoreAdmin) Delete(ctx context.Context, in *model.StoreAdminDeleteIn) (out *model.DeleteOut, err error) {
|
||||
exist, err := dao.StoreAdmins.Ctx(ctx).WherePri(in.StoreAdminId).Exist()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("查询商户管理员失败")
|
||||
}
|
||||
if !exist {
|
||||
return nil, ecode.Params.Sub("该用户不存在")
|
||||
}
|
||||
value, err := dao.StoreAdmins.Ctx(ctx).WherePri(in.StoreAdminId).Fields(dao.StoreAdmins.Columns().IsPrimary).Value()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("查询商户管理员失败")
|
||||
}
|
||||
if value.Bool() {
|
||||
return nil, ecode.Params.Sub("该用户为门店管理员,不能删除")
|
||||
}
|
||||
if _, err = dao.StoreAdmins.Ctx(ctx).WherePri(in.StoreAdminId).Delete(); err != nil {
|
||||
return nil, ecode.Fail.Sub("删除商户管理员失败")
|
||||
}
|
||||
return &model.DeleteOut{
|
||||
Success: true,
|
||||
}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user