OwnerPriceExpressService.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace App\Services;
  3. use App\Logistic;
  4. use App\Owner;
  5. use App\OwnerPriceExpress;
  6. use App\OwnerPriceExpressProvince;
  7. use Illuminate\Database\Eloquent\Builder;
  8. use Illuminate\Support\Facades\DB;
  9. Class OwnerPriceExpressService
  10. {
  11. public function paginate($id = null)
  12. {
  13. $query = OwnerPriceExpress::query()->with(["owners","logistics"])
  14. ->orderByDesc("id");
  15. if ($id)$query->where("id",$id);
  16. return $query->paginate(50);
  17. }
  18. public function find($id, $withs = [])
  19. {
  20. return OwnerPriceExpress::query()->with($withs)->find($id);
  21. }
  22. public function updateDetail(array $params, array $values)
  23. {
  24. $query = OwnerPriceExpressProvince::query();
  25. foreach ($params as $column => $param){
  26. $query->where($column,$param);
  27. }
  28. return $query->update($values);
  29. }
  30. public function create(array $params)
  31. {
  32. return OwnerPriceExpress::query()->create($params);
  33. }
  34. public function createDetail(array $params)
  35. {
  36. return OwnerPriceExpressProvince::query()->create($params);
  37. }
  38. public function isExistDetail(array $params)
  39. {
  40. $query = OwnerPriceExpressProvince::query();
  41. foreach ($params as $column => $param){
  42. $query->where($column,$param);
  43. }
  44. return $query->count();
  45. }
  46. public function destroyDetail($id)
  47. {
  48. return OwnerPriceExpressProvince::destroy($id);
  49. }
  50. public function getExistOwnerName($owner_id, $id=null) :array
  51. {
  52. if (!is_array($owner_id))$owner_id = [$owner_id];
  53. $owners = Owner::query()->withCount(["ownerPriceExpresses"=>function($query)use($id){
  54. if ($id)$query->where("id","!=",$id);
  55. }])->whereIn("id",$owner_id)->get();
  56. $arr = [];
  57. foreach ($owners as $owner){
  58. if ($owner->owner_price_expresses_count > 0)$arr[] = $owner->name;
  59. }
  60. return $arr;
  61. }
  62. public function getExistLogisticName($logistic_id, $id=null):array
  63. {
  64. $logistics = Logistic::query()->withCount(["ownerPriceExpresses"=>function($query)use($id){
  65. if ($id)$query->where("id","!=",$id);
  66. }])->whereIn("id",$logistic_id)->get();
  67. $arr = [];
  68. foreach ($logistics as $logistic){
  69. if ($logistic->owner_price_expresses_count > 0)$arr[] = $logistic->name;
  70. }
  71. return $arr;
  72. }
  73. public function destroy($id)
  74. {
  75. OwnerPriceExpressProvince::query()->where("owner_price_express_id",$id)->delete();
  76. DB::table("owner_price_express_owner")->where("owner_price_express_id",$id)->delete();
  77. DB::table("owner_price_express_logistic")->where("owner_price_express_id",$id)->delete();
  78. return OwnerPriceExpress::destroy($id);
  79. }
  80. public function update(array $params, array $values)
  81. {
  82. $query = OwnerPriceExpress::query();
  83. foreach ($params as $column=>$param){
  84. $query->where($column,$params);
  85. }
  86. return $query->update($values);
  87. }
  88. /**
  89. * CODE: -1:未找到计费模型 -2:重量无效
  90. *
  91. * @param double $weight
  92. * @param integer $owner_id
  93. * @param integer $logistic_id
  94. * @param integer $province_id
  95. * @return double
  96. */
  97. public function matching($weight, $owner_id, $logistic_id, $province_id)
  98. {
  99. if (!$weight)return -2;
  100. $key = "price_express_".$province_id."_".$logistic_id."_".$owner_id;
  101. $model = app(CacheService::class)->getOrExecute($key,function ()use($owner_id, $logistic_id, $province_id){
  102. return OwnerPriceExpress::query()->with(["details"=>function($query)use($province_id){
  103. /** @var Builder $query */
  104. $query->where("province_id",$province_id);
  105. }])->whereHas("owners",function ($query)use($owner_id){
  106. /** @var Builder $query */
  107. $query->where("id",$owner_id);
  108. })->whereHas("logistics",function ($query)use($logistic_id){
  109. /** @var Builder $query */
  110. $query->where("id",$logistic_id);
  111. })->first();
  112. });
  113. if (!$model || !$model->details)return -1;
  114. if ($weight < $model->initial_weight)$weight = $model->initial_weight;
  115. $initialMoney = $model->initial_weight*$model->details[0]->initial_weight_price;
  116. $weight -= $model->initial_weight;
  117. return (ceil($weight/$model->additional_weight)*$model->details[0]->additional_weight_price)+$initialMoney;
  118. }
  119. }