| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312 |
- <?php
- namespace bizGhs\express\classes;
- use bizGhs\base\classes\BaseClass;
- use bizGhs\order\classes\OrderClass;
- use common\components\delivery\services\DispatchService;
- use common\components\dict;
- use common\components\push;
- use mikemadisonweb\rabbitmq\components\ConsumerInterface;
- use common\components\noticeUtil;
- use Yii;
- class GhsDeliveryOrderClass extends BaseClass
- {
- //public static $baseFile = '\bizGhs\express\models\GhsDelivery';
- public static $baseFile = GhsDeliveryOrderAR::class;
- public static function payAfter($order)
- {
- if (empty($order)) {
- return false;
- }
- if ($order->sendType != 2) {
- return false;
- }
- if ($order->payStatus != 1) {
- return false;
- }
- $orderId = $order->id;
- $shopId = $order->shopId;
- try {
- $cacheKey = 'ghs_pt_cacheData:' . $orderId;
- $cachedData = Yii::$app->redis->get($cacheKey);
- if (!empty($cachedData)) {
- noticeUtil::push('批发,付款成功之后要发跑腿的订单:' . $orderId, '15280215347');
- $cachedData = json_decode($cachedData, true);
- $data = [
- 'orderId' => $orderId,
- 'shopId' => $shopId,
- 'mainId' => $order->mainId,
- 'platform' => $cachedData['deliveryPlatform'],
- 'ip' => '127.0.0.1',
- 'deliveryPrice' => $cachedData['deliveryPrice'],
- 'deliveryDistance' => $cachedData['deliveryDistance'],
- 'deliveryBracketContent' => $cachedData['deliveryBracketContent'],
- 'pickupTime' => $cachedData['pickupTime'],
- 'allParams' => $cachedData['deliveryAllParams'],
- // ----------
- 'weight' => $cachedData['weight'] ?? 1,
- 'remark' => $cachedData['deliveryRemark'] ?? '',
- 'price' => $order->actPrice,
- 'consumerType' => '扫码付款'
- ];
- self::askCreateOrder($data);
- } else {
- //noticeUtil::push("批发-缓存数据不存在或已过期,cacheKey:{$cacheKey}");
- // 发送 app 通知
- // $title = '发跑腿失败';
- // $msg = "批发-缓存数据不存在或已过期,cacheKey:{$cacheKey}";
- // $allDevices = \bizGhs\device\classes\GhsDeviceClass::getAllByCondition(['shopId' => $shopId]);
- // $cids = array_column($allDevices, 'clientId');
- // $push = new push('ghs', push::MSG_TYPE_WORK);
- // $push->pushByCloud($cids, $title, $msg, []);
- return false;
- }
- } catch (\Exception $e) {
- $msg = $e->getMessage();
- $remind = "批发,付款成功之后要发跑腿的订单,报错 {$orderId} {$msg}";
- noticeUtil::push($remind);
- return false;
- }
- }
- //发跑腿准备数据
- public static function prepareAskData($post, $order)
- {
- try {
- $callErrand = $post['callErrand']; // 是否立即发跑腿 1:是 0:否
- $saleId = $order->id;
- if ($callErrand == 1) { // 是否立即发跑腿 1:是 0:否
- $hasPay = $post['hasPay'] ?? dict::getDict('hasPay', 'unPay'); //收款方式 0:扫码付 1:线下(已付款) 2:赊账(欠款)3:现金 4:余额
- if ($hasPay == dict::getDict('hasPay', 'unPay')) { // 0:扫码付
- $deliveryPlatform = $post['deliveryPlatform'];
- $expireTime = 0;
- if ($deliveryPlatform == 'shansong') {
- $expireTime = 1800 - 60;
- } elseif ($deliveryPlatform == 'didi') {
- $expireTime = 7200;
- } else {
- $expireTime = 7200; // 默认缓存时长2小时
- }
- // 把跑腿数据缓存到 redis 中,等待付款成功后,再发跑腿订单
- $cacheKey = 'ghs_pt_cacheData:' . $saleId;
- Yii::$app->redis->set($cacheKey, json_encode($post, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
- Yii::$app->redis->expire($cacheKey, $expireTime);
- } else {
- $sendType = $post['sendType'];
- if ($order->payStatus == 1 && $sendType == dict::getDict('sendType', 'thirdSend')) {
- $platform = $post['deliveryPlatform'];
- $deliveryPrice = $post['deliveryPrice'];
- $deliveryDistance = $post['deliveryDistance'];
- $deliveryBracketContent = $post['deliveryBracketContent'];
- $pickupTime = $post['pickupTime'];
- $allParams = $post['deliveryAllParams'];
- $data = [
- 'orderId' => $saleId,
- 'shopId' => $order->shopId,
- 'mainId' => $order->mainId,
- 'platform' => $platform,
- 'ip' => Yii::$app->request->userIP,
- 'deliveryPrice' => $deliveryPrice,
- 'deliveryDistance' => $deliveryDistance,
- 'deliveryBracketContent' => $deliveryBracketContent,
- 'pickupTime' => $pickupTime,
- 'allParams' => $allParams,
- // ----------
- 'weight' => $post['weight'] ?? 1,
- 'remark' => $post['deliveryRemark'] ?? '',
- 'price' => $order->actPrice,
- 'consumerType' => '已付款'
- ];
- self::askCreateOrder($data);
- }
- }
- }
- } catch (\Exception $e) {
- $msg = $e->getMessage();
- $remind = "批发,跑腿下单,准备数据报错 {$saleId} {$msg}";
- noticeUtil::push($remind);
- }
- }
- //生产者创建下单指令逻辑
- public static function askCreateOrder($data)
- {
- try {
- $orderId = $data['orderId'];
- $allParams = $data['allParams'];
- $producer = Yii::$app->rabbitmq->getProducer('ptProducer');
- $msg = serialize([
- 'type' => 'ghs_pt_create_order',
- 'orderId' => $orderId,
- 'mainId' => $data['mainId'],
- 'shopId' => $data['shopId'],
- 'pickupTime' => $data['pickupTime'],
- 'ip' => $data['ip'],
- 'allParams' => $allParams,
- 'weight' => $data['weight'],
- 'remark' => $data['remark'],
- 'price' => $data['price']
- ]);
- Yii::info('ghs askCreateOrder == ' . $msg);
- $producer->publish($msg, 'ptExchange', 'ptRoute', ['delivery_mode' => 2, 'content_type' => 'application/octet-stream']);
- } catch (\Exception $e) {
- $msg = $e->getMessage();
- $remind = "批发,跑腿下单,生产消息端报错 {$orderId} {$msg}";
- noticeUtil::push($remind);
- }
- }
- //消费者消费下单指令逻辑
- public static function beginCreateOrder($data)
- {
- try {
- //批发,进行下单流程处理
- $dataStr = json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
- //noticeUtil::push('ghs 已消费跑腿订单 -- ' . $dataStr);
- Yii::info('ghs消费者收到下单指令 -- ' . $dataStr);
- $orderId = $data['orderId']; // xhGhsOrder.id
- $params = $data['allParams'];
- //noticeUtil::push('params == ' . json_encode($params, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
- $platform = $params['en_name'];// 平台英文名,$post['platform'] 是中文
- $msg = '';
- if (empty($platform)) {
- Yii::error('platform为空');
- $msg = 'platform参数为空';
- }
- $pickupTime = $data['pickupTime'];
- $mainId = $data['mainId'];
- $shopId = $data['shopId'];
- $params['mainId'] = $mainId;
- $params['ip'] = $data['ip'];
- $params['weight'] = $data['weight'];
- $params['remark'] = $data['remark'] ?? '';
- $params['total_price_fen'] = $data['price'];
- $params['pickupTime'] = $pickupTime;
- $ghsOrder = OrderClass::getById($orderId, true);
- if (empty($ghsOrder)) {
- $msg = "没有找到订单ghs-orderId={$orderId}";
- }
- if ($ghsOrder->mainId != $mainId) {
- $msg = "ghs-orderId={$orderId}不是mainId={$mainId}的订单";
- }
- if (!in_array($ghsOrder['sendStatus'], [-4, -2, -1])) {
- $msg = "订单ghs-orderId={$orderId}已经发过跑腿";
- }
- if ($msg != '') {
- Yii::error($msg);
- noticeUtil::push($msg);
- return ConsumerInterface::MSG_REJECT;
- }
- $ds = new DispatchService($mainId, $platform);
- $ret = $ds->createOrder($ghsOrder, 'xhGhsOrder', $shopId, $params);
- if (isset($ret['code']) && $ret['code'] == 0) {
- $gdo = GhsDeliveryOrderClass::getByCondition(['mainId' => $mainId, 'orderId' => $ret['data']['orderId'], 'orderStatus!=' => -1], true);
- if (empty($gdo)) {
- $data = [
- 'mainId' => $mainId,
- 'shopId' => $shopId,
- 'deliveryId' => $ret['platform'],
- 'ghsOrderId' => $orderId,
- 'orderId' => $ret['data']['orderId'],
- 'distance' => $ret['data']['distance'],
- 'fee' => $ret['data']['fee'],
- 'orderStatus' => 0
- ];
- // createdAt 中保存了滴滴跑腿订单的后半截数据
- if ($platform == 'didi') {
- $data['createdAt'] = date('Y-m-d H:i:s', $ret['data']['timeStamp']);
- }
- GhsDeliveryOrderClass::add($data);
- } else {
- Yii::info('重复创建订单');
- noticeUtil::push('重复创建订单: ' . $ret['data']['orderId']);
- $gdo->orderId = $ret['data']['orderId'];
- $gdo->deliveryId = $ret['platform'];
- $gdo->distance = $ret['data']['distance'];
- $gdo->fee = $ret['data']['fee'];
- $gdo->orderStatus = 0;
- $gdo->save();
- }
- //更新订单
- $ghsOrder->sendDistance = $ret['data']['distance'];
- $ghsOrder->sendStatus = 0;
- $ghsOrder->deliveryId = $ret['platform'];
- $ghsOrder->sendType = 2;// 2指跑腿
- $ghsOrder->save();
- //发货
- $params = ['fhType' => 0, 'fhWl' => '', 'fhWlNo' => '', 'staffId' => 0, 'staffName' => ''];
- OrderClass::orderSend($ghsOrder, $params);
- if (empty($gdo)) {
- noticeUtil::push("ghs-orderId={$orderId}, 创建跑腿成功--" . json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
- } else {
- noticeUtil::push("ghs-orderId={$orderId}, 创建跑腿成功--" . json_encode($gdo->toArray(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
- }
- return ConsumerInterface::MSG_ACK;
- } else {
- if (isset($ret['msg'])) {
- Yii::error("ghs-orderId={$orderId}, 创建跑腿失败:" . $ret['msg']);
- noticeUtil::push("批发订单号:{$orderId}, 原因:" . $ret['msg']);
- self::sendAppNotice($shopId, $orderId, "订单号:{$orderId},原因:" . $ret['msg']); // 发 App 通知
- return ConsumerInterface::MSG_REJECT;
- }
- Yii::error("ghs-orderId={$orderId}, 创建跑腿失败");
- noticeUtil::push("批发订单号:{$orderId}, shopId={$shopId}, 创建跑腿失败");
- self::sendAppNotice($shopId, $orderId, '创建跑腿失败'); // 发 App 通知
- return ConsumerInterface::MSG_REJECT;
- }
- } catch (\Exception $e) {
- $orderId = $data['orderId'];
- Yii::error("ghs-orderId={$orderId}, 创建跑腿失败:" . $e->getMessage());
- noticeUtil::push("订单号:{$orderId}, 原因:" . $e->getMessage());
- $shopId = $data['shopId'];
- // 发 App 通知
- self::sendAppNotice($shopId, $orderId, "订单号:{$orderId},原因:" . $e->getMessage());
- return ConsumerInterface::MSG_REJECT;
- }
- }
- // 发 App 通知
- public static function sendAppNotice($shopId, $orderId, $msg)
- {
- $title = '发跑腿失败';
- $msg = "批发订单创建跑腿失败," . $msg;
- $allDevices = \bizGhs\device\classes\GhsDeviceClass::getAllByCondition(['shopId' => $shopId, 'status' => 1]);
- $cids = array_column($allDevices, 'clientId');
- $payload = [
- "page" => "pagesOrder/detail",
- "params" => ["id" => $orderId]
- ];
- $push = new push('ghs', push::MSG_TYPE_WORK);
- $push->pushByCloud($cids, $title, $msg, $payload);
- }
- }
- // 在同文件末尾追加一个内联的 AR 类
- class GhsDeliveryOrderAR extends \bizGhs\base\models\Base
- {
- public static function tableName()
- {
- return 'xhGhsDeliveryOrder';
- }
- }
- // 可选:为了兼容历史直接引用 models 的代码(如果存在),做一个别名映射
- if (!class_exists('\bizGhs\express\models\GhsDeliveryOrder')) {
- class_alias(\bizGhs\express\classes\GhsDeliveryOrderAR::class, '\bizGhs\express\models\GhsDeliveryOrder');
- }
|