OwnerPriceExpressService.php 5.1 KB

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