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
PHTML

2 years ago
<?php
declare(strict_types=1);
namespace App\Service;
use App\Exception\BusinessException;
2 years ago
use App\Helper\Client\Lanhuo;
2 years ago
use App\Model\GameData;
use App\Model\Spend;
use App\Model\UserData;
use App\Model\UserPlayDataCount;
use App\Helper\Log;
use Hyperf\DbConnection\Db;
2 years ago
use Hyperf\Di\Annotation\Inject;
use App\Repository\PromoteRepository;
2 years ago
class SpendService extends Service
{
2 years ago
/**
* @Inject
* @var PromoteRepository
*/
private $promoteRepository;
2 years ago
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();
2 years ago
2 years ago
if (in_array($spend->game_id, [335, 336])) {
2 years ago
$levelPromotes = $this->promoteRepository->getLevelPromotesById($spend->promote_id, ['id', 'level', 'account']);
$levelPromotes = $levelPromotes->indexBy('level');
2 years ago
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)
2 years ago
'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 : '',
2 years ago
]);
2 years ago
}
2 years ago
}
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)]
);
}
}