From 2841bd490b9570775201bae4a2fda1911d8cf7a1 Mon Sep 17 00:00:00 2001 From: songliang Date: Tue, 20 Jun 2023 15:15:12 +0800 Subject: [PATCH] ... --- webapp/controller/ShopController.class.php | 128 ++++++++++++++++++ .../callback/BuyShopGoodsCbService.php | 105 +++++++++----- 2 files changed, 200 insertions(+), 33 deletions(-) diff --git a/webapp/controller/ShopController.class.php b/webapp/controller/ShopController.class.php index 3889e38e..225e08fb 100644 --- a/webapp/controller/ShopController.class.php +++ b/webapp/controller/ShopController.class.php @@ -809,6 +809,134 @@ class ShopController extends BaseAuthedController ); } + public function buyGoodsNormal() + { + $id = getReqVal('id', 0); + $token_type = getReqVal('token_type', ''); + $goods_num = getReqVal('goods_num', 0); + + $row = mt\ShopGoods::get($id); + + $desired_token_type = $row['token_type']; + $check_token_type = splitStr1($desired_token_type); + $token_pos = array_search($token_type, $check_token_type, true); + if (!in_array($token_type, $check_token_type)) { + $this->_rspErr(1, "token_type parameter error, desired_token_type: {$desired_token_type}"); + return; + } + + if ($goods_num > $row['max_amount']) { + $this->_rspErr(1, "goods_num parameter error, max_amount: {$row['max_amount']}"); + return; + } + + // 这里命名混乱了, 购买个数,一捆个数命名冲突 + $goods_count = $row['goods_num']; + + $buyRecordHash = ShopBuyRecord::allToHash(); + $boughtTimes = 1; + switch ($row['limit_type']) { + case ShopController::DAILY_BUY_LIMIT: { + $buyRecord = getXVal($buyRecordHash, $id); + $boughtTimes = $buyRecord ? $buyRecord['this_day_buy_times'] + 1 : 1; + if ($buyRecord && getXVal($buyRecord, 'this_day_buy_times', 0) >= $row['limit_num']) { + $this->_rspErr(2, 'Has reached the maximum number of purchase restrictions today'); + return; + } + if ($row['limit_num'] <= 0) { + $this->_rspErr(2, 'The maximum number of purchase restrictions has been reached'); + return; + } + } + break; + case ShopController::WEEKLY_BUY_LIMIT: { + $buyRecord = getXVal($buyRecordHash, $id); + $boughtTimes = $buyRecord ? $buyRecord['this_week_buy_times'] + 1 : 1; + if ($buyRecord && getXVal($buyRecord, 'this_week_buy_times', 0) >= $row['limit_num']) { + $this->_rspErr(2, 'The maximum number of purchase restrictions this week has been reached'); + return; + } + if ($row['limit_num'] <= 0) { + $this->_rspErr(2, 'The maximum number of purchase restrictions has been reached'); + return; + } + } + break; + case ShopController::TOTAL_BUY_LIMIT: { + $buyRecord = getXVal($buyRecordHash, $id); + $boughtTimes = $buyRecord ? $buyRecord['total_buy_times'] + 1 : 1; + if ($buyRecord && getXVal($buyRecord, 'total_buy_times', 0) >= $row['limit_num']) { + $this->_rspErr(2, 'The maximum number of purchase restrictions has been reached'); + return; + } + if ($row['limit_num'] <= 0) { + $this->_rspErr(2, 'he maximum number of purchase restrictions has been reached'); + return; + } + } + break; + default: { + } + break; + } + + $price_array = splitStr1($row['price']); + $discount_array = splitStr1($row['discount']); + + $need_price = $price_array[$token_pos]; + $discount = $discount_array[$token_pos]; + + $discount_begin = strtotime($row['discount_begin'] . ' UTC'); + $discount_end = strtotime($row['discount_end'] . ' UTC'); + $nowTime = $this->_getNowTime(); + + if ($nowTime >= $discount_begin && $nowTime < $discount_end) { + + $need_price = ceil($need_price * ($discount / 100.0)); + } + + $costItemId = $this->getCostItemIdByTokenType($token_type); + + switch ($token_type) { + case ShopController::TOKEN_TYPE_CEG: + case ShopController::TOKEN_TYPE_CEC: + + $price = $this->normalizeWeb3Price($goods_num * $need_price); + $item_id = $row['goods_id']; + $item_count = $goods_num; + + $response = services\BlockChainService::gameItemMallBuy( + Transaction::BUY_GOODS_ACTION_TYPE, + $price, + $item_id, + $item_count, + ); + + BcOrder::upsert( $response['trans_id'], array( + 'item_id' => $item_id, + 'item_num' => $item_count, + 'order_type' => 1, + 'ext_data' => json_encode(array( + 'mode' => SHOP_BUY_MODE_NORMAL, + )), + )); + + $this->_rspOK(); + break; + + case ShopController::TOKEN_TYPE_BCEG: + break; + + case ShopController::TOKEN_TYPE_USDT: + case ShopController::TOKEN_TYPE_USDC: + case ShopController::TOKEN_TYPE_BUSD: + case ShopController::TOKEN_TYPE_MATIC: + case ShopController::TOKEN_TYPE_BNB: + default: + $this->_rspErr(1, "token_type is unsupport, {$token_type}"); + } + } + public function buyGoodsDS() { $address = $this->_getAddress(); diff --git a/webapp/services/callback/BuyShopGoodsCbService.php b/webapp/services/callback/BuyShopGoodsCbService.php index 05c2de70..289d8b1b 100644 --- a/webapp/services/callback/BuyShopGoodsCbService.php +++ b/webapp/services/callback/BuyShopGoodsCbService.php @@ -3,55 +3,94 @@ namespace services; +require_once('mt/Dailyselection.php'); + define('V_ORDER_TYPE_BUY_SHOP_GOODS', 1); use phpcommon\SqlHelper; +use mt\Dailyselection; class BuyShopGoodsCbService { - public function process($order){ - switch($order['order_type']){ - case V_ORDER_TYPE_BUY_SHOP_GOODS : { - $ext_data = json_decode($order['ext_data'], true); - switch($ext_data['mode']) - { - case SHOP_BUY_MODE_NORMAL: - $this->_buyNormal($order, $ext_data); - break; - case SHOP_BUY_MODE_DAILY_SELECTION: - $this->_buyDailySelection($order, $ext_data); - break; + public function process($order) + { + switch ($order['order_type']) { + case V_ORDER_TYPE_BUY_SHOP_GOODS: { + $ext_data = json_decode($order['ext_data'], true); + switch ($ext_data['mode']) { + case SHOP_BUY_MODE_NORMAL: + $this->_buyNormal($order, $ext_data); + break; + case SHOP_BUY_MODE_DAILY_SELECTION: + $this->_buyDailySelection($order, $ext_data); + break; + } + } + break; + default: { } - } - break; - default : { - - } } } - private function _buyNormal($order, $ext_data){ + private function _buyNormal($order, $ext_data) + { + $self = myself(); + if (!$self) { + return; + } + + $address = $order['address']; $ext_data = $order['ext_data']; - $goods_id = $ext_data['goods_id']; - $goods_num = $ext_data['goods_num']; - $goodsDb = SqlHelper::ormSelectOne( - myself()->_getMysql($order['address']), - 't_shop_goods', - array( - 'id' => $goods_id - ) - ); - $goodsDb['goods_num'] = $goods_num; - $this->_addGoods($order['address'],$goodsDb); + $goods_id = $order['item_id']; + $goods_num = $order['item_num']; + + $this->_addGoods($address, array( + 'goods_id' => $goods_id, + 'goods_num' => $goods_num, + )); } - private function _buyDailySelection($order, $ext_data){ + private function _buyDailySelection($order, $ext_data) + { + $self = myself(); + if (!$self) { + return; + } + $order_id = $order['order_id']; + $item_id = $order['item_id']; + $item_num = $order['item_num']; + $address = $order['address']; $idx = $ext_data['idx']; $grid = $ext_data['grid']; $count = $ext_data['count']; - + $conn = $self->_getMysql($address); + $sql = "SELECT count_$grid FROM t_shop_dailyselection WHERE idx = $idx"; + $chk = $conn->execQuery($sql); + if (!$chk) { + return; + } + if ($chk[0]['count_' . $grid] < $count) { + error_log("BuyShopGoodsCbService::_buyDailySelection() count not enough, address: $address, order_id: $order_id, idx: $idx, grid: $grid, count: $count"); + return; + } + + $sql = "UPDATE t_shop_dailyselection SET count_$grid = count_$grid - $count WHERE idx = $idx"; + $chk = $conn->execScript($sql); + + if ($chk) { + $this->_addGoods($address, array( + 'goods_id' => $item_id, + 'goods_num' => $item_num, + )); + } else { + error_log("BuyShopGoodsCbService::_buyDailySelection() decDailySelectionItem failed, address: $address, order_id: $order_id, idx: $idx, grid: $grid, count: $count"); + } } - -} \ No newline at end of file + + private function _addGoods($address, $goods) + { + error_log("BuyShopGoodsCbService::_addGoods() address: $address, goods: " . json_encode($goods)); + } +}