fix: 修复微信通知字段截断导致的编码错误 feat: 添加有效邀请相关字段和任务中心常量 refactor: 重构一番赏奖品格位逻辑 perf: 优化道具卡列表聚合显示 docs: 更新项目说明文档和API文档 test: 添加字符串截断工具测试
47 lines
1.3 KiB
Go
47 lines
1.3 KiB
Go
package user
|
||
|
||
import (
|
||
"bindbox-game/internal/repository/mysql/dao"
|
||
"context"
|
||
"fmt"
|
||
)
|
||
|
||
// CancelShipping 取消发货申请
|
||
func (s *service) CancelShipping(ctx context.Context, userID int64, inventoryID int64) error {
|
||
// 1. 开启事务
|
||
return s.writeDB.Transaction(func(tx *dao.Query) error {
|
||
// 2. 查询发货记录(必须是待发货状态 status=1)
|
||
sr, err := tx.ShippingRecords.WithContext(ctx).
|
||
Where(tx.ShippingRecords.InventoryID.Eq(inventoryID)).
|
||
Where(tx.ShippingRecords.UserID.Eq(userID)).
|
||
Where(tx.ShippingRecords.Status.Eq(1)).
|
||
First()
|
||
|
||
if err != nil {
|
||
return fmt.Errorf("shipping record not found or already processed")
|
||
}
|
||
|
||
// 3. 更新发货记录状态为已取消 (status=5)
|
||
if _, err := tx.ShippingRecords.WithContext(ctx).
|
||
Where(tx.ShippingRecords.ID.Eq(sr.ID)).
|
||
Update(tx.ShippingRecords.Status, 5); err != nil {
|
||
return err
|
||
}
|
||
|
||
// 4. 恢复库存状态为可用 (status=1)
|
||
// 并追加备注
|
||
// 使用原生SQL以确保CONCAT行为一致
|
||
remark := fmt.Sprintf("|shipping_cancelled_by_user:%d", userID)
|
||
if err := tx.UserInventory.WithContext(ctx).UnderlyingDB().Exec(
|
||
"UPDATE user_inventory SET status=1, remark=CONCAT(IFNULL(remark,''), ?) WHERE id=? AND user_id=?",
|
||
remark,
|
||
inventoryID,
|
||
userID,
|
||
).Error; err != nil {
|
||
return err
|
||
}
|
||
|
||
return nil
|
||
})
|
||
}
|