WaybillObserver.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace App\Observers;
  3. use App\Waybill;
  4. use Illuminate\Support\Facades\Log;
  5. class WaybillObserver
  6. {
  7. /**
  8. * 监听插入事件
  9. *
  10. * @param Waybill|\stdClass $waybill
  11. * @return void
  12. */
  13. public function created(Waybill $waybill)
  14. {
  15. if(config('app.env')!='production')return;
  16. if ($waybill->type=='德邦物流'){
  17. if ($waybill->carrier_bill)return;
  18. $waybill->load("order");
  19. if (!$waybill->order){
  20. Log::error("德邦单号获取失败",["no"=>$waybill->waybill_number,"info"=>"运单无绑定订单"]);
  21. return;
  22. }
  23. $update = $this->paramDefault($waybill);
  24. $waybill->update($update);
  25. $bill = app('DbOpenService')->getDbOrderNo($waybill);
  26. if (!$bill || $bill["result"]=="false"){
  27. Log::error("德邦单号获取失败",["no"=>$waybill->toArray(),"info"=>$bill]);
  28. return;
  29. }
  30. $waybill->update([
  31. "carrier_bill"=>$bill['mailNo'],
  32. "waybill_number"=>$bill['mailNo'],
  33. "station_no"=>$bill['stationNo'],
  34. "arrived_org_simple_name"=>$bill['arrivedOrgSimpleName'],
  35. "much_higher_delivery"=>$bill['muchHigherDelivery'],
  36. ]);
  37. if (!app("WaybillService")->notifyFlux($waybill)){
  38. Log::error("德邦单号回传FLUX失败",["no"=>$waybill->waybill_number,"info"=>$bill]);
  39. return;
  40. }
  41. }
  42. }
  43. private function paramDefault(&$waybill):array
  44. {
  45. $update = [];
  46. if (!$waybill->order_type){
  47. $update["order_type"] = $waybill->order_type = Waybill::ORDER_TYPE_DEFAULT;
  48. }
  49. if (!$waybill->transport_type){
  50. $update["transport_type"] = $waybill->transport_type = "JZKH";
  51. }
  52. if (!$waybill->cargo_name){
  53. $update["cargo_name"] = $waybill->cargo_name = "补货";
  54. }
  55. if (!$waybill->total_number){
  56. $update["total_number"] = $waybill->total_number = 1;
  57. }
  58. if (!$waybill->total_weight){
  59. $update["total_weight"] = $waybill->total_weight = 1;
  60. }
  61. if (!$waybill->package_service){
  62. $update["package_service"] = $waybill->package_service = '托膜';
  63. }
  64. if (!$waybill->deliveryType){
  65. $update["deliveryType_id"] = $waybill->deliveryType_id = 3;
  66. }
  67. if (!$waybill->pay_type){
  68. $update["pay_type"] = $waybill->pay_type = Waybill::PAY_TYPE_DEFAULT;
  69. }
  70. if (!$waybill->back_sign_bill){
  71. $update["back_sign_bill"] = $waybill->back_sign_bill = Waybill::BACK_SIGN_BILL_DEFAULT;
  72. }
  73. return $update;
  74. }
  75. }