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.

605 lines
28 KiB
PHP

<?php
namespace Admin\Controller;
use Think\Controller;
use Admin\Model\PromoteGameRatioModel;
class PromoteGameRatioController extends ThinkController
{
const MODEL_NAME = 'promote_game_ratio';
const STATUS_REFUSE = -1;
const STATUS_WAIT = 0;
const STATUS_PASS = 1;
public static $statusList = [
self::STATUS_REFUSE => '审核未通过',
self::STATUS_WAIT => '待审核',
self::STATUS_PASS => '已审核',
];
public function lists()
{
$params = I('get.');
$group = $params['group'] ?? 1;
$companyId = $params['company_id'] ?? 0;
$companyBelong = $params['company_belong'] ?? '';
$promoteId = $params['promote_id'] ?? 0;
$gameId = $params['game_id'] ?? 0;
$gameTypeId = $params['game_type_id'] ?? 0;
$status = $params['status'] ?? '';
$page = $params['p'] ? intval($params['p']) : 1;
$row = $params['row'] ? intval($params['row']) : 10;
$map['_string'] = 'game_id = relation_game_id';
$where['_string'] = '1 = 1';
if ($companyId || ($companyBelong !== '' && in_array($companyBelong, [0, 1]))) {
$promoteMap['_string'] = '1 = 1';
if ($companyId) {
$promoteMap['company_id'] = $companyId;
}
if ($companyBelong !== '' && in_array($companyBelong, [0, 1])) {
$promoteMap['company_belong'] = $companyBelong;
}
$promoteIds = D('promote')->where($promoteMap)->getField('id', true);
$promoteIds = $promoteIds ?? [-1];
$where['promote_id'] = ['in', $promoteIds];
}
if ($promoteId) {
$map['promote_id'] = intval($promoteId);
}
if ($gameTypeId) {
$gameIds = D('game')->where(['game_type_id' => $gameTypeId])->getField('id', true);
$gameIds = $gameIds ?? [-1];
$where['game_id'] = ['in', $gameIds];
}
if ($gameId) {
$map['game_id'] = intval($gameId);
}
if ($status !== '') {
$map['status'] = intval($status);
}
$map['_complex'] = $where;
$field = 'create_time, update_time';
if ($group == 1) {
$query = D(self::MODEL_NAME)->field($field, true)
->where($map)
->group('promote_id, relation_game_id')
->order('update_time desc, id desc');
$metaTitle = $csvTitle = '公会分成管理';
} else {
$query = M(self::MODEL_NAME . '_log', 'tab_')->field($field, true)
->where($map)
->group('promote_id, relation_game_id, create_time')
->order('create_time desc, id desc');
$metaTitle = $csvTitle = '公会分成申请记录';
}
if (I('export', 0) != 1) {
$query->page($page, $row);
}
$promoteGameRatios = $query->select();
if (I('export', 0) != 1) {
$count = count(D(self::MODEL_NAME)->where($map)
->group('promote_id, relation_game_id')
->order('update_time desc, id desc')
->select());
}
$records = [];
if ($promoteGameRatios) {
$promoteIds = array_column($promoteGameRatios, 'promote_id');
$gameIds = array_column($promoteGameRatios, 'game_id');
$promoteFiled = 'id, account, mobile_phone, create_time, status, ver_status, company_id, company_belong';
$gameFiled = 'id, relation_game_name, ratio, game_type_name';
$promotes = M('promote', 'tab_')->where(array('id' => ['in', $promoteIds]))->getField($promoteFiled, true);
$games = M('game', 'tab_')->where(array('id' => ['in', $gameIds]))->getField($gameFiled, true);
foreach ($promoteGameRatios as $promoteGameRatio) {
$thisPromoteId = $promoteGameRatio['promote_id'];
$thisGameId = $promoteGameRatio['game_id'];
$issetPromote = isset($promotes[$thisPromoteId]);
$issetGame = isset($games[$thisGameId]);
$thisPromoteAccount = '未知';
$thisPromoteMobilePhone = '未知';
$thisPromoteCreateTime = '未知';
$thisPromoteStatus = '待审核';
$thisPromoteVerStatus = '未知';
$thisCompanyName = '未知';
$thisCompanyBelong = '未知';
$thisGameTypeName = '未知';
$thisGameName = '未知';
$thisGameRatio = '0.00%';
$thisLastRatio = $promoteGameRatio['last_ratio'] . '%';
$thisLastTurnoverRatio = $promoteGameRatio['last_turnover_ratio'] ? json_decode($promoteGameRatio['last_turnover_ratio'], true) : [];
$thisLastRatio = $thisLastTurnoverRatio ? "{$thisLastRatio} - " . $thisLastTurnoverRatio[count($thisLastTurnoverRatio) - 1]['ratio'] . '%' : $thisLastRatio;
$thisTurnoverRatios = $promoteGameRatio['turnover_ratio'] ? json_decode($promoteGameRatio['turnover_ratio'], true) : [];
$thisRatioDtl = "默认:{$promoteGameRatio['ratio']}%";
if ($thisTurnoverRatios) {
if (I('export', 0) == 1) {
$symbol = "\n";
} else {
$symbol = "<br>";
}
foreach ($thisTurnoverRatios as $thisTurnoverRatio) {
$thisIntervalClosedStatusText = isset($thisTurnoverRatio['instanceof']) ? ($thisTurnoverRatio['instanceof'] == 1 ? '≥' : '>') : '≥';
$thisRatioDtl .= "{$symbol}月流水{$thisIntervalClosedStatusText}{$thisTurnoverRatio['turnover']},比例:{$thisTurnoverRatio['ratio']}%";
}
}
$thisLastRatioStatus = $promoteGameRatio['last_ratio_status'];
$thisStatusText = self::$statusList[$promoteGameRatio['status']];
if (I('export', 0) == 1) {
$thisStatusText = ($promoteGameRatio['status'] == -1) ? $thisStatusText : $thisStatusText;
} else {
$thisStatusText = ($promoteGameRatio['status'] == -1) ? '<span style="color: red;">' . $thisStatusText . '</span>' : $thisStatusText;
}
$thisApplicant = get_admin_account($promoteGameRatio['applicant_id']);
$thisReviewer = $promoteGameRatio['reviewer_id'] ? get_admin_account($promoteGameRatio['reviewer_id']) : '待确认';
$thisBeninTime = date('Y/m', $promoteGameRatio['begin_time']);
$thisEndTime = $promoteGameRatio['end_time'] ? date('Y/m', $promoteGameRatio['end_time']) : '永久';
$validDate = $thisBeninTime . ' - ' . $thisEndTime;
if ($issetPromote) {
$thisPromoteAccount = $promotes[$thisPromoteId]['account'];
$thisPromoteMobilePhone = $promotes[$thisPromoteId]['mobile_phone'];
$thisPromoteCreateTime = date('Y-m-d H:i:s', $promotes[$thisPromoteId]['create_time']);
$thisPromoteStatus = get_info_status($promotes[$thisPromoteId]['status'], 3);
$thisPromoteStatus = $thisPromoteStatus ?? '待审核';
$thisPromoteVerStatus = getPromoteVerStatus($promotes[$thisPromoteId]['ver_status'], 2);
$thisCompanyName = getPromoteCompanyName($promotes[$thisPromoteId]['company_id']);
$thisCompanyBelong = $promotes[$thisPromoteId]['company_belong'] == 1 ? '外团' : '内团';
}
if ($issetGame) {
$thisGameName = $games[$thisGameId]['relation_game_name'];
$thisGameRatio = $games[$thisGameId]['ratio'];
$thisGameRatio = ($thisGameRatio ?? '0.00') . '%';
$thisGameTypeName = $games[$thisGameId]['game_type_name'];
}
$record = [
'id' => $promoteGameRatio['id'],
'promote_id' => $promoteGameRatio['promote_id'],
'promote_account' => $thisPromoteAccount,
'promote_mobile_phone' => $thisPromoteMobilePhone,
'promote_create_time' => $thisPromoteCreateTime,
'promote_status_text' => $thisPromoteStatus,
'promote_ver_status_text' => $thisPromoteVerStatus,
'company_name' => $thisCompanyName,
'company_belong' => $thisCompanyBelong,
'game_name' => $thisGameName,
'game_type_name' => $thisGameTypeName,
'last_ratio' => (($thisLastRatioStatus == 1 || $group != 1) ? $thisLastRatio : $thisGameRatio),
'ratio_dtl' => $thisRatioDtl,
'valid_date' => $validDate,
'remark' => $promoteGameRatio['remark'],
'status_text' => $thisStatusText,
'applicant' => $thisApplicant ?? '未知',
'reviewer' => $thisReviewer ?? '未知',
];
if (I('export', 0) != 1) {
$record['status'] = $promoteGameRatio['status'];
}
$records[] = $record;
}
}
if (I('export', 0) == 1) {
$field = [
'promote_id' => '会长Id',
'promote_account' => '会长账号',
'promote_mobile_phone' => '手机号码',
'promote_create_time' => '注册时间',
'promote_status_text' => '状态',
'promote_ver_status_text' => '身份状态',
'company_name' => '推广公司',
'company_belong' => '工会归属',
'game_name' => '已申请游戏',
'game_type_name' => '游戏类型',
'last_ratio' => '原分成比例',
'ratio_dtl' => '公会阶梯比例详细信息',
'valid_date' => '开始时间',
'remark' => '备注',
'status_text' => '分成比例审核',
'applicant' => '申请人',
'reviewer' => '确认人',
];
data2csv($records,$csvTitle, $field);
exit;
}
$gameTypelist = getGameTypes();
$companys = getPromoteCompanys();
$reviewRule = $this->getReviewRule();
$page = set_pagination($count, $row);
if($page) {
$this->assign('_page', $page);
}
$this->assign('companys', $companys);
$this->assign('gameTypeList', $gameTypelist);
$this->assign('group', $group);
$this->assign('records', $records);
$this->assign('count', $count);
$this->assign('gameList', getAllGameList(true));
$this->assign('promoteList', getPromoteByLevel(1, intval(I('company_id', 0))));
$this->assign('statusList', self::$statusList);
$this->assign('reviewRule', $reviewRule);
$this->assign('is_admin', is_administrator());
$this->meta_title = $metaTitle;
$this->display();
}
public function applyRatio()
{
if ($_POST) {
$params = I('post.');
$time = time();
if (empty($params['begin_time'])) {
$this->error('请选择开始时间');
}
if (!empty($params['end_time'])) {
if (strtotime($params['end_time']) < strtotime($params['begin_time'])) {
$this->error('结束时间不得小于开始时间');
}
}
if (!isset($params['ratio']) || $params['ratio'] === '') {
$this->error('默认分成比例不能为空');
}
$save['turnover_ratio'] = [];
if (!empty(array_filter($params['turnover'])) || !empty(array_filter($params['turnover_ratio'])) || !empty(array_filter($params['instanceof']))) {
if (is_array($params['turnover']) && is_array($params['turnover_ratio']) && is_array($params['instanceof'])) {
foreach ($params['turnover'] as $turnover) {
if (empty($turnover)) {
$this->error('月流水不能为空');
}
}
foreach ($params['turnover_ratio'] as $turnoverRatio) {
if (empty($turnoverRatio)) {
$this->error('月流水分成比例不能为空');
}
}
foreach ($params['instanceof'] as $intervalClosedStatus) {
if (!in_array($intervalClosedStatus, [1, 2])) {
$this->error('月流水分符号不能为空');
}
}
$turnoverCount = count($params['turnover']);
$sortTurnover = $params['turnover'];
sort($sortTurnover);
if ($params['turnover'] != $sortTurnover || $turnoverCount != count(array_unique($params['turnover']))) {
$this->error('月流水必须以正序的方式填写,且必须大于上一个月流水');
}
$ratio = $params['ratio'] ?? 0;
if ($params['turnover_ratio'][0] <= $ratio) {
$this->error('月流水分成比例必须大于默认分成比例');
}
$turnoverRatioCount = count($params['turnover_ratio']);
$sortTurnoverRatio = $params['turnover_ratio'];
sort($sortTurnoverRatio);
if ($params['turnover_ratio'] != $sortTurnoverRatio || $turnoverRatioCount != count(array_unique($params['turnover_ratio']))) {
$this->error('月流水分成比例必须以正序的方式填写,且必须大于上一个月流水分成比例');
}
foreach ($params['turnover'] as $key => $turnover) {
$save['turnover_ratio'][] = [
'turnover' => bcdiv($turnover, 1, 2),
'ratio' => bcdiv($params['turnover_ratio'][$key], 1, 2),
'instanceof' => (isset($params['instanceof'][$key]) ? $params['instanceof'][$key] : 1),
];
}
$save['turnover_ratio'] = json_encode($save['turnover_ratio']);
}
}
$save['ratio'] = $params['ratio'] ?? 0;
$save['begin_time'] = strtotime($params['begin_time']);
$save['end_time'] = $params['end_time'] ? (strtotime('+1 month', strtotime($params['end_time'])) - 1) : 0;
$save['remark'] = $params['remark'] ?? '';
$save['status'] = 0;
$save['update_time'] = $time;
$model = new PromoteGameRatioModel();
if (!empty($params['id'])) {//修改
$promoteGameRatio = D(self::MODEL_NAME)->find($params['id']);
if (empty($promoteGameRatio)) {
$this->error('参数异常');
}
if ($promoteGameRatio['status'] == -1 || $save['instanceof'] != $promoteGameRatio['instanceof'] || $save['begin_time'] != $promoteGameRatio['begin_time'] || $save['end_time'] != $promoteGameRatio['end_time'] || $save['ratio'] != $promoteGameRatio['ratio'] || $save['remark'] != $promoteGameRatio['remark'] || $save['turnover_ratio'] != $promoteGameRatio['turnover_ratio']) {
$this->isWithdraw($promoteGameRatio['promote_id'], $save['begin_time'], $save['end_time']);
if ($promoteGameRatio['status'] == 1) {
$save['last_turnover_ratio'] = $promoteGameRatio['turnover_ratio'];
$save['last_ratio'] = $promoteGameRatio['ratio'];
$save['last_ratio_status'] = 1;
}
$saveMap['promote_id'] = $promoteGameRatio['promote_id'];
$saveMap['relation_game_id'] = $promoteGameRatio['relation_game_id'];
$result = D(self::MODEL_NAME)->where($saveMap)->save($save);
$logResult = $result;
} else {
$result = true;
$logResult = false;
}
if ($result === false) {
$this->error('保存失败');
}
if ($logResult) {
$promoteGameRatioIds = D(self::MODEL_NAME)->where(array('relation_game_id' => $promoteGameRatio['relation_game_id']))->getField('id', true);
if (!empty($promoteGameRatioIds)) {
foreach ($promoteGameRatioIds as $promoteGameRatioId) {
$model->addLog($promoteGameRatioId);
}
}
}
} else {//新增
if (empty($params['promote_id'])) {
$this->error('请选择会长账号');
}
if (empty($params['game_id'])) {
$this->error('请选择要申请的游戏');
}
$promoteId = intval($params['promote_id']);
$gameId = intval($params['game_id']);
$relationGameId = D('game')->where(array('id' => $gameId))->getField('relation_game_id');
if (empty($relationGameId)) {
$this->error('数据异常');
}
$gameIds = D('game')->where(array('relation_game_id' => $relationGameId))->getField('id', true);
if (empty($gameIds)) {
$this->error('数据异常');
}
$promote = M('promote', 'tab_')->find($promoteId);
if (empty($promote) || $promote['level'] != 1) {
$this->error('参数异常');
}
$this->isWithdraw($promoteId, $save['begin_time'], $save['end_time']);
$map['promote_id'] = $promoteId;
$save['promote_id'] = $promoteId;
$save['last_turnover_ratio'] = '';
$save['applicant_id'] = is_login();
$save['create_time'] = $time;
M()->startTrans();
foreach ($gameIds as $gameId) {
$map['game_id'] = $gameId;
$promoteGameRatio = D(self::MODEL_NAME)->where($map)->find();
if ($promoteGameRatio) {
$this->error('网络异常');
}
$save['game_id'] = $gameId;
$save['relation_game_id'] = $relationGameId;
$result = D(self::MODEL_NAME)->add($save);
$logResult = $result;
if ($result === false) {
M()->rollback();
$this->error('保存失败');
}
if ($logResult) {
$model->addLog($result);
}
}
M()->commit();
}
$this->success('保存成功', U('lists'));
} else {
$params = I('get.');
$id = $params['id'] ?? 0;
$id = intval($id);
$companyId = 0;
$metaTitle = '游戏分成比例申请';
if ($id) {
$metaTitle .= '--修改';
$map['id'] = $id;
$promoteGameRatio = D(self::MODEL_NAME)->where($map)->find();
if (empty($promoteGameRatio)) {
$this->error('数据异常');
}
$promoteGameRatio['begin_time'] = $promoteGameRatio['begin_time'] ? date('Y-m', $promoteGameRatio['begin_time']) : '';
$promoteGameRatio['end_time'] = $promoteGameRatio['end_time'] ? date('Y-m', $promoteGameRatio['end_time']) : '';
$promoteGameRatio['turnover_ratio'] = $promoteGameRatio['turnover_ratio'] ? json_decode($promoteGameRatio['turnover_ratio'], true) : $promoteGameRatio['turnover_ratio'];
if ($promoteGameRatio['last_ratio_status'] == 1) {
$lastRatio = $promoteGameRatio['last_ratio'] . '%';
} else {
$gameRatio = M('game', 'tab_')->where(array(['id' => $promoteGameRatio['game_id']]))->getField('ratio');
$lastRatio = ($gameRatio ?? '0.00') . '%';
}
$companyId = $promoteGameRatio['company_id'];
$promoteGameRatio['company_id'] = D('promote')->where(['id' => $promoteGameRatio['promote_id']])->getField('company_id');
$this->assign('record', $promoteGameRatio);
$this->assign('lastRatio', $lastRatio);
}
$companys = getPromoteCompanys();
$this->assign('companys', $companys);
$this->assign('gameList', getAllGameList(true));
$this->assign('promoteList', getPromoteByLevel(1, $companyId));
$this->meta_title = $metaTitle;
$this->display();
}
}
public function getPromotes()
{
$level = 1;
$companyId = intval(I('company_id', 0));
$promotes = getPromoteByLevel($level, $companyId);
$this->ajaxReturn($promotes);
}
private function isWithdraw($promoteId, $beginTime, $endTime)
{
$promote = M('promote', 'tab_')->find($promoteId);
if (empty($promote)) {
$this->error("数据异常");
}
if ($promote['level'] != 1) {
$this->error("该推广员不是会长账号,无法执行此操作");
}
$withdrawMap['promote_id'] = $promoteId;
$withdrawMap['status'] = ['neq', -2];
if (empty($endTime)) {
$withdrawWhere['settlement_begin_time'] = ['egt', $beginTime];
$withdrawWhere['settlement_end_time'] = ['egt', $beginTime];
} else {
$withdrawWhere['settlement_begin_time'] = ['between', [$beginTime, $endTime]];
$withdrawWhere['settlement_end_time'] = ['between', [$beginTime, $endTime]];
}
$withdrawWhere['_logic'] = 'or';
$withdrawMap['_complex'] = $withdrawWhere;
$withdrawMap['settlement_begin_time'] = ['egt', $beginTime];
$withdraws = M('withdraw', 'tab_')->field('settlement_begin_time, settlement_end_time')
->where($withdrawMap)
->order('settlement_begin_time asc, settlement_end_time asc')
->select();
if (!empty($withdraws)) {
$times = [];
$text = '';
foreach ($withdraws as $withdraw) {
$settlementBeginTime = !empty($withdraw['settlement_begin_time']) ? date('Y-m', $withdraw['settlement_begin_time']) : '';
$settlementEndTime = date('Y-m', $withdraw['settlement_end_time']);
if ($settlementBeginTime && !in_array($settlementBeginTime, $times)) {
$times[] = $settlementBeginTime;
$text .= "{$settlementBeginTime}月、";
}
if (!in_array($settlementEndTime, $times)) {
$times[] = $settlementEndTime;
$text .= "{$settlementEndTime}月、";
}
}
$text = rtrim($text, '、');
$text .= '已有订单申请提现, 无法变更分成比例,请重新选择开始时间';
$this->error($text);
}
}
public function setStatus($status)
{
$params = I('post.');
$ids = $params['ids'] ?? [];
$remark = $params['remark'] ?? '';
if (empty($ids)) {
$this->error('操作失败');
}
if (empty($status) || !in_array($status, [-1, 1])) {
$this->error('操作失败');
}
$promoteGameRatioMap['id'] = ['in', $ids];
$relationGameIds = D(self::MODEL_NAME)->where($promoteGameRatioMap)->getField('relation_game_id', true);
$time = time();
$map['relation_game_id'] = ['in', $relationGameIds];
$map['status'] = 0;
$save['status'] = $status;
$save['reviewer_id'] = is_login();
$save['review_time'] = $time;
$save['update_time'] = $time;
if ($remark) {
$save['remark'] = $remark;
}
$result = D(self::MODEL_NAME)->where($map)->save($save);
if ($result) {
$model = new PromoteGameRatioModel();
$promoteGameRatios = D(self::MODEL_NAME)->where(array('relation_game_id' => ['in', $relationGameIds]))->select();
if (!empty($promoteGameRatios)) {
foreach ($promoteGameRatios as $promoteGameRatio) {
$promoteId = $promoteGameRatio['promote_id'];
$promoteMap['chain'] = ['like', "/{$promoteId}/%"];
$promoteIds = M('promote', 'tab_')->where($promoteMap)->getField('id', true);
$promoteIds[] = $promoteId;
$model->addLog($promoteGameRatio['id']);
if ($status == 1) {
if ($promoteGameRatio['begin_time'] <= strtotime(date('Y-m-d', time()))) {
$spendMap['promote_id'] = ['in', $promoteIds];
$spendMap['game_id'] = $promoteGameRatio['game_id'];
if ($promoteGameRatio['end_time'] > 0) {
$spendMap['pay_time'] = ['between', [$promoteGameRatio['begin_time'], $promoteGameRatio['end_time'] + 3600 * 24 - 1]];
} else {
$spendMap['pay_time'] = ['egt', $promoteGameRatio['begin_time']];
}
$spendMap['pay_status'] = 1;
$spendMap['selle_status'] = 0;
$spendSave['selle_ratio'] = $promoteGameRatio['ratio'];
M('spend', 'tab_')->where($spendMap)->save($spendSave);
}
}
}
}
$this->success('操作成功');
} else {
$this->error('操作失败');
}
}
private function getPromoteApplyCreateTime($promoteId, $gameId)
{
$map['promote_id'] = $promoteId;
$map['game_id'] = $gameId;
$createTime = $apply = M('apply', 'tab_')->where($map)->getField('apply_time');
return $createTime;
}
private function getReviewRule()
{
$rules = getAdminRules(is_login());
$rulesName = BIND_MODULE . '/' . CONTROLLER_NAME . '/setStatus';
$ruleId = getRule($rulesName, 'admin');
$reviewRule = in_array($ruleId, $rules) ? true : false;
$reviewRule = (is_login() == 1) ? true : $reviewRule;
return $reviewRule;
}
public function getPromoteGameRatio()
{
$promoteId = I('post.promote_id', 0);
$promoteId = intval($promoteId);
$gameId = I('post.game_id', 0);
$gameId = intval($gameId);
$record['last_ratio'] = '';
$status = 0;
if ($promoteId || $gameId) {
if ($promoteId && $gameId) {
$map['promote_id'] = $promoteId;
$map['game_id'] = $gameId;
$record = D(self::MODEL_NAME)->where($map)->find();
if ($record) {
$status = 2;
$record['begin_time'] = date('Y-m-d', $record['begin_time']);
$record['end_time'] = empty($record['end_time']) ? '' : date('Y-m-d', $record['end_time']);
if ($record['last_ratio_status'] == 0) {
$record['last_ratio'] = $this->getGameRatio($gameId);
}
} else {
$status = 1;
$record['last_ratio'] = $this->getGameRatio($gameId);
}
} elseif ($gameId) {
$status = 1;
$record['last_ratio'] = $this->getGameRatio($gameId);
}
}
$data = [
'status' => $status,
'record' => $record,
];
$this->ajaxReturn($data);
}
public function getGameRatio($gameId)
{
$gameId = intval($gameId);
$gameRatio = '0.00';
if ($gameId) {
$map['id'] = $gameId;
$gameRatio = M('game', 'tab_')->where($map)->getField('ratio');
$gameRatio = ($gameRatio ?? '0.00') . '%';
}
return $gameRatio;
}
}