game2006api/webapp/controller/InGameMallController.class.php
hujiabin 8c1ac9e827 1
2024-04-25 14:42:09 +08:00

511 lines
16 KiB
PHP

<?php
require_once('phpcommon/bignumber.php');
require_once('mt/Item.php');
require_once('mt/Parameter.php');
require_once('models/BcOrder.php');
require_once('models/Mall.php');
require_once('models/InGameMall.php');
require_once('models/OrderId.php');
require_once('models/Hero.php');
require_once('models/Chip.php');
require_once('models/Bag.php');
require_once('services/BlockChainService.php');
require_once('services/LogService.php');
require_once('services/AwardService.php');
require_once('services/PropertyChgService.php');
use phpcommon\SqlHelper;
use models\BcOrder;
use models\Mall;
use models\InGameMall;
use models\OrderId;
use models\Hero;
use models\Chip;
use models\Bag;
use services\LogService;
use services\BlockChainService;
use services\AwardService;
use services\PropertyChgService;
/*
碎片、芯片、宝箱、英雄
*/
class InGameMallController extends BaseAuthedController {
public function _handlePre()
{
parent::_handlePre();
}
public function productList()
{
$page = getReqVal('page', 0);
$seller = getReqVal('seller', '');
$queryData = array();
if (!empty($seller)) {
$queryData['seller'] = $seller;
}
$type_filter = getReqVal('type_filter', '');
if (!empty($type_filter)) {
$queryData['type_filter'] = $type_filter;
}
$item_filter = getReqVal('item_filter', '');
if (!empty($item_filter)) {
$queryData['item_filter'] = $item_filter;
}
$queryData['price_filter'] = getReqVal('price_filter', '');
$orderBy = '';
$orderAsc = 'ASC';
if (getReqVal('order_asc', '') == 1) {
$orderAsc = 'DESC';
}
switch (getReqVal('order_method', '')) {
case 1:
{
$orderBy = 'ORDER BY createtime ' . $orderAsc;
}
break;
case 2:
{
$orderBy = 'ORDER BY length(price) ' . $orderAsc . ', price ' . $orderAsc;
}
break;
}
$out = array(
'pagination' => array(),
'rows' => array()
);
SqlHelper::rawQueryPage(
myself()->_getMySql(''),
'SELECT * FROM t_ingame_mall WHERE status=:status',
array(
':status' => Mall::PENDING_STATE
),
array(
'page' => $page,
'perPage' => 8,
'filter' => array(
'data' => $queryData,
'fields' => array(
array(
'name' => 'seller',
'field_name' => 'seller',
'cond' => '=',
'ignore_empty' => true,
),
array(
'name' => 'price_filter',
'field_name' => '',
'cond' => 'custom',
'ignore_empty' => true,
'custom_func' => function () use ($queryData) {
$priceFilters = $queryData['price_filter'];
$arrPriceFilter = explode('|', $priceFilters);
$priceLow = $arrPriceFilter[0];
$priceHigh = $arrPriceFilter[1];
return " AND (length(price) >= length('${priceLow}') AND length(price) <= length('${priceHigh}')) "
. " AND (price >= '${priceLow}' AND price <= '${priceHigh}') ";
}
),
array(
'name' => 'item_filter',
'field_name' => 'item_id',
'cond' => '=',
'ignore_empty' => true,
),
// array(
// 'name' => 'type_filter',
// 'field_name' => 'order_type',
// 'cond' => '=',
// 'ignore_empty' => true,
// ),
array(
'name' => 'type_filter',
'field_name' => '',
'cond' => 'custom',
'ignore_empty' => true,
'custom_func' => function () use ($queryData) {
$typeFilter = $queryData['type_filter'];
if ($typeFilter >= 4){
return "AND (order_type >= 4)";
}else{
return "AND (order_type = '${typeFilter}')";
}
}
),
)
),
'orderBy' => $orderBy,
'handle' => function ($row) use(&$out) {
array_push($out['rows'], $row);
}
),
$out['pagination']
);
myself()->_rspData($out);
}
public function sell()
{
$goodsUniid = getReqVal('goods_uniid', '');
$itemId = getReqVal('item_id', '');
$amount = intval(getReqVal('amount', ''), 10);
$priceBn = intval(getReqVal('price', ''));
$itemMeta = \mt\Item::get($itemId);
if (!$itemMeta){
$this->_rspErr(1, 'param item_id error');
return;
}
if (! $this->_isGoodsType($itemMeta['type'])){
$this->_rspErr(1, 'param item_id error');
return;
}
switch ($itemMeta['type']){
case \mt\Item::HERO_TYPE : {
$heroDb = Hero::find($goodsUniid);
if (!$heroDb || !empty($heroDb['token_id']) || $heroDb['hero_id']!= $itemId){
$this->_rspErr(1, 'param goods_unnid error');
return;
}
}
break;
case \mt\Item::CHIP_TYPE : {
$chipDb = Chip::find($goodsUniid);
if (!$chipDb || !empty($chipDb['token_id']) || $chipDb['item_id']!= $itemId){
$this->_rspErr(1, 'param goods_unnid error');
return;
}
}
break;
case \mt\Item::GOLD_SYN :{
$itemDb = Bag::findByUniId($goodsUniid);
if (!$itemDb || !empty($itemDb['token_id']) || $itemDb['item_id']!= $itemId){
$this->_rspErr(1, 'param goods_unnid error');
return;
}
if ($itemDb['item_num'] <= 0 || $amount > 1){
$this->_rspErr(1, 'amount not enough');
return;
}
}
}
if ($amount <= 0) {
$this->_rspErr(1, 'amount must > 0');
return;
}
if (!$this->_isNumber($priceBn) || $priceBn < 0) {
$this->_rspErr(1, 'param price error');
return;
}
$propertyChgService = new PropertyChgService();
if (\mt\Item::isBagItem($itemMeta['type'],$itemMeta['sub_type'])){
$costItems = array(
array(
'item_id' => $itemId,
'item_num' => $amount
)
);
$lackItem = null;
if (!$this->_hasEnoughItems($costItems, $lackItem)) {
$this->_rspErr(2, $this->_getLackItemErrMsg($lackItem));
return;
}
$this->_decItems($costItems);
$propertyChgService->addBagChg();
}elseif ($itemMeta['type'] == \mt\Item::HERO_TYPE){
SqlHelper::update
(myself()->_getSelfMysql(),
't_hero',
array(
'idx' => $goodsUniid,
),
array(
'account_id' => InGameMall::SYSTEM_MALL_ACCOUNT
)
);
$propertyChgService->addHeroChg();
}elseif ($itemMeta['type'] == \mt\Item::CHIP_TYPE){
SqlHelper::update
(myself()->_getSelfMysql(),
't_chip',
array(
'idx' => $goodsUniid,
),
array(
'account_id' => InGameMall::SYSTEM_MALL_ACCOUNT
)
);
$propertyChgService->addChip();
}elseif ($itemMeta['type'] == \mt\Item::GOLD_SYN){
SqlHelper::update
(myself()->_getSelfMysql(),
't_bag',
array(
'idx' => $goodsUniid,
),
array(
'account_id' => InGameMall::SYSTEM_MALL_ACCOUNT
)
);
$propertyChgService->addBagChg();
}
$orderId = OrderId::gen();
$orderType = $this->_getGoodsType($itemMeta['type']);
InGameMall::add(
$orderId,
$orderType,
$goodsUniid,
$itemId,
$amount,
$priceBn
);
myself()->_rspData(array(
'property_chg' => $propertyChgService->toDto(),
));
}
public function buy(){
$orderId = getReqVal('order_id', '');
$goodsDb = InGameMall::findByOrderId($orderId);
if (!$goodsDb) {
myself()->_rspErr(1, 'goods not found');
return;
}
if ($goodsDb['status'] != InGameMall::PENDING_STATE){
myself()->_rspErr(1, 'cannot cancel the goods');
return;
}
$price = $goodsDb['price'];
$costItems = array(
array(
'item_id' => V_ITEM_GOLD,
'item_num' => $price
)
);
$lackItem = null;
if (!$this->_hasEnoughItems($costItems, $lackItem)) {
$this->_rspErr(2, $this->_getLackItemErrMsg($lackItem));
return;
}
$this->_decItems($costItems);
SqlHelper::update
($this->_getSelfMysql(),
't_user',
array(
'account_id' => $goodsDb['seller']
),
array(
'gold' => function () use($price) {
return "gold + ${price}";
}
)
);
$propertyChgService = new PropertyChgService();
switch ($goodsDb['order_type']){
case InGameMall::HERO_TYPE :{
SqlHelper::update
(myself()->_getSelfMysql(),
't_hero',
array(
'idx' => $goodsDb['goods_uniid'],
),
array(
'account_id' => myself()->_getAccountId()
)
);
$propertyChgService->addHeroChg();
}
break;
case InGameMall::CHIP_TYPE:{
SqlHelper::update
(myself()->_getSelfMysql(),
't_chip',
array(
'idx' => $goodsDb['goods_uniid'],
),
array(
'account_id' => myself()->_getAccountId()
)
);
$propertyChgService->addChip();
}
break;
case InGameMall::GOLD_TYPE :{
SqlHelper::update
(myself()->_getSelfMysql(),
't_bag',
array(
'idx' => $goodsDb['goods_uniid'],
),
array(
'account_id' => myself()->_getAccountId()
)
);
$propertyChgService->addBagChg();
}
break;
default : {
Bag::addItem($goodsDb['item_id'],$goodsDb['item_num']);
$propertyChgService->addBagChg();
}
}
InGameMall::buyOk($orderId,myself()->_getAccountId());
myself()->_rspData(array(
'property_chg' => $propertyChgService->toDto(),
));
}
public function cancel()
{
$orderId = getReqVal('order_id', '');
$goodsDb = InGameMall::findByOrderId($orderId);
if (!$goodsDb) {
myself()->_rspErr(1, 'goods not found');
return;
}
if ($goodsDb['status'] != InGameMall::PENDING_STATE){
myself()->_rspErr(1, 'cannot cancel the goods');
return;
}
if ($goodsDb['seller'] != myself()->_getAccountId()){
myself()->_rspErr(1, 'Not the seller');
return;
}
$propertyChgService = new PropertyChgService();
switch ($goodsDb['order_type']){
case InGameMall::HERO_TYPE :{
SqlHelper::update
(myself()->_getSelfMysql(),
't_hero',
array(
'idx' => $goodsDb['goods_uniid'],
),
array(
'account_id' => myself()->_getAccountId()
)
);
$propertyChgService->addHeroChg();
}
break;
case InGameMall::CHIP_TYPE:{
SqlHelper::update
(myself()->_getSelfMysql(),
't_chip',
array(
'idx' => $goodsDb['goods_uniid'],
),
array(
'account_id' => myself()->_getAccountId()
)
);
$propertyChgService->addChip();
}
break;
case InGameMall::GOLD_TYPE:{
SqlHelper::update
(myself()->_getSelfMysql(),
't_bag',
array(
'idx' => $goodsDb['goods_uniid'],
),
array(
'account_id' => myself()->_getAccountId()
)
);
$propertyChgService->addBagChg();
}
break;
default : {
Bag::addItem($goodsDb['item_id'],$goodsDb['item_num']);
$propertyChgService->addBagChg();
}
}
InGameMall::cancel($orderId);
myself()->_rspData(array(
'property_chg' => $propertyChgService->toDto(),
));
}
public function modifyPrice()
{
$orderId = getReqVal('order_id', '');
$priceBn =intval(getReqVal('price', ''));
if (!$this->_isNumber($priceBn)) {
$this->_rspErr(1, 'param price error');
return;
}
$goodsDb = InGameMall::findByOrderId($orderId);
if (!$goodsDb) {
myself()->_rspErr(1, 'goods not found');
return;
}
if ($goodsDb['status'] != InGameMall::PENDING_STATE){
myself()->_rspErr(1, 'cannot cancel the goods');
return;
}
if ($goodsDb['seller'] != myself()->_getAccountId()){
myself()->_rspErr(1, 'Not the seller');
return;
}
InGameMall::modifyPrice($orderId, $priceBn);
myself()->_rspOk();
}
private function _isNumber($number){
if (is_int($number) && $number > 0){
return true;
}else{
return false;
}
}
private function _isGoodsType($type){
if (in_array($type,array(
\mt\Item::HERO_TYPE,
\mt\Item::CHIP_TYPE,
\mt\Item::FRAGMENT_TYPE,
\mt\Item::BATTLE_REWARD_BOX,
\mt\Item::GOLD_SYN,
))){
return true;
}else{
return false;
}
}
private function _getGoodsType($type){
switch ($type){
case \mt\Item::HERO_TYPE : {
return InGameMall::HERO_TYPE;
}
case \mt\Item::CHIP_TYPE : {
return InGameMall::CHIP_TYPE;
}
case \mt\Item::FRAGMENT_TYPE : {
return InGameMall::FRAGMENT_TYPE;
}
case \mt\Item::BATTLE_REWARD_BOX : {
return InGameMall::BOX_TYPE;
}
case \mt\Item::GOLD_SYN : {
return InGameMall::GOLD_TYPE;
}
default : {
return 0;
}
}
}
}