Commit 80437d5c authored by luhongguang's avatar luhongguang

update:tcc 取消流程优化

parent 1fa5962e
......@@ -43,5 +43,6 @@ class GoodsException extends BaseException
29 => '发布商品的生活号不存在',
30 => '当前商品已过期',
31 => '当前商品锁定库存不足',
32 => '当前商品已下单数量不足',
];
}
\ No newline at end of file
......@@ -453,15 +453,21 @@ class TccService
}
$num = $numList[$goodsSkuId];
if ($sku["total_amount_order"] < $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, "total_amount_order[>=]" => $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" => 32]);
}
Tcc::save([
"tid" => $tid,
......@@ -580,30 +586,42 @@ class TccService
}
$num = $numList[$goodsSkuId];
if ($sku["inventory_lock"] < $num) {
GoodsSku::rollback();
return self::TCC_RESULT_FAIL;
}
if ($type == self::TYPE_CANCEL_ORDER) {
$params = [
"inventory_lock" => $sku["inventory_lock"] - $num,
"inventory_rest" => $sku["inventory_rest"] + $num,
];
}
if ($type == self::TYPE_BACK_ORDER) {
if ($sku["total_amount_sold"] < $num) {
$rowCount = GoodsSku::save($params, ["goods_sku_id" => $goodsSkuId, "inventory_lock[>=]" => $num]);
if ($rowCount <= 0) {
GoodsSku::rollback();
return self::TCC_RESULT_FAIL;
Tcc::save([
"tid" => $tid,
"goods_sku_id" => $goodsSkuId,
"type" => Tcc::STATUS_CONFIRM,
"operator_result" => Tcc::OPERATOR_RESULT_FAIL,
]);
throw new GoodsException(["cus" => 31]);
}
}
if ($type == self::TYPE_BACK_ORDER) {
$params = [
"inventory_lock" => $sku["inventory_lock"] - $num,
"inventory_rest" => $sku["inventory_rest"] + $num,
"total_amount_sold" => $sku["total_amount_sold"] - $num,
];
$rowCount = GoodsSku::save($params,
["goods_sku_id" => $goodsSkuId, "inventory_lock[>=]" => $num, "total_amount_sold[>=]" => $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" => 32]);
}
}
GoodsSku::save($params, ["goods_sku_id" => $goodsSkuId]);
Tcc::save([
"tid" => $tid,
......@@ -692,7 +710,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;
......@@ -722,15 +740,20 @@ 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, "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,
......
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