CallBackHandler.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. namespace common\components\delivery\platform\dada;
  3. use biz\common\classes\GhsNotifyClass;
  4. use biz\common\classes\HdNotifyClass;
  5. use bizGhs\express\classes\GhsDeliveryOrderClass;
  6. use bizHd\express\classes\HdDeliveryOrderClass;
  7. use bizGhs\order\classes\OrderClass;
  8. use bizHd\order\classes\OrderClass as HdOrderClass;
  9. use bizHd\purchase\classes\PurchaseClass;
  10. use common\components\noticeUtil;
  11. use common\components\util;
  12. use Yii;
  13. /**
  14. * 达达回调处理类
  15. */
  16. class CallBackHandler
  17. {
  18. /**
  19. * 处理达达回调
  20. *
  21. * @param array $data 回调数据
  22. * @return bool 处理结果
  23. */
  24. public function handle($data)
  25. {
  26. try {
  27. // 验证签名
  28. if (!$this->verifySignature($data)) {
  29. Yii::error('达达回调签名验证失败: ' . json_encode($data, JSON_UNESCAPED_UNICODE), 'dada-callback');
  30. return false;
  31. }
  32. // 处理订单状态变更
  33. return $this->processOrderCallback($data);
  34. } catch (\Exception $e) {
  35. Yii::error('达达回调处理异常: ' . $e->getMessage() . ' | ' . $e->getTraceAsString(), 'dada-callback');
  36. return false;
  37. }
  38. }
  39. /**
  40. * 处理订单回调
  41. */
  42. private function processOrderCallback($data)
  43. {
  44. $orderSn = $data['order_id']; // 对应 orderSn
  45. $dadaStatus = $data['order_status'];
  46. $clientId = isset($data['client_id']) ? $data['client_id'] : ''; //达达物流订单号
  47. // 查找主订单
  48. $order = OrderClass::getByCondition(['orderSn' => $orderSn], true);
  49. $isGhs = true;
  50. if (!$order) {
  51. $order = HdOrderClass::getByCondition(['orderSn' => $orderSn], true);
  52. $isGhs = false;
  53. }
  54. if (!$order) {
  55. Yii::error('达达回调未找到对应订单: ' . $orderSn, 'dada-callback');
  56. return false;
  57. }
  58. // 查找配送订单
  59. $deliveryOrder = $this->getDeliveryOrder($order, $isGhs);
  60. if (!$deliveryOrder) {
  61. Yii::error('达达配送订单不存在: orderSn=' . $orderSn . ' ghsOrderId=' . $order->id, 'dada-callback');
  62. return false;
  63. }
  64. // 更新 client_id (达达物流订单号)
  65. if (!empty($clientId) && empty($deliveryOrder->orderId)) {
  66. // $deliveryOrder->orderId = $clientId;
  67. // $deliveryOrder->save();
  68. }
  69. $sendStatus = $this->convertOrderSendStatus($dadaStatus);
  70. $orderStatus = $this->convertOrderStatus($dadaStatus);
  71. if ($sendStatus === null) {
  72. return true;
  73. }
  74. // 更新配送订单状态
  75. $deliveryOrder->orderStatus = $sendStatus;
  76. $deliveryOrder->save();
  77. // 更新主订单
  78. if ($isGhs) {
  79. util::checkRepeatCommit('ghs_pt_' . $order->id . '_' . $sendStatus, 2);
  80. //$order = OrderClass::getById($order->id, true, 'id,sendType,sendStatus,deliveryId,purchaseId,customId,status');
  81. if ($order) {
  82. // 已送达
  83. if ($sendStatus == 5) {
  84. OrderClass::confirmReach($order, ['staffId' => 0, 'staffName' => '']);
  85. }
  86. // 取消/异常 (且当前状态未取消)
  87. if (!in_array($order->sendStatus, [-2, -1]) && in_array($sendStatus, [-1, -2])) {
  88. OrderClass::cancelOrderSend($order, ['staffId' => 0, 'staffName' => '']);
  89. $event = $this->cancelFrom($sendStatus, $data['cancel_from'], $dadaStatus);
  90. GhsNotifyClass::ptErrorNotify($order->id, $event);
  91. }
  92. $cg = PurchaseClass::getById($order->purchaseId, true, 'id,sendStatus');
  93. if ($cg) {
  94. $cg->sendStatus = $sendStatus;
  95. $cg->save();
  96. }
  97. $order->sendStatus = $sendStatus;
  98. if ($orderStatus !== null) {
  99. $order->status = $orderStatus;
  100. }
  101. $order->save();
  102. }
  103. } else {
  104. // 零售订单处理
  105. util::checkRepeatCommit('hd_pt_' . $order->id . '_' . $sendStatus, 2);
  106. //$order = HdOrderClass::getById($order->id, true, 'id,sendType,sendStatus,deliveryId,customId,status');
  107. // 取消/异常 (且当前状态未取消)
  108. if (!in_array($order->sendStatus, [-2, -1]) && in_array($sendStatus, [-1, -2])) {
  109. $event = $this->cancelFrom($sendStatus, $data['cancel_from'], $dadaStatus);
  110. HdNotifyClass::ptErrorNotify($order->id, $event);
  111. }
  112. if ($order) {
  113. $order->sendStatus = $sendStatus;
  114. if ($orderStatus !== null) {
  115. $order->status = $orderStatus;
  116. }
  117. $order->save();
  118. }
  119. }
  120. return true;
  121. }
  122. /**
  123. * 验证签名
  124. */
  125. private function verifySignature($data)
  126. {
  127. if (!isset($data['signature'])) {
  128. return false;
  129. }
  130. // client_id, order_id, update_time
  131. // 值为 null 时转为空字符串
  132. $clientId = isset($data['client_id']) ? $data['client_id'] : '';
  133. $orderId = isset($data['order_id']) ? $data['order_id'] : '';
  134. $updateTime = isset($data['update_time']) ? $data['update_time'] : '';
  135. $list = [$clientId, $orderId, $updateTime];
  136. sort($list, SORT_STRING);
  137. $str = implode('', $list);
  138. $sign = md5($str);
  139. return $sign === $data['signature'];
  140. }
  141. private function getDeliveryOrder($order, $isGhs)
  142. {
  143. if ($isGhs) {
  144. $gdos = GhsDeliveryOrderClass::getAllByCondition([
  145. 'deliveryId' => 'dada',
  146. 'ghsOrderId' => $order->id,
  147. 'orderStatus!=' => -1
  148. ], 'id DESC', '*', null, true);
  149. if(count($gdos)>=2){
  150. noticeUtil::push("xhGhsDeliveryOrder ghsOrderId={$order->id} 有两笔以上是在配送中");
  151. $gdo = $gdos[0];
  152. }elseif(count($gdos)==1){
  153. $gdo = $gdos[0];
  154. }else{
  155. $gdo = null;
  156. }
  157. return $gdo;
  158. } else {
  159. $hdos = HdDeliveryOrderClass::getAllByCondition([
  160. 'deliveryId' => 'dada',
  161. 'hdOrderId' => $order->id,
  162. 'orderStatus!=' => -1
  163. ], 'id DESC', '*', null, true);
  164. if(count($hdos)>=2){
  165. noticeUtil::push("xhHdDeliveryOrder hdOrderId={$order->id} 有两笔以上是在配送中");
  166. $hdo = $hdos[0];
  167. }elseif(count($hdos)==1){
  168. $hdo = $hdos[0];
  169. }else{
  170. $hdo = null;
  171. }
  172. return $hdo;
  173. }
  174. }
  175. private function convertOrderSendStatus($dadaStatus)
  176. {
  177. $map = [
  178. 1 => 2, // 待接单 -> 派单中
  179. 2 => 20, // 待取货 -> 已分配骑手
  180. 3 => 4, // 配送中 -> 配送中
  181. 4 => 5, // 已完成 -> 已送达
  182. 5 => -1, // 已取消 -> 已取消
  183. 8 => 2, // 已追加待接单 -> 派单中
  184. 9 => -2, // 妥投异常之物品返回中=9 -> 异常
  185. 10 => -2, // 妥投异常之物品返回完成=10 -> 异常
  186. 100 => 30,// 骑士到店 -> 骑手已到店
  187. 1000 => -2,// 创建失败 -> 异常
  188. ];
  189. return $map[$dadaStatus] ?? null;
  190. }
  191. private function convertOrderStatus($dadaStatus)
  192. {
  193. $map = [
  194. 1 => 3, // 待接单 -> 配送中
  195. 2 => 3, // 待取货 -> 配送中
  196. 3 => 3, // 配送中 -> 配送中
  197. 4 => 4, // 已完成 -> 已完成
  198. 100 => 3, // 到店 -> 配送中
  199. ];
  200. return $map[$dadaStatus] ?? null;
  201. }
  202. private function cancelFrom($sendStatus, $from, $dadaStatus)
  203. {
  204. if($sendStatus == -1){
  205. switch($from){
  206. case 1:
  207. $which = '达达配送员取消';
  208. break;
  209. case 2:
  210. $which = '商家主动取消';
  211. break;
  212. case 3:
  213. $which = '系统或客服取消';
  214. break;
  215. default:
  216. $which = '未知';
  217. }
  218. return $which;
  219. }
  220. if($sendStatus == -2){
  221. $reasons = [
  222. 9 => '妥投异常之物品返回中',
  223. 10 => '妥投异常之物品返回完成',
  224. 1000 => '创建达达运单失败',
  225. ];
  226. return $reasons[$dadaStatus] ?? '未知';
  227. }
  228. }
  229. }