|
@@ -61,32 +61,50 @@ class Order
|
|
|
if (!$value || $value <= 0) {
|
|
if (!$value || $value <= 0) {
|
|
|
Dever::error('面值无效');
|
|
Dever::error('面值无效');
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- # 查询折扣 扣费
|
|
|
|
|
- $seller_goods = Dever::db('seller/goods')->find(array('seller_id' => $seller_id, 'goods_id' => $goods_id, 'sku_id' => $sku_id));
|
|
|
|
|
- if (!$seller_goods) {
|
|
|
|
|
- $seller_goods = Dever::db('seller/goods')->find(array('seller_id' => $seller_id, 'goods_id' => $goods_id, 'sku_id' => -1));
|
|
|
|
|
|
|
+ $lockKey = 'seller_cash_lock_' . $seller_id;
|
|
|
|
|
+ $lockToken = uniqid('cash_', true);
|
|
|
|
|
+ if (!Redis::lock($lockKey, $lockToken, 5)) {
|
|
|
|
|
+ Dever::error('扣费繁忙,请稍后再试');
|
|
|
}
|
|
}
|
|
|
|
|
+ try {
|
|
|
|
|
+ # 查询折扣 扣费
|
|
|
|
|
+ $seller_goods = Dever::db('seller/goods')->find(array('seller_id' => $seller_id, 'goods_id' => $goods_id, 'sku_id' => $sku_id));
|
|
|
|
|
+ if (!$seller_goods) {
|
|
|
|
|
+ $seller_goods = Dever::db('seller/goods')->find(array('seller_id' => $seller_id, 'goods_id' => $goods_id, 'sku_id' => -1));
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- $info = Dever::db('seller/info')->find($seller_id);
|
|
|
|
|
- if ($seller_goods && $seller_goods['discount']) {
|
|
|
|
|
- $info['discount'] = $seller_goods['discount'];
|
|
|
|
|
- }
|
|
|
|
|
- if (!$info['discount']) {
|
|
|
|
|
- $info['discount'] = 1;
|
|
|
|
|
- }
|
|
|
|
|
- $cash = round($value * $info['discount'], 2) * $num;
|
|
|
|
|
|
|
+ $info = Dever::db('seller/info')->find($seller_id, [], true);
|
|
|
|
|
+ if ($seller_goods && $seller_goods['discount']) {
|
|
|
|
|
+ $info['discount'] = $seller_goods['discount'];
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!$info['discount']) {
|
|
|
|
|
+ $info['discount'] = 1;
|
|
|
|
|
+ }
|
|
|
|
|
+ $cash = round($value * $info['discount'], 2) * $num;
|
|
|
|
|
|
|
|
- # 查询余额是否充足
|
|
|
|
|
- $info['yue'] = $info['credit'] + $info['cash'];
|
|
|
|
|
- if ($info['yue'] < $cash) {
|
|
|
|
|
- Dever::error('余额不足');
|
|
|
|
|
- }
|
|
|
|
|
- $state = Dever::load(Info::class)->dec($info['id'], $cash, $info['version']);
|
|
|
|
|
- if (!$state) {
|
|
|
|
|
|
|
+ # 查询余额是否充足
|
|
|
|
|
+ $info['yue'] = $info['credit'] + $info['cash'];
|
|
|
|
|
+ if ($info['yue'] < $cash) {
|
|
|
|
|
+ Dever::error('余额不足');
|
|
|
|
|
+ }
|
|
|
|
|
+ $retry = 0;
|
|
|
|
|
+ while ($retry < 3) {
|
|
|
|
|
+ $state = Dever::load(Info::class)->dec($info['id'], $cash, $info['version']);
|
|
|
|
|
+ if ($state) {
|
|
|
|
|
+ return $cash;
|
|
|
|
|
+ }
|
|
|
|
|
+ $retry++;
|
|
|
|
|
+ usleep(50000);
|
|
|
|
|
+ $info = Dever::db('seller/info')->find($seller_id, [], true);
|
|
|
|
|
+ $info['yue'] = $info['credit'] + $info['cash'];
|
|
|
|
|
+ if ($info['yue'] < $cash) {
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
Dever::error('余额不足');
|
|
Dever::error('余额不足');
|
|
|
|
|
+ } finally {
|
|
|
|
|
+ Redis::unlock($lockKey, $lockToken);
|
|
|
}
|
|
}
|
|
|
- return $cash;
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
# 向渠道发起请求
|
|
# 向渠道发起请求
|