<?php

namespace App\Services;

use App\OrderPackage;
use App\Services\common\QueryService;
use Illuminate\Database\Eloquent\Builder;

const TABLE = "order_packages.";use App\Traits\ServiceAppAop;


class WeighExceptedService
{
    use ServiceAppAop;
    protected $modelClass=WeighExcepted::class;
    /**
     * @param array $params
     * @return Builder
     */
    private function conditionQuery(array $params){
        $query = OrderPackage::query()->orderBy(TABLE.'id','DESC')->selectRaw(TABLE."* ");
        $columnQueryRules=[
            'id' => ['multi' => ','],
        ];
        return app(QueryService::class)->query($params,$query,$columnQueryRules,"order_packages");
    }

    public function getCreateExceptionSql(array $params){
        return $this->conditionQuery($params)->where(TABLE.'uploaded_to_wms',"异常")->orWhere(TABLE.'status',"测量异常")
            ->leftJoin('orders',TABLE.'order_id','orders.id')
            ->LeftJoin('logistics','orders.logistic_id','logistics.id')
                ->selectRaw('logistics.name logistic_name')
            ->LeftJoin('paper_boxes',TABLE.'paper_box_id','paper_boxes.id')
                ->selectRaw('paper_boxes.model paper_box_name')
            ->LeftJoin('measuring_machines',TABLE.'measuring_machine_id','measuring_machines.id')
                ->selectRaw('measuring_machines.name measuring_machine_name')
            ->sql();
    }

    public function getIssuedExceptionSql(array $params){
        return $this->conditionQuery($params)->where(TABLE.'status',"下发异常")->orWhere(TABLE.'status',"记录异常")
            ->orWhere(TABLE.'uploaded_to_wms',"异常")
            ->leftJoin('orders',TABLE.'order_id','orders.id')
            ->LeftJoin('logistics','orders.logistic_id','logistics.id')
                ->selectRaw('logistics.name logistic_name')
            ->sql();
    }
}
