Commit 2edd23df authored by luhongguang's avatar luhongguang

update:tcc 下单 t

parent a37df770
...@@ -13,11 +13,11 @@ class GoodsTccValidate extends BaseValidate ...@@ -13,11 +13,11 @@ class GoodsTccValidate extends BaseValidate
{ {
protected $rule = [ protected $rule = [
't_id' => 'require', 't_id' => 'require',
'goods_id' => 'require', 'keys' => 'require',
]; ];
protected $message = [ protected $message = [
"t_id" => "tcc事务id不能为空", "t_id" => "tcc事务id不能为空",
"goods_id" => "商品id不能为空", "keys" => "商品信息不能为空",
]; ];
} }
\ No newline at end of file
...@@ -15,7 +15,7 @@ class TccController extends Base ...@@ -15,7 +15,7 @@ class TccController extends Base
{ {
(new GoodsTccValidate())->validate(); (new GoodsTccValidate())->validate();
$res = TccService::placeAnOrderTry($this->params["goods_sku_id"], $this->params["t_id"]); $res = TccService::placeAnOrderTry($this->params["keys"], $this->params["t_id"]);
$this->success(["result"=>$res]); $this->success(["result"=>$res]);
} }
...@@ -27,7 +27,7 @@ class TccController extends Base ...@@ -27,7 +27,7 @@ class TccController extends Base
{ {
(new GoodsTccValidate())->validate(); (new GoodsTccValidate())->validate();
$res = TccService::placeAnOrderConfirm($this->params["goods_sku_id"], $this->params["t_id"]); $res = TccService::placeAnOrderConfirm($this->params["keys"], $this->params["t_id"]);
$this->success(["result"=>$res]); $this->success(["result"=>$res]);
} }
...@@ -39,7 +39,7 @@ class TccController extends Base ...@@ -39,7 +39,7 @@ class TccController extends Base
{ {
(new GoodsTccValidate())->validate(); (new GoodsTccValidate())->validate();
$res = TccService::placeAnOrderCancel($this->params["goods_sku_id"], $this->params["t_id"]); $res = TccService::placeAnOrderCancel($this->params["keys"], $this->params["t_id"]);
$this->success(["result"=>$res]); $this->success(["result"=>$res]);
} }
...@@ -51,7 +51,7 @@ class TccController extends Base ...@@ -51,7 +51,7 @@ class TccController extends Base
{ {
(new GoodsTccValidate())->validate(); (new GoodsTccValidate())->validate();
$res = TccService::cancelOrderTry($this->params["goods_sku_id"], $this->params["t_id"]); $res = TccService::cancelOrderTry($this->params["keys"], $this->params["t_id"]);
$this->success(["result"=>$res]); $this->success(["result"=>$res]);
} }
...@@ -63,7 +63,7 @@ class TccController extends Base ...@@ -63,7 +63,7 @@ class TccController extends Base
{ {
(new GoodsTccValidate())->validate(); (new GoodsTccValidate())->validate();
$res = TccService::cancelOrderConfirm($this->params["goods_sku_id"], $this->params["t_id"]); $res = TccService::cancelOrderConfirm($this->params["keys"], $this->params["t_id"]);
$this->success(["result"=>$res]); $this->success(["result"=>$res]);
} }
...@@ -75,7 +75,7 @@ class TccController extends Base ...@@ -75,7 +75,7 @@ class TccController extends Base
{ {
(new GoodsTccValidate())->validate(); (new GoodsTccValidate())->validate();
$res = TccService::cancelOrderCancel($this->params["goods_sku_id"], $this->params["t_id"]); $res = TccService::cancelOrderCancel($this->params["keys"], $this->params["t_id"]);
$this->success(["result"=>$res]); $this->success(["result"=>$res]);
} }
...@@ -87,7 +87,7 @@ class TccController extends Base ...@@ -87,7 +87,7 @@ class TccController extends Base
{ {
(new GoodsTccValidate())->validate(); (new GoodsTccValidate())->validate();
$res = TccService::tccIsFinish($this->params["goods_sku_id"], $this->params["t_id"]); $res = TccService::tccIsFinish($this->params["keys"], $this->params["t_id"]);
$this->success(["result"=>$res]); $this->success(["result"=>$res]);
} }
} }
\ No newline at end of file
...@@ -16,41 +16,67 @@ class TccService ...@@ -16,41 +16,67 @@ class TccService
/** /**
* 下单 商品tcc try * 下单 商品tcc try
* @param $goodsSkuId * @param $keys
* @param $tid * @param $tid
* @return int * @return int
*/ */
public static function placeAnOrderTry($goodsSkuId, $tid) public static function placeAnOrderTry($keys, $tid)
{ {
$tccInfo = Tcc::getRecordMaster(["t_id" => $tid, "goods_sku_id" => $goodsSkuId, "status" => Tcc::STATUS_TRY]); $goodsInfoList = json_decode($keys, true);
if (empty($goodsInfoList)) {
return self::TCC_RESULT_FAIL;
}
$goodsSkuIds = $numList = [];
foreach ($goodsInfoList as $item) {
if (empty($item["goods_sku_id"]) || empty($item["num"])) {
return self::TCC_RESULT_FAIL;
}
$goodsSkuIds[] = $item["goods_sku_id"];
$numList[$item["goods_sku_id"]] = $item["num"];
}
//有结果返回原来的结果,幂等 //有结果返回原来的结果,幂等
if (!empty($tccInfo)) { $tccInfoList = Tcc::getRecordMaster(["t_id" => $tid, "goods_sku_id" => $goodsSkuIds, "status" => Tcc::STATUS_TRY]);
if ($tccInfo[0]["operator_result"] == Tcc::OPERATOR_RESULT_FAIL) { if (!empty($tccInfoList)) {
foreach ($tccInfoList as $tccInfo) {
if ($tccInfo["operator_result"] == Tcc::OPERATOR_RESULT_FAIL) {
return self::TCC_RESULT_FAIL; return self::TCC_RESULT_FAIL;
} else {
return self::TCC_RESULT_SUCCESS;
} }
} }
return self::TCC_RESULT_SUCCESS;
}
//如果已经有 confirm 或者 cancel,则直接失败 //如果已经有 confirm 或者 cancel,则直接失败
$cTccInfo = Tcc::getRecordMaster(["t_id" => $tid, "goods_sku_id" => $goodsSkuId, "status" => [Tcc::STATUS_CONFIRM, Tcc::STATUS_CANCEL], "operator_result" => Tcc::OPERATOR_RESULT_SUCCESS]); $cTccInfoList = Tcc::getRecordMaster(["t_id" => $tid, "goods_sku_id" => $goodsSkuIds, "status" => [Tcc::STATUS_CONFIRM, Tcc::STATUS_CANCEL], "operator_result" => Tcc::OPERATOR_RESULT_SUCCESS]);
if (!empty($cTccInfo)) { if (!empty($cTccInfoList)) {
foreach ($goodsSkuIds as $goodsSkuId) {
Tcc::save([ Tcc::save([
"t_id" => $tid, "t_id" => $tid,
"goods_sku_id" => $goodsSkuId, "goods_sku_id" => $goodsSkuId,
"status" => Tcc::STATUS_TRY, "status" => Tcc::STATUS_TRY,
"operator_result" => Tcc::OPERATOR_RESULT_FAIL, "operator_result" => Tcc::OPERATOR_RESULT_FAIL,
]); ]);
}
return self::TCC_RESULT_FAIL; return self::TCC_RESULT_FAIL;
} }
$skus = GoodsSku::getRecords(["goods_sku_id" => $goodsSkuIds]);
if (!empty($skus)) {
GoodsSku::beginTransaction();
foreach ($skus as $sku) {
$goodsSkuId = $sku["goods_sku_id"];
$sku = GoodsSku::getRecord(["goods_sku_id" => $goodsSkuId]);
if (!empty($sku["inventory_rest"]) && $sku["online_status"] == GoodsSku::ONLINE_STATUS_ONLINE) { if (!empty($sku["inventory_rest"]) && $sku["online_status"] == GoodsSku::ONLINE_STATUS_ONLINE) {
if (empty($numList[$goodsSkuId])) {
return self::TCC_RESULT_FAIL;
}
$num = $numList[$sku["goods_sku_id"]];
GoodsSku::beginTransaction(); GoodsSku::beginTransaction();
GoodsSku::save([ GoodsSku::save([
"inventory_lock" => $sku["inventory_lock"] + 1, "inventory_lock" => $sku["inventory_lock"] + $num,
"inventory_rest" => $sku["inventory_rest"] - 1, "inventory_rest" => $sku["inventory_rest"] - $num,
], ["goods_sku_id" => $goodsSkuId]); ], ["goods_sku_id" => $goodsSkuId]);
Tcc::save([ Tcc::save([
...@@ -59,8 +85,7 @@ class TccService ...@@ -59,8 +85,7 @@ class TccService
"status" => Tcc::STATUS_TRY, "status" => Tcc::STATUS_TRY,
"operator_result" => Tcc::OPERATOR_RESULT_SUCCESS, "operator_result" => Tcc::OPERATOR_RESULT_SUCCESS,
]); ]);
} else {
if (!GoodsSku::commit()) {
GoodsSku::rollback(); GoodsSku::rollback();
Tcc::save([ Tcc::save([
...@@ -71,14 +96,20 @@ class TccService ...@@ -71,14 +96,20 @@ class TccService
]); ]);
return self::TCC_RESULT_FAIL; return self::TCC_RESULT_FAIL;
} }
} else { }
if (!GoodsSku::commit()) {
GoodsSku::rollback();
foreach ($skus as $sku) {
Tcc::save([ Tcc::save([
"t_id" => $tid, "t_id" => $tid,
"goods_sku_id" => $goodsSkuId, "goods_sku_id" => $sku["goods_sku_id"],
"status" => Tcc::STATUS_TRY, "status" => Tcc::STATUS_TRY,
"operator_result" => Tcc::OPERATOR_RESULT_FAIL, "operator_result" => Tcc::OPERATOR_RESULT_FAIL,
]); ]);
return self::TCC_RESULT_FAIL; }
}
} }
return self::TCC_RESULT_SUCCESS; return self::TCC_RESULT_SUCCESS;
......
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