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.

486 lines
19 KiB
PHTML

4 years ago
<?php
namespace Base\Service;
use Base\Facade\Request;
use Base\Tool\GameCatClient;
class TestingResourceService
{
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 verify($batch)
{
if ($batch['verify_status'] != 0) {
throw new \Exception('审核状态异常');
}
$batchData = [];
$batchData['verify_time'] = time();
$batchData['update_time'] = time();
if (!in_array($batch['game_id'], [229, 230])) {
$batchData['verify_status'] = 2;
$batchData['verify_remark'] = '该游戏发放功能暂未实现';
M('testing_resource_batch', 'tab_')->where(['id' => $batch['id']])->save($batchData);
throw new \Exception('该游戏发放功能暂未实现');
}
$batchData['verify_status'] = 1;
$batchData['verify_remark'] = '审核成功';
M('testing_resource_batch', 'tab_')->where(['id' => $batch['id']])->save($batchData);
}
public function provide($batch)
{
if ($batch['verify_status'] != 1) {
throw new \Exception('该申请未审核通过');
}
4 years ago
if ($batch['provide_status'] != 0) {
throw new \Exception('发放状态异常');
}
$role = M('user_play_info', 'tab_')
->field(['id', 'role_id', 'user_id', 'promote_id', 'user_account', 'sdk_version'])
4 years ago
->where(['game_id' => $batch['game_id'], 'role_id' => $batch['role_id']])
->find();
$orders = M('testing_resource_order', 'tab_')
->where(['batch_id' => $batch['id']])
->select();
$hasError = false;
$provideAmount = 0;
foreach ($orders as $order) {
$result = $this->provideFromGameCat($order, $role);
$orderData = [
'result' => json_encode(['code' => $result['code'], 'message' => $result['message']]),
];
if (!$result['status']) {
$hasError = true;
$orderData['provide_status'] = 2;
} else {
$orderData['provide_status'] = 1;
4 years ago
}
$provideAmount += round($order['ref_amount'] * $order['num'], 2);
$orderData['provide_time'] = time();
M('testing_resource_order', 'tab_')
->where(['id' => $order['id']])
->save($orderData);
}
$batchData = [];
if ($hasError) {
$batchData['provide_status'] = 2;
} else {
$batchData['provide_status'] = 1;
4 years ago
}
$batchData['provide_time'] = time();
$batchData['provide_amount'] = $provideAmount;
$batchData['update_time'] = time();
M('testing_resource_batch', 'tab_')
->where(['id' => $batch['id']])
->save($batchData);
}
public function provideFromGameCat($order, $role)
{
$gameCatClient = new GameCatClient();
$result = $gameCatClient->api('provide', [
'roleId' => $role['role_id'],
4 years ago
'amount' => intval($order['ref_amount']),
4 years ago
'supportItem' => $order['ref_id'],
4 years ago
'supportType' => '0',
4 years ago
'channelUid' => $role['user_account'],
4 years ago
'applyRemark' => $order['remark'] == '' ? '测试资源申请' : $order['remark'],
4 years ago
'applyId' => $order['order_no'],
4 years ago
'device_type' => $role['sdk_version'] == 1 ? 'andriod' : 'ios',
4 years ago
]);
4 years ago
if ($result['state'] == 1 && $result['data']) {
4 years ago
return [
'status' => true,
4 years ago
'message' => $result['msg'],
4 years ago
'code' => 1,
];
} else {
return [
'status' => false,
4 years ago
'msg' => $result['msg'],
'code' => $result['state'],
4 years ago
];
}
}
4 years ago
public function getRemainQuota($role, $bindRole = null, $gameSetting = null)
{
if (is_null($gameSetting)) {
$gameSetting = $this->getGameSettingByGameId($role['game_id']);
}
if (is_null($gameSetting)) {
throw new \Exception('游戏未设置测试资源');
}
$totalQuota = $role['testing_other_quota'] + ($gameSetting['base_quota'] ?? 0);
if (!is_null($bindRole)) {
$totalQuota += M('spend', 'tab_')
->where(['game_id' => $role['game_id'], 'game_player_id' => $bindRole['role_id'], 'pay_status' => 1])
->group('game_id,game_player_id')
->sum('pay_amount');
}
$providedQuota = M('testing_resource_batch', 'tab_')
->where(['provide_status' => [in, [1, 2]], 'game_id' => $role['game_id'], 'role_id' => $role['role_id']])
->sum('provide_amount');
$providingQuota = M('testing_resource_batch', 'tab_')
->where(['verify_status' => [in, [0, 1]], 'provide_status' => 0, 'game_id' => $role['game_id'], 'role_id' => $role['role_id']])
->sum('apply_amount');
return round(floatval($totalQuota) - floatval($providedQuota) - floatval($providingQuota), 2);
}
public function getGameSetting($baseGameId)
{
return M('testing_game_setting', 'tab_')->where(['base_game_id' => $baseGameId])->find();
}
public function getGameSettingByGameId($gameId)
{
$baseGameId = M('base_game', 'tab_')->where('android_game_id=' . $gameId . ' or ios_game_id=' . $gameId)->getField('id');
return $this->getGameSetting($baseGameId);
}
public function addTestingUsers($accounts, $promote = null)
{
$accounts = array_unique($accounts);
$existAccounts = M('testing_user', 'tab_')->where(['user_account' => ['in', $accounts]])->getField('user_account', true);
$existAccounts = $existAccounts ?? [];
$existCount = count($existAccounts);
$newAccounts = array_diff($accounts, $existAccounts);
$errorCount = 0;
$successCount = 0;
if (count($newAccounts)) {
$strCondition = '1=1';
if ($promote) {
$promoteService = new PromoteService();
$strCondition = ' and promote_id in (' . $promoteService->subInSql($promote) . ')';
}
$users = M('user', 'tab_')->field(['id', 'account'])->where(['account' => ['in', $newAccounts], '_string' => $strCondition])->select();
$errorAccounts = array_diff($newAccounts, array_column($users, 'account'));
$errorCount = count($errorAccounts);
foreach ($users as $user) {
if (in_array($user['account'], $errorAccounts)) {
continue;
}
$data = [
'user_id' => $user['id'],
'user_account' => $user['account'],
'status' => 1,
'create_time' => time(),
'update_time' => time(),
];
M('testing_user', 'tab_')->add($data);
$successCount ++;
}
}
return [
'errorCount' => $errorCount,
'successCount' => $successCount,
'existCount' => $existCount,
];
}
public function saveGameSetting($params)
{
$gameId = $params['base_game_id'] ?? 0;
$baseQuota = $params['base_quota'] ?? 0;
$rate = $params['rate'] ?? 0;
if ($gameId == 0) {
throw new \Exception('请选择游戏');
}
$data = [
'base_game_id' => $gameId,
'base_quota' => $baseQuota,
'rate' => $rate,
];
$setting = M('testing_game_setting', 'tab_')->where(['base_game_id' => $gameId])->find();
if ($setting) {
$data['update_time'] = time();
M('testing_game_setting', 'tab_')->where(['base_game_id' => $gameId])->save($data);
} else {
$data['create_time'] = time();
$data['update_time'] = time();
M('testing_game_setting', 'tab_')->add($data);
}
}
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'));
}
private function getGameRoleId($gameId, $roleId)
{
return $gameId . '#' . $roleId;
}
public 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'];
}
}
return [
'users' => $users,
'spendItems' => $spendItems,
'bindings' => $bindings,
'bindingRoles' => $bindingRoles,
'applyRecords' => $applyRecords,
'provideRecords' => $provideRecords,
'verifyRecords' => $verifyRecords,
'todayProvideRecords' => $todayProvideRecords,
];
}
public function makeTestingUserRecords($roles)
{
$result = $this->statByRoles($roles);
$users = $result['users'];
$spendItems = $result['spendItems'];
$bindings = $result['bindings'];
$bindingRoles = $result['bindingRoles'];
$applyRecords = $result['applyRecords'];
$provideRecords = $result['provideRecords'];
$verifyRecords = $result['verifyRecords'];
$todayProvideRecords = $result['todayProvideRecords'];
$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;
}
$gameSetting = $this->getGameSettingByGameId($role['game_id']);
$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'],
'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' => $bindingRole && isset($spendItems[$bindingRole['game_role_id']]) ? $spendItems[$bindingRole['game_role_id']] : 0,
'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 bindRole($params, $promote)
{
$gameId = $params['game_id'] ?? 0;
$testingRoleId = $params['testing_role_id'] ?? '';
$bindRoleId = $params['bind_role_id'] ?? '';
$testingGameRoleId = $this->getGameRoleId($gameId, $testingRoleId);
$bindGameRoleId = $this->getGameRoleId($gameId, $bindRoleId);
$testingRole = M('user_play_info', 'tab_')
->field(['id', 'role_id', 'user_id', 'game_id', 'promote_id'])
->where(['game_role_id' => $testingGameRoleId])
->find();
if (is_null($testingRole)) {
throw new \Exception('测试账号角色不存在');
}
$testingUser = M('testing_user', 'tab_')->where(['user_id' => $testingRole['user_id']])->find();
if (is_null($testingUser)) {
throw new \Exception('测试账号不存在');
}
$promoteService = new PromoteService();
$testPromote = M('promote', 'tab_')->field(['id', 'chain'])->where(['id' => $testingRole['promote_id']])->find();
if (is_null($testPromote) || !$promoteService->isSubOrSelf($testPromote, $promote)) {
throw new \Exception('测试角色所属推广员异常');
}
$bindRole = M('user_play_info', 'tab_')
->field(['id', 'role_id', 'user_id', 'promote_id'])
->where(['game_role_id' => $bindGameRoleId])
->find();
if (is_null($bindRole)) {
throw new \Exception('玩家角色不存在');
}
$bindPromote = M('promote', 'tab_')->field(['id', 'chain'])->where(['id' => $bindRole['promote_id']])->find();
if (is_null($bindPromote) || !$promoteService->isSubOrSelf($bindPromote, $promote)) {
throw new \Exception('玩家账号所属推广员异常');
}
/* if ($testPromote['id'] != $bindPromote['id']) {
throw new \Exception('玩家账号与测试账号非同一推广员');
} */
$bindIsTesting = M('testing_user', 'tab_')->where(['user_id' => $bindRole['user_id']])->find();
if ($bindIsTesting) {
throw new \Exception('该玩家账号为测试账号,无法绑定');
}
$existBind = M('testing_binding', 'tab_')->field(['id'])->where(['game_id' => $gameId, 'bind_role_id' => $bindRoleId])->find();
if ($existBind) {
throw new \Exception('该玩家角色已被绑定');
}
$testExistBind = M('testing_binding', 'tab_')->field(['id'])->where(['game_id' => $gameId, 'role_id' => $testingRoleId])->find();
if ($testExistBind) {
throw new \Exception('该测试账号角色已绑定有角色');
}
$status = M('testing_binding', 'tab_')->add([
'game_id' => $testingRole['game_id'],
'user_id' => $testingRole['user_id'],
'role_id' => $testingRole['role_id'],
'bind_user_id' => $bindRole['user_id'],
'bind_role_id' => $bindRole['role_id'],
'create_time' => time(),
'update_time' => time()
]);
if (!$status) {
throw new \Exception('绑定角色异常');
}
}
public function getBatches()
{
}
4 years ago
}