<?php

namespace App\Console\Commands\Daily;

use App\Models\Daily\DailyPushModel;
use App\Models\Daily\SubscribeModel;
use App\Services\NoticeService\Channels\WeChatTemplateChannel;
use Illuminate\Console\Command;

class NoticeCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'daily:syj';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '时遇记每日推送';

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


    private $public_id;

    private $title = "每日订阅通知-时遇记公众号";

    private $article;

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $this->public_id = "gh_c94c95866ca5";

        $this->article = DailyPushModel::where('date', date("Y-m-d"))->first();

        if (!$this->article) {
            return $this->error("今日没有推送内容");
        }


        $this->kfAccount();
        $this->gzh();
    }

    public function kfAccount()
    {
        $kfService = \WeChat::KfAccount($this->public_id);

        // 客服消息推送
        $fillet_openids = \DB::connection('mysql_datalog')
            ->table("notice_logs")
            ->where('title', $this->title)
            ->where('date', date("Y-m-d"))
            ->pluck('openid');

        $unscrube_openids = SubscribeModel::where('subscribe', 0)->pluck('openid');

        $openids = \DB::table('pocket.kdgx_fans_5')
            ->where('public_id', $this->public_id)
            ->where('interaction_time', '>', strtotime("-2 day"))
            ->where('subscribe', 1)
//            ->where('openid','oBGbrw_176vAKgClwGdS-9WSc-xM')
            ->whereNotIn('openid', $fillet_openids)
            ->whereNotIn('openid', $unscrube_openids)
            ->pluck('openid');

        foreach ($openids as $openid) {
            $content = $this->article->push_content . "\n\n点击<a href='weixin://bizmsgmenu?msgmenucontent=今日文章&msgmenuid=234234'>【这里】</a>获取今日推送";


            try {
                $result = $kfService->send($openid, 'text', [
                    'content' => $content
                ]);
                \DB::connection('mysql_datalog')
                    ->table("notice_logs")
                    ->insert([
                        'openid' => $openid,
                        'title' => $this->title,
                        'uuid' => uuid(),
                        'notice_type' => '公众号客服消息',
                        'content' => $content,
                        'result' => true,
                        'date' => date("Y-m-d"),
                        'created_at' => time(),
                    ]);
                dump($result);
            } catch (\Exception $e) {
                dump($e->getMessage());
            }
        }
    }


// 发送模版消息


    public function gzh()
    {


        $fillet_openids = \DB::connection('mysql_datalog')
            ->table("notice_logs")
            ->where('title', $this->title)
            ->where('date', date("Y-m-d"))
            ->where('result', true)
            ->pluck('openid');

        $subscribe_openids = SubscribeModel::where('subscribe', 1)
//            ->where('openid','oBGbrw_176vAKgClwGdS-9WSc-xM')
            ->whereNotIn('openid', $fillet_openids)
            ->pluck('openid');

        foreach ($subscribe_openids as $openid) {
            $to_uid = $openid;
            $templateId = '-OlcUriUhBniEGGQ5j0EY1E6_QI9H-1Hs2dwIbhRTLo';
            $parameters = [
                'first' => [
                    'value' => $this->article->first . "\n",
                ],
                'keyword1' => [
                    'value' => $this->article->keyword1,
                ],
                'keyword2' => [
                    'value' => $this->article->keyword2,
                ],
                'remark' => [
                    'value' => "\n点此查看今日文章",
                    'color' => '#FF7E98',
                ],
            ];
            $page = "pages/article-redirect/article-redirect?url=" . urlencode($this->article->article_url);
            $weChatTemplate = new WeChatTemplateChannel();
            $weChatTemplate->setAppId('gh_c94c95866ca5')
                ->setTitle($this->title)
                ->toUser($to_uid)
                ->setPage($page)
                ->setTemplateId($templateId)
                ->setParameters($parameters)
                ->send();

            dump($weChatTemplate->getMessage());
        }
    }
}
