OwnerPriceLogisticService.php 5.5 KB

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