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.

792 lines
26 KiB
PHP

<?php
namespace Base\Service;
use Base\Model\PromoteModel;
use Base\Model\UserPlayInfoModel;
use Base\Model\UserPlayModel;
use Base\Model\UserModel;
use Think\Model;
class PromoteService {
private $model;
public function __construct()
{
$this->model = new PromoteModel();
}
public function froze($id)
{
$data = [
'status' => 2
];
return $this->model->where("id=".$id)->save($data);
}
public function unfreeze($id)
{
$data = [
'status' => 1
];
return $this->model->where("id=".$id)->save($data);
}
public function resetPassword($id, $password = null)
{
if (!$password) {
$password = $this->getRandomPassword(10, false);
}
$passwordSecret = $this->password($password, UC_AUTH_KEY);
if ($this->model->where("id=".$id)->save(['password' => $passwordSecret])) {
return $password;
}
}
public function password($str, $key = 'ThinkUCenter')
{
return '' === $str ? '' : md5(sha1($str) . $key);
}
private function getRandomPassword($length, $special = true){
$chars = array(
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2',
'3', '4', '5', '6', '7', '8', '9'
);
if($special){
$chars = array_merge($chars, array(
'!', '@', '#', '$', '?', '|', '{', '/', ':', ';',
'%', '^', '&', '*', '(', ')', '-', '_', '[', ']',
'}', '<', '>', '~', '+', '=', ',', '.'
));
}
$charsLen = count($chars) - 1;
shuffle($chars);
$password = '';
for($i=0; $i<$length; $i++){
$password .= $chars[mt_rand(0, $charsLen)];
}
return $password;
}
public function addShiftTask($params)
{
$fromPromoteId = isset($params['from_promote_id']) ? $params['from_promote_id'] : 0;
$toPromoteId = isset($params['to_promote_id']) ? $params['to_promote_id'] : 0;
$orderTime = isset($params['order_time']) ? $params['order_time'] : '';
$balanceCoinMode = isset($params['balance_coin_mode']) ? $params['balance_coin_mode'] : 0;
$type = isset($params['type']) ? $params['type'] : 0;
if ($fromPromoteId == $toPromoteId) {
return [
'status' => false,
'msg'=>'相同推广帐号不可迁移'
];
}
if ($orderTime == 0) {
return [
'status' => false,
'msg'=>'订单日期不能为空'
];
}
if ($type == 1 && $balanceCoinMode == 0) {
return [
'status' => false,
'msg'=>'请选择平台币管理方式'
];
}
$isFuture = false;
if (strtotime($orderTime) > strtotime(date('Y-m-d 23:59:59'))) {
$isFuture = true;
}
$data = [
'from_promote_id' => $fromPromoteId,
'to_promote_id' => $toPromoteId,
'order_time' => strtotime($orderTime),
'balance_coin_mode' => $balanceCoinMode,
'create_time' => time(),
'create_promote_id' => session('promote_auth.pid'),
'status' => 0,
'type' => $type
];
if (M('ShiftTask')->add($data)) {
$id = M()->getLastInsID();
$data['id'] = $id;
if ($isFuture) {
return [
'status' => true,
'msg'=>'迁移任务创建成功'
];
} else {
if ($type == 1) {
return $this->shiftPromote($data);
} elseif ($type == 2) {
return $this->shiftPlayer($data);
} else {
return [
'status' => false,
'msg'=>'数据异常'
];
}
}
} else {
return [
'status' => false,
'msg'=>'迁移失败'
];
}
}
public function shiftPromote($task)
{
$model = new Model();
$model->startTrans();
$coinRecordService = new PromoteCoinRecordService();
$promoteCoinService = new PromoteCoinService();
$toPromoteId = $task['to_promote_id'];
$fromPromoteId = $task['from_promote_id'];
$balanceCoinMode = $task['balance_coin_mode'];
$toPromote = D('promote')->where(['id' => $toPromoteId])->find();
$fromPromote = D('promote')->where(['id' => $fromPromoteId])->find();
$topPromoteId = 0;
if ($fromPromote['grand_id'] > 0) {
$topPromoteId = $fromPromote['grand_id'];
} else {
$topPromoteId = $fromPromote['parent_id'];
}
$topPromote = D('promote')->where(['id' => $topPromoteId])->find();
$status = $this->shiftRemoveCoin($fromPromote, $topPromote, $task);
// 测试
// $model->rollback();
if (!$status) {
$model->rollback();
return ['status' => false, 'msg' => '系统异常2'];
}
$promote = new PromoteModel();
if ($fromPromote['grand_id'] == 0) {
$status = $promote->where('parent_id=' . $fromPromoteId)->save(['parent_id' => $toPromoteId]);
} else {
$status = $promote->where('id=' . $fromPromoteId)->save(['parent_id' => $toPromoteId]);
}
if (!$status) {
$model->rollback();
return ['status' => false, 'msg' => '系统异常1'];
}
$status1 = M('ShiftTask')->where('id=' . $task['id'])->save(['status' => 1]);
if (!$status1) {
$model->rollback();
return ['status' => false, 'msg' => '系统异常3'];
}
$model->commit();
return ['status' => true, 'msg' => '推广帐号迁移成功'];
}
public function shiftRemoveCoin($promote, $topPromote, $task)
{
$coinRecordService = new PromoteCoinRecordService();
$promoteCoinService = new PromoteCoinService();
$topBalanceCoin = $this->getBalanceCoin($topPromote['id'], 0);
$topBalancePlus = 0;
$promotes = D('promote')->field(['id', 'balance_coin'])->where(['parent_id' => $promote['id']])->select();
$ids = array_column($promotes, 'id');
$ids[] = $promote['id'];
$promoteCoins = [];
$balances = M('PromoteBalanceCoin', 'tab_')->where(['promote_id' => ['in', $ids]])->select();
if (count($balances) == 0) {
return true;
}
$records = [];
foreach ($balances as $balance) {
if ($balance['num'] == 0) {
continue;
}
$sourceId = 0;
if ($task['balance_coin_mode'] == 1 && $balance['game_id'] == 0) {
$sourceId = $topPromote['id'];
}
$item = [
'game_id' => $balance['game_id'],
'banlan_type' => $balance['game_id'] > 0 ? 2 : 1,
'num' => $balance['num'],
'promote_id' => $balance['promote_id'],
'source_id' => $sourceId,
'type' => 2,
'op_id' => $task['create_promote_id']
];
$refId = $promoteCoinService->addRecord($item);
$records[] = $coinRecordService->createRecord([
'type' => 2,
'sub_type' => $balance['game_id'] > 0 ? 5 : 7,
'ref_id' => $refId,
'target_id' => $balance['promote_id'],
'target_type' => 1,
'coin' => $balance['num'],
'balance_coin' => 0,
'description' => $balance['game_id'] > 0 ? '绑定币回收' : '迁移扣除',
]);
if ($task['balance_coin_mode'] == 1 && $balance['game_id'] == 0) {
$item = [
'game_id' => 0,
'banlan_type' => 1,
'num' => $balance['num'],
'promote_id' => $topPromote['id'],
'source_id' => $balance['promote_id'],
'type' => 1,
'op_id' => $task['create_promote_id'],
];
$refId = $promoteCoinService->addRecord($item);
$topBalanceCoin += $balance['num'];
$topBalancePlus += $balance['num'];
$records[] = $coinRecordService->createRecord([
'type' => 1,
'sub_type' => 6,
'ref_id' => $refId,
'target_id' => $topPromote['id'],
'target_type' => 1,
'coin' => $balance['num'],
'balance_coin' => $topBalanceCoin,
'description' => '迁移回收',
]);
}
}
M('PromoteBalanceCoin', 'tab_')->where(['promote_id' => ['in', $ids]])->save(['num' => 0]);
M('Promote', 'tab_')->where(['id' => ['in', $ids]])->save(['balance_coin' => 0]);
if (count($records) > 0) {
M('PromoteCoinRecord', 'tab_')->addAll($records);
}
if ($topBalancePlus > 0) {
return $this->incCoin($topPromote['id'], $topBalancePlus, 0);
}
return true;
}
public function shiftPlayer($task)
{
/**
* @todo 加事务
*/
$toPromoteId = $task['to_promote_id'];
$fromPromoteId = $task['from_promote_id'];
$createPromote = D('promote')->where(['create_promote_id' => $task['create_promote_id']])->find();
$toPromote = D('promote')->where(['id' => $toPromoteId])->find();
$fromPromote = D('promote')->where(['id' => $fromPromoteId])->find();
// $promote = new PromoteModel();
// $promote->where('parent_id=' . $fromPromoteId)->save(['parent_id' => $toPromoteId]);
$user1 = new UserModel();
$users = $user1->field(['id', 'account', 'nickname'])->where('promote_id=' . $fromPromoteId)->select();
foreach ($users as $item) {
$data = [
'user_id' => $item['id'],
'user_account' => $item['account'],
'user_nickname' => $item['nickname'],
'promote_id' => $fromPromote['id'],
'promote_account' => $fromPromote['account'],
'promote_id_to' => $toPromote['id'],
'promote_account_to' => $toPromote['account'],
'remark' => '玩家迁移',
'create_time' => time(),
'op_id' => $createPromote['id'],
'op_account' => $createPromote['account'],
'op_type' => 1,
'bind_type' => 1,
];
M('Mend', 'tab_')->add($data);
}
$user = new UserModel();
$user->where('promote_id=' . $fromPromoteId)->save(['promote_id' => $toPromoteId, 'promote_account' => $toPromote['account']]);
$userPlayer = new UserPlayModel();
$userPlayer->where('promote_id=' . $fromPromoteId)->save(['promote_id' => $toPromoteId, 'promote_account' => $toPromote['account']]);
$userPlayerInfo = new UserPlayInfoModel();
$userPlayerInfo->where('promote_id=' . $fromPromoteId)->save(['promote_id' => $toPromoteId, 'promote_account' => $toPromote['account']]);
M('ShiftTask')->where('id=' . $task['id'])->save(['status' => 1]);
return ['status' => true, 'msg' => '玩家迁移成功'];
}
public function cancelShift($params)
{
$promoteId = $params['promote_id'];
$type = $params['type'];
$status = M('ShiftTask')->where(['from_promote_id' => $promoteId, 'type' => $type, 'status' => 0])->save(['status' => 2]);
if ($status) {
return ['status' => true, 'msg' => '取消成功'];
} else {
return ['status' => false, 'msg' => '取消失败'];
}
}
public function shiftUserCoin($params)
{
$model = new Model();
$model->startTrans();
$promoteId = $params['promote_id'];
$userId = $params['user_id'];
$remark = $params['remark'];
$num = $params['num'];
$gameId = $params['game_id'];
$isUseBind = $params['is_use_bind'];
$promote = D('promote')->where(['id' => $promoteId])->find();
$user = D('User')->where(['id' => $userId])->find();
$userPlay = D('UserPlay')->where(['user_id' => $userId])->find();
$game = M('Game', 'tab_')->where(['id' => $gameId])->find();
$fromBalanceCoin = 0;
if ($isUseBind) {
$fromBalanceCoin = $this->getBalanceCoin($promoteId, $gameId);
} else {
$fromBalanceCoin = $this->getBalanceCoin($promoteId, 0);
}
if ($fromBalanceCoin < $num) {
return [
'msg' => '平台币不足',
'status' => false,
];
}
$agentParams = [];
$agentParams['game_name'] = $game['game_name'];
$agentParams['game_appid'] = $game['game_appid'];
$agentParams['game_id'] = $gameId;
$agentParams['pay_order_number'] = '';
$agentParams['order_number'] = '';
$agentParams['promote_id'] = $promoteId;
$agentParams['promote_account'] = $promote['account'];
$agentParams['user_id'] = $userId;
$agentParams['user_account'] = $user['account'];
$agentParams['user_nickname'] = $user['nickname'];
$agentParams['amount'] = $num;
$agentParams['real_amount'] = $num;
$agentParams['pay_status'] = 1;
$agentParams['pay_type'] = 0;
$agentParams['create_time'] = time();
$agentParams['zhekou'] = 10;
$agentParams['pay_way'] = 4;
$fromParams = [];
$fromParams['game_id'] = $gameId;
$fromParams['banlan_type'] = $isUseBind ? 2 : 1;
$fromParams['num'] = $num;
$fromParams['promote_id'] = $promoteId;
$fromParams['source_id'] = $userId;
$fromParams['type'] = 2;
$fromParams['source_type'] = 2;
$fromParams['remark'] = $remark;
$fromParams['description'] = $isUseBind ? '绑定币转账' : '通用币转账';
$agentRefId = D('Agent')->addRecord($agentParams);
$promoteCoinService = new PromoteCoinService();
$formRefId = $promoteCoinService->addRecord($fromParams);
if ($formRefId == 0 || $agentRefId == 0) {
$model->rollback();
return [
'msg' => '系统异常',
'status' => false,
];
}
$promoteCoinRecordService = new PromoteCoinRecordService();
$fromRecord = [
'type' => 2,
'sub_type' => 4,
'ref_id' => $formRefId,
'target_id' => $promoteId,
'target_type' => 1,
'coin' => $num,
'balance_coin' => $fromBalanceCoin - $num,
'description' => $isUseBind ? '绑定币转账' : '通用币转账',
'remark' => $remark,
];
$promoteCoinRecordService->addRecord($fromRecord);
$toRecord = [
'type' => 1,
'sub_type' => 3,
'ref_id' => $agentRefId,
'target_id' => $userId,
'target_type' => 2,
'coin' => $num,
'balance_coin' => $userPlay['bind_balance'] + $num,
'description' => $isUseBind ? '绑定币转账' : '通用币转账',
'remark' => $remark,
];
$promoteCoinRecordService->addRecord($toRecord);
$incStatus = M('UserPlay', 'tab_')->where(['game_id' => $gameId, 'user_id' => $userId])->setInc('bind_balance', $num);
$decStatus = $this->decCoin($promoteId, $num, $isUseBind ? $gameId : 0);
if (!$incStatus || !$decStatus) {
$model->rollback();
return [
'msg' => '系统异常',
'status' => false,
];
}
$model->commit();
return [
'msg' => '转帐成功',
'status' => true,
];
}
public function shiftCoin($params)
{
$toPromoteId = $params['to_promote_id'];
$fromPromoteId = $params['from_promote_id'];
$remark = $params['remark'];
$num = $params['num'];
$gameId = $params['game_id'];
$isUseBind = $params['is_use_bind'];
$toPromote = D('promote')->where(['id' => $toPromoteId])->find();
$fromPromote = D('promote')->where(['id' => $fromPromoteId])->find();
$toBalanceCoin = $this->getBalanceCoin($toPromoteId, $gameId);
$fromBalanceCoin = 0;
if ($isUseBind) {
$fromBalanceCoin = $this->getBalanceCoin($fromPromoteId, $gameId);
} else {
$fromBalanceCoin = $this->getBalanceCoin($fromPromoteId, 0);
}
if ($fromBalanceCoin < $num) {
return [
'msg' => '平台币不足',
'status' => false,
];
}
$model = new Model();
$model->startTrans();
$params = [];
$params['game_id'] = $gameId;
$params['banlan_type'] = $gameId > 0 ? 2 : 1;
$params['num'] = $num;
$params['source_type'] = 1;
$params['remark'] = $remark;
$params['description'] = $isUseBind ? '绑定币转账' : '通用币转账';
$fromParams = $params;
$fromParams['promote_id'] = $fromPromoteId;
$fromParams['source_id'] = $toPromoteId;
$fromParams['type'] = 2;
$toParams = $params;
$toParams['promote_id'] = $toPromoteId;
$toParams['source_id'] = $fromPromoteId;
$toParams['type'] = 1;
$promoteCoinService = new PromoteCoinService();
$formRefId = $promoteCoinService->addRecord($fromParams);
$toRefId = $promoteCoinService->addRecord($toParams);
if ($formRefId == 0 || $toRefId == 0) {
$model->rollback();
return [
'msg' => '系统异常',
'status' => false,
];
}
$promoteCoinRecordService = new PromoteCoinRecordService();
$fromRecord = [
'type' => 2,
'sub_type' => 4,
'ref_id' => $formRefId,
'target_id' => $fromPromoteId,
'target_type' => 1,
'coin' => $num,
'balance_coin' => $fromBalanceCoin - $num,
'description' => '平台币转账',
'remark' => $remark,
];
$promoteCoinRecordService->addRecord($fromRecord);
$toRecord = [
'type' => 1,
'sub_type' => 3,
'ref_id' => $toRefId,
'target_id' => $toPromoteId,
'target_type' => 1,
'coin' => $num,
'balance_coin' => $toBalanceCoin + $num,
'description' => '平台币转账',
'remark' => $remark,
];
$promoteCoinRecordService->addRecord($toRecord);
$incStatus = $this->incCoin($toPromoteId, $num, $gameId);
$decStatus = $this->decCoin($fromPromoteId, $num, $isUseBind ? $gameId : 0);
if (!$incStatus || !$decStatus) {
$model->rollback();
return [
'msg' => '系统异常',
'status' => false,
];
}
$model->commit();
return [
'msg' => '转帐成功',
'status' => true,
];
}
/**
* 添加平台币(不主动产生流水)
*/
public function incCoin($promoteId, $num, $gameId = 0)
{
$status = M('Promote', 'tab_')->where(['id' => $promoteId])->setInc('balance_coin', $num);
if (!$status) {
return false;
}
$promoteBalance = M('PromoteBalanceCoin', 'tab_')->where(['promote_id' => $promoteId, 'game_id' => $gameId])->find();
if (empty($promoteBalance)) {
$record = [
'promote_id' => $promoteId,
'game_id' => $gameId,
'num' => $num,
'status' => 1
];
return M('PromoteBalanceCoin', 'tab_')->add($record);
} else {
return M('PromoteBalanceCoin', 'tab_')->where(['promote_id' => $promoteId, 'game_id' => $gameId])->setInc('num', $num);
}
}
/**
* 减少平台币(不主动产生流水)
*/
public function decCoin($promoteId, $num, $gameId = 0)
{
$status = M('Promote', 'tab_')->where(['id' => $promoteId])->setDec('balance_coin', $num);
if (!$status) {
return false;
}
$promoteBalance = M('PromoteBalanceCoin', 'tab_')->where(['promote_id' => $promoteId, 'game_id' => $gameId])->find();
if (empty($promoteBalance)) {
return false;
} else {
return M('PromoteBalanceCoin', 'tab_')->where(['promote_id' => $promoteId, 'game_id' => $gameId])->setDec('num', $num);
}
}
/**
* 清除绑定平台币(不主动产生流水)
*/
public function clearBindCoin($promoteId)
{
$num = M('PromoteBalanceCoin', 'tab_')->where(['promote_id' => $promoteId, 'game_id' => ['gt', 0]])->sum('num');
$num = intval($num);
if ($num == 0) {
return true;
}
$status = M('Promote', 'tab_')->where(['id' => $promoteId])->setDec('balance_coin', $num);
if (!$status) {
return false;
}
return M('PromoteBalanceCoin', 'tab_')->where(['promote_id' => $promoteId, 'game_id' => ['gt', 0]])->save(['num' => 0]);
}
/**
* 管理员给推广员扣回平台币
*/
public function adminDecCoin($promoteId, $num, $adminId, $gameId = 0)
{
$balanceCoin = $this->getBalanceCoin($promoteId, $gameId);
$model = new Model();
$model->startTrans();
$log = [];
$log['game_id'] = $gameId;
$log['banlan_type'] = $gameId > 0 ? 2 : 1;
$log['num'] = $num;
$log['promote_id'] = $promoteId;
$log['source_id'] = 0;
$log['type'] = 2;
$log['op_id'] = $adminId;
$promoteCoinService = new PromoteCoinService();
$refId = $promoteCoinService->addRecord($log);
$promoteCoinRecordService = new PromoteCoinRecordService();
$record = [
'type' => 2,
'sub_type' => 5,
'ref_id' => $refId,
'target_id' => $promoteId,
'target_type' => 1,
'coin' => $num,
'balance_coin' => $balanceCoin - $num,
'description' => '后台回收',
'remark' => '',
];
$promoteCoinRecordService->addRecord($record);
$status = $this->decCoin($promoteId, $num, $gameId);
if ($refId && $status) {
$model->commit();
return true;
}
$model->rollback();
return false;
}
/**
* 管理员给推广员添加平台币
*/
public function adminIncCoin($promoteId, $num, $adminId, $gameId = 0)
{
$balanceCoin = $this->getBalanceCoin($promoteId, $gameId);
$model = new Model();
$model->startTrans();
$log = [];
$log['game_id'] = $gameId;
$log['banlan_type'] = $gameId > 0 ? 2 : 1;
$log['num'] = $num;
$log['promote_id'] = $promoteId;
$log['source_id'] = 0;
$log['type'] = 1;
$log['op_id'] = $adminId;
$promoteCoinService = new PromoteCoinService();
$refId = $promoteCoinService->addRecord($log);
$status = $this->incCoin($promoteId, $num, $gameId);
$promoteCoinRecordService = new PromoteCoinRecordService();
$record = [
'type' => 1,
'sub_type' => 2,
'ref_id' => $refId,
'target_id' => $promoteId,
'target_type' => 1,
'coin' => $num,
'balance_coin' => $balanceCoin + $num,
'description' => '后台发放',
'remark' => '',
];
$promoteCoinRecordService->addRecord($record);
if ($refId && $status) {
$model->commit();
return true;
}
$model->rollback();
return false;
}
public function getBalanceCoin($promoteId, $gameId = 0)
{
$promoteBalance = M('PromoteBalanceCoin', 'tab_')->where(['promote_id' => $promoteId, 'game_id' => $gameId])->find();
$balanceCoin = 0;
if (!empty($promoteBalance)) {
$balanceCoin = $promoteBalance['num'];
}
return $balanceCoin;
}
public function getTopPromote($promote)
{
$chain = trim($promote['chain'], '/');
if ($chain == '') {
return $promote;
} else {
$topPromoteId = explode('/', $chain)[0];
return M('promote', 'tab_')->where(['id' => $topPromoteId])->find();
}
}
public function getPromoteById($id, $with = [])
{
$promote = M('promote', 'tab_')->where(['id' => $id])->find();
if ($promote && in_array('parent', $with)) {
$promote['parent'] = M('promote', 'tab_')->where(['id' => $promote['parent_id']])->find();
}
if ($promote && in_array('top', $with)) {
$promote['top'] = $this->getTopPromote($promote);
}
return $promote;
}
public function getMarketAdminIdByPromoteId($promoteId)
{
$promote = $this->getPromoteById($promoteId, ['top']);
return $this->getMarketAdminIdByPromote($promote);
}
public function getMarketAdminIdByPromote($promote)
{
$marketAdminId = 0;
if ($promote) {
$topPromote = $promote['top'] ?? null;
if (is_null($topPromote)) {
$topPromote = $this->getTopPromote($promote);
}
$marketAdminId = $topPromote['admin_id'];
}
return $marketAdminId;
}
public function getSettleGameIds($promote)
{
$topPromote = $promote['top'] ?? null;
if (is_null($topPromote)) {
$topPromote = $this->getTopPromote($promote);
}
return $topPromote['game_ids'] == '' ? [] : explode(',', $topPromote['game_ids']);
}
public function isGameIdSettle($promote, $gameId)
{
$gameIds = $this->getSettleGameIds($promote);
if (in_array($gameId, $gameIds)) {
return true;
}
return false;
}
}