<?php

namespace App\Console\Commands\Upgrade;

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

class V20181205Command extends Command
{
    protected $signature = "upgrade:v20181205";

    protected $description = "版本升级脚本|2018-12-05号更新版本升级\n
                            更新内容：\n
                                1. 增加活动契约系统\n 
                                2. 72小时活动报名加入契约\n 
                                2. 移除首页feed流解锁功能，添加邀请交换微信功能并加入契约\n 
                            脚本任务：\n 
                                1. 将未使用的解锁卡1:7.5转换为红花\n
                                2. 已报名活动未创建契约的人创建默认契约";

    public function handle()
    {
        $this->changeTicket2Flower();
        $this->addPairDeed();
    }

    public function changeTicket2Flower()
    {
        dump("「解锁卡转换成小花」开始");
        $datas = TicketModel::select(\DB::raw('uid, count(*) as c'))
            ->where([['type', 5], ['state', 0], ['validity_time', '>', time()]])
            ->groupBy('uid')->take(5000)->get();
        foreach ($datas as $data) {
            dump($data);
            \DB::beginTransaction();
            try {
                \DB::table('kdgx_partner_charge_user')->where('uid', $data->uid)
                    ->increment('red_flower', $data->c * 7.5);
                \DB::table('kdgx_partner_charge_pay_logs')->insert([
                    'uid' => $data->uid,
                    'type' => 5,
                    'red_flower' => $data->c * 7.5,
                    'remark' => '更新补偿',
                    'create_at' => time()
                ]);
                TicketModel::where([
                    ['type', 5],
                    ['state', 0],
                    ['validity_time', '>', time()],
                    ['uid', $data->uid]
                ])->update(['validity_time' => mktime(12, 0, 0)]);
                \DB::commit();
            } catch (\Exception $e) {
                dump($e);
                \DB::rollBack();
            }
        }
        dump("「解锁卡转换成小花」结束");
    }


    /**
     * @deprecated
     */
    public function addPairDeed()
    {
        dump("「补全报名契约」开始");
        $ings = ActivityModel::getActivitys();
        $ings['last'] = 67;
        dump("当前处理的期数：「{$ings['last']}」");
        $activity = ActivityModel::findOrFail($ings['last']);
        $acks = \DB::table('kdgx_fpdx_deed')->where([
            ['type', 1],
            ['way', 0],
            ['publish_at', $activity->open_time]
        ])->pluck('way_id');
        $datas = \DB::table('kdgx_partner_charge_pair')->where('stage_id', $ings['last'])
            ->whereNotIn('id', $acks)->get();
        foreach ($datas as $data) {
            dump($data->id);
            \DB::beginTransaction();
            try {
                if (
                    \DB::table('kdgx_fpdx_deed')->where([
                    ['type', 1],
                    ['ack', $data->id]
                    ])->exists()
                ) {
                    continue;
                } else {
                    \DB::table('kdgx_fpdx_deed')->insertGetId([
                        'created_at' => time(),
                        'updated_at' => time(),
                        'uid' => $data->uid,
                        'sign' => '签名',
                        'type' => 1,
                        'ack' => $data->id,
                        'state' => 1,
                        'sign_at' => $data->create_time,
                        'publish_at' => $activity->open_time,
                        'confirm_at' => $activity->rematched_at,
                        'end_at' => $activity->close_time,
                        'way' => 0,
                        'way_id' => $data->id
                    ]);
                }
                \DB::commit();
            } catch (\Exception $e) {
                dump($e);
                \DB::rollBack();
            }
        }
        dump("「补全报名契约」结束");
    }
}
