OwnerPriceExpressService.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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, $id=null) :array
  53. {
  54. if (!is_array($owner_id))$owner_id = [$owner_id];
  55. $owners = Owner::query()->withCount(["ownerPriceExpresses"=>function($query)use($id){
  56. if ($id)$query->where("id","!=",$id);
  57. }])->whereIn("id",$owner_id)->get();
  58. $arr = [];
  59. foreach ($owners as $owner){
  60. if ($owner->owner_price_expresses_count > 0)$arr[] = $owner->name;
  61. }
  62. return $arr;
  63. }
  64. public function getExistLogisticName($logistic_id, $id=null):array
  65. {
  66. $logistics = Logistic::query()->withCount(["ownerPriceExpresses"=>function($query)use($id){
  67. if ($id)$query->where("id","!=",$id);
  68. }])->whereIn("id",$logistic_id)->get();
  69. $arr = [];
  70. foreach ($logistics as $logistic){
  71. if ($logistic->owner_price_expresses_count > 0)$arr[] = $logistic->name;
  72. }
  73. return $arr;
  74. }
  75. public function destroy($id)
  76. {
  77. OwnerPriceExpressProvince::query()->where("owner_price_express_id",$id)->delete();
  78. DB::table("owner_price_express_owner")->where("owner_price_express_id",$id)->delete();
  79. DB::table("owner_price_express_logistic")->where("owner_price_express_id",$id)->delete();
  80. return OwnerPriceExpress::destroy($id);
  81. }
  82. public function update(array $params, array $values)
  83. {
  84. $query = OwnerPriceExpress::query();
  85. foreach ($params as $column=>$param){
  86. $query->where($column,$params);
  87. }
  88. return $query->update($values);
  89. }
  90. /**
  91. * CODE: -1:未找到计费模型 -2:重量无效
  92. *
  93. * @param double $weight
  94. * @param integer $owner_id
  95. * @param integer $logistic_id
  96. * @param integer $province_id
  97. * @return double
  98. */
  99. public function matching($weight, $owner_id, $logistic_id, $province_id)
  100. {
  101. if (!$weight)return -2;
  102. $key = "price_express_".$province_id."_".$logistic_id."_".$owner_id;
  103. $model = app(CacheService::class)->getOrExecute($key,function ()use($owner_id, $logistic_id, $province_id){
  104. return OwnerPriceExpress::query()->with(["details"=>function($query)use($province_id){
  105. /** @var Builder $query */
  106. $query->where("province_id",$province_id);
  107. }])->whereHas("owners",function ($query)use($owner_id){
  108. /** @var Builder $query */
  109. $query->where("id",$owner_id);
  110. })->whereHas("logistics",function ($query)use($logistic_id){
  111. /** @var Builder $query */
  112. $query->where("id",$logistic_id);
  113. })->first();
  114. });
  115. if (!$model || !$model->details)return -1;
  116. if ($weight < $model->initial_weight)$weight = $model->initial_weight;
  117. $initialMoney = $model->initial_weight*$model->details[0]->initial_weight_price;
  118. $weight -= $model->initial_weight;
  119. return (ceil($weight/$model->additional_weight)*$model->details[0]->additional_weight_price)+$initialMoney;
  120. }
  121. }