<?php

namespace App\Console\Commands;

use App\Models\Docter;
use App\Models\DocterOrganization;
use App\Models\OrganizationVaccine;
use EasyWeChat\Factory;
use Illuminate\Console\Command;

class ScheduleNotice extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'scheduleNotice';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '排班提醒 每周六18:00 提醒一次';

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //门诊医生排班提醒
        $docters = Docter::where(['type'=>1])->get();
        $nowDay = intval(date('N',time()));
        $endDay = (7-$nowDay)*86400 +time();
        $time = date('Y-m-d',strtotime('this week')).'至'.date('Y-m-d',$endDay);
        foreach ($docters as $d){
            if(empty($d->openid)) continue;
            $teams = get_docter_organization($d->id);
            //$openid = 'oflME5eixHMij2TIVyy52WbfaQvA';
            $data = [$d->openid,'',$d->name,$time, $teams];
            $ret = admin_send_docter_message('schedule_notice',$data);
            dd($ret);
        }

    }

    public function test()
    {
        $openid = 'oflME5eixHMij2TIVyy52WbfaQvA';
        $data =  [
            'touser' => $openid,
            'mp_template_msg' => [
                'appid' => env('OFFICE_APPID'),
                'template_id' => 'Bd12nbwEtcrsN2IsVA2URiPH1qVGk3uyCemLyxBa-4U',
                'url' => '',
                'miniprogram' => [
                    'appid' => env('WECHAT_APPID', 'wx6131f74e623bf6bf'),
                    'page' => '',
                ],
                'data' => [
                    'first' => [
                        'value' => '尊敬的大哥，你下周的排班已经完成，具体如下！',
                    ]
                    ,'keyword1' => [
                        'value' => '2021-01-18 - 2021-01-24',
                    ],
                    'keyword2' => [
                        'value' => '测试团队',
                    ],
                    'keyword3' => [
                        'value' => '总后台',
                    ],
                    'remark' => [
                        'value' => '点击进入小程序查看详情，有疑问请联系客服',
                    ],
                ]

            ],
        ];

        $app = Factory::miniProgram(config('config.docter_small_program'));
        //先发送模板消息

        $ret = $app->uniform_message->send($data);
    }


}
