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.

81 lines
2.5 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Service;
use App\Exception\BusinessException;
use App\Model\GameData;
use App\Model\Spend;
use App\Model\UserData;
use App\Model\UserPlayDataCount;
use App\Helper\Log;
use Hyperf\DbConnection\Db;
class SpendService extends Service
{
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();
}
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)]
);
}
}