OwnerPriceLogisticService.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace App\Services;
  3. use App\OwnerPriceLogistic;
  4. use App\OwnerPriceLogisticDetail;
  5. use Illuminate\Database\Eloquent\Builder;
  6. use App\Traits\ServiceAppAop;
  7. Class OwnerPriceLogisticService
  8. {
  9. use ServiceAppAop;
  10. public function paginate($id = null)
  11. {
  12. $query = OwnerPriceLogistic::query()->with(["owners","logistics","unit","otherUnit"]);
  13. if ($id)$query->where("id",$id);
  14. return $query->paginate(50);
  15. }
  16. public function create(array $params)
  17. {
  18. return OwnerPriceLogistic::query()->create($params);
  19. }
  20. public function find($id, $withs=[])
  21. {
  22. return OwnerPriceLogistic::query()->with($withs)->find($id);
  23. }
  24. public function update(array $params, array $values)
  25. {
  26. $query = OwnerPriceLogistic::query();
  27. foreach ($params as $column=>$param){
  28. $query->where($column,$params);
  29. }
  30. return $query->update($values);
  31. }
  32. public function destroy($id)
  33. {
  34. OwnerPriceLogistic::destroy($id);
  35. OwnerPriceLogisticDetail::query()->where("owner_price_logistic_id",$id)->delete();
  36. }
  37. public function updateDetail(array $params, array $values)
  38. {
  39. $query = OwnerPriceLogisticDetail::query();
  40. foreach ($params as $column => $param){
  41. $query->where($column,$param);
  42. }
  43. return $query->update($values);
  44. }
  45. public function isExistDetail(array $params)
  46. {
  47. $query = OwnerPriceLogisticDetail::query();
  48. foreach ($params as $column => $param){
  49. $query->where($column,$param);
  50. }
  51. return $query->count();
  52. }
  53. public function createDetail(array $params)
  54. {
  55. return OwnerPriceLogisticDetail::query()->create($params);
  56. }
  57. public function destroyDetail($id)
  58. {
  59. return OwnerPriceLogisticDetail::destroy($id);
  60. }
  61. public function checkRange($range):bool
  62. {
  63. $arrRanges = explode(",",$range);
  64. $len = count($arrRanges);
  65. if ($len==0)return false; //范围为空 false
  66. $previous = []; //慢指针数组 不记录第一个值
  67. foreach ($arrRanges as $index => $value){
  68. if ($index == $len-1)$preg = "/\d*\-/"; //最后一个范围不允许封闭
  69. else $preg = "/\d*\-\d*/";
  70. preg_match($preg, $value, $match);
  71. if (!$match || $match[0]!=$value)return false;//匹配结果不等于值 false
  72. $arr = explode("-",$value); //二次切割判断是否连续
  73. if ($index != 0){
  74. if ($index == $len-1){
  75. if ((int)$arr[0] != (int)$previous[$index-1][1])return false; //最后一个范围只需要判断起始值
  76. }else{
  77. if ((int)$arr[0] != (int)$previous[$index-1][1] || (int)$arr[1] <= (int)$arr[0])return false; //普通范围起始值必须为上一个结束值 当前结束值必须大于起始值
  78. }
  79. }else{
  80. if ((int)$arr[1] <= (int)$arr[0])return false; //第一个范围只需判断当前结束值大于起始值
  81. }
  82. $previous[] = $arr;
  83. }
  84. return true;
  85. }
  86. /**
  87. * CODE: -1:未找到计费模型
  88. *
  89. * @param double $amount
  90. * @param integer $owner_id
  91. * @param integer $logistic_id
  92. * @param integer $unit_id
  93. * @param integer $province_id
  94. * @param integer $city_id
  95. * @return double
  96. */
  97. public function matching($amount, $owner_id, $logistic_id, $unit_id, $province_id, $city_id)
  98. {
  99. $model = OwnerPriceLogistic::query()->with(["details"=>function($query)use($province_id,$city_id){
  100. /** @var Builder $query */
  101. $query->where("province_id",$province_id)->where("city_id",$city_id);
  102. }])->where(function ($query)use($unit_id){
  103. /** @var Builder $query */
  104. $query->where("unit_id",$unit_id)->orWhere("other_unit_id",$unit_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. if (!$model || !$model->details)return -1;
  113. $fee = $model->pick_up_price + $model->fuel_price + $model->service_price; //服务费
  114. foreach ($model->details as $detail){
  115. if ($unit_id != $detail->unit_id)continue;
  116. $arr = explode("-",$detail->range);
  117. if (count($arr) < 2 || !$arr[1]){
  118. if ($amount >= $arr[0])return $this->calculation($amount,$detail,$fee);
  119. }else{
  120. if ($amount >= $arr[0] && $amount < $arr[1])return $this->calculation($amount,$detail,$fee);
  121. }
  122. }
  123. return -1;
  124. }
  125. private function calculation($amount, $detail, $fee)
  126. {
  127. if ($amount < $detail->initial_amount)$amount = $detail->initial_amount; //小于起始数以起始数为准
  128. $money = $amount * $detail->unit_price;
  129. if ($money < $detail->initial_fee)$money = $detail->initial_fee; //小于起始计费以起始计费为准
  130. return $money+$detail->delivery_fee+$fee;
  131. }
  132. }