package logic import ( "errors" "fusenapi/constants" "fusenapi/model/gmodel" "fusenapi/utils/auth" "fusenapi/utils/basic" "time" "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 UserOderDeleteLogic struct { logx.Logger ctx context.Context svcCtx *svc.ServiceContext } func NewUserOderDeleteLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserOderDeleteLogic { return &UserOderDeleteLogic{ Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx, } } func (l *UserOderDeleteLogic) UserOderDelete(req *types.RequestOrderId, userinfo *auth.UserInfo) (resp *basic.Response) { // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) // userinfo 传入值时, 一定不为null if !userinfo.IsUser() { return resp.SetStatus(basic.CodeUnAuth) // 如果不是用户信息, 返回未授权错误 } //订单id orderId := req.OrderId if orderId < 1 { return resp.SetStatus(basic.CodeRequestParamsErr) } m := gmodel.NewFsOrderModel(l.svcCtx.MysqlConn) order, err := m.FindOne(l.ctx, userinfo.UserId, orderId) if err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { return resp.SetStatus(basic.CodeOrderNotFoundErr) } logx.Error(err) return resp.SetStatus(basic.CodeDbSqlErr) } if auth.CheckValueRange[constants.Order]( constants.Order(*order.Status), constants.STATUS_NEW_NOT_PAY, constants.STATUS_NEW_PAY_COMPLETED, constants.STATUS_NEW_PART_PAY, ) { return resp.SetStatus(basic.CodeOrderNotCancelledErr) } if *order.IsPayCompleted == 1 && time.Now().After(time.Unix(*order.Ctime, 0).Add(48*time.Hour)) { return resp.SetStatus(basic.CodeOrderNotCancelledErr) } //这里是将给定的PHP代码翻译成Golang代码: uOrder := &gmodel.FsOrder{ Id: orderId, } var ( isCancel int64 = 1 ustatus int64 = int64(constants.STATUS_NEW_CANCEL) ) // 修改取消状态和取消原因 uOrder.Status = &ustatus uOrder.IsCancel = &isCancel uOrder.RefundReasonId = &req.RefundReasonId uOrder.RefundReason = &req.RefundReason err = m.Update(l.ctx, uOrder) if err != nil { logx.Error(err) return resp.SetStatus(basic.CodeDbUpdateErr) } var ( IsRefund int64 = 0 CreatedAt = time.Now().Unix() ) refund := &gmodel.FsRefundReason{ IsRefund: &IsRefund, RefundReasonId: &req.RefundReasonId, RefundReason: &req.RefundReason, OrderId: &order.Id, CreatedAt: &CreatedAt, } mFsRefund := gmodel.NewFsRefundReasonModel(l.svcCtx.MysqlConn) err = refund.Save() // 退款 // 调用第三方接口发起退款 config := GetStripeConfig() // 查询支付信息 pays, err := Pay.Find(db.Where("order_number = ? AND is_refund = ? AND pay_status = ?", order.SN, 0, 1)) for _, pay := range pays { stripe := stripe.New(config.SK) _, err := stripe.Refunds.Create(stripe.RefundParams{ PaymentIntent: pay.TradeNo, }) } if err == nil && err == nil { return ResponseSuccess(200, "Cancel successfully", order) } return ResponseError(500, "Cancellation failure") return resp.SetStatus(basic.CodeOK) } // func (l *OrderLogic) CancelOrder(req *types.RequestCancelOrder, userinfo *auth.UserInfo) (resp *basic.Response) { // // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) // // userinfo 传入值时, 一定不为null // if !userinfo.IsUser() { // return resp.SetStatus(basic.CodeUnAuth) // 如果不是用户信息, 返回未授权错误 // } // //订单id // id := req.ReqParam.Get("id") // if id == "" { // return resp.SetStatus(basic.CodeParamErr) // } // //验证当前订单是否可以取消 // order, err := gmodel.NewFsOrderModel(l.svcCtx.MysqlConn).GetOne(l.ctx, map[string]interface{}{"user_id": userinfo.UserId, "id": id}) // if err != nil { // return resp.SetStatus(basic.CodeDbQueryErr) // } // if order == nil { // return resp.SetStatus(basic.CodeNotFound) // } // //那些状态下是不能关闭的? (订单完成,退款完成,关闭订单) // if order.Status == gconst.OrderStatusComplete || // order.Status == gconst.OrderStatusRefund || // order.Status == gconst.OrderStatusClose { // return resp.SetStatus(basic.CodeCannotOperate) // } // //订单状态修改 // order.Status = gconst.OrderStatusDelete // order.IsDeleted = 1 // result, err := gmodel.NewFsOrderModel(l.svcCtx.MysqlConn).UpdateOne(l.ctx, order) // if err != nil { // return resp.SetStatus(basic.CodeDbUpdateErr) // } // return resp.SetStatus(basic.CodeOK, result) // }