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.

506 lines
23 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;
$promoteId = $params['promote_id'] ?? 0;
$gameId = $params['game_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';
if ($promoteId) {
$map['promote_id'] = intval($promoteId);
}
if ($gameId) {
$map['game_id'] = intval($gameId);
}
if ($status !== '') {
$map['status'] = intval($status);
}
$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';
$gameFiled = 'id, relation_game_name, ratio';
$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 = '未知';
$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;
$thisRatio = $promoteGameRatio['ratio'] . '%';
$thisTurnoverRatio = $promoteGameRatio['turnover_ratio'] ? json_decode($promoteGameRatio['turnover_ratio'], true) : [];
$thisRatio = $thisTurnoverRatio ? "{$thisRatio} - " . $thisTurnoverRatio[count($thisTurnoverRatio) - 1]['ratio'] . '%' : $thisRatio;
$thisLastRatioStatus = $promoteGameRatio['last_ratio_status'];
$thisStatusText = self::$statusList[$promoteGameRatio['status']];
$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);
}
if ($issetGame) {
$thisGameName = $games[$thisGameId]['relation_game_name'];
$thisGameRatio = $games[$thisGameId]['ratio'];
$thisGameRatio = ($thisGameRatio ?? '0.00') . '%';
}
$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,
'game_name' => $thisGameName,
'last_ratio' => (($thisLastRatioStatus == 1 || $group != 1) ? $thisLastRatio : $thisGameRatio),
'ratio' => $thisRatio,
'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' => '身份状态',
'game_name' => '已申请游戏',
'last_ratio' => '原分成比例',
'ratio' => '当前分成比例',
'valid_date' => '开始时间',
'remark' => '备注',
'status_text' => '分成比例审核',
'applicant' => '申请人',
'reviewer' => '确认人',
];
data2csv($records,$csvTitle, $field);
exit;
}
$reviewRule = $this->getReviewRule();
$page = set_pagination($count, $row);
if($page) {
$this->assign('_page', $page);
}
$this->assign('group', $group);
$this->assign('records', $records);
$this->assign('count', $count);
$this->assign('gameList', getAllGameList(true));
$this->assign('promoteList', getPromoteByLevel(1));
$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']))) {
if (is_array($params['turnover']) && is_array($params['turnover_ratio'])) {
foreach ($params['turnover'] as $turnover) {
if (empty($turnover)) {
$this->error('月流水不能为空');
}
}
foreach ($params['turnover_ratio'] as $turnoverRatio) {
if (empty($turnoverRatio)) {
$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),
];
}
$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['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']);
if ($promoteGameRatio['status'] == 1) {
$save['last_turnover_ratio'] = $promoteGameRatio['turnover_ratio'];
$save['last_ratio'] = $promoteGameRatio['ratio'];
$save['last_ratio_status'] = 1;
}
$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']);
$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);
$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') . '%';
}
$this->assign('record', $promoteGameRatio);
$this->assign('lastRatio', $lastRatio);
}
$this->assign('gameList', getAllGameList(true));
$this->assign('promoteList', getPromoteByLevel(1));
$this->meta_title = $metaTitle;
$this->display();
}
}
private function isWithdraw($promoteId, $beginTime)
{
$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];
$withdrawMap['settlement_begin_time'] = ['egt', $beginTime];
$withdraw = M('withdraw', 'tab_')->where($withdrawMap)->order('create_time desc')->find();
if (!empty($withdraw) && $withdraw['status'] != -2) {
$time = date('Y-m', $beginTime);
$this->error("{$time}月已有订单申请提现, 无法变更分成比例,请重新选择开始时间");
}
}
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;
}
}