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.

432 lines
18 KiB
PHP

<?php
namespace Base\Repository;
use Base\Tool\Registry;
use Base\Service\PromoteService;
class TestingResourceRepository
{
public static $provideStatusList = [
'0' => '待发放',
'1' => '已发放',
'2' => '异常',
];
public static $verifyStatusList = [
'0' => '未审核',
'1' => '审核通过',
'2' => '审核拒绝',
];
public function getProvideStatusText($provideStatus)
{
return self::$provideStatusList[$provideStatus] ?? '未知';
}
public function getVerifyStatusText($verifyStatus)
{
return self::$verifyStatusList[$verifyStatus] ?? '未知';
}
public function getGameRoleId($gameId, $roleId)
{
return $gameId . '#' . $roleId;
}
public function getGameSettings($isRefresh = false)
{
$settings = Registry::get('testing_game_settings');
if ($isRefresh || is_null($settings)) {
$settings = M('testing_game_setting', 'tab_')->where('1=1')->select();
Registry::set('testing_game_settings', $settings);
}
return $settings;
}
public function getGameSetting($baseGameId)
{
return M('testing_game_setting', 'tab_')->where(['base_game_id' => $baseGameId])->find();
}
public function getGameSettingByGameId($gameId)
{
$gameRepository = new GameRepository();
$baseGame = $gameRepository->getBaseGameByGameId($gameId);
$baseGameId = $baseGame ? $baseGame['id'] : 0;
return $this->getGameSetting($baseGameId);
}
public function getHadSettingGameIds()
{
$baseGameIds = M('testing_game_setting', 'tab_')->getField('base_game_id', true);
if (empty($baseGameIds)) {
return [];
}
$baseGames = M('base_game', 'tab_')->where(['id' => ['in', $baseGameIds]])->select();
return array_merge(array_column($baseGames, 'android_game_id'), array_column($baseGames, 'ios_game_id'));
}
public function getBatchesQuery($params, $promote = null)
{
$gameId = $params['game_id'] ?? 0;
$serverId = $params['server_id'] ?? '';
$createTimeStart = $params['create_time_start'] ?? '';
$createTimeEnd = $params['create_time_end'] ?? '';
$verifyStatus = $params['verify_status'] ?? -1;
$provideStatus = $params['provide_status'] ?? -1;
$account = $params['account'] ?? '';
$conditions = [];
$conditions['_string'] = '1=1';
if (!is_null($promote)) {
$promoteService = new PromoteService();
$subSql = M('user', 'tab_')
->where('id=tab_testing_resource_batch.user_id and promote_id in (' . $promoteService->subInSql($promote) . ')')
->select(false);
$conditions['_string'] .= ' and exists (' . $subSql . ')';
}
if ($createTimeStart) {
$conditions['_string'] .= ' and create_time >=' . strtotime($createTimeStart . ' 00:00:00');
}
if ($createTimeEnd) {
$conditions['_string'] .= ' and create_time <=' . strtotime($createTimeEnd . ' 23:59:59');
}
if ($verifyStatus != -1) {
$conditions['verify_status'] = $verifyStatus;
}
if ($provideStatus != -1) {
$conditions['provide_status'] = $provideStatus;
}
if ($gameId) {
$conditions['game_id'] = $gameId;
}
if ($serverId) {
$conditions['server_id'] = $serverId;
}
if ($account) {
$user = M('user', 'tab_')->field(['id'])->where('account like "' . $account . '%"')->find();
if ($user) {
$conditions['user_id'] = $user['id'];
} else {
$conditions['_string'] .= ' and 1<>1';
}
}
return M('testing_resource_batch', 'tab_')->where($conditions)->order('create_time desc');
}
private function getBatchesRelations($batches)
{
$roles = [];
$applyPromotes = [];
$users = [];
$promotes = [];
if (count($batches) > 0) {
$gameRoleIds = [];
foreach ($batches as $batch) {
$gameRoleIds[] = $this->getGameRoleId($batch['game_id'], $batch['role_id']);
}
$roles = M('user_play_info', 'tab_')
->field(['id', 'game_name', 'server_name', 'role_name', 'game_role_id', 'user_account'])
->where(['game_role_id' => ['in', $gameRoleIds]])
->select();
$roles = index_by_column('game_role_id', $roles);
$users = M('user', 'tab_')->field(['id', 'account', 'phone', 'promote_id'])->where(['id' => ['in', array_column($batches, 'user_id')]])->select();
$users = index_by_column('id', $users);
$applyPromotes = M('promote', 'tab_')->field(['id', 'account'])->where(['id' => ['in', array_column($batches, 'apply_promote_id')]])->select();
$applyPromotes = index_by_column('id', $applyPromotes);
if (count($users) > 0) {
$promotes = M('promote', 'tab_')->field(['id', 'account'])->where(['id' => ['in', array_column($users, 'promote_id')]])->select();
$promotes = index_by_column('id', $promotes);
}
}
return [
'roles' => $roles,
'applyPromotes' => $applyPromotes,
'users' => $users,
'promotes' => $promotes,
];
}
public function makeBatchesRecords($batches)
{
$result = $this->getBatchesRelations($batches);
$roles = $result['roles'];
$applyPromotes = $result['applyPromotes'];
$users = $result['users'];
$promotes = $result['promotes'];
$records = [];
foreach ($batches as $batch) {
$roleKey = $this->getGameRoleId($batch['game_id'], $batch['role_id']);
$role = isset($roles[$roleKey]) ? $roles[$roleKey] : null;
$user = $users[$batch['user_id']] ?? null;
$applyPromote = $applyPromotes[$batch['apply_promote_id']] ?? null;
$promote = $user && isset($promotes[$user['promote_id']]) ? $promotes[$user['promote_id']] : null;
$records[] = [
'id' => $batch['id'],
'batch_no' => substr($batch['batch_no'], 14),
'create_time' => date('Y-m-d H:i:s', $batch['create_time']),
'game_name' => $role ? $role['game_name'] : '--',
'server_name' => $role ? $role['server_name'] : '--',
'role_name' => $role ? $role['role_name'] : '--',
'user_account' => $role ?$role['user_account'] : '--',
'user_phone' => $user ? $user['phone'] : '',
'apply_promote_account' => $applyPromote ? $applyPromote['account'] : '',
'promote_account' => $promote['account'],
// 'history_provide_amount' => 0.00,
'apply_amount' => $batch['apply_amount'],
'provide_amount' => $batch['provide_amount'],
'verify_status' => $batch['verify_status'],
'verify_status_text' => $this->getVerifyStatusText($batch['verify_status']),
'verify_time' => $batch['verify_time'] == 0 ? '--' : date('Y-m-d H:i:s', $batch['verify_time']),
'provide_status' => $batch['provide_status'],
'provide_status_text' => $this->getProvideStatusText($batch['provide_status']),
'provide_time' => $batch['provide_time'] == 0 ? '--' : date('Y-m-d H:i:s', $batch['provide_time']),
'content' => $content,
];
}
return $records;
}
private function statByRoles($roles)
{
$bindingOrWhere = [];
foreach ($roles as $role) {
$bindingOrWhere[] = '(role_id="' . $role['role_id'] . '" and game_id=' . $role['game_id'] . ')';
}
$userIds = array_unique(array_column($roles, 'user_id'));
$users = [];
$bindings = [];
$bindingRoles = [];
$applyRecords = [];
if (count($roles) > 0) {
$users = M('user', 'tab_')->field(['id', 'phone', 'lock_status'])->where(['id' => ['in', $userIds]])->select();
$users = index_by_column('id', $users);
$bindingRows = M('testing_binding', 'tab_')->where(['_string' => implode(' or ', $bindingOrWhere)])->select();
$bindingRoleIds = [];
foreach ($bindingRows as $bindingRow) {
$bindings[$this->getGameRoleId($bindingRow['game_id'], $bindingRow['role_id'])] = $bindingRow;
$bindingRoleIds[] = $this->getGameRoleId($bindingRow['game_id'], $bindingRow['bind_role_id']);
}
if (count($bindings) > 0) {
$bindingRoles = M('user_play_info', 'tab_')
->field(['id', 'role_id', 'role_name', 'user_account', 'game_id', 'game_role_id'])
->where(['game_role_id' => ['in', $bindingRoleIds]])
->select();
$bindingRoles = index_by_column('game_role_id', $bindingRoles);
}
$verifyItems = M('testing_resource_batch', 'tab_')
->field('sum(apply_amount) amount, game_id, role_id')
->where([
'verify_status' => 0,
'_string' => implode(' or ', $bindingOrWhere)
])
->group('game_id,role_id')->select();
$verifyRecords = [];
foreach ($verifyItems as $item) {
$verifyRecords[$this->getGameRoleId($item['game_id'], $item['role_id'])] = $item['amount'] ;
}
$provideItems = M('testing_resource_batch', 'tab_')
->field('sum(provide_amount) amount, game_id, role_id')
->where([
'verify_status' => 1,
'_string' => implode(' or ', $bindingOrWhere)
])
->group('game_id,role_id')->select();
$provideRecords = [];
foreach ($provideItems as $item) {
$provideRecords[$this->getGameRoleId($item['game_id'], $item['role_id'])] = $item['amount'] ;
}
$todayProvideItems = M('testing_resource_batch', 'tab_')
->field('sum(provide_amount) amount, game_id, role_id')
->where([
'verify_status' => 1,
'_string' => implode(' or ', $bindingOrWhere),
'provide_time' => ['egt', strtotime(date('Y-m-d 00:00:00'))],
'provide_time' => ['elt', strtotime(date('Y-m-d 23:59:59'))],
])
->group('game_id,role_id')->select();
$todayProvideRecords = [];
foreach ($todayProvideItems as $item) {
$todayProvideRecords[$this->getGameRoleId($item['game_id'], $item['role_id'])] = $item['amount'] ;
}
$applyItems = M('testing_resource_batch', 'tab_')
->field('sum(apply_amount) amount, game_id, role_id')
->where([
'_string' => implode(' or ', $bindingOrWhere),
])
->group('game_id,role_id')->select();
$applyRecords = [];
foreach ($applyItems as $item) {
$applyRecords[$this->getGameRoleId($item['game_id'], $item['role_id'])] = $item['amount'] ;
}
}
$spendItems = [];
if (count($bindingRoles) > 0) {
$spendOrWhere = [];
foreach ($bindingRoles as $bindingRole) {
$spendOrWhere[] = '(game_player_id="' . $bindingRole['role_id'] . '" and game_id=' . $bindingRole['game_id'] . ')';
}
$spendCondition = [
'pay_status' => 1,
'_string' => '(' . implode(' or ', $spendOrWhere) . ')',
];
$spendList = M('spend', 'tab_')->field('sum(pay_amount) amount, game_id, game_player_id')->where($spendCondition)->group('game_id,game_player_id')->select();
foreach ($spendList as $item) {
$spendItems[$this->getGameRoleId($item['game_id'], $item['game_player_id'])] = $item['amount'];
}
}
$gameSettings = $this->getGameSettings();
return [
'users' => $users,
'spendItems' => $spendItems,
'bindings' => $bindings,
'bindingRoles' => $bindingRoles,
'applyRecords' => $applyRecords,
'provideRecords' => $provideRecords,
'verifyRecords' => $verifyRecords,
'todayProvideRecords' => $todayProvideRecords,
'gameSettings' => $gameSettings,
];
}
public function makeTestingUserRecords($roles)
{
$result = $this->statByRoles($roles);
$users = $result['users'];
$spendItems = $result['spendItems'];
$gameSettings = $result['gameSettings'];
$bindings = $result['bindings'];
$bindingRoles = $result['bindingRoles'];
$applyRecords = $result['applyRecords'];
$provideRecords = $result['provideRecords'];
$verifyRecords = $result['verifyRecords'];
$todayProvideRecords = $result['todayProvideRecords'];
$gameSettings = index_by_column('base_game_id', $gameSettings);
$gameRepository = new GameRepository();
$baseGames = $gameRepository->getBaseGames();
$records = [];
foreach ($roles as $role) {
$user = $users[$role['user_id']] ?? null;
$binding = $bindings[$role['game_role_id']] ?? null;
$bindingRole = null;
if ($binding) {
$bindGameRoleId = $this->getGameRoleId($binding['game_id'], $binding['bind_role_id']);
$bindingRole = $bindingRoles[$bindGameRoleId] ? $bindingRoles[$bindGameRoleId] : null;
}
$baseGame = $gameRepository->getBaseGameByGameId($role['game_id'], $baseGames);
$gameSetting = null;
if ($baseGame && isset($gameSettings[$baseGame['id']])) {
$gameSetting = $gameSettings[$baseGame['id']];
}
$spendQuota = $bindingRole && isset($spendItems[$bindingRole['game_role_id']]) ? $spendItems[$bindingRole['game_role_id']] : 0;
$quota = $gameSetting ? round($spendQuota * $gameSetting['rate'] / 100, 2) : 0;
$records[] = [
'id' => $role['id'],
'game_name' => $role['game_name'],
'server_name' => $role['server_name'],
'server_id' => $role['server_id'],
'role_id' => $role['role_id'],
'user_account' => $role['user_account'],
'user_phone' => $user ? $user['phone'] : '',
'role_name' => $role['role_name'],
'binding_id' => $binding ? $binding['id'] : 0,
'bind_user_account' => $bindingRole ? $bindingRole['user_account'] : '',
'bind_role_name' => $bindingRole ? $bindingRole['role_name'] : '',
'base_quota' => $gameSetting ? $gameSetting['base_quota'] : 0,
'other_quota' => $role['testing_other_quota'],
'quota' => $quota,
'verify_amount' => $verifyRecords[$role['game_role_id']] ?? 0,
'provide_amount' => $provideRecords[$role['game_role_id']] ?? 0,
'today_amount' => $todayProvideRecords[$role['game_role_id']] ?? 0,
'apply_amount' => $applyRecords[$role['game_role_id']] ?? 0,
'status' => $user && $user['lock_status'] == 1 ? '正常' : '锁定',
'create_time' => date('Y-m-d H:i:s', $role['create_time'])
];
}
return $records;
}
public function getTestingUsersQuery($params, array $promote = null)
{
$createTimeStart = $params['create_time_start'] ?? '';
$createTimeEnd = $params['create_time_end'] ?? '';
$gameId = $params['game_id'] ?? 0;
$serverId = $params['server_id'] ?? '';
$account = $params['account'] ?? '';
$roleName = $params['role_name'] ?? '';
$conditions = [];
$subConditions = [
'_string' => '1=1'
];
$strCondition = '1=1';
$promoteService = new PromoteService();
if ($promote) {
$visibleGameIds = $promoteService->getVisibleGameIds($promote);
if (count($visibleGameIds) > 0) {
$strCondition .= ' and game_id in (' . implode(',', $visibleGameIds) . ')';
} else {
$strCondition .= ' and 1=0';
}
$strCondition .= ' and promote_id in (' . $promoteService->subInSql($promote) . ')';
}
$gameIds = $this->getHadSettingGameIds();
if (!empty($gameIds)) {
$strCondition .= ' and game_id in ('. implode(',', $gameIds) . ')';
} else {
$strCondition .= ' and 1=0';
}
if ($createTimeStart) {
$strCondition .= ' and create_time >=' . strtotime($createTimeStart . ' 00:00:00');
}
if ($createTimeEnd) {
$strCondition .= ' and create_time <=' . strtotime($createTimeEnd . ' 23:59:59');
}
$subSql = M('testing_user', 'tab_')->field(['user_id'])->where($subConditions)->select(false);
$strCondition .= ' and user_id in (' . $subSql . ')';
if ($account) {
$user = M('user', 'tab_')->field(['id'])->where('account like "' . $account . '%"')->find();
if ($user) {
$conditions['user_id'] = $user['id'];
} else {
$strCondition .= ' and 1<>1';
}
}
if ($gameId) {
$conditions['game_id'] = $gameId;
}
if ($serverId) {
$conditions['server_id'] = $serverId;
}
if ($roleName) {
$conditions['role_name'] = ['like', $roleName . '%'];
}
$conditions['_string'] = $strCondition;
return M('user_play_info', 'tab_')->where($conditions)->order('create_time desc');
}
}