100 lines
2.7 KiB
Go
100 lines
2.7 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
"strconv"
|
|
|
|
"lone-services/pkg/modelbase"
|
|
"lone-services/pkg/utils"
|
|
"lone-services/pkg/validate"
|
|
sale "lone-services/rpc/sale/pb"
|
|
"lone-services/services/sale/internal/dao"
|
|
"lone-services/services/sale/internal/model"
|
|
"lone-services/services/sale/internal/svc"
|
|
"lone-services/services/sale/validator"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type StatusLogic struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
logx.Logger
|
|
}
|
|
|
|
func NewStatusLogic(ctx context.Context, svcCtx *svc.ServiceContext) *StatusLogic {
|
|
return &StatusLogic{
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
Logger: logx.WithContext(ctx),
|
|
}
|
|
}
|
|
|
|
func (l *StatusLogic) Status(in *sale.StatusReq) (*sale.Response, error) {
|
|
var req validator.StatusValidator
|
|
if msg := validate.ValidateFromProto(in, &req); msg != utils.StringEmpty {
|
|
return outResponse(utils.ErrorParams, msg), nil
|
|
}
|
|
|
|
adminInfo := utils.GetUserFromCtx(l.ctx)
|
|
if adminInfo.ID < utils.NumberOne {
|
|
return failResponse(utils.ErrorNoLoginInfo), nil
|
|
}
|
|
|
|
w := modelbase.Params{
|
|
Eq: map[string]string{"id": strconv.FormatInt(req.Id, 10)},
|
|
}
|
|
var info dao.SaleStatusUpdate
|
|
m := model.SaleModel{}.Init()
|
|
if err := m.GetOne(w, &info); err != nil {
|
|
l.Errorf("sale status get: %v", err)
|
|
return failResponse(utils.Fail), nil
|
|
}
|
|
if info.Id < utils.NumberOne {
|
|
return failResponse(utils.ErrorNotFund), nil
|
|
}
|
|
|
|
newStatus := uint8(req.Status)
|
|
if newStatus == info.Status {
|
|
return okStringResponse(utils.StringEmpty), nil
|
|
}
|
|
|
|
if info.UserId > utils.NumberZero {
|
|
switch newStatus {
|
|
case dao.SaleStatusDisabled:
|
|
if _, err := revokeSaleUserClient(l.ctx, l.svcCtx.UserSvcName, info.UserId); err != nil {
|
|
l.Errorf("sale status revoke client: %v", err)
|
|
if msg, ok := userRPCError(err); ok {
|
|
return outResponse(utils.Fail, msg), nil
|
|
}
|
|
return failResponse(utils.Fail), nil
|
|
}
|
|
case dao.SaleStatusEnabled:
|
|
plainMobile, dErr := utils.DecryptPhone(info.Mobile)
|
|
if dErr != nil || plainMobile == utils.StringEmpty {
|
|
l.Errorf("sale status decrypt mobile: %v", dErr)
|
|
return failResponse(utils.Fail), nil
|
|
}
|
|
if _, err := reopenSaleUserClient(l.ctx, l.svcCtx.UserSvcName, plainMobile); err != nil {
|
|
l.Errorf("sale status reopen client: %v", err)
|
|
if msg, ok := userRPCError(err); ok {
|
|
return outResponse(utils.Fail, msg), nil
|
|
}
|
|
return failResponse(utils.Fail), nil
|
|
}
|
|
}
|
|
}
|
|
|
|
info.Status = newStatus
|
|
info.Reason = req.Reason
|
|
info.AdminId = adminInfo.ID
|
|
info.AdminName = adminInfo.Name
|
|
|
|
if _, err := m.Edit(modelbase.Params{Eq: map[string]string{"id": strconv.FormatInt(req.Id, 10)}}, &info); err != nil {
|
|
l.Errorf("sale status edit: %v", err)
|
|
return failResponse(utils.Fail), nil
|
|
}
|
|
|
|
return okStringResponse(utils.StringEmpty), nil
|
|
}
|