<?php
namespace Admin\Controller;
use User\Api\UserApi as UserApi;
/**
* 后台首页控制器
* @author 麦当苗儿 < zuojiazi @ vip . qq . com >
*/
class SpendController extends ThinkController
{
const model_name = 'Spend';
public $OpAuthList;
public function lists()
{
$startDate = empty($_REQUEST['timestart']) ? date('Y-m-d', strtotime('-6 day', time())) : $_REQUEST['timestart'];
$endDate = empty($_REQUEST['timeend']) ? date('Y-m-d') : $_REQUEST['timeend'];
$startTime = strtotime($startDate);
$endTime = strtotime($endDate) + 86399;
$map['pay_time'] = array('BETWEEN', array($startTime, $endTime));
unset($_REQUEST['timestart']);
unset($_REQUEST['timeend']);
if (isset($_REQUEST['is_check'])) {
$map['is_check'] = (int)($_REQUEST['is_check']);
}
if (isset($_REQUEST['is_refund'])) {
$map['is_refund'] = (int)($_REQUEST['is_refund']);
}
if (isset($_REQUEST['user_account'])) {
$map['user_account'] = array('like', trim($_REQUEST['user_account']));
unset($_REQUEST['user_account']);
}
if (isset($_REQUEST['spend_ip'])) {
$map['spend_ip'] = array('like', trim($_REQUEST['spend_ip']) . '%');
unset($_REQUEST['spend_ip']);
}
if (isset($_REQUEST['game_name'])) {
if ($_REQUEST['game_name'] == '全部') {
unset($_REQUEST['game_name']);
} else {
$map['game_id'] = ['in', array_column(getGameByName($_REQUEST['game_name'], $_REQUEST['game_type']), 'id')];
unset($_REQUEST['game_name']);
}
}
if (isset($_REQUEST['game_type']) & & $_REQUEST['game_type']) {
$map['sdk_version'] = $_REQUEST['game_type'];
}
if (isset($_REQUEST['server_id'])) {
$map['server_id'] = $_REQUEST['server_id'];
unset($_REQUEST['server_id']);
}
if (isset($_REQUEST['pay_order_number'])) {
$map['pay_order_number'] = array('like', trim($_REQUEST['pay_order_number']));
unset($_REQUEST['pay_order_number']);
}
if (isset($_REQUEST['extend'])) {
$map['extend'] = array('like', trim($_REQUEST['extend']));
unset($_REQUEST['extend']);
}
if (isset($_REQUEST['pay_status'])) {
$map['pay_status'] = $_REQUEST['pay_status'];
unset($_REQUEST['pay_status']);
}
if (isset($_REQUEST['pay_way'])) {
if ($_REQUEST['pay_way'] == 2) {
$map['pay_way'] = ['in', '2,3,4'];
} else {
$map['pay_way'] = $_REQUEST['pay_way'];
}
unset($_REQUEST['pay_way']);
}
if (isset($_REQUEST['pay_game_status'])) {
$map['pay_game_status'] = $_REQUEST['pay_game_status'];
unset($_REQUEST['pay_game_status']);
}
if ($_REQUEST['merchant_id']) {
$map['merchant_id'] = $_REQUEST['merchant_id'];
unset($_REQUEST['merchant_id']);
}
if (isset($_REQUEST['promote_id'])) {
$promoteId = intval($_REQUEST['promote_id']);
if ($promoteId == 0) {
$map['promote_id'] = 0;
} else {
$promoteMap['chain'] = ['like', "%/$promoteId/%"];
$promoteIds = M('Promote', 'tab_')->where($promoteMap)->getField('id', true);
$promoteIds[] = $promoteId;
$map['promote_id'] = ['in', $promoteIds];
}
}
$isMarketAdmin = isMarketAdmin();
$isMarketLeader = isMarketLeader();
$marketAdminIds = getSearchAdminIds('market_admin_id');
if (is_array($marketAdminIds)) {
$map['market_admin_id'] = ['in', $marketAdminIds];
}
setPowerPromoteIds($map,'promote_id');
if ($_REQUEST['data_order'] != '') {
$data_order = reset(explode(',', $_REQUEST['data_order']));
$data_order_type = end(explode(',', $_REQUEST['data_order']));
$this->assign('userarpu_order', $data_order);
$this->assign('userarpu_order_type', $data_order_type);
$sort = $data_order == 3 ? 'desc' : 'asc';
$order = $data_order_type . ' ' . $sort;
} else {
$order = ' pay_time desc ';
}
if (I('platform_type', 0) != 0) {
$sy = I('platform_type', 0) == 1 ? '=' : '< >';
$game_id = M("Game","tab_")->where('unique_code ' . $sy . ' ""')->getField("id",true);
$map['game_id'] = ['in',$game_id];
}
// if (intval(($endTime + 1) - $startTime) / (24 * 3600) < = 31) {
// $map1 = $map;
// $map1['pay_status'] = 1;
// } else {
// $map = '1 = 2';
// $map1 = $map;
// }
$this->checkListOrCountAuthRestMap($map,["extend", "pay_order_number", "user_account"]);
D("Spend")->addSubsiteWhere($map);
$map1 = $map;
$map1['pay_status'] = 1;
$total = null_to_0(M(self::model_name,"tab_")->where($map1)->sum('pay_amount'));
$this->assign('total', $total);
$this->meta_title = $this->m_title = '游戏充值';
$this->assign('commonset', M('Kuaijieicon')->where(['url' => 'Spend/lists', 'status' => 1])->find());
$data = D(self::model_name)->lists($_GET["p"], $map, $order);
if (!empty($data['data'])) {
$adminIds = array_column($data['data'], 'market_admin_id');
$adminUsernameList = getAdminUsernameList($adminIds);
$adminDepartments = getAdminDepartmentList($adminIds);
foreach ($data['data'] as $key=>& $value) {
($value['promote_account']=='官方渠道')?($value['promote_account']=C('OFFICIEL_CHANNEL')):'';
$value['market_admin_username'] = $adminUsernameList[$value['market_admin_id']] ?? '无';
$value['admin_department'] = $adminDepartments[$value['market_admin_id']] ?? '无';
$value['is_check_str'] = D(self::model_name)::IsCheckStr[$value['is_check']];
$value['is_refund_str'] = D(self::model_name)::IsRefundStr[$value['is_refund']];
//拆分游戏名称和设备类型
$game_arr = explode("(", rtrim($value['game_name'], ")"));
$value['game_name'] = $game_arr[0];
$value['sdk_version_name'] = $game_arr[1];
}
}
$departments = M('department', 'sys_')->select();
$this->assign('departments', $departments);
$this->assign('isMarketLeader', $isMarketLeader);
$this->assign('showMarketAdmin', session('user_auth')['show_market_admin']);
$this->assign('showPromote', session('user_auth')['show_promote']);
$this->assign('isMarketAdmin', $isMarketAdmin);
$this->assign('marketAdmins', getMarketAdmins());
$this->assign('startDate', $startDate);
$this->assign('endDate', $endDate);
$this->assign('list_data', $data['data']);
$this->assign('_page', $data['page']);
$show_data_power = (is_administrator()|| session('user_auth')['show_data']);
$this->assign('show_data_power', $show_data_power);
if(!IS_ROOT){
$this->OpAuthList= getModuleControllerAuth();
}
$this->assign('menubtn',$this->menuAuth());
$this->display();
}
protected function menuAuth()
{
$mentBtn = [
"addStatement"=>"< a class = 'butn' id = 'addStatement' > 参与结算< / a > ",
"removeStatement"=>"< a class = 'butn' id = 'removeStatement' style = 'background-color: red;' > 移出结算< / a > ",
"addRefund"=>"< a class = 'butn' id = 'addRefund' > 登记退款< / a > ",
"removeRefund"=>"< a class = 'butn' id = 'removeRefund' style = 'background-color: red;' > 取消退款< / a > ",
];
$resarr = [];
foreach ($mentBtn as $k => $v) {
if(IS_ROOT){
$resarr[] = $v;
}else{
if(in_array($k,$this->OpAuthList)){
$resarr[] = $v;
}
}
}
return $resarr;
}
//是否结算
public function addStatement()
{
if(!isset($_REQUEST['ids'])) $this->error("参数错误");
$ids = $_REQUEST['ids'];
$this->editOrderIscheck($ids,2,1);
$this->ajaxReturn(array(
'status' => 1,
"info"=>"操作成功"
));
}
public function removeStatement()
{
if(!isset($_REQUEST['ids'])) $this->error("参数错误");
$ids = $_REQUEST['ids'];
$this->editOrderIscheck($ids,1,2);
$this->ajaxReturn(array(
'status' => 1,
"info"=>"操作成功"
));
}
protected function editOrderIscheck($ids,$oldval,$newval){
$dbres = D(self::model_name)->where("id in ({$ids}) and is_check = '{$oldval}'")->select();
if(empty($dbres)) return false;
$changeIds = implode(",",array_column($dbres,"id"));
//执行操作
$res = D(self::model_name)->where("id in ({$changeIds}) and is_check = '{$oldval}'")->save(['is_check'=>$newval]);
if(empty($res)) return false;
//日志
if($newval == 1){
$op_name = "加入结算";
$key = "addStatement";
}else{
$op_name = "移出结算";
$key = "removeStatement";
}
addOperationLog(['op_type'=>1,'key'=>$key,"op_name"=>$op_name,"content"=>json_encode(["ids"=>$_REQUEST['ids'],"is_check"=>$newval])]);
return true;
}
//是否退款
public function addRefund()
{
if(!isset($_REQUEST['ids'])) $this->error("参数错误");
$ids = $_REQUEST['ids'];
$this->editOrderIsrefund($ids,0,1);
$this->ajaxReturn(array(
'status' => 1,
"info"=>"操作成功"
));
}
public function removeRefund()
{
if(!isset($_REQUEST['ids'])) $this->error("参数错误");
$ids = $_REQUEST['ids'];
$this->editOrderIsrefund($ids,1,0);
$this->ajaxReturn(array(
'status' => 1,
"info"=>"操作成功"
));
}
protected function editOrderIsrefund($ids,$oldval,$newval){
$time = time();
$dbres = D(self::model_name)->field("id as s_id,user_id,user_account,pay_amount as refund_amount,pay_time,{$time} as create_time")->where("id in ({$ids}) and is_refund = '{$oldval}' and pay_status = 1")->select();
if(empty($dbres)) return false;
$changeIds = implode(",",array_column($dbres,"s_id"));
//执行操作
$res = D(self::model_name)->where("id in ({$changeIds}) and is_refund = '{$oldval}'")->save(['is_refund'=>$newval]);
if(empty($res)) return false;
//日志
if($newval == 1){
$op_name = "登记退款";
$key = "addRefund";
SM('spend_refund',"tab_")->addAll($dbres);
}else{
$op_name = "取消退款";
$key = "removeRefund";
SM('spend_refund',"tab_")->where("s_id in ({$changeIds}) ")->delete();
}
addOperationLog(['op_type'=>1,'key'=>$key,"op_name"=>$op_name,"content"=>json_encode(["ids"=>$_REQUEST['ids'],"is_refund"=>$newval])]);
return true;
}
}