| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- <?php
- namespace App\Services;
- use App\Logistic;
- use App\Owner;
- use App\OwnerPriceExpress;
- use App\OwnerPriceExpressProvince;
- use Illuminate\Database\Eloquent\Builder;
- use Illuminate\Support\Facades\DB;
- Class OwnerPriceExpressService
- {
- public function paginate($id = null)
- {
- $query = OwnerPriceExpress::query()->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, $id=null) :array
- {
- if (!is_array($owner_id))$owner_id = [$owner_id];
- $owners = Owner::query()->withCount(["ownerPriceExpresses"=>function($query)use($id){
- if ($id)$query->where("id","!=",$id);
- }])->whereIn("id",$owner_id)->get();
- $arr = [];
- foreach ($owners as $owner){
- if ($owner->owner_price_expresses_count > 0)$arr[] = $owner->name;
- }
- return $arr;
- }
- 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)$weight = $model->initial_weight;
- $initialMoney = $model->initial_weight*$model->details[0]->initial_weight_price;
- $weight -= $model->initial_weight;
- return (ceil($weight/$model->additional_weight)*$model->details[0]->additional_weight_price)+$initialMoney;
- }
- }
|