OwnerPriceExpressService.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace App\Services;
  3. use App\Logistic;
  4. use App\Owner;
  5. use App\OwnerPriceExpress;
  6. use App\OwnerPriceExpressProvince;
  7. use Illuminate\Database\Eloquent\Builder;
  8. use Illuminate\Support\Facades\DB;
  9. use App\Traits\ServiceAppAop;
  10. Class OwnerPriceExpressService
  11. {
  12. use ServiceAppAop;
  13. public function paginate($id = null)
  14. {
  15. $query = OwnerPriceExpress::query()->with(["owners","logistics"])
  16. ->orderByDesc("id");
  17. if ($id)$query->where("id",$id);
  18. return $query->paginate(50);
  19. }
  20. public function find($id, $withs = [])
  21. {
  22. return OwnerPriceExpress::query()->with($withs)->find($id);
  23. }
  24. public function updateDetail(array $params, array $values)
  25. {
  26. $query = OwnerPriceExpressProvince::query();
  27. foreach ($params as $column => $param){
  28. $query->where($column,$param);
  29. }
  30. return $query->update($values);
  31. }
  32. public function create(array $params)
  33. {
  34. return OwnerPriceExpress::query()->create($params);
  35. }
  36. public function createDetail(array $params)
  37. {
  38. return OwnerPriceExpressProvince::query()->create($params);
  39. }
  40. public function isExistDetail(array $params)
  41. {
  42. $query = OwnerPriceExpressProvince::query();
  43. foreach ($params as $column => $param){
  44. $query->where($column,$param);
  45. }
  46. return $query->count();
  47. }
  48. public function destroyDetail($id)
  49. {
  50. return OwnerPriceExpressProvince::destroy($id);
  51. }
  52. public function getExistOwnerName($owner_id, $logistic_id, $id=null, $type = 'ownerPriceExpresses') :array
  53. {
  54. if (!is_array($owner_id))$owner_id = [$owner_id];
  55. if (!is_array($logistic_id))$logistic_id = [$logistic_id];
  56. $owners = Owner::query()->with([$type=>function($query)use($id){
  57. /** @var Builder $query */
  58. if ($id)$query->where("id","!=",$id);
  59. $query->with(["logistics"]);
  60. }])->whereIn("id",$owner_id)->get();
  61. $result = [];
  62. foreach ($owners as $owner){
  63. $arr = [];
  64. if ($owner->ownerPriceExpresses){
  65. foreach ($owner->ownerPriceExpresses as $express){
  66. if ($express->logistics){
  67. foreach ($express->logistics as $logistic){
  68. if (array_search($logistic->id,$logistic_id) !== false)$arr[] = $logistic->name;
  69. }
  70. }
  71. }
  72. }
  73. if (count($arr)>0)$result[] = "“".$owner->name."”已绑定:".implode(",",$arr);
  74. }
  75. return $result;
  76. }
  77. public function getExistLogisticName($logistic_id, $id=null):array
  78. {
  79. $logistics = Logistic::query()->withCount(["ownerPriceExpresses"=>function($query)use($id){
  80. if ($id)$query->where("id","!=",$id);
  81. }])->whereIn("id",$logistic_id)->get();
  82. $arr = [];
  83. foreach ($logistics as $logistic){
  84. if ($logistic->owner_price_expresses_count > 0)$arr[] = $logistic->name;
  85. }
  86. return $arr;
  87. }
  88. public function destroy($id)
  89. {
  90. OwnerPriceExpressProvince::query()->where("owner_price_express_id",$id)->delete();
  91. DB::table("owner_price_express_owner")->where("owner_price_express_id",$id)->delete();
  92. DB::table("owner_price_express_logistic")->where("owner_price_express_id",$id)->delete();
  93. return OwnerPriceExpress::destroy($id);
  94. }
  95. public function update(array $params, array $values)
  96. {
  97. $query = OwnerPriceExpress::query();
  98. foreach ($params as $column=>$param){
  99. $query->where($column,$params);
  100. }
  101. return $query->update($values);
  102. }
  103. /**
  104. * CODE: -1:未找到计费模型 -2:重量无效
  105. *
  106. * @param double $weight
  107. * @param integer $owner_id
  108. * @param integer $logistic_id
  109. * @param integer $province_id
  110. * @return double
  111. */
  112. public function matching($weight, $owner_id, $logistic_id, $province_id)
  113. {
  114. if (!$weight)return -2;
  115. $key = "price_express_".$province_id."_".$logistic_id."_".$owner_id;
  116. $model = app(CacheService::class)->getOrExecute($key,function ()use($owner_id, $logistic_id, $province_id){
  117. return OwnerPriceExpress::query()->with(["details"=>function($query)use($province_id){
  118. /** @var Builder $query */
  119. $query->where("province_id",$province_id);
  120. }])->whereHas("owners",function ($query)use($owner_id){
  121. /** @var Builder $query */
  122. $query->where("id",$owner_id);
  123. })->whereHas("logistics",function ($query)use($logistic_id){
  124. /** @var Builder $query */
  125. $query->where("id",$logistic_id);
  126. })->first();
  127. });
  128. if (!$model || !$model->details)return -1;
  129. if ($weight < $model->initial_weight)$weight = $model->initial_weight;
  130. $initialMoney = $model->initial_weight*$model->details[0]->initial_weight_price;
  131. $weight -= $model->initial_weight;
  132. return (ceil($weight/$model->additional_weight)*$model->details[0]->additional_weight_price)+$initialMoney;
  133. }
  134. }