You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
4.2 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Service;
use App\Exception\BusinessException;
use App\Helper\Client\Lanhuo;
use App\Model\GameData;
use App\Model\Spend;
use App\Model\UserData;
use App\Model\UserPlayDataCount;
use App\Helper\Log;
use Hyperf\DbConnection\Db;
use Hyperf\Di\Annotation\Inject;
use App\Repository\PromoteRepository;
class SpendService extends Service
{
/**
* @Inject
* @var PromoteRepository
*/
private $promoteRepository;
public function afterSpend($payOrderNumber)
{
Log::info('[GAME-RECHARGE]产生充值订单:' . $payOrderNumber);
$spend = Spend::where('pay_order_number', $payOrderNumber)->first();
if (!$spend) {
throw new BusinessException('订单不存在');
}
if ($spend->pay_status != 1) {
throw new BusinessException('订单支付未成功');
}
if ($spend->event_status == 1) {
throw new BusinessException('该订单已处理');
}
$this->saveUserPlayDataCountBySpend($spend);
$this->addUserDataPayAmount($spend);
$this->addGameDataPayAmount($spend);
$spend->event_status = 1;
$spend->save();
if (in_array($spend->game_id, [335, 336])) {
$levelPromotes = $this->promoteRepository->getLevelPromotesById($spend->promote_id, ['id', 'level', 'account']);
$levelPromotes = $levelPromotes->indexBy('level');
Lanhuo::addOrder([
'order_code' => $spend->pay_order_number,
'user_id' => $spend->user_id,
'account' => $spend->user_account,
'server_id' => $spend->server_id,
'server_name' => $spend->server_name,
'role_id' => $spend->game_player_id,
'role_name' => $spend->game_player_name,
'game_name' => $spend->game_name,
'order_amount' => $spend->pay_amount,
'pay_time' => date('Y-m-d H:i:s', $spend->spend_time),
'status' => $spend->pay_status, //支付状态(待支付 0 已支付 1 支付失败 2)
'minister_id' => isset($levelPromotes[2]) ? $levelPromotes[2]->id : 0,
'minister_name' => isset($levelPromotes[2]) ? $levelPromotes[2]->account : '',
'headman_id' => isset($levelPromotes[3]) ? $levelPromotes[3]->id : 0,
'headman_name' => isset($levelPromotes[3]) ? $levelPromotes[3]->account : '',
'promoter_id' => isset($levelPromotes[4]) ? $levelPromotes[4]->id : 0,
'promoter_name' => isset($levelPromotes[4]) ? $levelPromotes[4]->account : '',
]);
}
}
private function saveUserPlayDataCountBySpend(Spend $spend)
{
$date = strtotime(date('Y-m-d 00:00:00', $spend->pay_time));
$record = UserPlayDataCount::where('user_id', $spend->user_id)
->where('game_id', $spend->game_id)
->where('server_id', $spend->server_id)
->where('role_id', $spend->game_player_id)
->where('create_time', $date)
->first();
if (!$record) {
$record = new UserPlayDataCount();
$record->user_id = $spend->user_id;
$record->promote_id = $spend->promote_id;
$record->game_id = $spend->game_id;
$record->server_id = $spend->server_id;
$record->role_id = $spend->game_player_id;
$record->create_time = $date;
$record->recharge_cost = $spend->pay_amount;
$record->recharge_count = 1;
} else {
$record->recharge_cost = Db::raw('recharge_cost+' . $spend->pay_amount);
$record->recharge_count = Db::raw('recharge_count+1');
}
$record->save();
}
private function addUserDataPayAmount($spend)
{
UserData::updateOrCreate(
['user_id' => $spend->user_id],
['pay_amount' => Db::raw('pay_amount+' . $spend->pay_amount)]
);
}
private function addGameDataPayAmount($spend)
{
GameData::updateOrCreate(
['game_id' => $spend->game_id],
['pay_amount' => Db::raw('pay_amount+' . $spend->pay_amount)]
);
}
}