<?php

namespace App\Console\Commands\Notices;

use App\Models\Fpdx\PairModel;
use App\Models\User\UserModel;
use App\Models\PartnerModel;
use App\Models\User\Openid;
use Illuminate\Console\Command;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '通知数据';

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //
        $this->vip();
        $this->fpdx();
        $this->visitor();
        $this->recommendCancel();
        $this->phoneNotCard();
        $this->oncePair();
    }

    public function vip()
    {

        dump("老用户召回-激活会员通知");
        $uids = \DB::connection('mysql_datalog')
            ->table("notice_logs")
            ->where('title', '老用户召回-激活会员通知')
            ->distinct()
            ->pluck('uid');

        $unsubscribe_count = Openid::whereIn('uid', $uids)
            ->where('public_id', 'gh_b598cb7474d8')
            ->where('subscribe', 0)
            ->count();

        $vip_count = UserModel::where('be_vip_at', '>', 0)->whereIn('uid', $uids)->count();

        dump("取关人数{$unsubscribe_count}");
        dump("会员人数{$vip_count}");
    }


    public function fpdx()
    {
        dump("老用户召回-对象包分配通知");
        $uids = \DB::connection('mysql_datalog')
            ->table("notice_logs")
            ->where('title', '老用户召回-对象包分配通知')
            ->distinct()
            ->pluck('uid');

        $unsubscribe_count = Openid::whereIn('uid', $uids)
            ->where('public_id', 'gh_b598cb7474d8')
            ->where('subscribe', 0)
            ->count();

        $pair_count = PairModel::where('uid', $uids)->distinct('uid')->count();

        dump("取关人数{$unsubscribe_count}");
        dump("匹配人数{$pair_count}");
    }


    public function visitor()
    {
        dump("老用户召回-访客通知");
        $uids = \DB::connection('mysql_datalog')
            ->table("notice_logs")
            ->where('title', '老用户召回-访客通知')
            ->distinct()
            ->pluck('uid');

        $unsubscribe_count = Openid::whereIn('uid', $uids)
            ->where('public_id', 'gh_b598cb7474d8')
            ->where('subscribe', 0)
            ->count();

        dump("取关人数{$unsubscribe_count}");
    }


    public function recommendCancel()
    {
        dump("交友卡片-卡片取消推荐通知");
        $uids = \DB::connection('mysql_datalog')
            ->table("notice_logs")
            ->where('title', '交友卡片-卡片取消推荐通知')
            ->where('result', 1)
            ->distinct()
            ->pluck('uid');

        $unsubscribe_count = Openid::whereIn('uid', $uids)
            ->where('public_id', 'gh_b598cb7474d8')
            ->where('subscribe', 0)
            ->count();

        $number = $uids->count();
        $card = PartnerModel::where('upload_at', '>', 1560873600)->whereIn('uid', $uids)->count();

        dump("发送人数{$number}");
        dump("取关人数{$unsubscribe_count}");
        dump("更换照片人数{$card}");
    }


    public function phoneNotCard()
    {
        dump("老用户召回-有手机号但无卡片者通知");
        $uids = \DB::connection('mysql_datalog')
            ->table("notice_logs")
            ->where('title', '老用户召回-有手机号但无卡片者通知')
            ->distinct()
            ->pluck('uid');


        $unsubscribe_count = Openid::whereIn('uid', $uids)
            ->where('public_id', 'gh_b598cb7474d8')
            ->where('subscribe', 0)
            ->count();

        $number = $uids->count();
        $card_count = UserModel::where('partner_id', '>', 0)->whereIn('uid', $uids)->count();

        dump("发送人数{$number}");
        dump("取关人数{$unsubscribe_count}");
        dump("交友卡片人数{$card_count}");
    }


    public function oncePair()
    {
        dump("老用户召回-仅参与过一次活动的用户");
        $uids = \DB::connection('mysql_datalog')
            ->table("notice_logs")
            ->where('title', '老用户召回-仅参与过一次活动的用户')
            ->distinct()
            ->pluck('uid');

        $unsubscribe_count = Openid::whereIn('uid', $uids)
            ->where('public_id', 'gh_b598cb7474d8')
            ->where('subscribe', 0)
            ->count();

        $pair_count = PairModel::where('create_time', '>', 1560787200)
            ->whereIn('uid', $uids)
            ->distinct()
            ->count();

        dump("取关人数{$unsubscribe_count}");
        dump("报名人数{$pair_count}");
    }
}
