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.

871 lines
31 KiB
PHP

<?php
namespace Base\Service;
use Base\Tool\GameResource\JmxyClient;
use Base\Tool\GameResource\ZzylClient;
use Think\Model;
class GameRebateService
{
public $sendStatusList = [
0 => '待发放',
1 => '发放成功',
2 => '发放异常',
];
public $reviewStatusList = [
0 => '待审核',
1 => '审核通过',
2 => '审核拒绝',
];
public $typeDisplayNames = [
'A' => '单笔充值福利',
'B' => '月卡福利发放',
'C' => '创角累充福利',
'D' => '首充福利发放',
'E' => '单日累充福利发放',
'F' => '周卡福利发放',
'G' => '首次进游福利',
'H' => '签到福利发放',
'I' => '充值返利发放',
'J' => '定制道具申请',
'K' => '新服七日礼包',
'L' => '等级福利发放',
];
public $typeNames = [
'A' => 'single',
'B' => 'daily',
'C' => 'accumulative',
'D' => 'firstPay',
'E' => 'dayAccumulative',
'F' => 'weekly',
'G' => 'newRole',
'H' => 'dailySign',
'I' => 'singleTimes',
'J' => 'propsApplication',
'K' => 'openSevenDay',
'L' => 'roleLevel',
];
public $rebateBaseGameIds = [11, 17];
public function isGiftItemSend($order, $item)
{
// 每日发放奖励暂时通过send_status来判断是否第一次发放
if ($order['send_status'] == 1 && isset($item['once']) && $item['once']) {
return false;
}
return true;
}
public function sendByOrder($order, $handleDate = null)
{
$gift = null;
if ($order['gift_key']) {
$gift = M('rebate_gifts', 'tab_')
->where(['base_game_id' => $order['base_game_id'], 'type' => $order['type'], 'gift_key' => $order['gift_key'], 'group' => $order['gift_group']])->find();
}
$order['type_name'] = $this->typeDisplayNames[$order['type']] ?: '返利福利';
$hasError = false;
$sendResult = '';
if ($gift && $gift['game_currency'] > 0) {
$result = $this->sendGold($order, $gift['game_currency']);
if (!$result['status']) {
$hasError = true;
}
$sendResult .= ($result['message'] ?? '') . ';';
}
$index = 1;
$giftItems = $gift ? json_decode($gift['gifts'], true) : [];
foreach ($giftItems as $giftItem) {
$giftItem['index'] = $index;
$index ++;
if (!$this->isGiftItemSend($order, $giftItem)) {
continue;
}
$result = $this->sendGift($order, $giftItem);
if (!$result['status']) {
$hasError = true;
}
$sendResult .= ($result['message'] ?? '') . ';';
}
if ($order['props'] && $order['props'] != 'null') {
$result = $this->sendProps($order);
if (!$result['status']) {
$hasError = true;
}
$sendResult .= ($result['message'] ?? '') . ';';
}
$data = [
'send_status' => $hasError ? 2 : 1,
'send_time' => time(),
'send_result' => $sendResult,
];
if ($handleDate) {
$data['current_award_date'] = $handleDate;
}
M('rebate_orders', 'tab_')->where(['id' => $order['id']])->save($data);
}
private function getClient($baseGameId)
{
$client = null;
switch ($baseGameId) {
case 11:
$client = new ZzylClient();
break;
case 17:
$client = new JmxyClient();
break;
default:
throw new \Exception('客户端未实现');
break;
}
return $client;
}
public function sendGold($order, $gold)
{
$client = $this->getClient($order['base_game_id']);
return $client->sendGold($gold, $order);
}
public function sendGift($order, $giftItem)
{
$client = $this->getClient($order['base_game_id']);
return $client->sendEmail($giftItem, $order);
}
public function sendProps($order)
{
$client = $this->getClient($order['base_game_id']);
return $client->sendProps($order);
}
private function checkPropNumOfOrders($orders, $numMap)
{
$settings = M('rebate_times_setting', 'tab_')->where(['type' => 'I'])->select();
$settings = index_by_column('base_game_id', $settings);
foreach ($orders as $order) {
$setting = $settings[$order['base_game_id']] ?? null;
$maxTimes = $setting ? $setting['max_times'] : 0;
if ($numMap[$order['id']] > $maxTimes) {
throw new \Exception('返利倍数超过设定的最大值');
}
}
}
public function review(array $ids, $status, $handler)
{
if (!in_array($status, [1, 2])) {
throw new \Exception('状态异常');
}
if (count($ids) == 0) {
throw new \Exception('请选择要操作的记录');
}
$isSingleUpdate = is_array($ids[0]);
$numMap = [];
$idList = [];
if ($isSingleUpdate) {
foreach ($ids as $item) {
$idList[] = $item[0];
$numMap[$item[0]] = $item[1];
}
} else {
$idList = $ids;
}
$orders = M('rebate_orders', 'tab_')->where(['review_status' => 0, 'id' => ['in', $idList]])->select();
$orders = index_by_column('id', $orders);
if (count($orders) != count($idList)) {
throw new \Exception('含有不存在的记录或者已审核的记录');
}
$this->checkPropNumOfOrders($orders, $numMap);
$commonData = [
'review_type' => $handler['type'],
'review_status' => $status,
'review_time' => time(),
'reviewer_id' => $handler['id'],
'reviewer_username' => $handler['username'],
];
if ($isSingleUpdate) {
foreach ($ids as $item) {
$order = $orders[$item[0]];
$props = json_decode($order['props'], true);
foreach ($props as $key => $prop) {
$prop['num'] = $item[1];
$props[$key] = $prop;
}
$giftContent = $this->getGiftContentByProps($props);
M('rebate_orders', 'tab_')
->where(['review_status' => 0, 'id' => $item[0]])
->save(array_merge($commonData, [
'times' => $item[1],
'gift_content' => $giftContent,
'props' => json_encode($props)
]));
}
} else {
M('rebate_orders', 'tab_')->where(['review_status' => 0, 'id' => ['in', $idList]])->save($commonData);
}
}
public function sendOnce($type)
{
$orders = M('rebate_orders', 'tab_')->where(['type' => $type, 'review_status' => 1, 'send_status' => 0])->select();
foreach ($orders as $order) {
$this->sendByOrder($order);
}
}
public function sendDaily($type, $date)
{
$map = [
'type' => $type,
'review_status' => 1,
'award_started_at' => ['elt', $date],
'award_ended_at' => ['egt', $date],
'_string' => '(current_award_date is null or current_award_date < "' . $date . '")'
];
$orders = M('rebate_orders', 'tab_')->where($map)->select();
foreach ($orders as $order) {
$this->sendByOrder($order, $date);
}
}
public function listQuery($params, $awardType, $promote = null)
{
$isExport = $params['is_export'] ?? 0;
$page = $params['page'] ?? 0;
$limit = $params['limit'] ?? 0;
$baseGameId = $params['base_game_id'] ?? 0;
$serverId = $params['server_id'] ?? '';
$roleName = $params['role_name'] ?? '';
$userAccount = $params['user_account'] ?? '';
$reviewStatus = $params['review_status'] ?? -1;
$sendStatus = $params['send_status'] ?? -1;
$reviewerId = $params['reviewer_id'] ?? 0;
$reviewType = $params['review_type'] ?? -1;
$where = [
'_string' => '1=1',
];
$where['type'] = $awardType;
if ($baseGameId != 0) {
$where['base_game_id'] = $baseGameId;
}
if ($userAccount != '') {
$where['user_account'] = ['like', $userAccount . '%'];
}
if ($roleName != '') {
$where['role_name'] = ['like', $roleName . '%'];
}
if ($serverId != '') {
$where['server_id'] = $serverId;
}
if ($reviewStatus != -1) {
$where['review_status'] = $reviewStatus;
}
if ($sendStatus != -1) {
$where['send_status'] = $sendStatus;
}
if ($reviewerId != 0) {
$where['reviewer_id'] = $reviewerId;
}
if ($reviewType != -1) {
$where['review_type'] = $reviewType;
}
if (I('send_time_start', '') != '') {
$where['_string'] .= ' and send_time>=' . strtotime(I('send_time_start') . ' 00:00:00');
}
if (I('send_time_end', '') != '') {
$where['_string'] .= ' and send_time<=' . strtotime(I('send_time_end') . ' 23:59:59');
}
if (I('create_time_start', '') != '') {
$where['_string'] .= ' and create_time>=' . strtotime(I('create_time_start') . ' 00:00:00');
}
if (I('create_time_end', '') != '') {
$where['_string'] .= ' and create_time<=' . strtotime(I('create_time_end') . ' 23:59:59');
}
if ($promote) {
$promoteService = new PromoteService();
$subSql = M('user', 'tab_')->field(['id'])->where(['_string' => 'promote_id in(' . $promoteService->subInSql($promote) . ')'])->select(false);
$where['_string'] .= ' and user_id in(' . $subSql . ')';
}
$query = M('rebate_orders', 'tab_')->where($where);
$count = 0;
$records = [];
if ($isExport == 1 || $limit == 'all') {
$records = $query->order('create_time desc')->select();
$count = count($records);
} else {
$countQuery = clone $query;
$records = $query->order('create_time desc')->page($page, $limit)->select();
$count = $countQuery->count();
}
return [$records, $count];
}
public function listRange(array $records, $isExport = false)
{
$users = [];
if (count($records) > 0) {
$userIds = array_column($records, 'user_id');
$users = M('user', 'tab_')->field(['id', 'promote_account'])->where(['id' => ['in', $userIds]])->select();
$users = index_by_column('id', $users);
$settings = M('rebate_times_setting', 'tab_')->where(['type' => 'I'])->select();
$settings = index_by_column('base_game_id', $settings);
}
$newLine = $isExport == 1 ? "\n" : '<br/>';
foreach ($records as $key => $record) {
$user = $users[$record['user_id']];
$setting = $settings[$record['base_game_id']] ?? null;
$records[$key]['max_times'] = $setting ? $setting['max_times'] : 0;
$records[$key]['promote_account'] = $user['promote_account'];
$records[$key]['review_status_text'] = $this->reviewStatusList[$record['review_status']];
$records[$key]['review_time'] = $record['review_time'] > 0 ? date('Y-m-d H:i:s', $record['review_time']) : '--';
$records[$key]['send_status_text'] = $this->sendStatusList[$record['send_status']];
$records[$key]['send_time'] = $record['send_time'] > 0 ? date('Y-m-d H:i:s', $record['send_time']) : '--';
$records[$key]['create_time'] = $record['create_time'] > 0 ? date('Y-m-d H:i:s', $record['create_time']) : '--';
$records[$key]['award_date_range'] = is_null($record['award_started_at']) || is_null($record['award_ended_at'])
? '' : $record['award_started_at'] . ' ~ ' . $record['award_ended_at'];
$records[$key]['gift_content'] = implode($newLine, explode('|', $record['gift_content']));
}
return $records;
}
public function getExportHeadings($awardType)
{
$fields = [
'base_game_name' => '游戏名称',
'server_name' => '区服',
'user_account' => '账号',
'role_id' => '角色ID',
'role_name' => '角色名称',
'promote_account' => '所属推广员',
];
if ($awardType == 'A') {
$fields['pay_amount'] = '充值金额';
} elseif ($awardType == 'B') {
$fields['pay_amount'] = '当日累计充值金额';
$fields['award_date_range'] = '可领取福利日期';
} elseif ($awardType == 'C') {
$fields['amount'] = '达到档位金额';
} elseif ($awardType == 'D') {
$fields['pay_amount'] = '首充金额';
} elseif($awardType == 'E') {
$fields['pay_amount'] = '当日累计充值金额';
$fields['award_date'] = '达成日期';
} elseif($awardType == 'F') {
$fields['award_date_range'] = '可领取福利日期';
} elseif($awardType == 'H') {
$fields['award_date'] = '签到日期';
} elseif ($awardType == 'I') {
$fields['pay_amount'] = '充值金额';
$fields['times'] = '返利倍数';
} elseif($awardType == 'J') {
$fields['award_date'] = '达成日期';
$fields['pay_amount'] = '当日累充总额';
$fields['times'] = '返利倍数';
$fields['amount'] = '申请总价值';
} elseif ($awardType == 'K') {
$fields['pay_amount'] = '充值金额';
} elseif ($awardType == 'L') {
$fields['pay_amount'] = '当前等级';
$fields['mount'] = '奖励等级';
}
if ($awardType == 'J') {
$fields['gift_content'] = '申请道具';
} elseif($awardType == 'I') {
} else {
$fields['gift_content'] = '奖励内容';
}
$fields = array_merge($fields, [
'create_time' => '订单生成时间',
'review_status_text' => '审核状态',
'review_time' => '审核时间',
'send_status_text' => '发放状态',
'send_time' => '发放时间',
'reviewer_username' => '审核人',
]);
return $fields;
}
public function addSimulateSpend($params, $handler)
{
$baseGameId = $params['base_game_id'] ?? 0;
$roleId = $params['role_id'] ?? '';
$amount = $params['amount'] ?? 0;
$remark = $params['remark'] ?? '';
if ($baseGameId == 0) {
throw new \Exception('请选择游戏');
}
if ($roleId == 0) {
throw new \Exception('请输入角色ID');
}
if ($amount <= 0) {
throw new \Exception('金额必须大于0');
}
$baseGame = M('base_game', 'tab_')->where(['id' => $baseGameId])->find();
if ($baseGame == null) {
throw new \Exception('游戏不存在');
}
$gameIds = [$baseGame['android_game_id'], $baseGame['ios_game_id']];
$role = M('user_play_info', 'tab_')->where(['role_id' => $roleId, 'game_id' => ['in', $gameIds]])->find();
if ($role == null) {
throw new \Exception('角色不存在');
}
$data = [
'user_id' => $role['user_id'],
'user_account' => $role['user_account'],
'base_game_id' => $baseGame['id'],
'base_game_name' => $baseGame['name'],
'role_id' => $role['role_id'],
'role_name' => $role['role_name'],
'server_id' => $role['server_id'],
'server_name' => $role['server_name'],
'order_no' => 'SS_' . date('YmdHis') . getRandomPassword(4),
'create_time' => time(),
'amount' => $amount,
'remark' => $remark,
'creator_id' => $handler['id'],
'creator_username' => $handler['username'],
];
M('simulate_spend', 'tab_')->add($data);
}
public function reviewSimulateSpend(array $ids, $status, $handler)
{
if (!in_array($status, [1, 2])) {
throw new \Exception('状态异常');
}
if (count($ids) == 0) {
throw new \Exception('请选择要操作的记录');
}
$orders = M('simulate_spend', 'tab_')->field(['id'])->where(['review_status' => 0, 'id' => ['in', $ids]])->select();
if (count($orders) != count($ids)) {
throw new \Exception('含有不存在的记录或者已审核的记录');
}
M('simulate_spend', 'tab_')->where(['status' => 0, 'id' => ['in', $ids]])->save([
'status' => $status,
'review_time' => time(),
'reviewer_id' => $handler['id'],
'reviewer_username' => $handler['username'],
]);
}
public function deleteProp($id)
{
M('rebate_props', 'tab_')->where(['id' => $id])->delete();
}
public function batchDeleteProp($ids, $baseGameId)
{
M('rebate_props', 'tab_')->where(['id' => ['in', $ids], 'base_game_id' => $baseGameId])->delete();
}
public function saveProp($params)
{
$id = $params['id'] ?? 0;
$this->checkPropItem($params);
$prop = null;
if ($id > 0) {
$prop = M('rebate_props', 'tab_')->where(['id' => $id])->find();
if (!$prop) {
throw new \Exception('道具不存在');
}
}
$data = [
'ref_id' => $params['ref_id'],
'name' => $params['name'],
'value' => $params['value'],
'update_time' => time()
];
if ($id > 0) {
M('rebate_props', 'tab_')->where(['id' => $id])->save($data);
} else {
$data['base_game_id'] = $params['base_game_id'] ?? 0;
$data['create_time'] = time();
M('rebate_props', 'tab_')->add($data);
}
}
private function checkPropItem($prop)
{
$value = $prop['value'] ?? 0;
if (!isset($prop['value'])) {
throw new \Exception('缺少参数道具价值');
}
if (!isset($prop['name'])) {
throw new \Exception('缺少参数道具名称');
}
if (!isset($prop['ref_id'])) {
throw new \Exception('缺少参数道具ID');
}
if (!is_numeric($value) || $value < 0) {
throw new \Exception('道具价值必须为大于0的整数');
}
}
public function saveProps($baseGameId, $props)
{
if (count($props) == 0) {
throw new \Exception('道具项为0');
}
foreach ($props as $prop) {
$this->checkPropItem($prop);
}
$refIds = array_column($props, 'ref_id');
$existItems = M('rebate_props', 'tab_')->field(['ref_id'])->where(['base_game_id' => $baseGameId, 'ref_id' => ['in', $refIds]])->select();
$existItems = index_by_column('ref_id', $existItems);
$model = new Model();
$model->startTrans();
try {
$updateRecords = [];
$newRecords = [];
foreach ($props as $prop) {
$record = [
'base_game_id' => $baseGameId,
'ref_id' => $prop['ref_id'],
'name' => $prop['name'],
'value' => $prop['value'],
'update_time' => time()
];
if (isset($existItems[$prop['ref_id']])) {
$updateRecords[] = $record;
} else {
$record['create_time'] = time();
$newRecords[] = $record;
}
}
M('rebate_props', 'tab_')->addAll($newRecords);
foreach ($updateRecords as $record) {
M('rebate_props', 'tab_')->where(['base_game_id' => $baseGameId, 'ref_id' => $record['ref_id']])->save($record);
}
$model->commit();
} catch (\Exception $e) {
$model->rollback();
throw new \Exception('保存失败');
}
}
public function saveSetting($params)
{
$baseGameId = $params['base_game_id'] ?? 0;
$defaultTimes = $params['default_times'] ?? 0;
$maxTimes = $params['max_times'] ?? 0;
$type = $params['type'] ?? 'J';
if ($baseGameId == 0) {
throw new \Exception('请选择游戏');
}
if (!is_numeric($defaultTimes)) {
throw new \Exception('默认倍数格式错误');
}
if (!is_numeric($maxTimes)) {
throw new \Exception('倍数上限格式错误');
}
$defaultTimes = intval($defaultTimes);
$maxTimes = intval($maxTimes);
if ($defaultTimes < 0) {
throw new \Exception('默认倍数需大于0');
}
if ($maxTimes < 0) {
throw new \Exception('倍数上限需大于0');
}
if ($defaultTimes > $maxTimes) {
throw new \Exception('默认倍数不能大于倍数上限');
}
$setting = M('rebate_times_setting', 'tab_')->where(['base_game_id' => $baseGameId, 'type' => $type])->find();
// $baseGame = M('base_game', 'tab_')->where(['id' => $baseGameId])->find();
$data = [
'base_game_id' => $params['base_game_id'],
'type' => $type,
'default_times' => $defaultTimes,
'max_times' => $maxTimes,
'update_time' => time()
];
if ($setting) {
M('rebate_times_setting', 'tab_')->where(['id' => $setting['id']])->save($data);
} else {
$data['create_time'] = time();
M('rebate_times_setting', 'tab_')->add($data);
}
}
public function deleteSetting($id)
{
$setting = M('rebate_times_setting', 'tab_')->where(['id' => $id])->find();
if (!$setting) {
throw new \Exception('记录不存在');
}
if ($setting['type'] == 'J') {
$count = M('rebate_props', 'tab_')->where(['base_game_id' => $setting['base_game_id']])->count();
if ($count > 0) {
throw new \Exception('该游戏道具列表含有道具,无法删除!');
}
}
M('rebate_times_setting', 'tab_')->where(['id' => $id])->delete();
}
private function getRoleDailyPayAmount($role, $date)
{
$spendMap = [
'pay_status' => 1,
'game_play_id' => $role['role_id'],
'user_id' => $role['user_id'],
'game_id' => ['in', $role['game_ids']],
'pay_time' => ['between', [strtotime($date . ' 00:00:00'), strtotime($date . ' 23:59:59')]]
];
$payAmount = M('spend', 'tab_')->where($spendMap)->sum('pay_amount');
$simulateMap = [
'status' => 1,
'role_id' => $role['role_id'],
'user_id' => $role['user_id'],
'base_game_id' => $role['base_game_id'],
'review_time' => ['between', [strtotime($date . ' 00:00:00'), strtotime($date . ' 23:59:59')]]
];
$simulateAmount = M('simulate_spend', 'tab_')->where($simulateMap)->sum('amount');
return floatval($payAmount) + floatval($simulateAmount);
}
private function getRoleDailyAppliedAmount($role, $date)
{
$map = [
'review_status' => ['in', [0, 1]],
'role_id' => $role['role_id'],
'user_id' => $role['user_id'],
'base_game_id' => $role['base_game_id'],
'date' => $date,
'type' => 'J',
];
$amount = M('rebate_orders', 'tab_')->where($map)->sum('amount');
return floatval($amount);
}
public function propsApply($params, $handler = null)
{
$baseGameId = $params['base_game_id'] ?? 0;
$roleId = $params['role_id'] ?? '';
$propItems = $params['props'] ?? [];
$date = $params['date'] ?? '';
if ($baseGameId == 0) {
throw new \Exception('请选择游戏');
}
if ($date == '') {
throw new \Exception('请选择日期');
}
if (count($propItems) == 0) {
throw new \Exception('至少选择一项道具');
}
$baseGame = M('base_game', 'tab_')->where(['id' => $baseGameId])->find();
if (!$baseGame) {
throw new \Exception('游戏不存在');
}
$roleFields = ['user_id', 'user_account', 'game_id', 'role_id', 'role_name', 'server_id', 'server_name'];
$role = M('user_play_info', 'tab_')->field($roleFields)->where(['base_game_id' => $baseGameId, 'role_id' => $roleId])->find();
if (!$role) {
throw new \Exception('角色不存在');
}
$setting = M('rebate_times_setting', 'tab_')->where(['base_game_id' => $baseGameId, 'type' => 'J'])->find();
if (!$setting) {
throw new \Exception('未设定定制道具返利倍数');
}
$props = M('rebate_props', 'tab_')->where(['id' => ['in', array_column($propItems, 'id')], 'base_game_id' => $baseGameId])->select();
$props = index_by_column('id', $props);
$applyAmount = 0;
$applyProps = [];
foreach ($propItems as $item) {
if (isset($props[$item['id']])) {
$prop = $props[$item['id']];
$applyAmount += ($prop['value'] * $item['num']);
$applyProps[] = [
'ref_id' => $prop['ref_id'],
'value' => $prop['value'],
'num' => $item['num'],
'name' => $prop['name']
];
} else {
throw new \Exception('道具不存在');
}
}
$gameIds = [$baseGame['android_game_id'], $baseGame['ios_game_id']];
$role['game_ids'] = $gameIds;
$role['base_game_id'] = $baseGameId;
$dailyPayAmount = $this->getRoleDailyPayAmount($role, $date);
$appliedAmount = $this->getRoleDailyAppliedAmount($role, $date);
if ($dailyPayAmount * $setting['max_times'] - $applyAmount - $appliedAmount < 0) {
throw new \Exception('额度不足');
}
$record = [
'base_game_id' => $baseGame['id'],
'base_game_name' => $baseGame['name'],
'user_id' => $role['user_id'],
'user_account' => $role['user_account'],
'server_id' => $role['server_id'],
'server_name' => $role['server_name'],
'role_name' => $role['role_name'],
'role_id' => $role['role_id'],
'type' => 'J',
'award_date' => $date,
'amount' => $applyAmount,
'pay_amount' => $dailyPayAmount,
'gift_key' => 'J-0',
'gift_content' => $this->getGiftContentByProps($applyProps),
'props' => json_encode($applyProps, JSON_UNESCAPED_UNICODE),
'times' => $setting['max_times'],
'create_time' => time(),
];
if ($handler) {
$record['creator_id'] = $handler['id'];
$record['creator_username'] = $handler['username'];
$record['creator_type'] = $handler['type'];
}
// 是否自动审核
if (false) {
$record['review_type'] = 2;
$record['reviewer_id'] = 0;
$record['reviewer_username'] = '系统';
$record['review_status'] = 1;
$record['review_time'] = time();
}
M('rebate_orders', 'tab_')->add($record);
}
private function getGiftContentByProps($props)
{
$items = [];
foreach ($props as $prop) {
$items[] = $prop['name'] . '*' . $prop['num'];
}
return implode('|', $items);
}
public function getRebateBaseGames()
{
return M('base_game', 'tab_')->where(['id' => ['in', $this->rebateBaseGameIds]])->select();
}
public function getRoleInfo($params, $permPromote = null)
{
$baseGameId = $params['base_game_id'] ?? 0;
$roleId = $params['role_id'] ?? '';
$date = $params['date'] ?? '';
$baseGame = M('base_game', 'tab_')->where(['id' => $baseGameId])->find();
if (!$baseGame) {
throw new \Exception('游戏不存在');
}
$gameIds = [$baseGame['android_game_id'] , $baseGame['ios_game_id']];
$roleStr = '1=1';
if ($permPromote != null) {
$promoteService = new PromoteService();
$roleStr .= ' and promote_id in (' . $promoteService->subInSql($permPromote) . ')';
}
$role = M('user_play_info', 'tab_')->where(['game_id' => ['in', $gameIds], 'role_id' => $roleId, '_string' => $roleStr])->order('play_time', 'desc')->limit(1)->find();
if (!$role) {
throw new \Exception('角色不存在');
}
$spendMap = ['game_id' => ['in', $gameIds], 'game_player_id' => $roleId, 'pay_status' => 1, 'user_id' => $role['user_id']];
$simulateMap = ['base_game_id' => $baseGameId, 'role_id' => $roleId, 'status' => 1, 'user_id' => $role['user_id']];
if ($date) {
$spendMap['pay_time'] = ['between', [strtotime($date . ' 00:00:00'), strtotime($date . ' 23:59:59')]];
$simulateMap['review_time'] = ['between', [strtotime($date . ' 00:00:00'), strtotime($date . ' 23:59:59')]];
}
$payAmount = M('spend', 'tab_')->where($spendMap)->sum('pay_amount');
$payAmount = floatval($payAmount);
$simulateAmount = M('simulate_spend', 'tab_')->where($simulateMap)->sum('amount');
$simulateAmount = floatval($simulateAmount);
$data = [
'server_name' => $role['server_name'],
'role_name' => $role['role_name'],
'pay_amount' => $payAmount,
'simulate_amount' => $simulateAmount
];
if ($date) {
$setting = M('rebate_times_setting', 'tab_')->where(['base_game_id' => $baseGameId, 'type' => 'J'])->find();
if (!$setting) {
throw new \Exception('返利倍数未设置');
}
$totalQuota = intval(($payAmount + $simulateAmount) * $setting['max_times']);
$appliedQuota = M('rebate_orders', 'tab_')
->where(['base_game_id' => $baseGameId, 'type' => 'J', 'role_id' => $roleId, 'award_date' => $date, 'review_status' => ['in', [0, 1]]])
->sum('amount');
$appliedQuota = intval($appliedQuota);
$remainQuota = $totalQuota - $appliedQuota;
$data = array_merge($data,
[
'applied_quota' => $appliedQuota,
'remain_quota' => $remainQuota,
'total_quota' => $totalQuota,
'max_times' => $setting['max_times'],
]
);
}
return $data;
}
}