<?php
/**
 * @copyright   Copyright (c) 2017 https://www.sapixx.com All rights reserved.
 * @license     Licensed (http://www.apache.org/licenses/LICENSE-2.0).
 * @author      pillar<ltmn@qq.com>
 * 管理首页
 */
namespace app\allwin\controller\Manage;
use app\allwin\controller\Common;
use app\allwin\model\Bank as ModelBank;
use app\allwin\model\BankBill;
use app\allwin\model\BankCash;
use app\allwin\model\Card;
use app\allwin\model\CardUser;
use app\allwin\model\CardUserOrder;
use app\allwin\model\Coupon;
use app\allwin\model\CouponUser;
use app\allwin\model\Fund;
use app\allwin\model\CouponSubsidize;
use app\allwin\model\MchIdQueen;
use app\allwin\model\AllwinStore;
use app\allwin\model\Order;
use app\allwin\model\Vip;
use app\common\model\SystemUser;
use think\facade\Request;
use think\helper\Time;

class Index extends Common{

    public function initialize() {
        parent::initialize();
        $this->assign('pathMaps', [['name'=>'管理首页','url'=>'javascript:;']]);
    }

    /**
     * 应用管理首页
     * @access public
     */
    public function Index(){
        $condition = [];
        $sqlUpdateTime = [];
        $sqlPaidTime = [];
        $cityId = [];
        $time      = Request::param('time/d',0);
        $starttime = Request::param('starttime/s');
        $endtime   = Request::param('endtime/s');
        if($time){
            switch ($time) {
                case 2:
                    list($start, $end) = Time::yesterday();
                    break;
                case 30:
                    list($start, $end) = Time::month();
                    break;
                case 60:
                    list($start, $end) = Time::lastMonth();
                    break;
                default:
                    list($start, $end) = Time::today();
                    break;
            }
            $condition[] = ['create_time','>=',$start];
            $condition[] = ['create_time','<=',$end];
            $sqlUpdateTime[] = ['update_time','>=',$start];
            $sqlUpdateTime[] = ['update_time','<=',$end];
            $sqlPaidTime[] = ['paid_time','>=',$start];
            $sqlPaidTime[] = ['paid_time','<=',$end];
        }else{
            if($starttime){
                $condition[] = ['create_time','>=',strtotime($starttime)];
                $sqlUpdateTime[] = ['update_time','>=',strtotime($starttime)];
                $sqlPaidTime[] = ['paid_time','>=',strtotime($starttime)];
            }
            if($endtime){
                $condition[] = ['create_time','<=',strtotime($endtime)];
                $sqlUpdateTime[] = ['update_time','<=',strtotime($endtime)];
                $sqlPaidTime[] = ['paid_time','<=',strtotime($endtime)];
            }
        }
        $view['usernum']    = SystemUser::where($this->mini_program)->where($condition)->where(['is_lock' => 0])->count();//优惠券数量
        //联盟商家
        $view['store_num']  = AllwinStore::where($this->mini_program)->where($condition)->where(['is_lock' => 0])->count(); //好店数量
        $view['coupon_num'] = Coupon::where($this->mini_program)->where($condition)->where(['is_end' => 0,'is_lock' => 0])->count();//优惠券数量
        $view['vipnum']     = Vip::where($this->mini_program)->where($sqlUpdateTime)->where(['is_lock' => 0,'state' => 1])->count(); //会员数量
        //买单管理
        $view['order_num']    = Order::where($this->mini_program)->where($sqlPaidTime)->where(['state' => 1])->count(); //单数统计
        $view['coupon_price'] = Order::where($this->mini_program)->where($sqlPaidTime)->where(['state' => 1])->sum('coupon_price'); //优惠金额
        $view['store_amount'] = Order::where($this->mini_program)->where($sqlPaidTime)->where(['state' => 1])->sum('store_amount'); //好店应收
        $view['order_amount'] = Order::where($this->mini_program)->where($sqlPaidTime)->where(['state' => 1])->sum('order_amount'); //实际付款
        //已领优惠券
        $view['coupon_size']      = CouponUser ::where($this->mini_program)->where($condition)->sum("size");//发行价值
        $view['coupon_user_size'] = CouponUser ::where($this->mini_program)->where($condition)->sum("user_size");//剩余价值
        $view['coupon_user']      = $view['coupon_size']-$view['coupon_user_size']; //已用价值
        $view['coupon_count']     = CouponUser::where($this->mini_program)->where($condition)->where(['is_end' => 0])->count();//优惠券数量
        $view['coupon_end_count'] = CouponUser::where($this->mini_program)->where($condition)->where(['is_end' => 1])->count();//失效数量
        //会员统计
        $view['store_vipnum']    = Vip::where($this->mini_program)->where($sqlUpdateTime)->where('store_id','>',0)->count();//好店会员数量
        $view['platform_vipnum'] = Vip::where($this->mini_program)->where($sqlUpdateTime)->where('store_id','=',0)->count();//平台会员数量
        $view['lock_vipnum']     = Vip::where($this->mini_program)->where($sqlUpdateTime)->where(['is_lock' => 1])->count();  //支付金额
        $view['vipamount']       = Vip::where($this->mini_program)->where($sqlUpdateTime)->sum('amount');     //支付金额
        //储值活动
        $view['card_num']         = Card::where($this->mini_program)->where($condition)->count();
        $view['card_amount']      = Card::where($this->mini_program)->where($condition)->sum('price');
        $view['carduser_num']     = CardUser::where($this->mini_program)->where($condition)->count();
        $view['carduser_amount']  = CardUserOrder::where($this->mini_program)->where($condition)->where(['state' => 1])->sum('amount');
        //分帐队列
        $view['amount_quree']   = MchIdQueen::where($this->mini_program)->where($sqlUpdateTime)->where(['is_finish' => 0])->sum('amount');//分账队列
        $view['amount_success'] = MchIdQueen::where($this->mini_program)->where($sqlUpdateTime)->where(['is_finish' => 2])->sum('amount');//分账成功
        $view['amount_fail']    = MchIdQueen::where($this->mini_program)->where($sqlUpdateTime)->where(['is_finish' => 1])->sum('amount');//分账失败
        $view['quree_num']      = MchIdQueen::where($this->mini_program)->where($sqlUpdateTime)->count();//分账失败
        //帐户管理
        $view['receipts'] = BankBill::where($this->mini_program)->where($sqlUpdateTime)->where('money','>',0)->sum('money');//流水入账
        $view['expenses'] = BankBill::where($this->mini_program)->where($sqlUpdateTime)->where('money','<',0)->sum('money');//流水出账
        $view['points']   = ModelBank::bank($this->member_miniapp_id)->sum('shop_money');//帐号积分
        $view['money']    = ModelBank::bank($this->member_miniapp_id)->sum('money');//帐号余额
        //提现管理
        $view['now_money']  = BankCash::where($this->mini_program)->where($sqlUpdateTime)->where(['state' => 0])->sum('money');//申请中
        $view['pass_money'] = BankCash::where($this->mini_program)->where($sqlUpdateTime)->where(['state' => 1])->sum('realmoney');//已通过
        $view['back_money'] = BankCash::where($this->mini_program)->where($sqlUpdateTime)->where(['state' => -1])->sum('money');//未通过
        $view['cash_num']   = BankCash::where($this->mini_program)->where($sqlUpdateTime)->count();//未通过
        //采购基金
        $fund = Fund::where($this->mini_program)->find();
        $counts['balance']         = empty($fund) ? 0 : $fund->balance;//采购基金
        $counts['subsidize']       = empty($fund) ? 0 : $fund->subsidize;//已补贴￥
        $counts['subsidize_num']   = empty($fund) ? 0 : $fund->subsidize_num;//已补贴(单)
        $view['fund']              = $counts;
        $view['fund_coupon_num']   = CouponSubsidize::where($this->mini_program)->count();
        $view['time']       = $time;
        $view['starttime']  = $starttime;
        $view['endtime']    = $endtime;
        return view('/index')->assign($view);
    }    
}