230 lines
7.1 KiB
Go
230 lines
7.1 KiB
Go
package store
|
||
|
||
import (
|
||
"context"
|
||
"github.com/gogf/gf/v2/database/gdb"
|
||
"github.com/gogf/gf/v2/errors/gerror"
|
||
"server/internal/consts"
|
||
"server/internal/dao"
|
||
"server/internal/model"
|
||
"server/internal/model/do"
|
||
"server/internal/service"
|
||
"server/utility/ecode"
|
||
"server/utility/snowid"
|
||
)
|
||
|
||
type sStore struct {
|
||
}
|
||
|
||
func New() service.IStore {
|
||
return &sStore{}
|
||
}
|
||
func init() {
|
||
service.RegisterStore(New())
|
||
}
|
||
func (s *sStore) List(ctx context.Context, in *model.StoreListIn) (out *model.StoreListOut, err error) {
|
||
// 1. 初始化返回数据
|
||
list := make([]model.Store, 0)
|
||
var total int
|
||
if err = dao.Stores.Ctx(ctx).Page(in.Page, in.Size).ScanAndCount(&list, &total, false); err != nil {
|
||
return nil, ecode.Fail.Sub("门店列表获取失败")
|
||
}
|
||
return &model.StoreListOut{
|
||
List: list,
|
||
Total: total,
|
||
}, nil
|
||
}
|
||
|
||
func (s *sStore) Create(ctx context.Context, in *model.StoreCreateIn) (out *model.CreateOut, err error) {
|
||
out = &model.CreateOut{}
|
||
// 1. 检查门店名称是否重复
|
||
exist, err := dao.Stores.Ctx(ctx).Where(do.Stores{Name: in.Name}).Exist()
|
||
if err != nil {
|
||
return nil, ecode.Fail.Sub("新增门店查重出现异常")
|
||
}
|
||
if exist {
|
||
return nil, ecode.Params.Sub("门店名称已存在")
|
||
}
|
||
|
||
// 2. 使用事务处理数据插入
|
||
if err = dao.Stores.Transaction(ctx, func(ctx context.Context, tx gdb.TX) (err error) {
|
||
// 2.1 根据商户 id 获取所属的商户
|
||
value, err := dao.MerchantAdmins.Ctx(ctx).WherePri(in.OperatorId).Fields(dao.MerchantAdmins.Columns().MerchantId).Value()
|
||
if err != nil {
|
||
return ecode.Fail.Sub("获取商户ID出现异常")
|
||
}
|
||
// 2,2 生成门店的相关信息
|
||
code, err := snowid.GetSnowClient().GenerateStoreCode()
|
||
if err != nil {
|
||
return ecode.Fail.Sub("生成门店ID出现异常")
|
||
}
|
||
// 2.3 插入门店数据
|
||
id, err := tx.Model(dao.Stores.Table()).Data(do.Stores{
|
||
MerchantId: value.Int(),
|
||
StoreCode: code,
|
||
Name: in.Name,
|
||
Address: in.Address,
|
||
ContactName: in.ContactName,
|
||
ContactPhone: in.ContactPhone,
|
||
}).InsertAndGetId()
|
||
if err != nil {
|
||
return ecode.Fail.Sub("新增门店出现异常")
|
||
}
|
||
|
||
// TODO 生成门店主管理账号,密码通知方式
|
||
|
||
// 2.5 插入门店管理员数据
|
||
_, err = tx.Model(dao.StoreAdmins.Table()).Data(do.StoreAdmins{
|
||
StoreId: id,
|
||
}).Insert()
|
||
if err != nil {
|
||
return ecode.Fail.Sub("新增门店管理员出现异常")
|
||
}
|
||
out.Id = id
|
||
return nil
|
||
}); err != nil {
|
||
return nil, err
|
||
}
|
||
return
|
||
}
|
||
|
||
func (s *sStore) Update(ctx context.Context, in *model.StoreUpdateIn) (out *model.UpdateOut, err error) {
|
||
exist, err := dao.Stores.Ctx(ctx).WherePri(in.Id).Exist()
|
||
if err != nil {
|
||
return nil, ecode.Fail.Sub("查询门店出现异常")
|
||
}
|
||
if !exist {
|
||
return nil, ecode.Params.Sub("门店不存在")
|
||
}
|
||
|
||
if in.OperatorId == 0 || (in.OperatorRole != consts.MerchantRoleCode && in.OperatorRole != consts.StoreRoleCode) {
|
||
return nil, ecode.InvalidOperation.Sub("无权限操作")
|
||
}
|
||
|
||
if in.Name != "" {
|
||
exist, err = dao.Stores.Ctx(ctx).Where(dao.Stores.Columns().Name, in.Name).WhereNot(dao.Stores.Columns().Id, in.Id).Exist()
|
||
if err != nil {
|
||
return nil, ecode.Fail.Sub("检查门店名称出现异常")
|
||
}
|
||
if exist {
|
||
return nil, ecode.Params.Sub("门店名称已存在")
|
||
}
|
||
}
|
||
|
||
_, err = dao.Stores.Ctx(ctx).WherePri(in.Id).Data(do.Stores{
|
||
Name: in.Name,
|
||
Address: in.Address,
|
||
ContactName: in.ContactName,
|
||
ContactPhone: in.ContactPhone, // TODO:暂定可以直接修改,后续可能需要验证手机号
|
||
Status: in.Status,
|
||
}).Update()
|
||
if err != nil {
|
||
return nil, ecode.Fail.Sub("更新门店出现异常")
|
||
}
|
||
return &model.UpdateOut{Success: true}, nil
|
||
}
|
||
|
||
func (s *sStore) Delete(ctx context.Context, in *model.StoreDeleteIn) (out *model.DeleteOut, err error) {
|
||
exist, err := dao.Stores.Ctx(ctx).WherePri(in.Id).Exist()
|
||
if err != nil {
|
||
return nil, ecode.Fail.Sub("查询门店出现异常")
|
||
}
|
||
if !exist {
|
||
return nil, ecode.Params.Sub("门店不存在")
|
||
}
|
||
|
||
if in.OperatorId == 0 || (in.OperatorRole != consts.MerchantRoleCode) {
|
||
return nil, ecode.InvalidOperation.Sub("无权限操作")
|
||
}
|
||
// TODO: 是否需要检查门店下存在未发放奖励等
|
||
|
||
return &model.DeleteOut{Success: true}, nil
|
||
}
|
||
|
||
func (s *sStore) Info(ctx context.Context, in *model.StoreInfoIn) (out *model.StoreInfoOut, err error) {
|
||
exist, err := dao.Stores.Ctx(ctx).Where(do.Stores{Id: in.Id}).Exist()
|
||
if err != nil {
|
||
return nil, ecode.Fail.Sub("查询门店出现异常")
|
||
}
|
||
if !exist {
|
||
return nil, ecode.Params.Sub("门店不存在")
|
||
}
|
||
|
||
// TODO 获取门店信息等相关信息
|
||
return
|
||
}
|
||
|
||
func (s *sStore) GetDesktopSetting(ctx context.Context, in *model.StoreGetDesktopSettingIn) (*model.StoreGetDesktopSettingOut, error) {
|
||
// 检查门店桌面设置是否存在
|
||
exist, err := dao.StoreDesktopSettings.Ctx(ctx).Where(do.StoreDesktopSettings{StoreId: in.StoreId}).Count()
|
||
if err != nil {
|
||
return nil, gerror.Wrap(ecode.Fail, "查询门店桌面设置出现异常")
|
||
}
|
||
if exist == 0 {
|
||
return &model.StoreGetDesktopSettingOut{}, nil
|
||
}
|
||
|
||
// 查询桌面设置
|
||
out := &model.StoreGetDesktopSettingOut{}
|
||
err = dao.StoreDesktopSettings.Ctx(ctx).Where(do.StoreDesktopSettings{StoreId: in.StoreId}).Scan(out)
|
||
if err != nil {
|
||
return nil, gerror.Wrap(ecode.Fail, "查询门店桌面设置出现异常")
|
||
}
|
||
|
||
return out, nil
|
||
}
|
||
|
||
func (s *sStore) SaveDesktopSetting(ctx context.Context, in *model.SaveDesktopSettingIn) (*model.SaveDesktopSettingOut, error) {
|
||
out := &model.SaveDesktopSettingOut{}
|
||
err := dao.StoreDesktopSettings.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
|
||
// 构建数据,包含所有字段
|
||
data := do.StoreDesktopSettings{
|
||
Id: in.Id,
|
||
StoreId: in.StoreId,
|
||
BackgroundUrl: in.BackgroundUrl,
|
||
Resolution: in.Resolution,
|
||
IsTopWidgetVisible: in.IsTopWidgetVisible,
|
||
IsRightWidgetVisible: in.IsRightWidgetVisible,
|
||
}
|
||
|
||
// 检查记录是否存在(基于 store_id)
|
||
exist, err := dao.StoreDesktopSettings.Ctx(ctx).TX(tx).Where(do.StoreDesktopSettings{StoreId: in.StoreId}).Count()
|
||
if err != nil {
|
||
return gerror.Wrap(ecode.Fail, "检查门店桌面设置失败")
|
||
}
|
||
|
||
if exist == 0 {
|
||
// 新增
|
||
result, err := dao.StoreDesktopSettings.Ctx(ctx).TX(tx).Data(data).Insert()
|
||
if err != nil {
|
||
return gerror.Wrap(ecode.Fail, "新增门店桌面设置失败")
|
||
}
|
||
id, err := result.LastInsertId()
|
||
if err != nil {
|
||
return gerror.Wrap(ecode.Fail, "获取新增记录ID失败")
|
||
}
|
||
out.Id = id
|
||
} else {
|
||
// 更新(如果提供了 Id,则优先使用 Id 作为条件,否则使用 StoreId)
|
||
updateWhere := do.StoreDesktopSettings{StoreId: in.StoreId}
|
||
if in.Id > 0 {
|
||
updateWhere = do.StoreDesktopSettings{Id: in.Id}
|
||
}
|
||
_, err := dao.StoreDesktopSettings.Ctx(ctx).TX(tx).Data(data).Where(updateWhere).Update()
|
||
if err != nil {
|
||
return gerror.Wrap(ecode.Fail, "更新门店桌面设置失败")
|
||
}
|
||
id, err := dao.StoreDesktopSettings.Ctx(ctx).TX(tx).Where(updateWhere).Fields(dao.StoreDesktopSettings.Columns().Id).Value()
|
||
if err != nil {
|
||
return gerror.Wrap(ecode.Fail, "获取更新记录ID失败")
|
||
}
|
||
out.Id = id.Int64()
|
||
}
|
||
return nil
|
||
})
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
return out, nil
|
||
}
|