<?php

namespace App\Services;

use App\Traits\ServiceAppAop;
use Illuminate\Support\Facades\Cache;

class OrderFreezeService
{
    use ServiceAppAop;
    protected $modelClass=OrderFreeze::class;


    /**
     * 获取冻结信息
     */
    public function getFreezes()
    {
        return app(CacheService::class)->getOrExecute("order_freeze_condition_pool",function (){
            return $this->get();
        },config("cache.expirations.rarelyChange"));
    }

    /**
     * 刷新冻结信息
     */
    public function refreshFreezes()
    {
        if (Cache::has("order_freeze_condition_pool")) Cache::put("order_freeze_condition_pool",$this->get());
    }

    /**
     * 获取所有冻结条件
     *
     */
    public function get()
    {
        return \App\OrderFreeze::query()
            ->with(["logistic:id,name","province:id,name","city:id,name","district:id,name","town:id,name","street:id,name"])
            ->where("status",0)
            ->get(["logistic_id","province_id","city_id","district_id","town_id","street_id","thawed_at"])->each(function (&$freeze){
                $freeze["logistic_name"] = $freeze->logistic ? $freeze->logistic->name : '';
                $freeze["province_name"] = $freeze->province ? $freeze->province->name : '';
                $freeze["city_name"]     = $freeze->city ? $freeze->city->name : '';
                $freeze["district_name"] = $freeze->district ? $freeze->district->name : '';
                $freeze["town_name"] = $freeze->town ? $freeze->town->name : '';
                $freeze["street_name"] = $freeze->street ? $freeze->street->name : '';
                unset($freeze->logistic,$freeze->province,$freeze->city,$freeze->district,$freeze->town,$freeze->street);
            })->toArray();
    }
}
