fusenapi/server/home-user-auth/internal/logic/userorderdeletelogic.go
2023-07-26 11:06:05 +08:00

79 lines
2.4 KiB
Go

package logic
import (
"errors"
"fusenapi/constants"
"fusenapi/model/gmodel"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"context"
"fusenapi/server/home-user-auth/internal/svc"
"fusenapi/server/home-user-auth/internal/types"
"github.com/zeromicro/go-zero/core/logx"
"gorm.io/gorm"
)
type UserOrderDeleteLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewUserOrderDeleteLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserOrderDeleteLogic {
return &UserOrderDeleteLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
// 处理进入前逻辑w,r
// func (l *UserOrderDeleteLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
// }
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
// func (l *UserOrderDeleteLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
// // httpx.OkJsonCtx(r.Context(), w, resp)
// }
func (l *UserOrderDeleteLogic) UserOrderDelete(req *types.UserOrderDeleteReq, userinfo *auth.UserInfo) (resp *basic.Response) {
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
// userinfo 传入值时, 一定不为null
if userinfo == nil || userinfo.UserId == 0 {
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "order not found")
}
orderModel := gmodel.NewFsOrderModel(l.svcCtx.MysqlConn)
orderInfo, err := orderModel.FindOne(l.ctx, userinfo.UserId, req.ID)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "order not found")
}
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get order info")
}
updateStatusMap := make(map[constants.Order]struct{}, 4)
updateStatusMap[constants.STATUS_NEW_COMPLETED] = struct{}{}
updateStatusMap[constants.STATUS_NEW_CANCEL] = struct{}{}
updateStatusMap[constants.STATUS_NEW_REFUNDED] = struct{}{}
updateStatusMap[constants.STATUS_NEW_CLOSE] = struct{}{}
if _, ok := updateStatusMap[constants.Order(*orderInfo.Status)]; !ok {
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "order not found")
}
*orderInfo.Status = int64(constants.STATUS_NEW_DELETE)
*orderInfo.IsDeleted = 1
err = orderModel.Update(l.ctx, orderInfo)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "fail to delete")
}
return resp.SetStatus(basic.CodeOK)
}