OwnerPriceLogisticService.php 5.1 KB

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