<?php

namespace App\Console\Commands\pair;

use App\Models\Fpdx\ActivityModel;
use Illuminate\Console\Command;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '匹配公布';

    private $stage_id;

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $this->stage_id = ActivityModel::where('signend_time', '<=', time())
            ->where('close_time', '>=', time())->value('stage_id');
        \DB::table('kdgx_partner_charge_pair')->where('stage_id', $this->stage_id)
            ->whereNotNull('assoc_id')->whereBetween('state', [100, 199])->increment('state', 300);
        \DB::table('kdgx_partner_charge_pair')->where('stage_id', $this->stage_id)
            ->whereNull('assoc_id')->whereBetween('state', [100, 199])->increment('state', 200);
        \DB::table('kdgx_partner_charge_pair')->where('stage_id', $this->stage_id)
            ->whereNull('assoc_id')->whereBetween('state', [0, 99])->increment('state', 900);

        $pairs = \DB::table('kdgx_partner_charge_pair')
            ->where('stage_id', $this->stage_id)->whereNull('assoc_id')->pluck('id');
        \DB::table('kdgx_fpdx_deed')->where('type', 1)->whereIn('ack', $pairs)->update(['state' => -1]);

        $pairs = \DB::table('kdgx_partner_charge_pair')
            ->where('stage_id', $this->stage_id)->whereNotNull('assoc_id')->pluck('id');
        \DB::table('kdgx_fpdx_deed')->where('type', 1)->where('state', 1)->whereIn('ack', $pairs)->update([
            'state' => 2,
        ]);
    }
}
