<?php

namespace App\Jobs;

use App\Commodity;
use App\CommodityBarcode;
use App\DeliveryAppointmentDetail;
use App\InventoryAccountMission;
use App\InventoryCompare;
use App\InventoryDailyLog;
use App\OrderCommodity;
use App\OrderCommodityAssign;
use App\OrderPackageCommodities;
use App\ProcessesContent;
use App\Services\LogService;
use App\StationTaskCommodity;
use App\StoreCheckingReceiveItem;
use App\StoreItem;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class DeleteRepetitionSkuItem implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

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

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        ini_set('max_execution_time', 15);
        //关联commodity_id 的表替换商品id
        StoreItem::query()->whereIn('commodity_id',data_get($this->param,'*.id'))->update(['commodity_id'=>$this->param[0]['id']]);
        CommodityBarcode::query()->whereIn('commodity_id',data_get($this->param,'*.id'))->update(['commodity_id'=>$this->param[0]['id']]);
        InventoryCompare::query()->whereIn('commodity_id',data_get($this->param,'*.id'))->update(['commodity_id'=>$this->param[0]['id']]);
        InventoryAccountMission::query()->whereIn('commodity_id',data_get($this->param,'*.id'))->update(['commodity_id'=>$this->param[0]['id']]);
        InventoryDailyLog::query()->whereIn('commodity_id',data_get($this->param,'*.id'))->update(['commodity_id'=>$this->param[0]['id']]);
        OrderCommodity::query()->whereIn('commodity_id',data_get($this->param,'*.id'))->update(['commodity_id'=>$this->param[0]['id']]);
        OrderPackageCommodities::query()->whereIn('commodity_id',data_get($this->param,'*.id'))->update(['commodity_id'=>$this->param[0]['id']]);
        OrderCommodityAssign::query()->whereIn('commodity_id',data_get($this->param,'*.id'))->update(['commodity_id'=>$this->param[0]['id']]);
        ProcessesContent::query()->whereIn('commodity_id',data_get($this->param,'*.id'))->update(['commodity_id'=>$this->param[0]['id']]);
        StationTaskCommodity::query()->whereIn('commodity_id',data_get($this->param,'*.id'))->update(['commodity_id'=>$this->param[0]['id']]);
        StoreCheckingReceiveItem::query()->whereIn('commodity_id',data_get($this->param,'*.id'))->update(['commodity_id'=>$this->param[0]['id']]);
        DeliveryAppointmentDetail::query()->whereIn('commodity_id',data_get($this->param,'*.id'))->update(['commodity_id'=>$this->param[0]['id']]);
        foreach ($this->param as $del){
            if ($del['id']==$this->param[0]['id']) continue;
            Commodity::query()->where('id',$del['id'])->delete();
            LogService::log(__METHOD__,"去除重复sku商品",json_encode($del));
        }
    }
}
