123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352 |
- <?php
- namespace app\admin\controller;
- use app\common\controller\Backend;
- use app\common\model\OrdersCollect;
- use think\Controller;
- use think\Db;
- use think\Log;
- /**
- * 数据统计-订单汇总
- *
- * @icon fa fa-circle-o
- */
- class Vipindex extends Backend
- {
- /**
- * @var OrdersCollect
- */
- protected $model = null;
- protected $groupId;
- protected $authId;
- /**
- * @var VipAdminBind VipAdminBind模型对象
- */
- protected $vipAdminBindModel = null;
- public function _initialize()
- {
- parent::_initialize();
- $this->model = model('OrdersCollect');
- $this->view->assign("typeList", $this->model->getTypeList());
- $this->view->assign("flagList", $this->model->getFlagList());
- $group = model('AuthGroupAccess')->where('uid',$this->auth->id)->find();
- $this->vipAdminBindModel = model('VipAdminBind');
- $truegroupId = $group->group_id;
- // if(isset($_GET['channel_id']) && intval($_GET['channel_id'])>0){
- // $groupObj = model('AuthGroupAccess')->where('uid',$_GET['channel_id'])->find();
- // if($groupObj){
- // $thisGroup = $groupObj->group_id;
- // if($thisGroup > 2){ //判断传过来的不是管理员的id
- // if($thisGroup==3){ //如果是渠道商
- // if($truegroupId <3){ //只有管理员才有此操作
- // $this->authId = $_GET['channel_id'];
- // $thisChannel = model('Admin')->where('id',$this->authId)->find();
- // $this->groupId = 3;
- // $this->assign('channelTitle','当前是渠道商('.$thisChannel->nickname.')的数据');
- // $this->assignconfig('channelstr','?channel_id='.$this->authId);
- // }
- // }else{
- // if($truegroupId <4){ //管理员或渠道商才有此操作
- // if($truegroupId == 3){ //如果是从渠道商查来的,判断当前代理商是不是渠道商下属
- // $isCh = model('AdminExtend')->where(['admin_id'=>$_GET['channel_id'],'create_by'=>$group->uid])->find();
- // if($isCh){ //如果是当前渠道商的下属
- // $this->authId = $_GET['channel_id'];
- // $thisChannel = model('Admin')->where('id',$this->authId)->find();
- // $this->groupId = 4;
- // $this->assign('channelTitle','当前是代理商('.$thisChannel->nickname.')的数据');
- // $this->assignconfig('channelstr','?channel_id='.$this->authId);
- // }
- // }else{ //管理员查过来的
- // $this->authId = $_GET['channel_id'];
- // $thisChannel = model('Admin')->where('id',$this->authId)->find();
- // $this->groupId = 4;
- // $this->assign('channelTitle','当前是代理商('.$thisChannel->nickname.')的数据');
- // $this->assignconfig('channelstr','?channel_id='.$this->authId);
- // }
- //
- // }
- // }
- // }
- // }
- //
- // }else{
- // $this->authId = $this->auth->id;
- // $this->groupId = $truegroupId;
- // $this->assignconfig('channelstr','?channel_id=0');
- // }
- $this->view->assign("groupId",$this->groupId);
- $this->assignconfig('groupId',$this->groupId);
- $this->assignconfig('authId',$this->authId);
- }
- public function index()
- {
- $vipadminIds = model("VipAdminBind")
- ->where("admin_id_master",$this->auth->id)
- ->where("flag = 1")
- ->column("admin_id_slave");
- $zeroKey = array_search(0, $vipadminIds);
- if ($zeroKey !== false) {
- array_splice($vipadminIds, $zeroKey, 1);
- }
- //今天
- $today = date('Ymd', time());
- //昨天
- $yesterday = date('Ymd', strtotime('-1 days'));
- //本月
- $month = date('Ym', time()).'01';
- $fields = "sum(recharge_money) recharge_money,sum(recharge_money_benefit) recharge_money_benefit,sum(normal_recharge_orders_count) normal_recharge_orders_count,sum(normal_recharge_orders) normal_recharge_orders,sum(normal_recharge_money) normal_recharge_money,sum(normal_recharge_money_benefit) normal_recharge_money_benefit,sum(normal_recharge_user_suc_count) normal_recharge_user_suc_count,sum(vip_recharge_orders_count) vip_recharge_orders_count,sum(vip_recharge_orders) vip_recharge_orders,sum(vip_recharge_money) vip_recharge_money,sum(pv) pv,sum(uv) uv";
- if ($vipadminIds) {
- $todayData = $this->model
- ->field($fields)
- ->where(['type' => '1', 'flag' => '1', 'createdate' => $today])
- ->where("admin_id", "in", $vipadminIds)
- ->find(); //今天自己的数据
- $todayAgentData = $this->model
- ->field($fields)
- ->where(['type' => '1', 'flag' => '2', 'createdate' => $today])
- ->where("admin_id", "in", $vipadminIds)
- ->find(); //今天代理商的数据
- $todayAllData = $this->model
- ->field($fields)
- ->where(['type' => '1', 'flag' => '3', 'createdate' => $today])
- ->where("admin_id", "in", $vipadminIds)
- ->find(); //今天所有的数据
- $pvuv = Db::table('admin_puv')
- ->field("sum(pv) pv,sum(uv) uv")
- ->where('admin_id', "in", $vipadminIds)
- ->where("updatetime", "egt", strtotime("today"))
- ->find();
- } else {
- $todayData = $todayAgentData = $todayAllData = false;
- $pvuv = ['pv' => 0, 'uv' => 0];
- }
- $this->assign('pvuv',$pvuv);
- if($todayAllData){
- if($todayData){
- $todayAllData['recharge_money'] = $todayData->recharge_money;
- $todayAllData['recharge_money_benefit'] = $todayData->recharge_money_benefit;
- }else{
- $todayAllData['recharge_money'] = 0;
- $todayAllData['recharge_money_benefit'] = 0;
- }
- if($todayAgentData){
- $todayAllData['agent_recharge'] = $todayAgentData->recharge_money;
- $todayAllData['agent_recharge_benefit'] = $todayAgentData->recharge_money_benefit;
- }else{
- $todayAllData['agent_recharge'] = 0;
- $todayAllData['agent_recharge_benefit'] = 0;
- }
- }
- if ($vipadminIds) {
- $yesterdayData = $this->model
- ->where(['type' => '1', 'flag' => '1', 'createdate' => $yesterday])
- ->where("admin_id", "in", $vipadminIds)
- ->field($fields)
- ->find(); //昨天自己的数据
- $yesterdayAgentData = $this->model
- ->where(['type' => '1', 'flag' => '2', 'createdate' => $yesterday])
- ->where("admin_id", "in", $vipadminIds)
- ->field($fields)
- ->find(); //昨天代理商的数据
- $yesterdayAllData = $this->model
- ->where(['type' => '1', 'flag' => '3', 'createdate' => $yesterday])
- ->where("admin_id", "in", $vipadminIds)
- ->field($fields)
- ->find(); //昨天所有的数据
- } else {
- $yesterdayData = $yesterdayAgentData = $yesterdayAllData = false;
- }
- if(!empty($yesterdayAllData)){
- if($yesterdayData){
- $yesterdayAllData['recharge_money'] = $yesterdayData->recharge_money;
- $yesterdayAllData['recharge_money_benefit'] = $yesterdayData->recharge_money_benefit;
- }else{
- $yesterdayAllData['recharge_money'] = 0;
- $yesterdayAllData['recharge_money_benefit'] = 0;
- }
- if($yesterdayAgentData){
- $yesterdayAllData['agent_recharge'] = $yesterdayAgentData->recharge_money;
- $yesterdayAllData['agent_recharge_benefit'] = $yesterdayAgentData->recharge_money_benefit;
- }else{
- $yesterdayAllData['agent_recharge'] = 0;
- $yesterdayAllData['agent_recharge_benefit'] = 0;
- }
- }
- $monthData = $this->model
- ->sumCollect($vipadminIds,1,1,$month,$yesterday); //当月自己的数据
- $monthAgentData = $this->model
- ->sumCollect($vipadminIds,1,2,$month,$yesterday); //当月代理商的数据
- $monthAllData = $this->model
- ->sumCollect($vipadminIds,1,3,$month,$yesterday); //当月所有的数据
- if(!empty($monthAllData)){
- if($monthData){
- $monthAllData['recharge_money'] = $monthData->recharge_money;
- $monthAllData['recharge_money_benefit'] = $monthData->recharge_money_benefit;
- }else{
- $monthAllData['recharge_money'] = 0;
- $monthAllData['recharge_money_benefit'] = 0;
- }
- if($monthAgentData){
- $monthAllData['agent_recharge'] = $monthAgentData->recharge_money;
- $monthAllData['agent_recharge_benefit'] = $monthAgentData->recharge_money_benefit;
- }else{
- $monthAllData['agent_recharge'] = 0;
- $monthAllData['agent_recharge_benefit'] = 0;
- }
- }
- $allData = $this->model
- ->sumCollect($vipadminIds,1,1); //汇总自己的数据
- $allAgentData = $this->model
- ->sumCollect($vipadminIds,1,2); //汇总代理商的数据
- $allAllData = $this->model
- ->sumCollect($vipadminIds,1,3); //汇总所有的数据
- if($allAllData){
- if($allData){
- $allAllData['recharge_money'] = $allData->recharge_money;
- $allAllData['recharge_money_benefit'] = $allData->recharge_money_benefit;
- }else{
- $allAllData['recharge_money'] = 0;
- $allAllData['recharge_money_benefit'] = 0;
- }
- if($allAgentData){
- $allAllData['agent_recharge'] = $allAgentData->recharge_money;
- $allAllData['agent_recharge_benefit'] = $allAgentData->recharge_money_benefit;
- }else{
- $allAllData['agent_recharge'] = 0;
- $allAllData['agent_recharge_benefit'] = 0;
- }
- }
- $this->assign('today',$todayAllData);
- $this->assign('yesterday',$yesterdayAllData);
- $this->assign('month',$monthAllData);
- $this->assign('all',$allAllData);
- if ($this->request->isAjax()) {
- $beginDate = date('Ymd', strtotime('-31 days'));
- $endDate = date('Ymd', strtotime('-1 days'));
- list($where, $sort, $order, $offset, $limit) = $this->buildparams();
- $condition = [];
- $condition['type'] = 1;
- $condition['flag'] = 3;
- // $total = $this->model
- // ->where($where)
- // ->where($condition)
- // ->where("createdate",$today)
- // ->where("admin_id","in",$vipadminIds)
- // ->order($sort, $order)
- // ->count();
- if ($vipadminIds) {
- $total = model("Admin")
- ->where("id","in",$vipadminIds)
- ->count();
- $list = $this->model
- ->alias("a")
- // ->where($condition)
- ->join("admin_config ac","a.admin_id = ac.admin_id AND a.type = 1 AND a.flag = 3 AND a.createdate = {$today}","right")
- // ->where("a.createdate",$today)
- ->where("ac.admin_id","in",$vipadminIds)
- ->field("a.type,a.flag,a.createdate,a.recharge_money,a.normal_recharge_money,a.normal_recharge_orders,a.vip_recharge_money
- ,a.vip_recharge_orders,ac.json,ac.admin_id")
- ->order("a.recharge_money", "desc")
- // ->order($sort, $order)
- ->limit($offset, $limit)
- ->select();
- foreach ($list as &$v){
- $v['name'] = $v['json']?json_decode($v['json'])->authorizer_info->nick_name:'';
- unset($v['json']);
- }
- $result = array("total" => $total, "rows" => $list);
- }else{
- $result = array("total" => [], "rows" => 0);
- }
- return json($result);
- }
- return $this->view->fetch();
- }
- /**
- * 近30天汇总,不包括今天
- * @return \think\response\Json
- */
- public function orderThirtyStatistics(){
- list($where, $sort, $order, $offset, $limit) = $this->buildparams();
- $vipadminIds = model("VipAdminBind")
- ->where("admin_id_master",$this->auth->id)
- ->where("flag = 1")
- ->column("admin_id_slave");
- $beginDate = date('Ymd', strtotime('-30 days'));
- $endDate = date('Ymd', strtotime('-1 days'));
- $total = $this->model
- ->where("type", 1)
- ->where("flag", 3)
- ->where("createdate", ">=", $beginDate)
- ->where("createdate", "<=", $endDate)
- ->where("admin_id", "in", $vipadminIds)
- ->group("createdate,type,flag")
- ->count();
- $list = $this->model
- ->where("type", 1)
- ->where("flag", 3)
- ->where("createdate", ">=", $beginDate)
- ->where("createdate", "<=", $endDate)
- ->where("admin_id", "in", $vipadminIds)
- ->field([
- "DATE_FORMAT(createdate, '%Y-%m-%d')" => "f_date",
- "SUM(recharge_money)" => "recharge_money_total",
- "SUM(normal_recharge_orders)" => "normal_recharge_orders_total",
- "SUM(normal_recharge_user_suc_count)" => "normal_user_num_total",
- "SUM(vip_recharge_money)" => "vip_recharge_money_total",
- "SUM(vip_recharge_orders)" => "vip_recharge_orders_total",
- "SUM(vip_recharge_user_suc_count)" => "vip_user_num_total",
- "type","flag"
- ])
- ->group("createdate,type,flag")
- ->limit($offset,$limit)
- ->order('createdate', 'desc')
- ->select();
- foreach ($list as $key => &$value){
- $list[$key]['money_total'] = $value['recharge_money_total'];
- $list[$key]['orders_total'] = $value['normal_recharge_orders_total'] + $value['vip_recharge_orders_total'];
- $list[$key]['user_num_total'] = $value['normal_user_num_total'] + $value['vip_user_num_total'];
- }
- $result = array("total" => $total, "rows" => $list);
- return json($result);
- }
- }
|