<?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\api\SiteDetail;
use app\model\system\Member;
use app\model\system\OrderInfo;
use app\model\system\Product;
use app\model\system\Recharge;
use app\model\system\Site;
use library\lib\weixinjs;
use library\utils\Qiniu;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\facade\Db;

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


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


    /**
     * [总后台]推送数据
     * @param $time
     */
    private function expSystemData($time) {
        $data = Db::name("weixin_push_user")->where('type','system')->select();
        $date = date('Y-m-d',$time);
        //今日订单量
        $post['today_order_count'] = (new \app\model\system\Order())
            ->where('status','>',0)
            ->whereDay('pay_time',$date)
            ->count();
        //其他数据
        $post['today_t_order_count'] = (new OrderInfo)
            ->where('status','>',0)
            ->whereDay('time',$date)
            ->count();

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

        $post['today_user'] = (new Member)->whereDay('regtime',$date)->count();
        $post['today_recharge'] = (new Recharge)->whereDay('time',$date)->where('status',1)->sum('v');
        $str = "昨日订单量：".$post['today_order_count'] .'单（其中子订单' . $post['today_t_order_count'] . '单）,';
        $str .= "昨日销售额：".(empty($post['today_money']) ? '0.00' : $post['today_money']).'元，新增加会员:'. $post['today_user'] .'人,';
        $str .= '充值金额：'.(empty($post['today_recharge']) ? '0.00' : $post['today_recharge'])  .'元';
        $templateAr = [
            'first' => [
                'value' => '昨日业绩汇总',
                "color" => "#000000"
            ],

            'keyword1' => [
                'value' => $date,
                "color" => "#409EFF"
            ],
            'keyword2' => [
                'value' => $str,
                "color" => "#409EFF"
            ],

            'keyword3' => [
                'value' => date('Y-m-d H:i:s'),
                "color" => "#409EFF"
            ],
            'remark' => [
                'value' => "今天会比昨日更加努力，加油！！！😁😁😁➡️👏👏👏👏\n\t 点击下方菜单，查看详情数据↓↓↓",
                "color" => "#f80505"
            ],
        ];
        $weixin = new weixinjs();
        foreach ($data as $v) {
            $token = base64_encode(crypto_encrypt(serialize(['time'=>$time]),'weixin_push'));
            $url = config('app')['API_URL'].'/api/wexinPush/systemTotal?token=' .$token;
            $a = $weixin->template('RKHWnRvPWKPBNk0fYuucOGAUu730HW2s0YFXsW_Z4gY',$v['openid'],$templateAr,$url);
            //var_dump($a);
        }

        echo $str.PHP_EOL;
    }


    /**
     * [分站]推送数据
     * @param $time
     */
    private function expAdminData($time) {
        $weixin                 = new weixinjs();
        $data = Db::name("weixin_push_user")->where('type','admin')->where('uid',0)->select();
        $date = date('Y-m-d',$time);
        foreach ($data as $v) {
            $siteData = (new Site)->where('sassid',$v['sassid'])->find();
            //今日订单量
            $post['today_order_count'] = (new \app\model\system\Order())
                ->where('status', '>', 0)
                ->where('sassid',$v['sassid'])
                ->whereDay('pay_time', $date)
                ->count();
            //其他数据
            $post['today_t_order_count'] = (new OrderInfo)
                ->where('status', '>', 0)
                ->whereDay('time', $date)
                ->where('sassid',$v['sassid'])
                ->count();

            //今日销售额
            $post['today_money'] = (new \app\model\system\Order())
                ->where('status', '>', 0)
                ->whereDay('pay_time', $date)
                ->where('sassid',$v['sassid'])
                ->sum('all_price');
            //
            $post['today_user']     = (new Member)->whereDay('regtime', $date)->where('sassid',$v['sassid'])->count();
            $post['today_recharge'] = (new Recharge)->whereDay('time', $date)->where('status', 1)->where('sassid',$v['sassid'])->sum('v');
            $post['today_commission'] = (new SiteDetail)->where('sassid',$v['sassid'])->where('type',1)->whereDay('time',$date)->sum('v');

            $str                    = "昨日订单量：" . $post['today_order_count'] . '单（其中子订单' . $post['today_t_order_count'] . '单）,';
            $str                    .= "昨日销售额：" . (empty($post['today_money']) ? '0.00' : $post['today_money']) . '元，新增加会员:' . $post['today_user'] . '人,';
            $str                    .= '充值金额：' . (empty($post['today_recharge']) ? '0.00' : $post['today_recharge']) . '元';
            $str                    .= '，获取佣金：' . (empty($post['today_commission']) ? '0.00' : $post['today_commission']) . '元';
            $templateAr             = [
                'first' => [
                    'value' => $siteData['name'].'-昨日业绩汇总',
                    "color" => "#000000"
                ],

                'keyword1' => [
                    'value' => $date,
                    "color" => "#409EFF"
                ],
                'keyword2' => [
                    'value' => $str,
                    "color" => "#409EFF"
                ],

                'keyword3' => [
                    'value' => date('Y-m-d H:i:s'),
                    "color" => "#409EFF"
                ],
                'remark'   => [
                    'value' => "今天会比昨日更加努力，加油！！！😁😁😁➡️👏👏👏👏\n\t 点击下方菜单，查看详情数据↓↓↓",
                    "color" => "#f80505"
                ],
            ];
            $token = base64_encode(crypto_encrypt(serialize(['time' => $time,'sassid'=>$v['sassid']]), 'weixin_push'));
            $url   = config('app')['API_URL'] . '/api/wexinPush/adminTotal?token=' . $token;
            $a     = $weixin->template('RKHWnRvPWKPBNk0fYuucOGAUu730HW2s0YFXsW_Z4gY', $v['openid'], $templateAr, $url);
            echo  $siteData['name'] . '['.$v['nickname'].']' .$str . PHP_EOL;
        }
    }

}
