<?php

namespace App\Console\Commands\SendOrder;
use App\Modules\SendOrder\Services\SendOrderService;
use App\Modules\SendOrder\Models\SendOrder;
use App\Modules\Sys\Services\PromotionGroupConfigService;
use Illuminate\Console\Command;
use App\Modules\Util;
use Log;
class UpdateSendOrderPreSendDate extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'updateSendOrderPreSendDate{--pre_date=}{--id=*}';

    /**
     * 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()
    {
        if($this->option('id'))
        {
            $distribution_channel_ids=$this->option('id');
        }else{
            $distribution_channel_ids = explode(',', PromotionGroupConfigService::CHANNEL_ID_LIST());
            $distribution_channel_ids = [2,14,13,188];
        }
        if($this->option('pre_date')){
            $pre_date=$this->option('pre_date');
        }

        $params = compact('distribution_channel_ids', 'show_agent','pre_date');
//        $params['promotion_type'] = 'EXTERNAL'; //只筛选外部
        $data = SendOrderService::searchForDaliy($params, true);
        $ids ='';
        $count=0;
        foreach ($data as $item) {
            $item->old_name = $item->name;
            if (strpos($item->name, "-")) {
                $strs = explode('-', $item->name);
                if (count($strs) == 4) {
                    $date_str = $strs[2];
//                    dump($item->name.' '.$item->id.' '.$date_str.' '.date('Y/m/d',strtotime($date_str)));
                    if($date_str==date('Y/m/d',strtotime($date_str))){
                        $item->real_pre_send_date = date('Y/m/d',strtotime($date_str));
                    }
                }elseif (count($strs) == 6) {
                    $date_str = $strs[2] .'-'.$strs[3] .'-'.$strs[4];
//                    dump($item->name.' '.$item->id.' '.$date_str.' '.date('Y-m-d',strtotime($date_str)));
                    if ($date_str == date('Y-m-d', strtotime($date_str))) {
                        $item->real_pre_send_date = date('Y/m/d', strtotime($date_str));
                    }
                }
                if(isset($item->real_pre_send_date)&&(date('Y-m-d',strtotime($item->real_pre_send_date))!=date('Y-m-d',strtotime($item->pre_send_date))||empty($item->pre_send_date))){

//                    dump($item->id.' '.' : '.date('Y-m-d',strtotime($item->real_pre_send_date)).'  '.date('Y-m-d',strtotime($item->pre_send_date)));

                    $effect = SendOrder::where('id',$item->id)->update(['pre_send_date'=>date('Y-m-d',strtotime($item->real_pre_send_date))]);
                    $ids =$ids.' '.$item->id;
                    $count+=$effect;
                }
            }
        }
        if($count){
            dump("共修改了{$count} 项数据,id为{$ids}");
            Log::info("共修改了{$count}项数据,id为{$ids}");
        }


    }
}
