2023-06-14 09:59:48 +00:00
|
|
|
package logic
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2023-06-20 09:28:28 +00:00
|
|
|
"errors"
|
2023-06-14 09:59:48 +00:00
|
|
|
"fusenapi/model/gmodel"
|
|
|
|
"fusenapi/utils/auth"
|
|
|
|
"fusenapi/utils/basic"
|
2023-06-20 09:28:28 +00:00
|
|
|
"gorm.io/gorm"
|
2023-06-14 09:59:48 +00:00
|
|
|
"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 {
|
2023-06-20 09:28:28 +00:00
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "the order is not exists")
|
|
|
|
}
|
2023-06-14 09:59:48 +00:00
|
|
|
logx.Error(err)
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get order info")
|
|
|
|
}
|
|
|
|
if *orderInfo.PayedAmount > 0 {
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeApiErr, "the order`s address cannot be changed for it is paid")
|
|
|
|
}
|
|
|
|
updData := gmodel.FsOrder{}
|
|
|
|
//地址
|
|
|
|
if req.AddressId > 0 {
|
|
|
|
addressModel := gmodel.NewFsAddressModel(l.svcCtx.MysqlConn)
|
|
|
|
addr, err := addressModel.GetOne(l.ctx, req.AddressId, userinfo.UserId)
|
|
|
|
if err != nil {
|
2023-06-20 09:28:28 +00:00
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeServiceErr, "address is not exists")
|
|
|
|
}
|
2023-06-14 09:59:48 +00:00
|
|
|
logx.Error(err)
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get address info")
|
|
|
|
}
|
|
|
|
infoJsonByte, _ := json.Marshal(addr)
|
|
|
|
strInfoJson := string(infoJsonByte)
|
|
|
|
updData.AddressInfo = &strInfoJson
|
|
|
|
}
|
|
|
|
updData.DeliveryMethod = &req.DeliveryMethod
|
|
|
|
updData.AddressId = &req.AddressId
|
|
|
|
updData.PayMethod = &req.PayMethod
|
|
|
|
if err = orderModel.Update(l.ctx, orderInfo.Id, updData); err != nil {
|
|
|
|
logx.Error(err)
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to save data")
|
|
|
|
}
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeOK, "success")
|
|
|
|
}
|