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