OwnerPriceLogisticService.php 5.4 KB

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