<?php
// +----------------------------------------------------------------------
// | [ WE CAN DO IT MORE SIMPLE  ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2020 rights reserved.
// +----------------------------------------------------------------------
// | Author: TABLE ME
// +----------------------------------------------------------------------
// | Date: 2020-11-11 16:54
// +----------------------------------------------------------------------

namespace app\api\controller\v1;


use app\BaseViewController;
use app\model\api\Order;
use app\model\api\Site;
use app\model\api\SiteDetail;
use app\model\system\Member;
use app\model\system\OrderInfo;
use app\model\system\Recharge;
use app\model\system\SiteTotal;
use app\Request;

class WexinPush extends BaseViewController
{

    /**
     * 总系统ttotal
     */
    public function systemTotal(Request $request){
        $token = $request->get('token');
        if(empty($token)) {
           return 'error';
        }
        $token = str_replace(" ","+",$token);
        $str = crypto_decrypt(base64_decode($token),'weixin_push');
        if(empty($str)) {
           return '';
        }
        $tAr = unserialize($str);
        //$tAr['time'] = strtotime('2020-11-10');
        $time = $tAr['time'];
        $date = date('Y-m-d',$time);
        //显示订单量
        $post['today_order_count'] = (new \app\model\system\Order())
            ->where('status','>',0)
            ->whereDay('pay_time',$date)
            ->count();
        //订单总量
        $post['all_order_count'] = (new \app\model\system\Order())
            ->where('status','>',0)
            ->count();

        //今日子订单量
        $post['today_t_order_count'] = (new OrderInfo)
            ->where('status','>',0)
            ->whereDay('time',$date)
            ->count();
        //今日订单总量
        $post['all_t_order_count'] = (new OrderInfo)->where('status','>',0)->count();


        //今日销售额
        $post['today_money'] = (new \app\model\system\Order())
            ->where('status','>',0)
            ->whereDay('pay_time',$date)
            ->sum('all_price');
        //总销售额
        $post['all_money'] = (new \app\model\system\Order())
            ->where('status','>',0)
            ->sum('all_price');

        //新增会员
        $post['today_user'] = (new Member)->whereDay('regtime',$date)->count();
        $post['all_user'] = (new Member)->count();

        //充值记录
        $post['today_recharge'] = (new Recharge)->whereDay('time',$date)->where('status',1)->sum('v');
        $post['recharge'] = (new Recharge)->where('status',1)->sum('v');


        //前面50个平台业绩
        $siteData = (new SiteTotal)->field("st.*,(SELECT name from table_site where st.sassid=sassid) as site_name")->alias("st")->where('time',$time)->limit(0,50)->order("order_money desc")->select();

       $this->assign('date',$date);
       $this->assign('miniDate',date('m月d日',$time));
       $this->assign('data',$post);
       $this->assign('siteData',$siteData);
       echo $this->display();
    }

    /**
     * 分站total
     */
    public function adminTotal(Request $request){
        $token = $request->get('token');
        if(empty($token)) {
              return 'error';
        }
        $token = str_replace(" ","+",$token);
        $str = crypto_decrypt(base64_decode($token),'weixin_push');
        if(empty($str)) {
              return '';
        }
        $tAr = unserialize($str);
       // $tAr['time'] = strtotime('2020-11-10');
        //$tAr['sassid'] = 3;
        $time = $tAr['time'];
        $date = date('Y-m-d',$time);
        $siteData = (new Site)->where('sassid',$tAr['sassid'])->find();

        //显示订单量
        $post['today_order_count'] = (new \app\model\system\Order())
            ->where('status','>',0)
            ->where('sassid',$tAr['sassid'])
            ->whereDay('pay_time',$date)
            ->count();
        //订单总量
        $post['all_order_count'] = (new \app\model\system\Order())
            ->where('status','>',0)
            ->where('sassid',$tAr['sassid'])
            ->count();

        //今日子订单量
        $post['today_t_order_count'] = (new OrderInfo)
            ->where('status','>',0)
            ->where('sassid',$tAr['sassid'])
            ->whereDay('time',$date)
            ->count();
        //今日订单总量
        $post['all_t_order_count'] = (new OrderInfo)->where('sassid',$tAr['sassid'])->where('status','>',0)->count();


        //今日销售额
        $post['today_money'] = (new \app\model\system\Order())
            ->where('status','>',0)
            ->where('sassid',$tAr['sassid'])
            ->whereDay('pay_time',$date)
            ->sum('all_price');
        //总销售额
        $post['all_money'] = (new \app\model\system\Order())
            ->where('status','>',0)
            ->where('sassid',$tAr['sassid'])
            ->sum('all_price');

        //新增会员
        $post['today_user'] = (new Member)->where('sassid',$tAr['sassid'])->whereDay('regtime',$date)->count();
        $post['all_user'] = (new Member)->where('sassid',$tAr['sassid'])->count();

        //充值记录
        $post['today_recharge'] = (new Recharge)->where('sassid',$tAr['sassid'])->whereDay('time',$date)->where('status',1)->sum('v');
        $post['recharge'] = (new Recharge)->where('sassid',$tAr['sassid'])->where('status',1)->sum('v');

        //总佣金
        $post['commission_money'] = (new Site)->where('sassid',$tAr['sassid'])->sum('money');
        $post['today_commission'] = (new SiteDetail)->where('sassid',$tAr['sassid'])->where('type',1)->whereDay('time',$date)->sum('v');

        //前面30天平台业绩
        $totalData = (new SiteTotal)
                        ->alias("st")
                        ->where('sassid',$tAr['sassid'])
                        ->limit(0,30)
                        ->order("id desc")
                        ->select();
        $this->assign('siteData',$siteData);
        $this->assign('date',$date);
        $this->assign('miniDate',date('m月d日',$time));
        $this->assign('data',$post);
        $this->assign('totalData',$totalData);
        echo $this->display();
    }


    /**
     * 订单数据
     * @param Request $request
     */
    public function order(Request $request) {
        $token = $request->get('token');
        if(empty($token)) {
            return 'error';
        }
        $token = str_replace(" ","+",$token);
        $str = crypto_decrypt(base64_decode($token),'weixin_push');
        if(empty($str)) {
            return '';
        }
        $tAr = unserialize($str);
        $ids = $tAr['oids'];
        $oData = (new Order)->whereIn('id',$ids)->select();
        foreach ($oData as $k=>$v) {
            $oData[$k]['data'] = (new OrderInfo)->where('o_id',$v['id'])->select();
        }

        $this->assign('data',$oData);
       echo $this->display();
    }


}
