Commit 1fa5962e authored by luhongguang's avatar luhongguang

update:tcc 下单优化

parent b118c058
......@@ -42,5 +42,6 @@ class GoodsException extends BaseException
28 => '上架状态不能编辑,需先下架,再做编辑操作',
29 => '发布商品的生活号不存在',
30 => '当前商品已过期',
31 => '当前商品锁定库存不足',
];
}
\ No newline at end of file
......@@ -78,15 +78,21 @@ class TccService
}
$num = $numList[$goodsSkuId];
if ($sku["inventory_rest"] < $num) {
GoodsSku::rollback();
return self::TCC_RESULT_FAIL;
}
GoodsSku::save([
//条件加上乐观锁
$rowCount = GoodsSku::save([
"inventory_lock" => $sku["inventory_lock"] + $num,
"inventory_rest" => $sku["inventory_rest"] - $num,
], ["goods_sku_id" => $goodsSkuId]);
], ["goods_sku_id" => $goodsSkuId, "inventory_rest[>=]" => $num]);
if ($rowCount <= 0) {
GoodsSku::rollback();
Tcc::save([
"tid" => $tid,
"goods_sku_id" => $goodsSkuId,
"type" => Tcc::STATUS_TRY,
"operator_result" => Tcc::OPERATOR_RESULT_FAIL,
]);
throw new GoodsException(["cus" => 24]);
}
Tcc::save([
"tid" => $tid,
"goods_sku_id" => $goodsSkuId,
......@@ -161,14 +167,14 @@ class TccService
}
//在confirm时候必须已经有try,如果没有就直接返回fail
$cTccInfoList = Tcc::getRecordMaster([
$tTccInfoList = Tcc::getRecordMaster([
"tid" => $tid,
"goods_sku_id" => $goodsSkuIds,
"type" => Tcc::STATUS_TRY,
"operator_result" => Tcc::OPERATOR_RESULT_SUCCESS
]);
if (empty($cTccInfoList) || count($cTccInfoList) != count($goodsSkuIds)) {
if (empty($tTccInfoList) || count($tTccInfoList) != count($goodsSkuIds)) {
foreach ($goodsSkuIds as $goodsSkuId) {
Tcc::save([
"tid" => $tid,
......@@ -203,15 +209,21 @@ class TccService
}
$num = $numList[$goodsSkuId];
if ($sku["inventory_lock"] < $num) {
GoodsSku::rollback();
return self::TCC_RESULT_FAIL;
}
GoodsSku::save([
//条件加上乐观锁
$rowCount = GoodsSku::save([
"inventory_lock" => $sku["inventory_lock"] - $num,
"total_amount_order" => $sku["total_amount_order"] + $num,
], ["goods_sku_id" => $goodsSkuId]);
], ["goods_sku_id" => $goodsSkuId, "inventory_lock[>=]" => $num]);
if ($rowCount <= 0) {
GoodsSku::rollback();
Tcc::save([
"tid" => $tid,
"goods_sku_id" => $goodsSkuId,
"type" => Tcc::STATUS_CONFIRM,
"operator_result" => Tcc::OPERATOR_RESULT_FAIL,
]);
throw new GoodsException(["cus" => 31]);
}
Tcc::save([
"tid" => $tid,
......@@ -300,7 +312,7 @@ class TccService
"tid" => $tid,
"goods_sku_id" => $goodsSkuId,
"type" => Tcc::STATUS_CANCEL,
"operator_result" => Tcc::OPERATOR_RESULT_FAIL,
"operator_result" => Tcc::OPERATOR_RESULT_SUCCESS,
]);
}
return self::TCC_RESULT_SUCCESS;
......@@ -329,16 +341,21 @@ class TccService
}
$num = $numList[$goodsSkuId];
if ($sku["inventory_lock"] < $num) {
GoodsSku::rollback();
return self::TCC_RESULT_FAIL;
}
GoodsSku::save([
//条件加上乐观锁
$rowCount = GoodsSku::save([
"inventory_lock" => $sku["inventory_lock"] - $num,
"inventory_rest" => $sku["inventory_rest"] + $num,
], ["goods_sku_id" => $goodsSkuId]);
], ["goods_sku_id" => $goodsSkuId, "inventory_lock[>=]" => $num]);
if ($rowCount <= 0) {
GoodsSku::rollback();
Tcc::save([
"tid" => $tid,
"goods_sku_id" => $goodsSkuId,
"type" => Tcc::STATUS_CANCEL,
"operator_result" => Tcc::OPERATOR_RESULT_FAIL,
]);
throw new GoodsException(["cus" => 31]);
}
Tcc::save([
"tid" => $tid,
"goods_sku_id" => $goodsSkuId,
......@@ -713,7 +730,7 @@ class TccService
GoodsSku::save([
"inventory_lock" => $sku["inventory_lock"] - $num,
"total_amount_order" => $sku["total_amount_order"] + $num,
], ["goods_sku_id" => $goodsSkuId]);
], ["goods_sku_id" => $goodsSkuId, "inventory_lock[>=]" => $num]);
Tcc::save([
"tid" => $tid,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment