<?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\Cache;
use think\facade\Db;

/**
 * 订单发货提醒
 * @author huangjianfeng
 */
class WeixinPushOrder extends Command
{
    protected function configure()
    {
        $this
            ->setName('WeixinPushOrder')
            ->setDescription('微信推送订单push数据')
            ->addArgument('action', null, "");
    }


    protected function execute(Input $input, Output $output)
    {
        $weixin                 = new weixinjs();
        $time = strtotime('-5 minute');
        $orderPush = Db::name("weixin_order_push")
                        ->where('push_time',"<=",$time)
                        ->where('status',0)
                        ->select();
        foreach ($orderPush as $v) {
            //推送订单
            $wxUser = Db::name("weixin_push_user")
                            ->where('uid',$v['uid'])
                            ->find();
            if(!empty($wxUser)) {
                $dataAr = unserialize($v['data']);
                $vStr = "\n\t";
                $oId = [];
                foreach ($dataAr as $kk=>$vv) {
                    $order = Db::name('order')->where('id',$kk)->find();
                    $Fcount = Db::name('orderInfo')
                                ->where('status',2)
                                ->where('o_id',$kk)
                                ->count();
                    $count = Db::name('orderInfo')->where('o_id',$kk)->count();
                    $oId[] = $kk;
                    $vStr   .= $order['order_id'].'  共:' . $count . "  已发:" . $Fcount . "\n\t";
                }
                $templateAr             = [
                    'first' => [
                        'value' => '您有新的订单打单发货',
                        "color" => "#000000"
                    ],
                    'keyword1' => [
                        'value' => $vStr,
                        "color" => "#409EFF"
                    ],
                    'keyword2' => [
                        'value' => '点击查看',
                        "color" => "#409EFF"
                    ],
                    'remark'   => [
                        'value' => "点击查看详情"
                    ],
                ];
                $token = base64_encode(crypto_encrypt(serialize(['oids'=>$oId]), 'weixin_push'));
                $url   = config('app')['API_URL'] . '/api/wexinPush/order?token=' . $token;
                $a     = $weixin->template('FTd4Nf_ov3N01AlONhO2h8TWNDS5bc7phArcZJn_vC0', $wxUser['openid'], $templateAr, $url);
                echo  $wxUser['nickname'] . '订单推送,' . $vStr . PHP_EOL;
                Db::name("weixin_order_push")->where('id',$v['id'])->update(['status'=>1,'send_time'=>time()]);
            }



        }






    }


}
