<?php

namespace App\Console\Commands\Temp;

use Log;
use Illuminate\Console\Command;
use DB;

class Siorder extends Command
{
    /**
     * 执行命令   php artisan temp:siorder
     *
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'temp:siorder';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '书币消耗';

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        /**
         * CREATE TABLE `si_orders` (
        `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
        `uid` int(11) DEFAULT NULL,
        `first_recharge_time` datetime DEFAULT NULL,
        `sec_recharge_time` datetime DEFAULT NULL,
        `price` decimal(11,2) DEFAULT NULL,
        `site_id` int(11) DEFAULT NULL,
        `sub_time` datetime DEFAULT NULL,
        `balance` decimal(11,2) DEFAULT NULL,
        `x` int(11) DEFAULT NULL,
        `xx` int(11) DEFAULT NULL,
        `xxx` int(11) DEFAULT NULL,
        `xxxxxxx` int(11) DEFAULT NULL,
        `site_name` varchar(255) DEFAULT NULL,
        PRIMARY KEY (`id`)
        ) ENGINE=InnoDB  DEFAULT CHARSET=utf8;
         *
         * select distinct uid,created_at,price,distribution_channel_id from orders o where status ='PAID' and created_at > '2018-04-27' and created_at < '2018-05-02' and not exists(select id from orders where created_at < '2018-04-27' and status ='PAID' and uid = o.uid LIMIT 1)

        update si_orders,orders set si_orders.sec_recharge_time = orders.created_at where si_orders.uid = orders.uid and orders.created_at > si_orders.first_recharge_time and status ='PAID'

        update si_orders,force_subscribe_users set si_orders.sub_time = force_subscribe_users.created_at where si_orders.uid = force_subscribe_users.uid

        update si_orders,users set si_orders.balance = users.balance where si_orders.uid = users.id
         *
         * update si_orders,distribution_channels set si_orders.site_name = distribution_channels.nickname where si_orders.site_id = distribution_channels.id

         *
         *
         */
        print_r("======书币消耗 【任务执行开始】=====".date("y-m-d H:i:s"."\n"));
        Log::info("======书币消耗 【任务执行开始】=====".date("y-m-d H:i:s"."\n"));


        DB::table('si_orders')->orderBy('id','desc')->chunk(1000,function ($orders){

            foreach ($orders as $order)
            {
                $uid = $order->uid;
                $book_fee1 = (int)DB::table('book_orders')->where('uid',$uid)->where('created_at','>',$order->first_recharge_time)->where('created_at','<',date('Y-m-d H:i:s',strtotime($order->first_recharge_time)+86400))->sum('fee');
                $book_fee2 = DB::table('book_orders')->where('uid',$uid)->where('created_at','>',$order->first_recharge_time)->where('created_at','<',date('Y-m-d H:i:s',strtotime($order->first_recharge_time)+86400*2))->sum('fee');
                $book_fee3 = DB::table('book_orders')->where('uid',$uid)->where('created_at','>',$order->first_recharge_time)->where('created_at','<',date('Y-m-d H:i:s',strtotime($order->first_recharge_time)+86400*3))->sum('fee');
                $book_fee7 = DB::table('book_orders')->where('uid',$uid)->where('created_at','>',$order->first_recharge_time)->where('created_at','<',date('Y-m-d H:i:s',strtotime($order->first_recharge_time)+86400*7))->sum('fee');
                $table = 'chapter_orders'.$uid%512;
                $x = $book_fee1 + (int)DB::connection('chapter_order_mysql')->table($table)->where('uid',$uid)->where('created_at','>',$order->first_recharge_time)->where('created_at','<',date('Y-m-d H:i:s',strtotime($order->first_recharge_time)+86400))->sum('fee');
                $xx = $book_fee2 + (int)DB::connection('chapter_order_mysql')->table($table)->where('uid',$uid)->where('created_at','>',$order->first_recharge_time)->where('created_at','<',date('Y-m-d H:i:s',strtotime($order->first_recharge_time)+86400*2))->sum('fee');
                $xxx = $book_fee3 + (int)DB::connection('chapter_order_mysql')->table($table)->where('uid',$uid)->where('created_at','>',$order->first_recharge_time)->where('created_at','<',date('Y-m-d H:i:s',strtotime($order->first_recharge_time)+86400*3))->sum('fee');
                $xxxxxxx = $book_fee7 + (int)DB::connection('chapter_order_mysql')->table($table)->where('uid',$uid)->where('created_at','>',$order->first_recharge_time)->where('created_at','<',date('Y-m-d H:i:s',strtotime($order->first_recharge_time)+86400*7))->sum('fee');

                DB::table('si_orders')->where('id',$order->id)->update(compact('x','xx','xxx','xxxxxxx'));
            }

        });

        Log::info("======书币消耗 【任务执行结束】=====" . date("y-m-d H:i:s" . "\n"));
        print_r("======书币消耗 【任务执行结束】=====" . date("y-m-d H:i:s" . "\n"));
    }
}