<?php
namespace app\command;
// +----------------------------------------------------------------------
// | [ WE CAN DO IT MORE SIMPLE  ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2020 rights reserved.
// +----------------------------------------------------------------------
// | Author: TABLE ME
// +----------------------------------------------------------------------
// | Date: 2020-09-08 16:19
// +----------------------------------------------------------------------
use app\model\system\Product;
use app\model\system\Site;
use library\utils\Qiniu;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\facade\Db;

/**
 * 生成业绩报表
 * @author huangjianfeng
 */
class Total extends Command
{
    protected function configure()
    {
        $this
            ->setName('Total')
            ->setDescription('每日统计分站业绩报表！')
            ->addArgument('action', null, "");
    }


    protected function execute(Input $input, Output $output){
        $time = strtotime(date('Y-m-d',strtotime('-1 day')));
        $this->expData($time);
    }


    private function expData($time){
        $siteData = (new Site)->select()->toArray();
        foreach ($siteData as $v) {
            $this->expTotal($v,$time);
        }
    }

    /**
     * 订单统计
     * @param $data
     */
    private function expTotal($data,$time) {
        $btime = $time;
        $etime = $time + 86400;
        $d = [];
        $d['sassid'] = $data['sassid'];
        //会员量
        $d['user_count'] = Db::name("member")
                                ->where('sassid',$data['sassid'])
                                ->whereRaw('regtime >= :time1 and regtime < :time2',['time1'=>$btime,'time2'=>$etime])
                                ->count();
        //主订单
        $d['order_count'] = Db::name("order")
                                ->where('sassid',$data['sassid'])
                                ->where('status','>',0)
                                ->whereRaw('time >= :time1 and time < :time2',['time1'=>$btime,'time2'=>$etime])
                                ->count();
        //子订单
        $d['sub_order_count'] = Db::name("order_info")
            ->where('sassid',$data['sassid'])
            ->where('status','>',0)
            ->whereRaw('time >= :time1 and time < :time2',['time1'=>$btime,'time2'=>$etime])
            ->count();
        //订单金额
        $d['order_money'] = Db::name("order")
            ->where('sassid',$data['sassid'])
            ->where('status','>',0)
            ->whereRaw('time >= :time1 and time < :time2',['time1'=>$btime,'time2'=>$etime])
            ->sum('all_price');
        $d['order_money'] = empty($d['order_money']) ? 0 : $d['order_money'];
        //充值金额
        $d['recharge_money'] = Db::name("recharge")
            ->where('sassid',$data['sassid'])
            ->where('status',1)
            ->whereRaw('time >= :time1 and time < :time2',['time1'=>$btime,'time2'=>$etime])
            ->sum('v');
        $d['recharge_money'] = empty($d['recharge_money']) ?  0 : $d['recharge_money'];
        //佣金金额
        $d['commission_money'] = Db::name("site_detail")
                                        ->where('sassid',$data['sassid'])
                                        ->where('type',1)
                                        ->whereRaw('time >= :time1 and time < :time2',['time1'=>$btime,'time2'=>$etime])
                                        ->sum('v');
        $count = Db::name("site_total")
                    ->where('time',$time)
                    ->where('sassid',$data['sassid'])
                    ->count();
        if($count > 0) {
            Db::name("site_total")
                ->where('time',$time)
                ->where('sassid',$data['sassid'])
                ->update($d);
        } else {
            $d['time'] = $time;
            $d['sassid'] = $data['sassid'];

            Db::name("site_total")->insert($d);
        }
        echo  "[" . date('Y-m-d',$time)."]".$data['name']." 会员量" . $d['user_count'] . ",订单量:" . $d['order_count'] . ",子订单量:" .$d['sub_order_count']
            .",订单总额:" . $d['order_money'] . ",充值金额:" .$d['recharge_money'].
            ",佣金金额:" . $d['commission_money'].PHP_EOL;
    }

}