OwnerPriceDirectLogisticService.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. namespace App\Services;
  3. use App\Owner;
  4. use App\OwnerPriceDirectLogistic;
  5. use App\OwnerPriceDirectLogisticCar;
  6. use App\Services\common\QueryService;
  7. use Illuminate\Database\Eloquent\Builder;
  8. use Illuminate\Support\Facades\DB;
  9. use App\Traits\ServiceAppAop;
  10. class OwnerPriceDirectLogisticService
  11. {
  12. use ServiceAppAop;
  13. protected $modelClass=OwnerPriceDirectLogistic::class;
  14. public function paginate($id = null)
  15. {
  16. $query = OwnerPriceDirectLogistic::query()->with("owners");
  17. if($id)$query->where("id",$id);
  18. return $query->paginate(50);
  19. }
  20. public function create(array $params)
  21. {
  22. $params["operation"] = "C";
  23. return OwnerPriceDirectLogistic::query()->create($params);
  24. }
  25. /**
  26. * 拷贝目标数据
  27. *
  28. * @param object|int $model
  29. * @param array $values
  30. * @param array|null $owners
  31. * @param array $items
  32. * @param bool $isLoadItem
  33. *
  34. * @return object|null
  35. */
  36. public function copy($model, $values = [], $owners = null, $items = [], $isLoadItem = true)
  37. {
  38. if (is_integer($model))$model = OwnerPriceDirectLogistic::query()->find($model);
  39. if (!$model)return null;
  40. $values["operation"] = "U";
  41. $values["target_id"] = $model->id;
  42. foreach ($model->getFillable() as $column){
  43. if (!array_key_exists($column,$values))$values[$column] = $model[$column];
  44. }
  45. /** @var OwnerPriceDirectLogistic $copyModel */
  46. $copyModel = OwnerPriceDirectLogistic::query()->create($values);
  47. if ($owners===null){
  48. $query = DB::raw("SELECT * FROM owner_price_direct_logistic_owner WHERE owner_price_direct_logistic_id = {$model->id}");
  49. $owners = array_column(DB::select($query),"owner_id");
  50. }
  51. $copyModel->owners()->sync($owners);
  52. $insert = [];
  53. if ($isLoadItem){
  54. $model->loadMissing("details");
  55. /** @var \stdClass $model */
  56. foreach ($model->details as $item){
  57. $columns = ["car_type_id", "base_fee", "additional_fee"];
  58. if ($items[$item->id] ?? false){
  59. foreach ($columns as $column){
  60. if (!array_key_exists($column,$items[$item->id]))$items[$item->id][$column] = $item[$column];
  61. }
  62. $obj = $items[$item->id];
  63. unset($items[$item->id]);
  64. }else{
  65. /** @var OwnerPriceDirectLogisticCar $item */
  66. $obj = $item->toArray();
  67. unset($obj["id"]);
  68. }
  69. $obj["owner_price_direct_logistic_id"] = $copyModel->id;
  70. $insert[] = $obj;
  71. }
  72. }else{
  73. foreach ($items as $item){
  74. $item["owner_price_direct_logistic_id"] = $copyModel->id;
  75. $insert[] = $item;
  76. }
  77. }
  78. if ($insert)OwnerPriceDirectLogisticCar::query()->insert($insert);
  79. return $copyModel;
  80. }
  81. /**
  82. * 审核或恢复目标集
  83. *
  84. * @param bool $isAudit
  85. * @param integer|null|array $ownerId
  86. * @param integer|null|array $ids
  87. */
  88. public function auditOrRecover($isAudit = true, $ownerId = null, $ids = null)
  89. {
  90. if (!$ownerId && !$ids)return;
  91. $result = app(QueryService::class)->priceModelAuditOrRecoverQuery($isAudit,OwnerPriceDirectLogistic::query(),$ownerId,$ids);
  92. if ($result["delete"])$this->destroy($result["delete"]);
  93. if ($result["update"])OwnerPriceDirectLogistic::query()->whereIn("id",$result["update"])->update(["operation"=>null,"target_id"=>null]);
  94. }
  95. public function destroy($id)
  96. {
  97. if (!is_array($id))$id = [$id];
  98. OwnerPriceDirectLogisticCar::query()->whereIn("owner_price_direct_logistic_id",$id)->delete();
  99. DB::table("owner_price_direct_logistic_owner")->whereIn("owner_price_direct_logistic_id",$id)->delete();
  100. return OwnerPriceDirectLogistic::destroy($id);
  101. }
  102. public function find($id)
  103. {
  104. return OwnerPriceDirectLogistic::query()->find($id);
  105. }
  106. public function update(array $params, array $values)
  107. {
  108. $query = OwnerPriceDirectLogistic::query();
  109. foreach ($params as $column=>$param){
  110. $query->where($column,$param);
  111. }
  112. return $query->update($values);
  113. }
  114. public function updateDetail(array $params, array $values)
  115. {
  116. $query = OwnerPriceDirectLogisticCar::query();
  117. foreach ($params as $column => $param){
  118. $query->where($column,$param);
  119. }
  120. return $query->update($values);
  121. }
  122. public function isExistDetail(array $params)
  123. {
  124. $query = OwnerPriceDirectLogisticCar::query();
  125. foreach ($params as $column => $param){
  126. $query->where($column,$param);
  127. }
  128. return $query->count();
  129. }
  130. public function createDetail(array $params)
  131. {
  132. return OwnerPriceDirectLogisticCar::query()->create($params);
  133. }
  134. public function destroyDetail($id)
  135. {
  136. return OwnerPriceDirectLogisticCar::destroy($id);
  137. }
  138. public function getExistOwnerName($owner_id, $id) :array
  139. {
  140. if (!is_array($owner_id))$owner_id = [$owner_id];
  141. $owners = Owner::query()->withCount(["ownerPriceDirectLogistics"=>function($query)use($id){
  142. if ($id)$query->where("id","!=",$id);
  143. }])->whereIn("id",$owner_id)->get();
  144. $arr = [];
  145. foreach ($owners as $owner){
  146. if ($owner->owner_price_direct_logistics_count > 0)$arr[] = $owner->name;
  147. }
  148. return $arr;
  149. }
  150. /**
  151. *
  152. * @param double $amount
  153. * @param integer $owner_id
  154. * @param integer $car_id
  155. * @return array
  156. */
  157. public function matching($amount, $owner_id, $car_id)
  158. {
  159. $model = OwnerPriceDirectLogistic::query()->with(["details"=>function($query)use($car_id){
  160. /** @var Builder $query */
  161. $query->where("car_type_id",$car_id);
  162. }])->whereHas("owners",function ($query)use($owner_id){
  163. /** @var Builder $query */
  164. $query->where("id",$owner_id);
  165. })->where(function(Builder $query){
  166. $query->whereNull("operation")->orWhere("operation","");
  167. })->first();
  168. if (!$model || !$model->details)return array(null,null);
  169. if ($amount < $model->base_km)$amount = $model->base_km;
  170. $initialMoney = $model->details[0]->base_fee;
  171. $amount -= $model->base_km;
  172. $money = ($amount*$model->details[0]->additional_fee)+$initialMoney;
  173. if ($model->tax_rate_id && $model->taxRate){
  174. $taxFee = $money*($model->taxRate->value/100);
  175. }else{
  176. /** @var Owner|\stdClass $owner */
  177. $owner = Owner::query()->with("taxRate")
  178. ->whereHas("taxRate")->find($owner_id);
  179. if ($owner)$taxFee = $money*($owner->taxRate->value/100);
  180. else $taxFee = null;
  181. }
  182. return array($money,$taxFee);
  183. }
  184. }