Commit 2edd23df authored by luhongguang's avatar luhongguang

update:tcc 下单 t

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