DeliveryAppointmentEvent.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace App\Events;
  3. use App\DeliveryAppointment;
  4. use App\DeliveryAppointmentCar;
  5. use Illuminate\Broadcasting\Channel;
  6. use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
  7. use Illuminate\Database\Eloquent\Builder;
  8. use Illuminate\Foundation\Events\Dispatchable;
  9. use Illuminate\Queue\SerializesModels;
  10. class DeliveryAppointmentEvent implements ShouldBroadcastNow
  11. {
  12. use Dispatchable, SerializesModels;
  13. public $delivery;
  14. /**
  15. * Create a new event instance.
  16. *
  17. * @param DeliveryAppointmentCar|\stdClass $delivery
  18. *
  19. * @return void
  20. */
  21. public function __construct(DeliveryAppointmentCar $delivery)
  22. {
  23. $delivery->load(["deliveryAppointment"=>function($query){
  24. /** @var Builder $query */
  25. $query->withCount("cars");
  26. }]);
  27. $owner = $delivery->deliveryAppointment->owner->name ?? "";
  28. $len = mb_strlen($owner);
  29. $ownerName = "";
  30. for($i=0;$i<$len-1;$i++)$ownerName .= "*";
  31. $ownerName .= mb_substr($owner,$len-1,1);
  32. $count = $delivery->deliveryAppointment->cars_count ?? 0;
  33. $delivery->warehouse = $delivery->deliveryAppointment->warehouse_id ?? "";
  34. $delivery->cubic_meter = isset($delivery->deliveryAppointment->cubic_meter) && $delivery->deliveryAppointment->cubic_meter>0 ? ($count>1 ? $delivery->deliveryAppointment->cubic_meter."/".$count : $delivery->deliveryAppointment->cubic_meter) : "";
  35. $delivery->tonne = isset($delivery->deliveryAppointment->tonne) && $delivery->deliveryAppointment->tonne>0 ? ($count>1 ? $delivery->deliveryAppointment->tonne."/".$count : $delivery->deliveryAppointment->tonne) : "";
  36. $delivery->license_plate_number = $delivery->license_plate_number ? $delivery->license_plate_number : substr($delivery->appointment_number,0,5)."****".substr($delivery->appointment_number,9,1);
  37. $delivery->owner_name = $ownerName;
  38. $delivery->status = DeliveryAppointmentCar::STATUS[$delivery->status];
  39. $delivery->type = DeliveryAppointment::TYPE[$delivery->deliveryAppointment->type_mark] ?? '';
  40. $delivery->period = isset($delivery->deliveryAppointment->date_period) ? ($delivery->deliveryAppointment->date_period==0 ? '上午' : '下午') : '';
  41. $delivery->delivery_time = $delivery->delivery_time ? substr($delivery->delivery_time,11,5) : '';
  42. $delivery->morrow = $delivery->deliveryAppointment->appointment_date != date('Y-m-d'/*,strtotime("+1 day")*/);
  43. $this->delivery = $delivery->withoutRelations();
  44. }
  45. /**
  46. * Get the channels the event should broadcast on.
  47. *
  48. * @return \Illuminate\Broadcasting\Channel|array
  49. */
  50. public function broadcastOn()
  51. {
  52. return new Channel('delivery');
  53. }
  54. public function broadcastAs()
  55. {
  56. return "car";
  57. }
  58. }