game2006api/webapp/controller/InGameMallController.class.php
2024-08-01 16:21:54 +08:00

741 lines
24 KiB
PHP

<?php
require_once('phpcommon/bignumber.php');
require_once('mt/Item.php');
require_once('mt/Parameter.php');
require_once('mt/Language.php');
require_once('models/BcOrder.php');
require_once('models/Mall.php');
require_once('models/InGameMall.php');
require_once('models/OrderId.php');
require_once('models/HeroSkin.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');
require_once('services/MailApiService.php');
use phpcommon\SqlHelper;
use models\BcOrder;
use models\Mall;
use models\InGameMall;
use models\OrderId;
use models\HeroSkin;
use models\Chip;
use models\Bag;
use services\LogService;
use services\BlockChainService;
use services\AwardService;
use services\PropertyChgService;
/*
碎片、芯片、宝箱、英雄
*/
class InGameMallController extends BaseAuthedController {
private $mailApiService = null;
public function _handlePre()
{
parent::_handlePre();
$this->mailApiService = new \services\MailApiService();
}
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;
}
$quality_filter = getReqVal('quality_filter', '');
if (!empty($quality_filter)) {
$queryData['quality_filter'] = $quality_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(unit_price) ' . $orderAsc . ', unit_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' => '',
'cond' => 'custom',
'ignore_empty' => true,
'custom_func' => function () use ($queryData) {
$itemFilters = $queryData['item_filter'];
$arrItemFilters = explode('|', $itemFilters);
if(count($arrItemFilters) > 1){
$strWhere = implode(',',$arrItemFilters);
return "AND item_id IN (${strWhere})";
}else{
return "AND item_id = '${itemFilters}'";
}
}
),
array(
'name' => 'quality_filter',
'field_name' => 'order1',
'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 >= 5){
// return "AND (order_type >= 5)";
// }else{
return "AND (order_type = '${typeFilter}')";
// }
}
),
)
),
'orderBy' => $orderBy,
'handle' => function ($row) use(&$out) {
switch ($row['order_type']){
case InGameMall::HERO_SKIN_TYPE :{
$row['item_info'] = HeroSkin::toDto(HeroSkin::findEx($row['goods_uniid']));
}
break;
case InGameMall::CHIP_TYPE :{
$row['item_info'] = Chip::toDto(Chip::findEx($row['goods_uniid']));
}
break;
default:{
$row['item_info'] = array();
}
}
array_push($out['rows'], $row);
}
),
$out['pagination']
);
myself()->_rspData($out);
}
public function sellS()
{
$goodsUniid = getReqVal('goods_uniid', '');
$itemId = getReqVal('item_id', '');
$amount = intval(getReqVal('amount', ''), 10);
$priceBn = intval(getReqVal('price', ''));
$itemMeta = \mt\Item::get($itemId);
$orderField = 0;
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_SKIN_TYPE : {
$skinDb = HeroSkin::find($goodsUniid);
if (!$skinDb || $skinDb['skin_state'] != HeroSkin::LOCK){
$this->_rspErr(1, 'param goods_unnid error');
return;
}
$orderField = $itemMeta['quality'];
}
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;
}
$orderField = $chipDb['quality'];
}
break;
default : {
$orderField = $itemMeta['quality'];
}
}
if ($amount <= 0) {
$this->_rspErr(1, 'amount must > 0');
return;
}
$minPrice = \mt\Parameter::getVal("market_place_min_price",100);
if (!$this->_isNumber($priceBn) || $priceBn < 0 || $priceBn < $minPrice) {
$this->_rspErr(1, 'param price error');
return;
}
if ($priceBn > 0xFFFFFFFF) {
$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::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::HERO_SKIN_TYPE){
SqlHelper::update
(myself()->_getSelfMysql(),
't_hero_skin',
array(
'idx' => $goodsUniid,
),
array(
'account_id' => InGameMall::SYSTEM_MALL_ACCOUNT
)
);
$propertyChgService->addHeroChg();
}
$orderId = OrderId::gen();
$orderType = $this->_getGoodsType($itemMeta);
InGameMall::add(
$orderId,
$orderType,
$goodsUniid,
$itemId,
$amount,
$priceBn,
$orderField
);
myself()->_rspData(array(
'property_chg' => $propertyChgService->toDto(),
));
}
public function buyS(){
$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['price'] < 0){
myself()->_rspErr(1, ' goods price error');
return;
}
$costItems = array(
array(
'item_id' => V_ITEM_GOLD,
'item_num' => $goodsDb['price']
)
);
$lackItem = null;
if (!$this->_hasEnoughItems($costItems, $lackItem)) {
$this->_rspErr(2, $this->_getLackItemErrMsg($lackItem));
return;
}
$propertyChgService = new PropertyChgService();
$this->_buyGoods($goodsDb,$propertyChgService);
myself()->_rspData(array(
'property_chg' => $propertyChgService->toDto(),
));
}
private function _buyGoods($goods,$propertyChgService){
if (!$goods){
return;
}
$price = $goods['price'];
InGameMall::buyOk($goods['order_id'],myself()->_getAccountId());
$costItems = array(
array(
'item_id' => V_ITEM_GOLD,
'item_num' => $price
)
);
$this->_decItems($costItems);
switch ($goods['order_type']){
case InGameMall::HERO_SKIN_TYPE :{
SqlHelper::update
(myself()->_getSelfMysql(),
't_hero_skin',
array(
'idx' => $goods['goods_uniid'],
),
array(
'account_id' => myself()->_getAccountId()
)
);
$propertyChgService->addHeroChg();
}
break;
case InGameMall::CHIP_TYPE:{
SqlHelper::update
(myself()->_getSelfMysql(),
't_chip',
array(
'idx' => $goods['goods_uniid'],
),
array(
'account_id' => myself()->_getAccountId()
)
);
$propertyChgService->addChip();
}
break;
default : {
Bag::addItem($goods['item_id'],$goods['item_num']);
$propertyChgService->addBagChg();
}
}
$event = array(
'ID' => 'Mail',
'SUB_ID' => 'Market Place',
'SUB_KEY' => 'sell_mail',
'mailId' =>$goods['order_id'],
'seller' =>$goods['seller'],
'buyer' =>myself()->_getAccountId(),
'change' => array(
'item_id' => $goods['item_id'],
'item_num' => $goods['item_num']
),
);
LogService::burialPointEvent($event);
//发邮件给卖家
$itemMeta = \mt\Item::get($goods['item_id']);
$languageMeta = \mt\Language::get($itemMeta['name']);
$itemName = $languageMeta ? $languageMeta['en'] : "The Item";
$taxRate = \mt\Parameter::getVal("market_place_tax_rate",0.05);
$taxGold = ceil($price*$taxRate);
$falGold = $price - $taxGold;
$mailContent = <<<EOD
{$itemName} has been successfully sold
Transaction price: + {$price} gold
Service fee: - {$taxGold} gold
After selling {$itemName}, you received {$falGold} gold
EOD;
$attachments = array(
array(
'itemid' => V_ITEM_GOLD,
'itemnum' => $falGold
)
);
$this->mailApiService->sendSellMail(
$goods['order_id'],
$goods['seller'],
\services\MailApiService::SELL_MAIL_SUBJECT,
$mailContent,
$attachments
);
$event1 = array(
'ID' => 'Mail',
'SUB_ID' => 'Market Place',
'SUB_KEY' => 'income_mail',
'mailId' =>$goods['order_id'],
'seller' =>$goods['seller'],
'buyer' =>myself()->_getAccountId(),
'change' => array(
'item_id' => V_ITEM_GOLD,
'item_num' => $falGold
),
);
LogService::burialPointEvent($event1);
}
public function cancelS()
{
$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;
}
InGameMall::cancel($orderId);
$propertyChgService = new PropertyChgService();
switch ($goodsDb['order_type']){
case InGameMall::HERO_SKIN_TYPE :{
SqlHelper::update
(myself()->_getSelfMysql(),
't_hero_skin',
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;
default : {
Bag::addItem($goodsDb['item_id'],$goodsDb['item_num']);
$propertyChgService->addBagChg();
}
}
myself()->_rspData(array(
'property_chg' => $propertyChgService->toDto(),
));
}
public function modifyPrice()
{
$orderId = getReqVal('order_id', '');
$priceBn =intval(getReqVal('price', ''));
$minPrice = \mt\Parameter::getVal("market_place_min_price",100);
if (!$this->_isNumber($priceBn) || $priceBn < $minPrice) {
$this->_rspErr(1, 'param price error');
return;
}
if ($priceBn > 0xFFFFFFFF) {
$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;
}
$unitPrice = $priceBn / $goodsDb['item_num'];
InGameMall::modifyPrice($orderId, $priceBn,$unitPrice);
myself()->_rspOk();
}
public function sellList(){
$page = getReqVal('page', 0);
$orderBy = '';
$orderAsc = 'ASC';
if (getReqVal('order_asc', '') == 1) {
$orderAsc = 'DESC';
}
switch (getReqVal('order_method', '')) {
case 1:
{
$orderBy = 'ORDER BY buy_ok_time ' . $orderAsc;
}
break;
case 2:
{
$orderBy = 'ORDER BY length(unit_price) ' . $orderAsc . ', unit_price ' . $orderAsc;
}
break;
}
$out = array(
'pagination' => array(),
'rows' => array()
);
SqlHelper::rawQueryPage(
myself()->_getMySql(''),
'SELECT * FROM t_ingame_mall WHERE seller=:seller AND status=:status',
array(
':seller' => myself()->_getAccountId(),
':status' => Mall::BUY_OK_STATE,
),
array(
'page' => $page,
'perPage' => 8,
'orderBy' => $orderBy,
'handle' => function ($row) use(&$out) {
array_push($out['rows'], $row);
}
),
$out['pagination']
);
myself()->_rspData($out);
}
public function buyList(){
$page = getReqVal('page', 0);
$orderBy = '';
$orderAsc = 'ASC';
if (getReqVal('order_asc', '') == 1) {
$orderAsc = 'DESC';
}
switch (getReqVal('order_method', '')) {
case 1:
{
$orderBy = 'ORDER BY buy_ok_time ' . $orderAsc;
}
break;
case 2:
{
$orderBy = 'ORDER BY length(unit_price) ' . $orderAsc . ', unit_price ' . $orderAsc;
}
break;
}
$out = array(
'pagination' => array(),
'rows' => array()
);
SqlHelper::rawQueryPage(
myself()->_getMySql(''),
'SELECT * FROM t_ingame_mall WHERE buyer=:buyer AND status=:status',
array(
':buyer' => myself()->_getAccountId(),
':status' => Mall::BUY_OK_STATE,
),
array(
'page' => $page,
'perPage' => 8,
'orderBy' => $orderBy,
'handle' => function ($row) use(&$out) {
array_push($out['rows'], $row);
}
),
$out['pagination']
);
myself()->_rspData($out);
}
public function shoppingCartList(){
$orderIds = getReqVal('order_ids', '');
if (!$orderIds){
$this->_rspErr(1, 'param is not null');
return;
}
$orderIdArr = explode("|",$orderIds);
$list = array();
foreach ($orderIdArr as $orderId){
$orderDb = InGameMall::findByOrderId($orderId);
if ($orderDb){
switch ($orderDb['order_type']){
case InGameMall::HERO_SKIN_TYPE :{
$orderDb['item_info'] = HeroSkin::toDto(HeroSkin::findEx($orderDb['goods_uniid']));
}
break;
case InGameMall::CHIP_TYPE :{
$orderDb['item_info'] = Chip::toDto(Chip::findEx($orderDb['goods_uniid']));
}
break;
default:{
$orderDb['item_info'] = array();
}
}
array_push($list,$orderDb);
}
}
$this->_rspData(array(
'list' => $list
));
}
public function shoppingCartBuyS(){
$orderIds = getReqVal('order_ids', '');
if (!$orderIds){
$this->_rspErr(1, 'param is not null');
return;
}
$orderIdArr = explode("|",$orderIds);
$price = 0;
foreach ($orderIdArr as $orderId) {
$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;
}
$propertyChgService = new PropertyChgService();
foreach ($orderIdArr as $orderId) {
$goodsDb = InGameMall::findByOrderId($orderId);
if ($goodsDb['status'] != InGameMall::PENDING_STATE){
continue;
}
$this->_buyGoods($goodsDb,$propertyChgService);
}
myself()->_rspData(array(
'property_chg' => $propertyChgService->toDto(),
));
}
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_SKIN_TYPE, //皮肤
\mt\Item::CHIP_TYPE, //芯片
\mt\Item::FRAGMENT_TYPE, //碎片
\mt\Item::BATTLE_REWARD_BOX,//宝箱
\mt\Item::MATERIAL_TYPE, //材料(芯片合成材料)
))){
return true;
}else{
return false;
}
}
private function _getGoodsType($itemMeta){
if (!$itemMeta){
return ;
}
switch ($itemMeta['type']){
case \mt\Item::HERO_SKIN_TYPE : {
return InGameMall::HERO_SKIN_TYPE;
}
case \mt\Item::CHIP_TYPE : {
return InGameMall::CHIP_TYPE;
}
case \mt\Item::FRAGMENT_TYPE : {
return InGameMall::HERO_FRAGMENT_TYPE;
}
case \mt\Item::BATTLE_REWARD_BOX : {
return InGameMall::OTHER_TYPE;
}
case \mt\Item::MATERIAL_TYPE : {
return InGameMall::OTHER_TYPE;
}
default : {
return 0;
}
}
}
}