237 lines
7.6 KiB
Go
237 lines
7.6 KiB
Go
package storeAdmin
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"server/internal/consts"
|
|
"server/internal/dao"
|
|
"server/internal/model"
|
|
"server/internal/model/do"
|
|
"server/internal/model/entity"
|
|
"server/internal/service"
|
|
"server/utility/encrypt"
|
|
|
|
"server/utility/ecode"
|
|
"server/utility/jwt"
|
|
)
|
|
|
|
type sStoreAdmin struct {
|
|
}
|
|
|
|
func New() service.IStoreAdmin {
|
|
return &sStoreAdmin{}
|
|
}
|
|
|
|
func init() {
|
|
service.RegisterStoreAdmin(New())
|
|
go checkStoreAdminRole()
|
|
}
|
|
|
|
func checkStoreAdminRole() {
|
|
ctx := context.Background()
|
|
exist, err := dao.Roles.Ctx(ctx).Where(do.Roles{Code: consts.StoreRoleCode}).Exist()
|
|
if err != nil {
|
|
return
|
|
}
|
|
if !exist {
|
|
_, err = dao.Roles.Ctx(ctx).Insert(do.Roles{
|
|
Name: "门店管理员",
|
|
Code: consts.StoreRoleCode,
|
|
Description: "门店管理员角色",
|
|
Status: consts.StoreAdminEnable,
|
|
IsDeletable: false,
|
|
})
|
|
if err != nil {
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
func (s *sStoreAdmin) Login(ctx context.Context, in *model.StoreAdminLoginIn) (out *model.StoreAdminLoginOut, err error) {
|
|
one, err := dao.StoreAdmins.Ctx(ctx).Where(do.StoreAdmins{Username: in.Username}).One()
|
|
if err != nil {
|
|
return nil, ecode.Fail.Sub("查询商户管理员失败")
|
|
}
|
|
if one.IsEmpty() {
|
|
return nil, ecode.Params.Sub("该用户不存在")
|
|
}
|
|
if one[dao.StoreAdmins.Columns().Status].Int() == consts.StoreAdminDisable {
|
|
return nil, ecode.Params.Sub("该用户已被禁用")
|
|
}
|
|
if !encrypt.ComparePassword(one[dao.StoreAdmins.Columns().PasswordHash].String(), in.Password) {
|
|
return nil, ecode.Params.Sub("密码错误")
|
|
}
|
|
var store entity.Stores
|
|
if err = dao.Stores.Ctx(ctx).WherePri(one[dao.StoreAdmins.Columns().StoreId]).Scan(&store); err != nil {
|
|
return nil, ecode.Fail.Sub("查询门店失败")
|
|
}
|
|
|
|
value, err := dao.Roles.Ctx(ctx).WherePri(one[dao.StoreAdmins.Columns().RoleId].Int()).Fields(dao.Roles.Columns().Code).Value()
|
|
if err != nil {
|
|
return nil, ecode.Fail.Sub("查询角色失败")
|
|
}
|
|
|
|
token, err := jwt.GenerateToken(&jwt.TokenIn{UserId: one[dao.StoreAdmins.Columns().Id].Int64(), Role: value.String()})
|
|
if err != nil {
|
|
return nil, ecode.Fail.Sub("生成token失败")
|
|
}
|
|
out = &model.StoreAdminLoginOut{
|
|
Token: token,
|
|
StoreId: store.Id,
|
|
StoreName: store.Name,
|
|
NetbarAccount: store.NetbarAccount,
|
|
}
|
|
return
|
|
}
|
|
func (s *sStoreAdmin) Info(ctx context.Context, in *model.StoreAdminInfoIn) (out *model.StoreAdminInfoOut, 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("该用户不存在")
|
|
}
|
|
var storeAdmin entity.StoreAdmins
|
|
err = dao.StoreAdmins.Ctx(ctx).WherePri(in.StoreAdminId).Scan(&storeAdmin)
|
|
if err != nil {
|
|
return nil, ecode.Fail.Sub("查询门店管理员失败")
|
|
}
|
|
out = &model.StoreAdminInfoOut{
|
|
Id: storeAdmin.Id,
|
|
StoreId: storeAdmin.StoreId,
|
|
Username: storeAdmin.Username,
|
|
RealName: storeAdmin.RealName,
|
|
IsPrimary: storeAdmin.IsPrimary,
|
|
}
|
|
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
|
|
}
|