OwnerPriceLogisticService.php 5.5 KB

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