<?php

namespace App\Console\Commands\ActionTrigger\BatchWechatMaterial;
use App\Http\Controllers\WechatController;
use Illuminate\Console\Command;
use DB;

/**
 * 删除微信素材
 *
 */
class testWechatMaterialMsgSend extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'testWechatMaterialMsgSend';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'testWechatMaterialMsgSend';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        \Log::info('SendBatchWechatMaterial start');
        $param = [
            'appid'=>'wxdbc486f1b4f6a8c3',
            'media_id'=>'XYhErlq3w-hlmwqOB4O5So616GrRvIOD8MoBjI8IlSY',
            'openids'=>[
                '0'=>'oAcqg1LRHNKN2jaEkJ5v56HOwPEQ',
                '1'=>'oq6ID0m4KrnpHwwZUh9BBmFsW_18',
            ],
            'task_id'=>2,
        ];
        $this->start($param);
        \Log::info('SendBatchWechatMaterial end');

    }

    public function start($param)
    {
        \Log::info('$param');\Log::info($param);

        //检查参数
        if(!($param['appid']&&$param['media_id']&&$param['openids']&&$param['task_id'])){
            \Log::info('params_empty');
            return;
        }
        //服务号高级发送接口openid限定数量为2-10000之间
        if(!is_array($param['openids'])||count($param['openids'])>10000||count($param['openids'])<2){
            \Log::info('SendBatchWechatMaterial:over_open_num_limt');
            return;
        }

        //调用服务号高级发送接口
        try{
            $WechatController = new WechatController($param['appid']);
            $res = $WechatController->app->broadcast->sendNews($param['media_id'],$param['openids']);
            \Log::info($res);
            //发送失败
            if($res['errcode']!=0){
                \Log::info("SendBatchWechatMateria {$param['task_id']} errcode:{$res['errcode']} errmsg:{{$res['errcode']}}");
                return;
            }
            //保存msg_id和msg_data_id
            DB::connection('api_mysql')
                ->table('wechat_material_send_msgs')
                ->where('id',$param['task_id'])
                ->update(['wechat_msg_id'=>$res['msg_id'],'wechat_msg_data_id'=>$res['msg_data_id']]);
            //发送成功更新发送人数
            DB::connection('api_mysql')
                ->table('wechat_material_send_msgs')
                ->where('id',$param['task_id'])
                ->increment('send_user_num',count($param['openids']));
            //若为最后一条消息则更新发送状态为发送完成
            if($param['type']=='last_task'){
                DB::connection('api_mysql')
                    ->table('wechat_material_send_msgs')
                    ->where('id',$param['task_id'])
                    ->update(['status'=>'has_send','updated_at'=>date('Y-m-d H:i:s')]);
            }
        }catch(\Exception $e){
            \Log::error($e->getMessage());
        }
        return;
    }
}
