WaybillObserver.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. $bill = app('DbOpenService')->getDbOrderNo($waybill);
  25. if (!$bill || $bill["result"]=="false"){
  26. Log::error("德邦单号获取失败",["no"=>$waybill->waybill_number,"info"=>$bill]);
  27. return;
  28. }
  29. $update["carrier_bill"] = $bill['mailNo'];
  30. $update["waybill_number"] = $bill['mailNo'];
  31. $update["station_no"] = $bill['stationNo'];
  32. $update["arrived_org_simple_name"] = $bill['arrivedOrgSimpleName'];
  33. $update["much_higher_delivery"] = $bill['muchHigherDelivery'];
  34. $waybill->update($update);
  35. if (!app("WaybillService")->notifyFlux($waybill)){
  36. Log::error("德邦单号回传FLUX失败",["no"=>$waybill->waybill_number,"info"=>$bill]);
  37. return;
  38. }
  39. }
  40. }
  41. private function paramDefault(&$waybill):array
  42. {
  43. $update = [];
  44. if (!$waybill->order_type){
  45. $update["order_type"] = $waybill->order_type = Waybill::ORDER_TYPE_DEFAULT;
  46. }
  47. if (!$waybill->transport_type){
  48. $update["transport_type"] = $waybill->transport_type = "JZKH";
  49. }
  50. if (!$waybill->cargo_name){
  51. $update["cargo_name"] = $waybill->cargo_name = "补货";
  52. }
  53. if (!$waybill->total_number){
  54. $update["total_number"] = $waybill->total_number = 1;
  55. }
  56. if (!$waybill->total_weight){
  57. $update["total_weight"] = $waybill->total_weight = 1;
  58. }
  59. if (!$waybill->package_service){
  60. $update["package_service"] = $waybill->package_service = '托膜';
  61. }
  62. if (!$waybill->deliveryType){
  63. $update["deliveryType_id"] = $waybill->deliveryType_id = 3;
  64. }
  65. if (!$waybill->pay_type){
  66. $update["pay_type"] = $waybill->pay_type = Waybill::PAY_TYPE_DEFAULT;
  67. }
  68. if (!$waybill->back_sign_bill){
  69. $update["back_sign_bill"] = $waybill->back_sign_bill = Waybill::BACK_SIGN_BILL_DEFAULT;
  70. }
  71. return $update;
  72. }
  73. }