<?php

namespace App\Services;

use App\Services\common\QueryService;
use App\StoreCheckingReceive;
use Illuminate\Database\Eloquent\Builder;
use App\Traits\ServiceAppAop;


class StoreCheckingReceiveService
{
    use ServiceAppAop;
    protected $modelClass=StoreCheckingReceive::class;
    /**
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    private function conditionQuery(Builder $query,array $params){
        $columnQueryRules=[
            'created_at_start' => ['alias' => 'created_at' , 'startDate' => ' 00:00:00'],
            'created_at_end' => ['alias' => 'created_at' , 'endDate' => ' 23:59:59'],
            'owner_id' => ['multi' => ','],
            'id' => ['multi' => ','],
            'asn' => ['batch' => ',']
        ];
        return app(QueryService::class)->query($params,$query,$columnQueryRules);
    }

    public function paginate(array $params){
        $query = StoreCheckingReceive::query()->orderByDesc('id');
        $query->with('owner');
        $query = $this->conditionQuery($query, $params);
        return $query->paginate($params['paginate'] ?? 50);
    }


    public function create(array $params){
        return StoreCheckingReceive::query()->create($params);
    }


    public function find($id, array $withs = []){
        $query = StoreCheckingReceive::query();
        if ($withs) $query -> with($withs);
        return $query->find($id);
    }

    public function updateFind(StoreCheckingReceive $storeCheckingReceive, array $params){
        $storeCheckingReceive->update($params);
        return $storeCheckingReceive;
    }
}
