diff --git a/webapp/controller/ShopController.class.php b/webapp/controller/ShopController.class.php index 14bb6294..4d67c510 100644 --- a/webapp/controller/ShopController.class.php +++ b/webapp/controller/ShopController.class.php @@ -44,7 +44,8 @@ use models\Transaction; use models\BcOrder; if (!function_exists('array_column')) { - function array_column($arr2, $column_key) { + function array_column($arr2, $column_key) + { $data = []; foreach ($arr2 as $arr) { $data[] = $arr[$column_key]; @@ -139,6 +140,10 @@ class ShopController extends BaseAuthedController if (empty($goods['goods_num'])) { $goods['goods_num'] = 1; } + if (!empty($goods['free_type'])) { + $count = $this->countFreeBuyTimes($goods); + $goods['free_num'] = $goods['free_num'] - $count; + } } $this->_rspData( @@ -834,11 +839,20 @@ class ShopController extends BaseAuthedController $row = mt\ShopGoods::get($id); $goods_id = $row['goods_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)) { + $isFreeBuy = false; + if (!empty($row['free_type'])) { + $count = $this->countFreeBuyTimes($row); + if ($count < $row['free_num']) { + $isFreeBuy = true; + } else { + $this->_rspErr(1, "free_num parameter error, free_num: {$row['free_num']}"); + return; + } + } else if (!in_array($token_type, $check_token_type)) { $this->_rspErr(1, "token_type parameter error, desired_token_type: {$desired_token_type}"); return; } @@ -964,36 +978,43 @@ class ShopController extends BaseAuthedController 'property_chg' => $propertyChgService->toDto(), 'goods_chg' => $goodsDto ) - ); + ); break; 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, - )), - )); - - $response['item_id'] = $item_id; - $response['item_num'] = $item_count; - $this->_rspData( - $response - ); + if ($isFreeBuy) { + $propertyChgService = new services\PropertyChgService(); + $this->addFreeBuyRecord($row); + $itemMeta = mt\Item::get($row['goods_id']); + $this->internalAddItem($propertyChgService, $itemMeta, $goods_count); + $this->_rspOk(); + } else { + $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, + )), + )); + + $response['item_id'] = $item_id; + $response['item_num'] = $item_count; + $this->_rspData( + $response + ); + } break; case ShopController::TOKEN_TYPE_BCEG: @@ -1028,7 +1049,7 @@ class ShopController extends BaseAuthedController 't_shop_dailyselection', array( 'idx', - 'address', + 'address', 'grid_' . $grid, 'count_' . $grid, ), @@ -1062,7 +1083,7 @@ class ShopController extends BaseAuthedController $price = $this->normalizeWeb3Price($goods['price'] * $count); $item_id = $goods['goods_id']; $item_count = $goods['goods_num'] * $count; - + $response = services\BlockChainService::gameItemMallBuy( Transaction::BUY_GOODS_ACTION_TYPE, $price, @@ -1070,7 +1091,7 @@ class ShopController extends BaseAuthedController $item_count ); - BcOrder::upsert( $response['trans_id'], array( + BcOrder::upsert($response['trans_id'], array( 'item_id' => $item_id, 'item_num' => $item_count, 'order_type' => 1, @@ -1093,8 +1114,8 @@ class ShopController extends BaseAuthedController } - private function buyGoodsFree() { - + private function buyGoodsFree() + { } private function decDailySelectionItem($idx, $grid, $count) @@ -1234,7 +1255,7 @@ class ShopController extends BaseAuthedController return $row[0]['cnt']; } - + private function outsideBuy($shopId, $itemId, $itemNum, $costItemId) { $propertyChgService = new services\PropertyChgService(); @@ -1534,4 +1555,51 @@ class ShopController extends BaseAuthedController return $price; } + + private function countFreeBuyTimes($goods) { + $conn = myself()->_getMysql(''); + $account = myself()->_getAccountId(); + + switch($goods['free_type']) + { + case 1: + { + $dayTime = myself()->_getNowDaySeconds(); + $sql = 'SELECT COUNT(*) as cnt FROM t_shop_free_record WHERE account_id = ? AND `id` = ? AND goods_id = ? AND createtime >= ?'; + $row = $conn->execQueryOne($sql, array($account, $goods['id'], $goods['goods_id'], $dayTime)); + return $row['cnt']; + } + break; + } + + return 0; + } + + private function addFreeBuyRecord($goods) { + $conn = myself()->_getMysql(''); + $account = myself()->_getAccountId(); + + switch($goods['free_type']) + { + case 1: + { + $dayTime = myself()->_getNowTime(); + SqlHelper::insert( + $conn, + 't_shop_free_record', + array( + 'account_id' => $account, + 'shop_id' => $goods['shop_id'], + 'id' => $goods['id'], + 'goods_id' => $goods['goods_id'], + 'goods_num' => $goods['goods_num'], + 'free_type' => $goods['free_type'], + 'free_num' => $goods['free_num'], + 'createtime' => $dayTime, + ) + ); + } + break; + } + } }