<?php

namespace App\Jobs;

use App\Services\LogService;
use App\Services\OrderFreezeService;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB;

class OrderFreeze implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable;

    protected $params;
    protected $pool;
    /**
     * Create a new job instance.
     * @param array $params
     * @return void
     */
    public function __construct(array $params)
    {
        $this->params = $params;
    }

    /**
     * Execute the job.
     *
     * @param OrderFreezeService $service
     *
     * @return void
     */
    public function handle(OrderFreezeService $service)
    {
        if (!$this->params)return;
        $this->pool = $service->getFreezes();
        if (isset($this->params[0][0]))$this->processUpdate();
        $this->processCreate();
    }

    protected function processUpdate()
    {
        unset($this->params[0]);
    }

    protected function processCreate()
    {
        $freezeOrders = [];
        foreach ($this->params as $param){
            if ($param["frozen"]=='是')continue;
            if (!$param["logistic_id"])continue;
            if ($this->isFreeze($param))$freezeOrders[] = $param["code"];
        }
        $this->pushWMS($freezeOrders);
    }

    private function isFreeze($param)
    {
        $status = ['分配完成','创建订单','拣货完成','播种完成'];
        if (array_search($param["wms_status"],$status)===false)return false;

        foreach ($this->pool as $pool){
            if ($pool["thawed_at"] && Carbon::parse($pool["thawed_at"])->lte(Carbon::today()))continue;
            if ($pool["logistic_id"]!=$param["logistic_id"])continue;
            if ($pool["province_name"] && (mb_strpos($param["province"],$pool["province_name"]) === false))continue;
            if ($pool["city_name"] && (mb_strpos($param["city"],$pool["city_name"]) === false))continue;
            if ($pool["district_name"] && (mb_strpos($param["district"],$pool["district_name"]) === false))continue;
            if ($pool["town_name"] && (mb_strpos($param["address"],$pool["town_name"]) === false))continue;
            if ($pool["street_name"] && (mb_strpos($param["address"],$pool["street_name"]) === false))continue;
            return true;
        }
        return false;
    }

    private function pushWMS($freezeOrders)
    {
        if (!$freezeOrders)return;
        $where = "''";
        foreach ($freezeOrders as $f)$where .= ",'{$f}'";
        app("OrderService")->freezeRemoveWave($where,true);
        $edit = date("Y-m-d H:i:s");
        $sql = <<<sql
UPDATE DOC_ORDER_HEADER SET edittime = TO_DATE('{$edit}','yyyy-mm-dd hh24:mi:ss'),releasestatus = 'H',waveno='*',notes = CASE WHEN notes IS NULL THEN '停运' ELSE  notes||',停运' END where ORDERNO in ({$where})
sql;
        DB::connection("oracle")->update($sql);
        LogService::log(__METHOD__,"订单同步-自动冻结",$sql);
    }
}
