GhsDeliveryOrderClass.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <?php
  2. namespace bizGhs\express\classes;
  3. use bizGhs\base\classes\BaseClass;
  4. use bizGhs\order\classes\OrderClass;
  5. use common\components\delivery\services\DispatchService;
  6. use common\components\dict;
  7. use common\components\push;
  8. use mikemadisonweb\rabbitmq\components\ConsumerInterface;
  9. use common\components\noticeUtil;
  10. use Yii;
  11. class GhsDeliveryOrderClass extends BaseClass
  12. {
  13. //public static $baseFile = '\bizGhs\express\models\GhsDelivery';
  14. public static $baseFile = GhsDeliveryOrderAR::class;
  15. public static function payAfter($order)
  16. {
  17. if (empty($order)) {
  18. return false;
  19. }
  20. if ($order->sendType != 2) {
  21. return false;
  22. }
  23. if ($order->payStatus != 1) {
  24. return false;
  25. }
  26. $orderId = $order->id;
  27. $shopId = $order->shopId;
  28. try {
  29. $cacheKey = 'ghs_pt_cacheData:' . $orderId;
  30. $cachedData = Yii::$app->redis->get($cacheKey);
  31. if (!empty($cachedData)) {
  32. noticeUtil::push('批发,付款成功之后要发跑腿的订单:' . $orderId, '15280215347');
  33. $cachedData = json_decode($cachedData, true);
  34. $data = [
  35. 'orderId' => $orderId,
  36. 'shopId' => $shopId,
  37. 'mainId' => $order->mainId,
  38. 'platform' => $cachedData['deliveryPlatform'],
  39. 'ip' => '127.0.0.1',
  40. 'deliveryPrice' => $cachedData['deliveryPrice'],
  41. 'deliveryDistance' => $cachedData['deliveryDistance'],
  42. 'deliveryBracketContent' => $cachedData['deliveryBracketContent'],
  43. 'pickupTime' => $cachedData['pickupTime'],
  44. 'allParams' => $cachedData['deliveryAllParams'],
  45. // ----------
  46. 'weight' => $cachedData['weight'] ?? 1,
  47. 'remark' => $cachedData['deliveryRemark'] ?? '',
  48. 'price' => $order->actPrice,
  49. 'consumerType' => '扫码付款'
  50. ];
  51. self::askCreateOrder($data);
  52. } else {
  53. //noticeUtil::push("批发-缓存数据不存在或已过期,cacheKey:{$cacheKey}");
  54. // 发送 app 通知
  55. // $title = '发跑腿失败';
  56. // $msg = "批发-缓存数据不存在或已过期,cacheKey:{$cacheKey}";
  57. // $allDevices = \bizGhs\device\classes\GhsDeviceClass::getAllByCondition(['shopId' => $shopId]);
  58. // $cids = array_column($allDevices, 'clientId');
  59. // $push = new push('ghs', push::MSG_TYPE_WORK);
  60. // $push->pushByCloud($cids, $title, $msg, []);
  61. return false;
  62. }
  63. } catch (\Exception $e) {
  64. $msg = $e->getMessage();
  65. $remind = "批发,付款成功之后要发跑腿的订单,报错 {$orderId} {$msg}";
  66. noticeUtil::push($remind);
  67. return false;
  68. }
  69. }
  70. //发跑腿准备数据
  71. public static function prepareAskData($post, $order)
  72. {
  73. try {
  74. $callErrand = $post['callErrand']; // 是否立即发跑腿 1:是 0:否
  75. $saleId = $order->id;
  76. if ($callErrand == 1) { // 是否立即发跑腿 1:是 0:否
  77. $hasPay = $post['hasPay'] ?? dict::getDict('hasPay', 'unPay'); //收款方式 0:扫码付 1:线下(已付款) 2:赊账(欠款)3:现金 4:余额
  78. if ($hasPay == dict::getDict('hasPay', 'unPay')) { // 0:扫码付
  79. $deliveryPlatform = $post['deliveryPlatform'];
  80. $expireTime = 0;
  81. if ($deliveryPlatform == 'shansong') {
  82. $expireTime = 1800 - 60;
  83. } elseif ($deliveryPlatform == 'didi') {
  84. $expireTime = 7200;
  85. } else {
  86. $expireTime = 7200; // 默认缓存时长2小时
  87. }
  88. // 把跑腿数据缓存到 redis 中,等待付款成功后,再发跑腿订单
  89. $cacheKey = 'ghs_pt_cacheData:' . $saleId;
  90. Yii::$app->redis->set($cacheKey, json_encode($post, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
  91. Yii::$app->redis->expire($cacheKey, $expireTime);
  92. } else {
  93. $sendType = $post['sendType'];
  94. if ($order->payStatus == 1 && $sendType == dict::getDict('sendType', 'thirdSend')) {
  95. $platform = $post['deliveryPlatform'];
  96. $deliveryPrice = $post['deliveryPrice'];
  97. $deliveryDistance = $post['deliveryDistance'];
  98. $deliveryBracketContent = $post['deliveryBracketContent'];
  99. $pickupTime = $post['pickupTime'];
  100. $allParams = $post['deliveryAllParams'];
  101. $data = [
  102. 'orderId' => $saleId,
  103. 'shopId' => $order->shopId,
  104. 'mainId' => $order->mainId,
  105. 'platform' => $platform,
  106. 'ip' => Yii::$app->request->userIP,
  107. 'deliveryPrice' => $deliveryPrice,
  108. 'deliveryDistance' => $deliveryDistance,
  109. 'deliveryBracketContent' => $deliveryBracketContent,
  110. 'pickupTime' => $pickupTime,
  111. 'allParams' => $allParams,
  112. // ----------
  113. 'weight' => $post['weight'] ?? 1,
  114. 'remark' => $post['deliveryRemark'] ?? '',
  115. 'price' => $order->actPrice,
  116. 'consumerType' => '已付款'
  117. ];
  118. self::askCreateOrder($data);
  119. }
  120. }
  121. }
  122. } catch (\Exception $e) {
  123. $msg = $e->getMessage();
  124. $remind = "批发,跑腿下单,准备数据报错 {$saleId} {$msg}";
  125. noticeUtil::push($remind);
  126. }
  127. }
  128. //生产者创建下单指令逻辑
  129. public static function askCreateOrder($data)
  130. {
  131. try {
  132. $orderId = $data['orderId'];
  133. $allParams = $data['allParams'];
  134. $producer = Yii::$app->rabbitmq->getProducer('ptProducer');
  135. $msg = serialize([
  136. 'type' => 'ghs_pt_create_order',
  137. 'orderId' => $orderId,
  138. 'mainId' => $data['mainId'],
  139. 'shopId' => $data['shopId'],
  140. 'pickupTime' => $data['pickupTime'],
  141. 'ip' => $data['ip'],
  142. 'allParams' => $allParams,
  143. 'weight' => $data['weight'],
  144. 'remark' => $data['remark'],
  145. 'price' => $data['price']
  146. ]);
  147. Yii::info('ghs askCreateOrder == ' . $msg);
  148. $producer->publish($msg, 'ptExchange', 'ptRoute', ['delivery_mode' => 2, 'content_type' => 'application/octet-stream']);
  149. } catch (\Exception $e) {
  150. $msg = $e->getMessage();
  151. $remind = "批发,跑腿下单,生产消息端报错 {$orderId} {$msg}";
  152. noticeUtil::push($remind);
  153. }
  154. }
  155. //消费者消费下单指令逻辑
  156. public static function beginCreateOrder($data)
  157. {
  158. try {
  159. //批发,进行下单流程处理
  160. $dataStr = json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
  161. //noticeUtil::push('ghs 已消费跑腿订单 -- ' . $dataStr);
  162. Yii::info('ghs消费者收到下单指令 -- ' . $dataStr);
  163. $orderId = $data['orderId']; // xhGhsOrder.id
  164. $params = $data['allParams'];
  165. //noticeUtil::push('params == ' . json_encode($params, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
  166. $platform = $params['en_name'];// 平台英文名,$post['platform'] 是中文
  167. $msg = '';
  168. if (empty($platform)) {
  169. Yii::error('platform为空');
  170. $msg = 'platform参数为空';
  171. }
  172. $pickupTime = $data['pickupTime'];
  173. $mainId = $data['mainId'];
  174. $shopId = $data['shopId'];
  175. $params['mainId'] = $mainId;
  176. $params['ip'] = $data['ip'];
  177. $params['weight'] = $data['weight'];
  178. $params['remark'] = $data['remark'] ?? '';
  179. $params['total_price_fen'] = $data['price'];
  180. $params['pickupTime'] = $pickupTime;
  181. $ghsOrder = OrderClass::getById($orderId, true);
  182. if (empty($ghsOrder)) {
  183. $msg = "没有找到订单ghs-orderId={$orderId}";
  184. }
  185. if ($ghsOrder->mainId != $mainId) {
  186. $msg = "ghs-orderId={$orderId}不是mainId={$mainId}的订单";
  187. }
  188. if (!in_array($ghsOrder['sendStatus'], [-4, -2, -1])) {
  189. $msg = "订单ghs-orderId={$orderId}已经发过跑腿";
  190. }
  191. if ($msg != '') {
  192. Yii::error($msg);
  193. noticeUtil::push($msg);
  194. return ConsumerInterface::MSG_REJECT;
  195. }
  196. $ds = new DispatchService($mainId, $platform);
  197. $ret = $ds->createOrder($ghsOrder, 'xhGhsOrder', $shopId, $params);
  198. if (isset($ret['code']) && $ret['code'] == 0) {
  199. $gdo = GhsDeliveryOrderClass::getByCondition(['mainId' => $mainId, 'orderId' => $ret['data']['orderId'], 'orderStatus!=' => -1], true);
  200. if (empty($gdo)) {
  201. $data = [
  202. 'mainId' => $mainId,
  203. 'shopId' => $shopId,
  204. 'deliveryId' => $ret['platform'],
  205. 'ghsOrderId' => $orderId,
  206. 'orderId' => $ret['data']['orderId'],
  207. 'distance' => $ret['data']['distance'],
  208. 'fee' => $ret['data']['fee'],
  209. 'orderStatus' => 0
  210. ];
  211. // createdAt 中保存了滴滴跑腿订单的后半截数据
  212. if ($platform == 'didi') {
  213. $data['createdAt'] = date('Y-m-d H:i:s', $ret['data']['timeStamp']);
  214. }
  215. GhsDeliveryOrderClass::add($data);
  216. } else {
  217. Yii::info('重复创建订单');
  218. noticeUtil::push('重复创建订单: ' . $ret['data']['orderId']);
  219. $gdo->orderId = $ret['data']['orderId'];
  220. $gdo->deliveryId = $ret['platform'];
  221. $gdo->distance = $ret['data']['distance'];
  222. $gdo->fee = $ret['data']['fee'];
  223. $gdo->orderStatus = 0;
  224. $gdo->save();
  225. }
  226. //更新订单
  227. $ghsOrder->sendDistance = $ret['data']['distance'];
  228. $ghsOrder->sendStatus = 0;
  229. $ghsOrder->deliveryId = $ret['platform'];
  230. $ghsOrder->sendType = 2;// 2指跑腿
  231. $ghsOrder->save();
  232. //发货
  233. $params = ['fhType' => 0, 'fhWl' => '', 'fhWlNo' => '', 'staffId' => 0, 'staffName' => ''];
  234. OrderClass::orderSend($ghsOrder, $params);
  235. if (empty($gdo)) {
  236. noticeUtil::push("ghs-orderId={$orderId}, 创建跑腿成功--" . json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
  237. } else {
  238. noticeUtil::push("ghs-orderId={$orderId}, 创建跑腿成功--" . json_encode($gdo->toArray(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
  239. }
  240. return ConsumerInterface::MSG_ACK;
  241. } else {
  242. if (isset($ret['msg'])) {
  243. Yii::error("ghs-orderId={$orderId}, 创建跑腿失败:" . $ret['msg']);
  244. noticeUtil::push("批发订单号:{$orderId}, 原因:" . $ret['msg']);
  245. self::sendAppNotice($shopId, $orderId, "订单号:{$orderId},原因:" . $ret['msg']); // 发 App 通知
  246. return ConsumerInterface::MSG_REJECT;
  247. }
  248. Yii::error("ghs-orderId={$orderId}, 创建跑腿失败");
  249. noticeUtil::push("批发订单号:{$orderId}, shopId={$shopId}, 创建跑腿失败");
  250. self::sendAppNotice($shopId, $orderId, '创建跑腿失败'); // 发 App 通知
  251. return ConsumerInterface::MSG_REJECT;
  252. }
  253. } catch (\Exception $e) {
  254. $orderId = $data['orderId'];
  255. Yii::error("ghs-orderId={$orderId}, 创建跑腿失败:" . $e->getMessage());
  256. noticeUtil::push("订单号:{$orderId}, 原因:" . $e->getMessage());
  257. $shopId = $data['shopId'];
  258. // 发 App 通知
  259. self::sendAppNotice($shopId, $orderId, "订单号:{$orderId},原因:" . $e->getMessage());
  260. return ConsumerInterface::MSG_REJECT;
  261. }
  262. }
  263. // 发 App 通知
  264. public static function sendAppNotice($shopId, $orderId, $msg)
  265. {
  266. $title = '发跑腿失败';
  267. $msg = "批发订单创建跑腿失败," . $msg;
  268. $allDevices = \bizGhs\device\classes\GhsDeviceClass::getAllByCondition(['shopId' => $shopId, 'status' => 1]);
  269. $cids = array_column($allDevices, 'clientId');
  270. $payload = [
  271. "page" => "pagesOrder/detail",
  272. "params" => ["id" => $orderId]
  273. ];
  274. $push = new push('ghs', push::MSG_TYPE_WORK);
  275. $push->pushByCloud($cids, $title, $msg, $payload);
  276. }
  277. }
  278. // 在同文件末尾追加一个内联的 AR 类
  279. class GhsDeliveryOrderAR extends \bizGhs\base\models\Base
  280. {
  281. public static function tableName()
  282. {
  283. return 'xhGhsDeliveryOrder';
  284. }
  285. }
  286. // 可选:为了兼容历史直接引用 models 的代码(如果存在),做一个别名映射
  287. if (!class_exists('\bizGhs\express\models\GhsDeliveryOrder')) {
  288. class_alias(\bizGhs\express\classes\GhsDeliveryOrderAR::class, '\bizGhs\express\models\GhsDeliveryOrder');
  289. }