with(["owners","logistics"]) ->orderByDesc("id"); if ($id)$query->where("id",$id); return $query->paginate(50); } public function find($id, $withs = []) { return OwnerPriceExpress::query()->with($withs)->find($id); } public function updateDetail(array $params, array $values) { $query = OwnerPriceExpressProvince::query(); foreach ($params as $column => $param){ $query->where($column,$param); } return $query->update($values); } public function create(array $params) { return OwnerPriceExpress::query()->create($params); } public function createDetail(array $params) { return OwnerPriceExpressProvince::query()->create($params); } public function isExistDetail(array $params) { $query = OwnerPriceExpressProvince::query(); foreach ($params as $column => $param){ $query->where($column,$param); } return $query->count(); } public function destroyDetail($id) { return OwnerPriceExpressProvince::destroy($id); } public function getExistOwnerName($owner_id, $logistic_id, $id=null, $type = 'ownerPriceExpresses') :array { if (!is_array($owner_id))$owner_id = [$owner_id]; if (!is_array($logistic_id))$logistic_id = [$logistic_id]; $owners = Owner::query()->with([$type=>function($query)use($id){ /** @var Builder $query */ if ($id)$query->where("id","!=",$id); $query->with(["logistics"]); }])->whereIn("id",$owner_id)->get(); $result = []; foreach ($owners as $owner){ $arr = []; if ($owner->ownerPriceExpresses){ foreach ($owner->ownerPriceExpresses as $express){ if ($express->logistics){ foreach ($express->logistics as $logistic){ if (array_search($logistic->id,$logistic_id) !== false)$arr[] = $logistic->name; } } } } if (count($arr)>0)$result[] = "“".$owner->name."”已绑定:".implode(",",$arr); } return $result; } public function getExistLogisticName($logistic_id, $id=null):array { $logistics = Logistic::query()->withCount(["ownerPriceExpresses"=>function($query)use($id){ if ($id)$query->where("id","!=",$id); }])->whereIn("id",$logistic_id)->get(); $arr = []; foreach ($logistics as $logistic){ if ($logistic->owner_price_expresses_count > 0)$arr[] = $logistic->name; } return $arr; } public function destroy($id) { OwnerPriceExpressProvince::query()->where("owner_price_express_id",$id)->delete(); DB::table("owner_price_express_owner")->where("owner_price_express_id",$id)->delete(); DB::table("owner_price_express_logistic")->where("owner_price_express_id",$id)->delete(); return OwnerPriceExpress::destroy($id); } public function update(array $params, array $values) { $query = OwnerPriceExpress::query(); foreach ($params as $column=>$param){ $query->where($column,$params); } return $query->update($values); } /** * CODE: -1:未找到计费模型 -2:重量无效 * * @param double $weight * @param integer $owner_id * @param integer $logistic_id * @param integer $province_id * @return double */ public function matching($weight, $owner_id, $logistic_id, $province_id) { if (!$weight)return -2; $key = "price_express_".$province_id."_".$logistic_id."_".$owner_id; $model = app(CacheService::class)->getOrExecute($key,function ()use($owner_id, $logistic_id, $province_id){ return OwnerPriceExpress::query()->with(["details"=>function($query)use($province_id){ /** @var Builder $query */ $query->where("province_id",$province_id); }])->whereHas("owners",function ($query)use($owner_id){ /** @var Builder $query */ $query->where("id",$owner_id); })->whereHas("logistics",function ($query)use($logistic_id){ /** @var Builder $query */ $query->where("id",$logistic_id); })->first(); }); if (!$model || !$model->details)return -1; if ($weight <= $model->initial_weight)return $model->details[0]->initial_weight_price; $weight -= $model->initial_weight; return (ceil($weight/$model->additional_weight)*$model->details[0]->additional_weight_price)+$model->details[0]->initial_weight_price; } }