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.

293 lines
9.4 KiB
PHP

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace Base\Business;
use Base\Service\PromoteCompanyService;
use Base\Service\PromoteService;
use Exception;
use Think\Model;
class ShiftPlayer
{
private $task;
private $fromPromote;
private $toPromote;
private $fromWord;
private $toWord;
private $orderTime;
private $userIds;
private $users;
private $payAmountItems = [];
private $notices = [];
public function __construct($task)
{
$this->task = $task;
$this->orderTime = $task['order_time'];
$this->userIds = json_decode($task['shift_ids'], true) ?? [];
$this->getPromotes();
$this->getShiftUsers();
$this->statShiftPayAmount();
}
public function handle()
{
if ($this->fromPromote['id'] == 0 && count($this->userIds) == 0) {
throw new Exception('官方渠道玩家不能全部迁移');
}
$this->getNoticeCommonWords();
$model = new Model();
$model->startTrans();
$hasError = false;
foreach ($this->users as $user) {
$amount = $this->payAmountItems[$user['id']] ?? 0;
$status = $this->updateMend($user['id'], $amount);
if (!$status) {
$hasError = true;
break;
}
$this->generateNotice($user, $amount);
}
if ($hasError) {
$model->rollback();
throw new Exception('系统异常,修改补链记录失败');
}
$this->saveNotices();
$status = $this->shiftUser();
if (!$status) {
$model->rollback();
throw new Exception('系统异常,修改用户推广员失败');
}
try {
$this->shiftPlayer();
$this->shiftSpend();
$this->shiftDeposit();
$this->shiftBindSpend();
$this->shiftUserDataCount();
$model->commit();
} catch (\Exception $e) {
$model->rollback();
throw new \Exception('系统异常: ' . $e->getMessage());
}
}
public function handleReplenish()
{
$model = new Model();
$model->startTrans();
$hasError = false;
foreach ($this->payAmountItems as $userId => $amount) {
$status = $this->updateMend($userId, $amount);
if (!$status) {
$hasError = true;
break;
}
}
if ($hasError) {
$model->rollback();
throw new Exception('系统异常,修改补链记录失败');
}
try {
$this->shiftSpend();
$this->shiftDeposit();
$this->shiftBindSpend();
$model->commit();
} catch (\Exception $e) {
$model->rollback();
throw new \Exception('系统异常: ' . $e->getMessage());
}
}
private function getPromotes()
{
$toPromoteId = $this->task['to_promote_id'];
$fromPromoteId = $this->task['from_promote_id'];
$toPromote = M('promote', 'tab_')->where(['id' => $toPromoteId])->find();
$fromPromote = M('promote', 'tab_')->where(['id' => $fromPromoteId])->find();
$this->toPromote = $toPromote ?? ['id' => 0, 'account' => C('OFFICIEL_CHANNEL'), 'company_id' => 0];
$this->fromPromote = $fromPromote ?? ['id' => 0, 'account' => C('OFFICIEL_CHANNEL'), 'company_id' => 0];
}
private function getNoticeCommonWords()
{
$belongs = PromoteCompanyService::$belongs;
$formConpany = M('promote_company', 'tab_')->field(['company_name', 'company_belong'])->where(['id' => $this->fromPromote['company_id']])->find();
$toConpany = M('promote_company', 'tab_')->field(['company_name', 'company_belong'])->where(['id' => $this->toPromote['company_id']])->find();
$this->fromWord = $this->fromPromote['account'] . ($formConpany ? '(' . $belongs[$formConpany['company_belong']] . '-' . $formConpany['company_name'] . ')' : '');
$this->toWord = $this->toPromote['account'] . ($toConpany ? '(' . $belongs[$toConpany['company_belong']] . '-' . $toConpany['company_name'] . ')' : '');
}
private function getShiftUsers()
{
$map = $this->getShiftCommonMap(true);
$this->users = M('user', 'tab_')->field(['id', 'account', 'nickname'])->where($map)->select();
}
private function getSpendMap()
{
$map = $this->getShiftCommonMap();
$map['pay_time'] = ['egt', $this->orderTime];
// $map['is_check'] = ['in','1,2'];
$map['settle_check'] = 0;
$map['selle_status'] = 0;
$map['pay_status'] = 1;
return $map;
}
private function statShiftPayAmount()
{
$spendMap = $this->getSpendMap();
$payAmountRows = M('spend', 'tab_')
->field(['user_id', 'sum(pay_amount) pay_amount'])
->where($spendMap)
->group('user_id')
->select();
foreach ($payAmountRows as $row) {
$this->payAmountItems[$row['user_id']] = round(floatval($row['pay_amount']), 2);
}
}
private function updateMend($userId, $amount)
{
return M('mend', 'tab_')
->where(['task_id' => $this->task['id'], 'user_id' => $userId])
->save(['status' => 1, 'pay_amount' => ['exp', 'pay_amount+' . $amount], 'update_time' => time()]);
}
private function generateNotice($user, $amount)
{
if ($amount > 500) {
$userWord = '玩家账号' . $user['account'];
$content = $userWord . ', 从' . $this->fromWord . '换绑到' . $this->toWord . '换绑金额超过500达到' . $amount . '元';
$this->notices[] = [
'type' => 'shift-player',
'title' => '换绑额度超500',
'content' => $content,
'admin_ids' => '31',
'create_time' => time(),
'target' => $user['account'],
'start_time' => time(),
'end_time' => time() + 7*24*3600,
];
}
}
private function saveNotices()
{
if (count($this->notices) > 0) {
M('admin_notice', 'tab_')->addAll($this->notices);
}
}
private function shiftUser()
{
$map = $this->getShiftCommonMap(true);
return M('user', 'tab_')->where($map)->save([
'promote_id' => $this->toPromote['id'],
'promote_account' => $this->toPromote['account']
]);
}
private function shiftPlayer()
{
$map = $this->getShiftCommonMap();
$updateData = [
'promote_id' => $this->toPromote['id'],
'promote_account' => $this->toPromote['account']
];
M('user_play', 'tab_')->where($map)->save($updateData);
M('user_play_info', 'tab_')->where($map)->save($updateData);
}
private function shiftSpend()
{
$toPromote = $this->toPromote;
$spendMap = $this->getSpendMap();
[$hasGameIds, $hasNotGameIds] = $this->getPromoteGameIds($toPromote);
unset($spendMap['pay_status']);
$updateData = [
'promote_id' => $toPromote['id'],
'promote_account' => $toPromote['account'],
'market_admin_id' => $toPromote['admin_id'],
];
if (count($hasGameIds) > 0) {
$spendMap['game_id'] = ['in', $hasGameIds];
M('spend', 'tab_')->where($spendMap)->save(array_merge($updateData, ['is_check' => 1]));
}
if (count($hasNotGameIds) > 0) {
$spendMap['game_id'] = ['in', $hasNotGameIds];
M('spend', 'tab_')->where($spendMap)->save(array_merge($updateData, ['is_check' => 2]));
}
}
private function getPromoteGameIds($promote)
{
$service = new PromoteService();
$toTopPromote = $service->getTopPromote($promote);
$hasGameIds = $toTopPromote['game_ids'] == '' ? [] : explode(',', $toTopPromote['game_ids']);
$hasNotGameIds = M('game', 'tab_')->where(['game_id' => ['not in', $hasGameIds]])->getField('id', true);
$hasNotGameIds = $hasNotGameIds ?? [];
return [$hasGameIds, $hasNotGameIds];
}
private function getShiftCommonMap($isUserTable = false)
{
$map = ['promote_id' => $this->fromPromote['id']];
if (count($this->userIds) > 0) {
$userColumn = $isUserTable ? 'id' : 'user_id';
$map[$userColumn] = ['in', $this->userIds];
}
return $map;
}
private function shiftDeposit()
{
$map = $this->getShiftCommonMap();
$map['create_time'] = ['egt', $this->orderTime];
$toPromote = $this->toPromote;
M('deposit', 'tab_')->where($map)->save([
'promote_id' => $toPromote['id'],
'promote_account' => $toPromote['account'],
'market_admin_id' => $toPromote['admin_id'],
]);
}
private function shiftBindSpend()
{
$map = $this->getShiftCommonMap();
$map['pay_time'] = ['egt', $this->orderTime];
$toPromote = $this->toPromote;
M('bind_spend', 'tab_')->where($map)->save([
'promote_id' => $toPromote['id'],
'promote_account' => $toPromote['account']
]);
}
private function shiftUserDataCount()
{
$map = $this->getShiftCommonMap();
$map['create_time'] = ['egt', $this->orderTime];
M('user_play_data_count', 'tab_')->where($map)->save(['promote_id' => $this->toPromote['id']]);
}
}