<?php

namespace App\Services;

use App\InventoryDailyLog;
use App\InventoryDailyLoggingOwner;
use App\Services\common\BatchUpdateService;
use App\Services\common\QueryService;
use App\Traits\ServiceAppAop;


class InventoryDailyLogService
{
    use ServiceAppAop;
    protected $modelClass=InventoryDailyLog::class;
    //库存体积条件
    function conditionQuery(array $param){
        $inventoryDailyLogs = InventoryDailyLog::query()->with(['depository','owner','commodity'=>function($query){
            $query->with('barcodes');
        }])->orderByDesc('inventory_daily_logs.id');
        $columnQueryRules=[
            'owner_id' => ['multi' => ','],
            'created_at_start' => ['alias' => 'created_at' , 'startDate' => ' 00:00:00'],
            'created_at_end' => ['alias' => 'created_at' , 'endDate' => ' 23:59:59'],
            'id' => ['multi' => ','],
        ];
        $inventoryDailyLogs = app(QueryService::class)->query($param,$inventoryDailyLogs,$columnQueryRules,"inventory_daily_logs");
        return $inventoryDailyLogs;

    }

    public function getSql(array $params){
        return $this->conditionQuery($params)->selectRaw("inventory_daily_logs.id,inventory_daily_logs.created_at,inventory_daily_logs.amount,inventory_daily_logs.volumn_occupied,inventory_daily_logs.gross_weight")
            ->leftJoin('owners','inventory_daily_logs.owner_id','owners.id')
                ->selectRaw('owners.name owner_name')
            ->leftJoin('depositories','inventory_daily_logs.depository_id','depositories.id')
                ->selectRaw('depositories.name depository_name')
            ->leftJoin('commodities','inventory_daily_logs.commodity_id','commodities.id')
                ->selectRaw('commodities.name commodity_name,commodities.sku commodity_sku,commodities.length commodity_length,commodities.width commodity_width,commodities.height commodity_height,commodities.volumn commodity_volumn')
            ->leftJoin('commodity_barcodes','commodity_barcodes.commodity_id','commodities.id')
                ->selectRaw("GROUP_CONCAT(commodity_barcodes.code) commodity_barcode_code")
            ->groupBy('id')
            ->sql();
    }

    //库存体积
    public function paginate(array $params){
        return $this->conditionQuery($params)->paginate($params['paginate'] ?? 50);
    }
    //获取开启监听记录货主
    public function getInventoryDailyLoggingOwner($column = ['id','owner_id'], $status = "启用"){
        if (!is_array($column)) {
            $column = [$column];
        }
        return InventoryDailyLoggingOwner::query()->select($column)->where('status',$status)->get();
    }

    //录入监听记录货主
    public function firstOrCreate($param,$column = null){
        if ($column)return InventoryDailyLoggingOwner::query()->firstOrCreate($param,$column);
        return InventoryDailyLoggingOwner::query()->firstOrCreate($param);
    }

    public function batchUpdateItself($column, array $params)
    {
        return app(BatchUpdateService::class)->batchUpdateItself('inventory_daily_logs', $column, $params);
    }

}
