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.

180 lines
6.1 KiB
PHTML

4 years ago
<?php
namespace Base\Service;
use Base\Facade\Request;
use Base\Tool\GameCatClient;
class PromoteGradeService
{
4 years ago
public static $symbols = [
1 => '>=',
2 => '>',
];
4 years ago
/**
* @config
* reach_level
* grades
* - name
* - value
*/
public function saveSetting($params, $promote)
{
$id = $params['id'] ?? 0;
$setting = null;
if ($id > 0) {
4 years ago
$setting = M('promote_grade_setting', 'tab_')->where(['id' => $id])->find();
4 years ago
if (is_null($setting)) {
throw new \Exception('记录不存在');
}
if ($setting['company_id'] != $promote['company_id']) {
throw new \Exception('不允许修改其他公司的配置');
}
4 years ago
} else {
$setting = M('promote_grade_setting', 'tab_')->where(['company_id' => $promote['company_id']])->find();
if ($setting) {
throw new \Exception('您已经设置过了');
}
4 years ago
}
$config = [];
$config['reach_level'] = $params['level'];
4 years ago
$config['grades'] = $this->sortGrades($params['grades']);
4 years ago
$config['default_grade_name'] = $params['default_grade_name'];
4 years ago
$data = [];
$data['status'] = 1;
4 years ago
$data['name'] = $params['name'];
4 years ago
$data['config'] = json_encode($config);
4 years ago
if ($setting) {
$data['create_time'] = time();
M('promote_grade_setting', 'tab_')->where(['id' => $id])->save($data);
} else {
$data['company_id'] = $promote['company_id'];
$data['create_time'] = time();
$data['update_time'] = time();
M('promote_grade_setting', 'tab_')->add($data);
}
4 years ago
}
4 years ago
private function sortGrades($grades)
4 years ago
{
4 years ago
if (count($grades) == 0) {
return $grades;
}
$values = [];
$symbols = [];
foreach ($grades as $key => $row) {
$values[$key] = $row['value'];
$symbols[$key] = $row['symbol'];
}
4 years ago
4 years ago
array_multisort($values, SORT_ASC, $symbols, SORT_ASC, $grades);
return $grades;
4 years ago
}
4 years ago
public function getCurrentSetting($promote)
4 years ago
{
4 years ago
return M('promote_grade_setting', 'tab_')->where(['status' => 1, 'company_id' => $promote['company_id']])->find();
4 years ago
}
4 years ago
public function searchGradeByPromotes($promotes, $params, $setting)
4 years ago
{
4 years ago
$config = json_decode($setting['config'], true);
$settingLevel = $config['reach_level'];
4 years ago
$month = $params['month'] ?? date('Y-m');
$promoteIds = array_column($promotes, 'id');
$beginTime = strtotime($month . '-01 00:00:00');
4 years ago
$endDate = date('Y-m-01 00:00:00', strtotime($month . '-01' . ' +1 month'));
$endTime = strtotime($endDate) - 1;
4 years ago
$betweenTime = [$beginTime, $endTime];
$userSubSql = M('user', 'tab_')
->field(['id'])
->where(['register_time' => ['between', $betweenTime], 'promote_id' => ['in', $promoteIds]])
->group('promote_id')
->select(false);
$accountItems = M('user_play_info', 'tab_')
->field(['promote_id', 'count(DISTINCT user_id) num'])
->where([
'role_level' => ['egt', $settingLevel],
'create_time' => ['between', $betweenTime],
'promote_id' => ['in', $promoteIds],
'_string' => 'user_id in (' . $userSubSql . ')'
])
->group('promote_id')
->select();
$accountItems = index_by_column('promote_id', $accountItems);
$amountItems = M('spend', 'tab_')
->field(['promote_id', 'sum(pay_amount) amount'])
->where([
'pay_time' => ['between', $betweenTime],
'pay_status' => 1, 'promote_id' => ['in', $promoteIds],
'_string' => 'user_id in (' . $userSubSql . ')'
])
->group('promote_id')
->select();
$amountItems = index_by_column('promote_id', $amountItems);
4 years ago
$promoteService = new PromoteService();
4 years ago
$records = [];
foreach ($promotes as $promote) {
$amountItem = $amountItems[$promote['id']] ?? null;
$accountItem = $accountItems[$promote['id']] ?? null;
4 years ago
$amount = $amountItem ? $amountItem['amount'] : 0;
$num = $accountItem ? $accountItem['num'] : 0;
$value = $num == 0 ? 0 : round($amount / $num, 2);
4 years ago
$records[] = [
4 years ago
'id' => $promote['id'],
'level' => $promote['level'],
'amount' => $amount,
'num' => $num,
'real_name' => hideRealName($promote['real_name']),
4 years ago
'account' => $promote['account'],
4 years ago
'promote_group' => $promoteService->getGroupNameByChain($promote['chain'], $promote['id']),
'value' => $value,
'grade_name' => $this->getGradeByValue($value, $setting),
'current_display' => ''
4 years ago
];
}
return $records;
}
4 years ago
public function getGradeByValue($value, $setting)
{
$config = json_decode($setting['config'], true);
$grades = $config['grades'];
$gradeName = $config['default_grade_name'];
foreach ($grades as $key => $grade) {
if ($key == 0 && !$this->isBigger($value, $grade)) {
$gradeName = $config['default_grade_name'];
break;
}
4 years ago
$nextGrade = $grades[$key + 1] ?? null;
4 years ago
if ($this->isBigger($value, $grade) && !$this->isBigger($value, $nextGrade)) {
$gradeName = $grade['name'];
break;
}
}
return $gradeName;
}
private function isBigger($value, $grade)
{
if (is_null($grade)) {
return false;
}
if ($grade['symbol'] == 1 && $value >= $grade['value']) {
return true;
} elseif ($grade['symbol'] == 2 && $value > $grade['value']) {
return true;
}
return false;
}
4 years ago
}