第 4 阶段:订单、交接与账务--ok

This commit is contained in:
yml
2026-05-22 16:14:23 +08:00
parent edda1fbc7b
commit 9ebdfab078
17 changed files with 719 additions and 4 deletions
+4
View File
@@ -39,6 +39,10 @@ type SubmitHandoffRequest struct {
Content string `json:"content" binding:"required"`
}
type SubmitReturnRequest struct {
Content string `json:"content" binding:"required"`
}
type HandoffRecordDTO struct {
ID uint64 `json:"id"`
OrderID uint64 `json:"order_id"`
+44
View File
@@ -145,6 +145,46 @@ func (h *Handler) HandoffRecords(c *gin.Context) {
response.OK(c, gin.H{"items": items})
}
func (h *Handler) SubmitReturn(c *gin.Context) {
userID, ok := currentUserID(c)
if !ok {
response.Unauthorized(c, "缺少用户上下文")
return
}
id, ok := parseID(c)
if !ok {
return
}
var req SubmitReturnRequest
if err := c.ShouldBindJSON(&req); err != nil {
response.BadRequest(c, "归还说明不能为空")
return
}
record, err := h.service.SubmitReturn(userID, id, req)
if err != nil {
writeOrderError(c, err)
return
}
response.Created(c, record)
}
func (h *Handler) ConfirmReturn(c *gin.Context) {
userID, ok := currentUserID(c)
if !ok {
response.Unauthorized(c, "缺少用户上下文")
return
}
id, ok := parseID(c)
if !ok {
return
}
if err := h.service.ConfirmReturn(userID, id); err != nil {
writeOrderError(c, err)
return
}
response.OK(c, gin.H{"completed": true})
}
func currentUserID(c *gin.Context) (uint64, bool) {
value, ok := c.Get(middleware.ContextUserID)
if !ok {
@@ -179,6 +219,10 @@ func writeOrderError(c *gin.Context, err error) {
response.Error(c, http.StatusConflict, "order_cannot_handoff", "当前订单不能交接")
case errors.Is(err, ErrOrderCannotReceive):
response.Error(c, http.StatusConflict, "order_cannot_receive", "当前订单不能确认收号")
case errors.Is(err, ErrOrderCannotReturn):
response.Error(c, http.StatusConflict, "order_cannot_return", "当前订单不能归还")
case errors.Is(err, ErrOrderCannotComplete):
response.Error(c, http.StatusConflict, "order_cannot_complete", "当前订单不能完成")
case errors.Is(err, ErrPermissionDenied):
response.Error(c, http.StatusForbidden, "permission_denied", "无权操作该订单")
case IsNotFound(err):
+150 -2
View File
@@ -5,10 +5,11 @@ import (
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"strconv"
"time"
"hfb_sys/backend/internal/model"
"hfb_sys/backend/internal/modules/wallet"
"gorm.io/datatypes"
"gorm.io/gorm"
@@ -74,6 +75,21 @@ func (r *Repository) Create(renterID uint64, req CreateRequest) (*OrderDTO, erro
if err := tx.Create(&order).Error; err != nil {
return err
}
orderID := order.ID
if err := wallet.AppendEntries(tx,
wallet.Entry{
UserID: renterID,
OrderID: &orderID,
Direction: "in",
Amount: order.RentAmount + order.DepositAmount,
BalanceType: "frozen",
BizType: "order_lock",
BizNo: order.OrderNo,
Remark: "开发态模拟冻结租金和押金",
},
); err != nil {
return err
}
listing.Status = "rented"
account.Status = "rented"
if err := tx.Save(&listing).Error; err != nil {
@@ -113,6 +129,21 @@ func (r *Repository) Cancel(userID uint64, orderID uint64) error {
order.Status = "cancelled"
order.HandoffStatus = "cancelled"
orderID := order.ID
if err := wallet.AppendEntries(tx,
wallet.Entry{
UserID: order.RenterID,
OrderID: &orderID,
Direction: "out",
Amount: order.RentAmount + order.DepositAmount,
BalanceType: "frozen",
BizType: "order_cancel",
BizNo: order.OrderNo,
Remark: "取消订单释放模拟冻结金额",
},
); err != nil {
return err
}
listing.Status = "published"
account.Status = "published"
if err := tx.Save(&order).Error; err != nil {
@@ -204,6 +235,123 @@ func (r *Repository) HandoffRecords(userID uint64, orderID uint64) ([]HandoffRec
return items, nil
}
func (r *Repository) SubmitReturn(userID uint64, orderID uint64, req SubmitReturnRequest) (*HandoffRecordDTO, error) {
var recordID uint64
err := r.db.Transaction(func(tx *gorm.DB) error {
var order model.RentalOrder
if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).First(&order, orderID).Error; err != nil {
return err
}
if order.RenterID != userID {
return ErrPermissionDenied
}
if order.Status != "renting" || order.HandoffStatus != "received" {
return ErrOrderCannotReturn
}
now := time.Now()
record := model.HandoffRecord{
OrderID: order.ID,
FromUserID: order.RenterID,
ToUserID: order.OwnerID,
Type: "renter_return",
Content: req.Content,
}
if err := tx.Create(&record).Error; err != nil {
return err
}
order.Status = "pending_return_confirm"
order.HandoffStatus = "pending_owner_return_confirm"
order.RentEndAt = &now
if err := tx.Save(&order).Error; err != nil {
return err
}
recordID = record.ID
return nil
})
if err != nil {
return nil, err
}
return r.findHandoffRecord(recordID)
}
func (r *Repository) ConfirmReturn(userID uint64, orderID uint64) error {
return r.db.Transaction(func(tx *gorm.DB) error {
var order model.RentalOrder
if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).First(&order, orderID).Error; err != nil {
return err
}
if order.OwnerID != userID {
return ErrPermissionDenied
}
if order.Status != "pending_return_confirm" || order.HandoffStatus != "pending_owner_return_confirm" {
return ErrOrderCannotComplete
}
now := time.Now()
if err := tx.Model(&model.HandoffRecord{}).
Where("order_id = ? AND type = ?", order.ID, "renter_return").
Update("confirmed_by_owner_at", now).Error; err != nil {
return err
}
var listing model.RentalListing
if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).First(&listing, order.ListingID).Error; err != nil {
return err
}
var account model.GameAccount
if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).First(&account, order.AccountID).Error; err != nil {
return err
}
order.Status = "completed"
order.HandoffStatus = "returned"
order.SettlementStatus = "settled"
order.SettledAt = &now
order.OwnerSettledAt = &now
orderID := order.ID
if err := wallet.AppendEntries(tx,
wallet.Entry{
UserID: order.RenterID,
OrderID: &orderID,
Direction: "out",
Amount: order.RentAmount + order.DepositAmount,
BalanceType: "frozen",
BizType: "order_settle",
BizNo: order.OrderNo,
Remark: "订单完成释放模拟冻结金额",
},
wallet.Entry{
UserID: order.OwnerID,
OrderID: &orderID,
Direction: "in",
Amount: order.RentAmount,
BalanceType: "available",
BizType: "owner_income",
BizNo: order.OrderNo,
Remark: "订单完成模拟结算租金",
},
wallet.Entry{
UserID: order.RenterID,
OrderID: &orderID,
Direction: "in",
Amount: order.DepositAmount,
BalanceType: "available",
BizType: "deposit_release",
BizNo: order.OrderNo,
Remark: "订单完成模拟退回押金",
},
); err != nil {
return err
}
listing.Status = "published"
account.Status = "published"
if err := tx.Save(&order).Error; err != nil {
return err
}
if err := tx.Save(&listing).Error; err != nil {
return err
}
return tx.Save(&account).Error
})
}
func (r *Repository) ListForUser(userID uint64) ([]OrderDTO, error) {
var rows []orderRow
err := r.baseQuery().
@@ -316,7 +464,7 @@ func newOrderNo() (string, error) {
if _, err := rand.Read(buf); err != nil {
return "", err
}
return fmt.Sprintf("RO%d%s", time.Now().UnixNano(), hex.EncodeToString(buf)), nil
return "RO" + strconv.FormatInt(time.Now().UnixNano(), 10) + hex.EncodeToString(buf), nil
}
func IsNotFound(err error) bool {
+19
View File
@@ -10,6 +10,8 @@ var (
ErrOrderCannotCancel = errors.New("order cannot cancel")
ErrOrderCannotHandoff = errors.New("order cannot handoff")
ErrOrderCannotReceive = errors.New("order cannot receive")
ErrOrderCannotReturn = errors.New("order cannot return")
ErrOrderCannotComplete = errors.New("order cannot complete")
ErrPermissionDenied = errors.New("permission denied")
)
@@ -62,6 +64,23 @@ func (s *Service) HandoffRecords(userID uint64, orderID uint64) ([]HandoffRecord
return s.repo.HandoffRecords(userID, orderID)
}
func (s *Service) SubmitReturn(userID uint64, orderID uint64, req SubmitReturnRequest) (*HandoffRecordDTO, error) {
if s.repo == nil {
return nil, ErrDependencyUnavailable
}
if orderID == 0 || req.Content == "" {
return nil, ErrOrderCannotReturn
}
return s.repo.SubmitReturn(userID, orderID, req)
}
func (s *Service) ConfirmReturn(userID uint64, orderID uint64) error {
if s.repo == nil {
return ErrDependencyUnavailable
}
return s.repo.ConfirmReturn(userID, orderID)
}
func (s *Service) ListForUser(userID uint64) ([]OrderDTO, error) {
if s.repo == nil {
return nil, ErrDependencyUnavailable