|
@@ -36,60 +36,14 @@ class Order
|
|
|
$productExtend = new \addons\unishop\extend\Product;
|
|
|
$prefix = \think\Config::get('database.prefix');
|
|
|
|
|
|
- if (Config::isPessimism()) {
|
|
|
+ if (1 == 1) {
|
|
|
// 悲观锁
|
|
|
foreach ($extra['specNumber'] as $spec => $number) {
|
|
|
$result = 0;
|
|
|
if (is_numeric($spec) && $params[$key]['use_spec'] == Product::SPEC_OFF) {
|
|
|
$result = Db::execute('UPDATE ' . $prefix . "unishop_product SET no_buy_yet = no_buy_yet+{$number}, real_sales = real_sales+{$number}, stock = stock-{$number} WHERE id = {$params[$key]['id']}");
|
|
|
} else if ($params[$key]['use_spec'] == Product::SPEC_ON) {
|
|
|
- $info = $productExtend->getBaseData($params[$key], $spec);
|
|
|
-
|
|
|
- // mysql<5.7.13时用
|
|
|
- //if (mysql < 5.7.13) {
|
|
|
- $spec = str_replace(',', '","', $spec);
|
|
|
- $search = '"stock":"' . $info['stock'] . '","value":["' . $spec . '"]';
|
|
|
- $stock = $info['stock'] - $number;
|
|
|
- $replace = '"stock":\"' . $stock . '\","value":["' . $spec . '"]';
|
|
|
- $sql = 'UPDATE ' . $prefix . "unishop_product SET no_buy_yet = no_buy_yet+{$number}, stock = stock-{$number}, real_sales = real_sales+{$number} ,`specTableList` = REPLACE(specTableList,'$search','$replace') WHERE id = {$params[$key]['id']}";
|
|
|
- $result = Db::execute($sql);
|
|
|
- //}
|
|
|
-
|
|
|
- //下面语句直接操作JSON
|
|
|
- //if (mysql >= 5.7.13) {
|
|
|
- //$info['stock'] -= $number;
|
|
|
- //$result = Db::execute("UPDATE fa_unishop_product SET no_buy_yet = no_buy_yet+{$number}, real_sales = real_sales+{$number}, stock = stock-{$number},specTableList = JSON_REPLACE(specTableList, '$[{$info['key']}].stock', {$info['stock']}) WHERE id = {$params[$key]['id']}");
|
|
|
- //}
|
|
|
- }
|
|
|
- if ($result == 0) { // 锁生效
|
|
|
- throw new Exception('下单失败,请重试');
|
|
|
- }
|
|
|
- $key++;
|
|
|
- }
|
|
|
- } else {
|
|
|
- // 乐观锁
|
|
|
- foreach ($extra['specNumber'] as $spec => $number) {
|
|
|
- $result = 0;
|
|
|
- if (is_numeric($spec) && $params[$key]['use_spec'] == Product::SPEC_OFF) {
|
|
|
- $result = Db::execute('UPDATE ' . $prefix . "unishop_product SET no_buy_yet = no_buy_yet+{$number}, real_sales = real_sales+{$number}, stock = stock-{$number} WHERE id = {$params[$key]['id']} AND stock = {$params[$key]['stock']}");
|
|
|
- } else if ($params[$key]['use_spec'] == Product::SPEC_ON) {
|
|
|
- $info = $productExtend->getBaseData($params[$key], $spec);
|
|
|
-
|
|
|
- // mysql<5.7.13时用
|
|
|
- //if (mysql < 5.7.13) {
|
|
|
- $spec = str_replace(',', '","', $spec);
|
|
|
- $search = '"stock":"' . $info['stock'] . '","value":["' . $spec . '"]';
|
|
|
- $stock = $info['stock'] - $number;
|
|
|
- $replace = '"stock":\"' . $stock . '\","value":["' . $spec . '"]';
|
|
|
- $sql = 'UPDATE ' . $prefix . "unishop_product SET no_buy_yet = no_buy_yet+{$number}, real_sales = real_sales+{$number}, stock = stock-{$number},`specTableList` = REPLACE(specTableList,'$search','$replace') WHERE id = {$params[$key]['id']} AND stock = {$params[$key]['stock']}";
|
|
|
- $result = Db::execute($sql);
|
|
|
- //}
|
|
|
-
|
|
|
- //下面语句直接操作JSON
|
|
|
- //if (mysql >= 5.7.13) {
|
|
|
- //$info['stock'] -= $number;
|
|
|
- //$result = Db::execute("UPDATE fa_unishop_product SET no_buy_yet = no_buy_yet+{$number}, real_sales = real_sales+{$number}, stock = stock-{$number},specTableList = JSON_REPLACE(specTableList, '$[{$info['key']}].stock', {$info['stock']}) WHERE id = {$params[$key]['id']} AND stock = {$params[$key]['stock']}");
|
|
|
- //}
|
|
|
+ $result = 1;
|
|
|
}
|
|
|
if ($result == 0) { // 锁生效
|
|
|
throw new Exception('下单失败,请重试');
|
|
@@ -98,17 +52,7 @@ class Order
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // 行为二
|
|
|
- if (!empty($extra['cart'])) {
|
|
|
- $cart = $extra['cart'];
|
|
|
- Db::execute('DELETE FROM ' . $prefix . "unishop_cart WHERE id IN ($cart) AND user_id = $extra[userId]");
|
|
|
- }
|
|
|
|
|
|
- // 行为三
|
|
|
- $coupon = $extra['coupon'];
|
|
|
- if (!empty($coupon)) {
|
|
|
- Db::name('unishop_coupon_user')->where('id',$coupon['cu_id'])->update(['status'=>1]);
|
|
|
- }
|
|
|
|
|
|
// More ...
|
|
|
}
|
|
@@ -128,12 +72,9 @@ class Order
|
|
|
public function createOrderBefore(&$params, $extra)
|
|
|
{
|
|
|
|
|
|
- $specs = explode(',', $extra['spec']);
|
|
|
- foreach ($specs as &$spec) {
|
|
|
- $spec = str_replace('|', ',', $spec);
|
|
|
- }
|
|
|
- $numbers = explode(',', $extra['number']);
|
|
|
- $productIds = explode(',', $extra['product_id']);
|
|
|
+ $specs = [''];
|
|
|
+ $numbers = [$extra['number']];
|
|
|
+ $productIds = [$extra['product_id']];
|
|
|
|
|
|
if (count($specs) !== count($numbers) || count($specs) !== count($productIds)) {
|
|
|
throw new Exception(__('Parameter error'));
|
|
@@ -145,7 +86,7 @@ class Order
|
|
|
// 条件一
|
|
|
$products = [];
|
|
|
foreach ($productIds as $key => &$productId) {
|
|
|
- $productId = Hashids::decodeHex($productId);
|
|
|
+
|
|
|
$products[$key] = Db::name('unishop_product')
|
|
|
->where(['id' => $productId, 'switch' => Product::SWITCH_ON])
|
|
|
->lock(Config::isPessimism()) // Todo 是否使用悲观锁
|
|
@@ -158,7 +99,7 @@ class Order
|
|
|
throw new Exception(__('There are offline product'));
|
|
|
}
|
|
|
// 从购物车下单多个商品时,有同一个商品的不同规格导致减库存问题
|
|
|
- if (count($productIds) > 0) {
|
|
|
+ /*if (count($productIds) > 0) {
|
|
|
$reduceStock = [];
|
|
|
foreach ($products as $key => $value) {
|
|
|
if (!isset($reduceStock[$value['id']])) {
|
|
@@ -168,7 +109,7 @@ class Order
|
|
|
$reduceStock[$value['id']] += $numbers[$key];
|
|
|
}
|
|
|
}
|
|
|
- }
|
|
|
+ }*/
|
|
|
|
|
|
// 条件二
|
|
|
foreach ($products as $key => $product) {
|
|
@@ -198,7 +139,7 @@ class Order
|
|
|
$address = [];
|
|
|
|
|
|
// 条件四
|
|
|
- if ($extra['coupon_id']) {
|
|
|
+ /*if ($extra['coupon_id']) {
|
|
|
|
|
|
// $coupon = Coupon::get($extra['coupon_id']);
|
|
|
|
|
@@ -230,7 +171,8 @@ class Order
|
|
|
|
|
|
} else {
|
|
|
$coupon = [];
|
|
|
- }
|
|
|
+ }*/
|
|
|
+ $coupon = [];
|
|
|
|
|
|
$params = [$products, $delivery, $coupon, $baseProductInfo, $address, $orderPrice, $specs, $numbers];
|
|
|
}
|