<?php
/**
 * Created by PhpStorm.
 * User: songdb
 * Date: 2017/12/26
 * Time: 下午5:26
 */

namespace App\Console\Commands\Channel;

use App\Modules\Channel\Services\ChannelService;
use App\Modules\SendOrder\Services\SendOrderService;
use App\Modules\Channel\Services\ChannelUserService;

use Log;
use Illuminate\Console\Command;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '检测渠道账号状态';

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        print_r("======检测渠道账号状态 【任务执行开始】=====".date("y-m-d H:i:s"."\n"));

        Log::info("======检测渠道账号状态 【任务执行开始】=====".date("y-m-d H:i:s"."\n"));

        $channel_users = ChannelUserService::getChannelList([],1);

        if(count($channel_users))
        {
            $channel_users->each(function($channel_user) {
                $channel_ids = ChannelService::getUserChannelIds($channel_user->id);

                $total_send_order_num = 0;
                foreach ($channel_ids as $channel_id)
                {
                    $distribution_channel_id = $channel_id;
                    $total_send_order_num += (int)SendOrderService::getPromotionCount(compact('distribution_channel_id'));
                }

                $except_channel_user_ids = [235,321];
                //判断是否有派单，并且注册时间大于15天  设置为待审核状态
                if($total_send_order_num == 0 && time() - strtotime($channel_user->created_at) > 86400*15 && !in_array($channel_user->id,$except_channel_user_ids))
                {
                    ChannelUserService::updateChannelData($channel_user->id,['is_enabled'=>0]);
                }

            });
        }

        Log::info("======检测渠道账号状态 【任务执行结束】=====".date("y-m-d H:i:s"."\n"));
        print_r("======检测渠道账号状态 【任务执行结束】=====".date("y-m-d H:i:s"."\n"));
    }
}