DbOpenService.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace App\Services;
  3. use App\Log;
  4. use App\OracleDOCOrderHeader;
  5. use App\OrderLogistic;
  6. use App\Traits\ServiceAppAop;
  7. use App\Waybill;
  8. use Illuminate\Support\Carbon;
  9. use Illuminate\Support\Facades\DB;
  10. class DbOpenService
  11. {
  12. use ServiceAppAop;
  13. protected $modelClass = DbOpenService::class;
  14. /**
  15. * 创建德邦订单,生成快递单号
  16. * @param array $params
  17. */
  18. public function getDbOrderNo($params = [])
  19. {
  20. //获取系统无快递单号订单信息
  21. $order_info = Waybill::query()
  22. ->with(['owner:id,name,phone_number', 'order', 'order.shop:id,name',
  23. 'order.warehouse:id,province_id,city_id,county_id,address',
  24. 'order.warehouse.province:id,name', 'order.warehouse.city:id,name',
  25. 'order.warehouse.county:id,name', 'deliveryType:id,name'])
  26. ->where('id', '=', $params['id'])
  27. ->first();
  28. //请求德邦API 生成新订单
  29. $model = new OrderLogistic();
  30. $uri = "http://dpsanbox.deppon.com/sandbox-web/dop-standard-ewborder/createOrderNotify.action";
  31. $header = [
  32. 'Content-Type' => 'application/x-www-form-urlencoded;charset=utf-8',
  33. "Accept" => "application/json"
  34. ];
  35. if ($order_info) {
  36. if (empty($order_info->order) || empty($order_info->owner) || empty($order_info->order->shop)) return ['code' => 0, 'msg' => '订单关联信息错误,请核实'];
  37. $res = $model->isExist($order_info->order->id);
  38. if (!$res || $res->status == 0 ?? 0) {
  39. $data = [
  40. 'logisticID' => config('api_logistic.DB.prod.sign') . date("YmdHis") . mt_rand(1000, 9999) . $order_info->order->id,
  41. 'custOrderNo' => $order_info->order->client_code ?? '',
  42. // 'mailNo' => '', // 不传会创建新的 运单号
  43. 'needTraceInfo' => config('api_logistic.DB.prod.needTraceInfo'),
  44. 'companyCode' => config('api_logistic.DB.prod.company_code'),
  45. 'orderType' => $order_info->order_type,
  46. 'transportType' => $order_info->transport_type,
  47. 'customerCode' => config('api_logistic.DB.prod.customer_Code'),
  48. 'sender' => [
  49. 'companyName' => $order_info->order->shop->name ?? '',
  50. 'businessNetworkNo' => '',
  51. 'name' => $order_info->owner->name ?? '',
  52. 'mobile' => $order_info->owner->phone_number ?? '',
  53. 'phone' => $order_info->owner->phone_number ?? '',
  54. 'province' => $order_info->order->warehouse->province->name ?? '',
  55. 'city' => $order_info->order->warehouse->city->name ?? '',
  56. 'country' => $order_info->order->warehouse->county->name ?? '',
  57. 'town' => '',
  58. 'address' => $order_info->order->warehouse->address ?? '',
  59. ],
  60. 'receiver' => [
  61. 'toNetworkNo' => '',
  62. 'name' => $order_info->order->consignee_name ?? '',
  63. 'phone' => $order_info->order->consignee_phone ?? '',
  64. 'mobile' => $order_info->order->consignee_phone ?? '',
  65. 'province' => $order_info->order->province ?? '',
  66. 'city' => $order_info->order->city ?? "",
  67. 'county' => $order_info->order->district ?? '',
  68. 'town' => '',
  69. 'address' => $order_info->order->address ?? '',
  70. 'companyName' => ''
  71. ],
  72. 'packageInfo' => [
  73. 'cargoName' => $order_info->cargo_name ?? '',
  74. 'totalNumber' => $order_info->total_number ?? '',
  75. 'totalWeight' => $order_info->total_weight ?? '',
  76. 'totalVolume' => '',
  77. 'packageService' => $order_info->package_service ?? '',
  78. 'deliveryType' => $order_info->deliveryType->name ?? '',
  79. ],
  80. 'gmtCommit' => date('Y-m-d H:i:s'),
  81. 'payType' => $order_info->pay_type,
  82. 'addServices' => [
  83. 'insuranceValue' => '',
  84. 'codType' => '',
  85. 'reciveLoanAccount' => '',
  86. 'accountName' => '',
  87. 'codValue' => '',
  88. 'backSignBill' => $order_info->back_sign_bill
  89. ],
  90. 'smsNotify' => config('api_logistic.DB.prod.smsNotify'),
  91. 'sendStartTime' => date("Y-m-d H:i:s"),
  92. 'sendEndTime' => $order_info->deliver_at ?? date("Y-m-d H:i:s", strtotime('+1 day')),
  93. 'originalWaybillNumber' => $order_info->wms_bill_number ?? '',
  94. 'remark' => $order_info->dispatch_remark ?? '',
  95. 'isOut' => 'N',
  96. 'passwordSigning' => config('api_logistic.DB.prod.passwordSigning'),
  97. 'isdispatched' => '',
  98. 'ispresaleorder' => '',
  99. 'isCenterDelivery' => '',
  100. 'orderExtendFields' => [
  101. 'value' => '',
  102. 'key' => ''
  103. ]
  104. ];
  105. $param = json_encode($data, true);
  106. $dd["params"] = $param;
  107. $dd["timestamp"] = (integer)getMillisecond();
  108. $dd["digest"] = base64_encode(md5($param . config('api_logistic.DB.prod.app_key') . $dd['timestamp']));
  109. $dd["companyCode"] = config('api_logistic.DB.prod.company_code');
  110. $return = httpPost($uri, $dd, $header);
  111. unset($data);
  112. if (array_key_exists('result', $return)) {
  113. //请求成功 快递单号 $return['mailNo'] 请求编号 $return['uniquerRequestNumber']
  114. //返回日志记录
  115. $id = $params['id'];
  116. $order_no = $order_info->wms_bill_number;
  117. $mail_no = $return['mailNo']??'';
  118. $add_data = [
  119. 'order_id' => $order_info->order->id,
  120. 'status' => $return['result'] == 'true' ? 1 : 0,
  121. 'code' => $mail_no ?? '',
  122. 'uniquer_request_number' => $return['uniquerRequestNumber'],
  123. 'reason' => $return['reason'] ?? '',
  124. 'created_at' => date('Y-m-d H:i:s')
  125. ];
  126. $roe = OrderLogistic::query()->insertGetId($add_data);
  127. if ($roe && $return['result'] == 'true') {
  128. DB::connection('mysql')->transaction(function () use ($id, $order_no, $mail_no) {
  129. $update_data = [
  130. 'type' => '德邦物流',
  131. 'waybill_number' => $mail_no,
  132. 'updated_at' => date('Y-m-d H:i:s')
  133. ];
  134. Waybill::query()->where('id', '=', $id)->update($update_data);
  135. DB::connection('oracle')->transaction(function () use ($order_no, $mail_no) {
  136. OracleDOCOrderHeader::query()->where('orderno', $order_no)->update(['edittime' => Carbon::now(), 'soreference5' => $mail_no]);
  137. });
  138. LogService::log(__METHOD__, "申请快递单号回传WMS", ['orderno' => $order_no, 'soreference5' => $mail_no, 'edittime' => date('Y-m-d H:i:s')]);
  139. });
  140. return ['code' => 1, 'msg' => '申请德邦物流单号成功'];
  141. }
  142. }
  143. LogService::log(__METHOD__, "申请快递单号回传WMS", ['orderno' => $order_no, 'content'=> $return]);
  144. return ['code' => 0, 'msg' => $return['reason']];
  145. }
  146. elseif($res->status == 1 ?? 0 ) return ['code' => 1, 'msg'=> '德邦快递单号已存在'];
  147. }
  148. return ['code' => 0, 'msg' => '订单信息错误,请核实'];
  149. }
  150. /**
  151. * 根据 德邦运单号 获取 订单物流轨迹
  152. * @param array $params
  153. * @return array
  154. */
  155. public function getOrderStatus($params = [])
  156. {
  157. if ( ($params['mailNo']??'') == '') return ['code' => 0 , 'msg' => '德邦运单号不能为空'];
  158. $uri = "http://dpsanbox.deppon.com/sandbox-web/standard-order/newTraceQuery.action";
  159. $header = [
  160. 'Content-Type' => 'application/x-www-form-urlencoded;charset=utf-8',
  161. "Accept" => "application/json"
  162. ];
  163. $data = [
  164. 'mailNo' => $params['mailNo']??''
  165. ];
  166. $param = json_encode($data, true);
  167. $dd["params"] = $param;
  168. $dd["timestamp"] = (integer)getMillisecond();
  169. $dd["digest"] = base64_encode(md5($param . config('api_logistic.DB.prod.app_key') . $dd['timestamp']));
  170. $dd["companyCode"] = config('api_logistic.DB.prod.company_code');
  171. $return = httpPost($uri, $dd, $header);
  172. if (array_key_exists('result', $return) && $return['result'] == 'true' && array_key_exists('resultCode', $return) && $return['resultCode'] == '1000'){
  173. return ['code'=> 1, 'msg'=> '正在加载中。。。', 'data' => $return['responseParam']];
  174. }
  175. return ['code' => 0, 'msg' => '暂无物流信息'];
  176. }
  177. }