fusenapi/server/shopping-cart-confirmation/internal/logic/changeordermethodlogic.go

78 lines
2.4 KiB
Go
Raw Normal View History

2023-06-14 09:59:48 +00:00
package logic
import (
"encoding/json"
"fusenapi/model/gmodel"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"strings"
"context"
"fusenapi/server/shopping-cart-confirmation/internal/svc"
"fusenapi/server/shopping-cart-confirmation/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type ChangeOrderMethodLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewChangeOrderMethodLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ChangeOrderMethodLogic {
return &ChangeOrderMethodLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *ChangeOrderMethodLogic) ChangeOrderMethod(req *types.ChangeOrderMethodReq, userinfo *auth.UserInfo) (resp *basic.Response) {
2023-06-15 04:00:32 +00:00
if userinfo.GetIdType() != auth.IDTYPE_User {
return resp.SetStatusWithMessage(basic.CodeUnAuth, "please login first")
}
2023-06-14 09:59:48 +00:00
req.Sn = strings.Trim(req.Sn, " ")
if req.Sn == "" {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "param sn is required")
}
//查询订单信息
orderModel := gmodel.NewFsOrderModel(l.svcCtx.MysqlConn)
orderInfo, err := orderModel.FindOneBySn(l.ctx, userinfo.UserId, req.Sn)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get order info")
}
if orderInfo.Id == 0 {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "the order is not exists")
}
if *orderInfo.PayedAmount > 0 {
return resp.SetStatusWithMessage(basic.CodeApiErr, "the order`s address cannot be changed for it is paid")
}
2023-06-20 09:29:02 +00:00
updData := &gmodel.FsOrder{Id: orderInfo.Id}
2023-06-14 09:59:48 +00:00
//地址
if req.AddressId > 0 {
addressModel := gmodel.NewFsAddressModel(l.svcCtx.MysqlConn)
addr, err := addressModel.GetOne(l.ctx, req.AddressId, userinfo.UserId)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get address info")
}
if addr.Id == 0 {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "address is not exists")
}
infoJsonByte, _ := json.Marshal(addr)
strInfoJson := string(infoJsonByte)
updData.AddressInfo = &strInfoJson
}
updData.DeliveryMethod = &req.DeliveryMethod
updData.AddressId = &req.AddressId
updData.PayMethod = &req.PayMethod
2023-06-20 09:29:02 +00:00
if err = orderModel.Update(l.ctx, updData); err != nil {
2023-06-14 09:59:48 +00:00
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to save data")
}
return resp.SetStatusWithMessage(basic.CodeOK, "success")
}