OwnerPriceDirectLogisticService.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. $query = "IN (";
  100. for ($i=0;$i<count($id)-1;$i++)$query .= "{$id[$i]},";
  101. $query .= "{$id[count($id)-1]})";
  102. $sql = "SELECT * FROM owner_price_direct_logistic_owner WHERE owner_price_direct_logistic_id {$query}";
  103. $owners = array_column(DB::select(DB::raw($sql)),"owner_id");
  104. DB::table("owner_price_direct_logistic_owner")->whereIn("owner_price_direct_logistic_id",$id)->delete();
  105. app("OwnerService")->refreshRelevance($owners,4,true);
  106. return OwnerPriceDirectLogistic::destroy($id);
  107. }
  108. public function find($id)
  109. {
  110. return OwnerPriceDirectLogistic::query()->find($id);
  111. }
  112. public function update(array $params, array $values)
  113. {
  114. $query = OwnerPriceDirectLogistic::query();
  115. foreach ($params as $column=>$param){
  116. $query->where($column,$param);
  117. }
  118. return $query->update($values);
  119. }
  120. public function updateDetail(array $params, array $values)
  121. {
  122. $query = OwnerPriceDirectLogisticCar::query();
  123. foreach ($params as $column => $param){
  124. $query->where($column,$param);
  125. }
  126. return $query->update($values);
  127. }
  128. public function isExistDetail(array $params)
  129. {
  130. $query = OwnerPriceDirectLogisticCar::query();
  131. foreach ($params as $column => $param){
  132. $query->where($column,$param);
  133. }
  134. return $query->count();
  135. }
  136. public function createDetail(array $params)
  137. {
  138. return OwnerPriceDirectLogisticCar::query()->create($params);
  139. }
  140. public function destroyDetail($id)
  141. {
  142. return OwnerPriceDirectLogisticCar::destroy($id);
  143. }
  144. public function getExistOwnerName($owner_id, $id) :array
  145. {
  146. if (!is_array($owner_id))$owner_id = [$owner_id];
  147. $owners = Owner::query()->withCount(["ownerPriceDirectLogistics"=>function($query)use($id){
  148. if ($id)$query->where("id","!=",$id);
  149. }])->whereIn("id",$owner_id)->get();
  150. $arr = [];
  151. foreach ($owners as $owner){
  152. if ($owner->owner_price_direct_logistics_count > 0)$arr[] = $owner->name;
  153. }
  154. return $arr;
  155. }
  156. /**
  157. * CODE: -1:未找到计费模型
  158. *
  159. * @param double $amount
  160. * @param integer $owner_id
  161. * @param integer $car_id
  162. * @return double
  163. */
  164. public function matching($amount, $owner_id, $car_id)
  165. {
  166. $model = OwnerPriceDirectLogistic::query()->with(["details"=>function($query)use($car_id){
  167. /** @var Builder $query */
  168. $query->where("car_type_id",$car_id);
  169. }])->whereHas("owners",function ($query)use($owner_id){
  170. /** @var Builder $query */
  171. $query->where("id",$owner_id);
  172. })->where(function(Builder $query){
  173. $query->whereNull("operation")->orWhere("operation","");
  174. })->first();
  175. if (!$model || !$model->details)return -1;
  176. if ($amount < $model->base_km)$amount = $model->base_km;
  177. $initialMoney = $model->details[0]->base_fee;
  178. $amount -= $model->base_km;
  179. return ($amount*$model->details[0]->additional_fee)+$initialMoney;
  180. }
  181. }