IntraCityController.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. <?php
  2. namespace ghs\controllers;
  3. use bizHd\express\classes\ExpressOrderClass;
  4. use bizHd\express\models\ExpressOrder;
  5. use bizHd\order\classes\OrderGoodsClass;
  6. use bizHd\user\classes\UserClass;
  7. use bizHd\order\classes\OrderItemClass;
  8. use hd\models\IntraCity\StoreFeeForm;
  9. // 以上类来自零售端
  10. use bizGhs\order\classes\OrderClass;
  11. use biz\shop\classes\MainClass;
  12. use biz\shop\classes\ShopClass;
  13. use common\components\noticeUtil;
  14. use common\components\util;
  15. use common\components\IntraCityExpress;
  16. use yii\helpers\Json;
  17. use yii\db\Query;
  18. use Yii;
  19. /**
  20. * 同城配送控制器
  21. *
  22. * 提供同城配送相关的API接口
  23. */
  24. class IntraCityController extends BaseController
  25. {
  26. public $guestAccess = ['callback'];
  27. /**
  28. * 创建门店
  29. * POST /intra-city/create-store
  30. */
  31. public function actionCreateStore()
  32. {
  33. try {
  34. $shop = $this->shop;
  35. $shopName = $shop->shopName ?? '';
  36. $sjName = $shop->merchantName ?? '';
  37. $name = $shopName == '首店' ? $sjName : $sjName . ' ' . $shopName;
  38. $storeData = [
  39. 'out_store_id' => $this->mainId, //自定义门店编号
  40. 'store_name' => $name,
  41. 'address_info' => [
  42. 'province' => $shop->province,
  43. 'city' => $shop->city,
  44. 'area' => $shop->dist,
  45. 'street' => '',
  46. 'house' => $shop->floor ? $shop->address . $shop->floor : $shop->address,
  47. 'lat' => $shop->lat,
  48. 'lng' => $shop->long,
  49. 'phone' => $shop->telephone
  50. ]
  51. ];
  52. // 验证必填参数
  53. $requiredFields = ['out_store_id', 'store_name', 'address_info'];
  54. foreach ($requiredFields as $field) {
  55. if (empty($storeData[$field])) {
  56. util::fail("缺少必填参数:{$field}");
  57. }
  58. }
  59. // 创建门店前,检查是否已经 开通门店权限 (有wxStoreId,即说明开通了)
  60. $shopExt = MainClass::getById($this->mainId, true, 'id, wxStoreId');
  61. // 查询是否存在 wx_store_id,不存在则执行apply
  62. if ($shopExt->wxStoreId == '') {
  63. $re = IntraCityExpress::applyStore();
  64. if (!is_array($re)) {
  65. $re = Json::decode($re, true);
  66. }
  67. if ($re['errcode'] != 0) {
  68. Yii::error("开通门店权限失败:" . $re['errmsg']);
  69. util::fail('开通门店权限失败');
  70. }
  71. }
  72. $result = IntraCityExpress::createStore($storeData);
  73. if ($result['errcode'] === 0) {
  74. // 保存微信门店编号(wx_store_id)
  75. $shopExt->wxStoreId = $result['wx_store_id'];
  76. $shopExt->save();
  77. util::success($result, "门店创建成功");
  78. } else {
  79. Yii::error("门店创建失败:" . $result['errmsg']);
  80. util::fail($result['errmsg']);
  81. }
  82. } catch (\Exception $e) {
  83. Yii::error("门店创建失败:" . $e->getMessage());
  84. util::fail("系统出错");
  85. }
  86. }
  87. /**
  88. * 查询门店创建情况
  89. */
  90. public function actionStore()
  91. {
  92. $shopId = $this->shopId;
  93. // 首先从门店扩展表查询是否已经创建
  94. $shopExt = MainClass::getById($this->mainId, true, 'id, wxStoreId');
  95. if ($shopExt->wxStoreId == '') { // 不存在,则从微信接口查询
  96. $result = IntraCityExpress::getStore($this->mainId);
  97. if ($result['errcode'] === 0) {
  98. $store = $result['store_list'][0];
  99. // 保存 wx_store_id
  100. $shopExt->wxStoreId = $store['wx_store_id'];
  101. $shopExt->save();
  102. util::success($store, "门店查询成功");
  103. } else {
  104. Yii::error("门店查询失败:" . $result['errmsg']);
  105. util::error(-1,'门店查询失败');
  106. }
  107. } else {
  108. $field = 'province, city, dist, lat, long, floor, address';
  109. $shop = ShopClass::getById($shopId, false, $field);
  110. $data = array_merge($shop, $shopExt->toArray());
  111. util::success($data, "门店查询成功");
  112. }
  113. }
  114. /**
  115. * 更新门店
  116. */
  117. public function actionUpdateStore()
  118. {
  119. // 首先从门店扩展表查询是否已经创建
  120. $shopExt = MainClass::getById($this->mainId, false, 'id, wxStoreId');
  121. if ($shopExt['wxStoreId'] == '') {
  122. util::fail('未创建门店,请先创建');
  123. }
  124. $shop = $this->shop;
  125. $shopName = $shop->shopName ?? '';
  126. $sjName = $shop->merchantName ?? '';
  127. $name = $shopName == '首店' ? $sjName : $sjName . ' ' . $shopName;
  128. $storeData = [
  129. 'keys' => ['wx_store_id' => $shopExt['wxStoreId']],
  130. 'content' => [
  131. 'store_name' => $name,
  132. 'address_info' => [
  133. 'province' => $shop->province,
  134. 'city' => $shop->city,
  135. 'area' => $shop->dist,
  136. 'street' => '',
  137. 'house' => $shop->floor ? $shop->address . $shop->floor : $shop->address,
  138. 'lat' => $shop->lat,
  139. 'lng' => $shop->long,
  140. 'phone' => $shop->telephone
  141. ],
  142. //"order_pattern" => 2,
  143. //"service_trans_prefer" => "SFTC" //order_pattern = 2时必填
  144. ]
  145. ];
  146. $result = IntraCityExpress::updateStore($storeData);
  147. if ($result['errcode'] === 0) {
  148. util::success($result, "门店更新成功");
  149. } else {
  150. Yii::error("门店更新失败:" . $result['errmsg']);
  151. util::fail($result['errmsg']);
  152. }
  153. }
  154. /**
  155. * 门店运费充值
  156. */
  157. public function actionStoreCharge()
  158. {
  159. $post = Yii::$app->request->post();
  160. $amount = floatval($post['amount']);
  161. $amountTurnFen = $amount * 100;
  162. if ($amountTurnFen < 5000) {
  163. util::fail('50元起充');
  164. }
  165. $expressId = $post['expressId'];
  166. $serviceTransIds = ['SFTC', 'DADA'];
  167. if (!in_array($expressId, $serviceTransIds)) {
  168. util::fail('快递动力ID出错');
  169. }
  170. $shopExt = MainClass::getById($this->mainId, false, 'id, wxStoreId');
  171. if ($shopExt['wxStoreId'] == '') {
  172. util::fail('微信门店编号为空');
  173. }
  174. $wxStoreId = $shopExt['wxStoreId'];
  175. $result = IntraCityExpress::storeCharge($wxStoreId, $expressId, $amountTurnFen);
  176. if ($result['errcode'] === 0) {
  177. util::success($result, "门店余额充值单已生成");
  178. } else {
  179. Yii::error("门店余额充值失败:" . $result['errmsg']);
  180. util::fail('门店余额充值失败');
  181. }
  182. }
  183. /**
  184. * 门店余额查询
  185. */
  186. public function actionStoreBalance()
  187. {
  188. $shopExt = MainClass::getById($this->mainId, false, 'id, wxStoreId');
  189. if ($shopExt['wxStoreId'] == '') {
  190. util::error(-1,'微信门店编号为空');
  191. }
  192. $wxStoreId = $shopExt['wxStoreId'];
  193. $result = IntraCityExpress::getBalance($wxStoreId);
  194. if ($result['errcode'] === 0) {
  195. $data = ['all_balance' => $result['all_balance'] / 100.0];
  196. foreach ($result['balance_detail'] as $item) {
  197. if ($item['service_trans_id'] == 'DADA') {
  198. $data['dada_balance'] = $item['balance'] / 100.0;
  199. }
  200. if ($item['service_trans_id'] == 'SFTC') {
  201. $data['sf_balance'] = $item['balance'] / 100.0;
  202. }
  203. }
  204. util::success($data, "门店余额查询成功");
  205. } else {
  206. Yii::error("门店余额查询失败:" . $result['errmsg']);
  207. util::fail('门店余额查询失败');
  208. }
  209. }
  210. /**
  211. * 查询运费
  212. */
  213. public function actionStoreFee()
  214. {
  215. $post = Yii::$app->request->post();
  216. // 创建表单验证模型
  217. $form = new StoreFeeForm();
  218. $form->setScenario('store_fee');
  219. $form->load($post, ''); // 直接从 post 数据加载,不使用模型名作为前缀
  220. // 执行表单验证
  221. $form->validateForm();
  222. $orderId = $post['orderId'];
  223. $order = OrderClass::getById($orderId);
  224. if (empty($order)) {
  225. util::fail('订单出错');
  226. }
  227. if ($order['mainId'] != $this->mainId) {
  228. util::fail('订单出错');
  229. }
  230. $sn = $order['orderSn'];
  231. $shopId = intval($this->shopId);
  232. if (empty($shopId)) {
  233. util::fail("门店不存在");
  234. }
  235. $shopExt = MainClass::getById($this->mainId, false, 'id, wxStoreId');
  236. if (empty($shopExt)) {
  237. util::fail("微信门店不存在");
  238. }
  239. $itemInfoList = OrderItemClass::getListBySn($sn);
  240. $orderType = 2; // 默认是 2。说明:1花束订单 2花材订单 3花束和花材都有
  241. if (empty($itemInfoList)) {
  242. $orderType = 1;
  243. }
  244. $cargoName = '花材'; // 商品名称,默认是花材
  245. $goodsInfoList = OrderGoodsClass::getListBySn($sn);
  246. if (count($goodsInfoList) == 1) {
  247. $goodsInfo = $goodsInfoList[0];
  248. $cargoName = $goodsInfo['name'];
  249. } else {
  250. if ($orderType != 2) {
  251. util::fail("订单商品数量不正确");
  252. }
  253. }
  254. $cargo = [
  255. 'cargo_name' => $cargoName, // 商品名称 -- $order 中没有,要在 goodsInfo 中取
  256. 'cargo_weight' => intval($post['weight'] * 1000), // 单位:克 -- 把千克转换为克
  257. 'cargo_price' => intval($post['price'] * 100), // 单位:分 -- 把元转换为分
  258. 'cargo_type' => IntraCityExpress::GOODS_TYPE_FLOWER,
  259. 'cargo_num' => intval($post['packageNum'])
  260. ];
  261. $originalLng = doubleval($post['user_lng']);
  262. $originalLat = doubleval($post['user_lat']);
  263. $orderData = [
  264. //'out_store_id' => $shopId,
  265. 'wx_store_id' => $shopExt['wxStoreId'],
  266. 'user_name' => $post['user_name'],
  267. 'user_phone' => $post['user_phone'],
  268. 'user_lng' => $originalLng,
  269. 'user_lat' => $originalLat,
  270. 'user_address' => $post['user_address'],
  271. 'cargo' => $cargo,
  272. //'use_sandbox' => 1 // 如果不需要沙箱,use_sandbox就不传就好了 -- 踩坑人的经验
  273. //'use_sandbox' => getenv('YII_ENV') == 'production' ? 0 : 1 // 根据环境变量判断是否使用沙盒环境 -- 没用,不生效
  274. ];
  275. //多处有用到这个方法,需修改请同步修改,搜索关键词 intra_city_express_pre_add
  276. $result = IntraCityExpress::preAddOrder($orderData);
  277. if ($result['errcode'] === 0) {
  278. util::success($result, "查询成功");
  279. } else {
  280. Yii::error("运费查询失败:" . json_encode($result));
  281. util::fail('运费查询失败' . json_encode($result));
  282. }
  283. }
  284. /**
  285. * 创建订单
  286. * POST /intra-city/create-order
  287. */
  288. public function actionCreateOrder()
  289. {
  290. try {
  291. $post = Yii::$app->request->post();
  292. $orderId = $post['orderId'];
  293. $order = OrderClass::getById($orderId);
  294. if (empty($order)) {
  295. util::fail('订单出错');
  296. }
  297. if ($order['mainId'] != $this->mainId) {
  298. util::fail('订单出错');
  299. }
  300. $sn = $order['orderSn'];
  301. $itemInfoList = OrderItemClass::getListBySn($sn, 'id, name, cover, num');
  302. $itemList = [];
  303. $orderType = 2; // 默认是 2。说明:1花束订单 2花材订单 3花束和花材都有
  304. if (empty($itemInfoList)) {
  305. $orderType = 1; // 花束订单
  306. $goodsInfoList = OrderGoodsClass::getListBySn($sn);
  307. $itemCount = 0;
  308. foreach ($goodsInfoList as $itemInfo) {
  309. $itemList[] = [
  310. 'item_name' => $itemInfo['name'],
  311. 'item_pic_url' => $itemInfo['bigCover'],
  312. 'count' => $itemInfo['num'],
  313. ];
  314. $itemCount += $itemInfo['num'];
  315. }
  316. $post['packageNum'] = $itemCount;
  317. } else {
  318. $itemCount = 0;
  319. foreach ($itemInfoList as $itemInfo) {
  320. $itemList[] = [
  321. 'item_name' => $itemInfo['name'],
  322. 'item_pic_url' => $itemInfo['bigCover'],
  323. 'count' => $itemInfo['num'],
  324. ];
  325. $itemCount += $itemInfo['num'];
  326. }
  327. $post['packageNum'] = $itemCount;
  328. }
  329. $shopExt = MainClass::getById($this->mainId, false, 'id, wxStoreId');
  330. if (empty($shopExt)) {
  331. util::fail("微信门店不存在");
  332. }
  333. $cargoName = '花材'; // 商品名称,默认是花材
  334. $goodsInfoList = OrderGoodsClass::getListBySn($sn);
  335. if (count($goodsInfoList) == 1) {
  336. $goodsInfo = $goodsInfoList[0];
  337. $cargoName = $goodsInfo['name'];
  338. } else {
  339. if ($orderType != 2) {
  340. Yii::error('订单商品数量不正确');
  341. util::fail("订单商品数量不正确");
  342. }
  343. }
  344. // 通过 userId 获取 openid(小程序OpenId)
  345. $user = UserClass::getById($order['userId'], false, 'id, miniOpenId');
  346. if (empty($user)) {
  347. Yii::error('获取用户openId出错');
  348. util::fail('数据出错');
  349. }
  350. $cargo = [
  351. 'cargo_name' => $cargoName,
  352. 'cargo_weight' => intval($post['weight'] * 1000),
  353. 'cargo_type' => IntraCityExpress::GOODS_TYPE_FLOWER,
  354. 'cargo_num' => intval($post['packageNum']),
  355. 'cargo_price' => intval($order['actPrice'] * 100), // $order 中有各个价格,请注意
  356. 'item_list' => $itemList,
  357. ];
  358. //生成 store_order_id
  359. $storeOrderId = $orderId;
  360. if ($post['isNew'] == 0) { // 如果不是首次用 $orderId 创建快递配送,则要生成新 store_order_id
  361. // store_order_id 门店订单编号, 即 orderId。如果是重新配送的第n次,订单编号的命名规则为:orderId_copy_n
  362. $query = (new Query())
  363. ->from('xhExpressOrder')
  364. ->where(new \yii\db\Expression('storeOrderId LIKE CONCAT(:prefix, "%")', [':prefix' => $orderId . '_copy_%']))
  365. ->andWhere(['mainId' => $this->mainId]);
  366. // $command = $query->createCommand();
  367. // $sql = $command->sql;
  368. $copyOrderCount = $query->count();
  369. $copyOrderCount += 1;
  370. $storeOrderId = $orderId . '_copy_' . $copyOrderCount;
  371. }
  372. $callbackUrl = Yii::$app->params['hdHost'] . '/intra-city/callback';
  373. $orderData = [
  374. 'wx_store_id' => $shopExt['wxStoreId'],
  375. 'store_order_id' => $storeOrderId, //同一个门店订单编号要保证唯一,相同的订单号会重入
  376. 'user_openid' => $user['miniOpenId'],
  377. 'user_lng' => $post['user_lng'],
  378. 'user_lat' => $post['user_lat'],
  379. 'user_address' => $post['user_address'],
  380. 'user_name' => $post['user_name'],
  381. 'user_phone' => $post['user_phone'],
  382. //'order_seq' => '', // 用于配送员快速寻找到匹配的商品(非必传)
  383. 'verify_code_type' => 0,
  384. 'order_detail_path' => '/admin/order/detail?id=' . $orderId, // TODO 使用花掌柜的地址,后期要重新选个地址
  385. 'callback_url' => $callbackUrl, // 订单状态回调地址(非必传)
  386. //'use_sandbox' => getenv('YII_ENV') == 'production' ? 0 : 1, // 是否使用沙箱(非必传)
  387. 'cargo' => $cargo
  388. ];
  389. $result = IntraCityExpress::createOrder($orderData);
  390. if (isset($result['errcode']) && $result['errcode'] === 0) {
  391. OrderClass::updateById($orderId, ['sendStatus' => 1]); // 更新订单状态为配送中
  392. // 保存进 xhExpressOrder 表
  393. $orderData = [
  394. 'mainId' => $this->mainId,
  395. 'shopId' => $this->shopId,
  396. 'deliveryId' => isset($result['service_trans_id']) ? $result['service_trans_id'] : '',
  397. 'wxOrderId' => isset($result['wx_order_id']) ? $result['wx_order_id'] : '',
  398. 'wxStoreId' => isset($result['wx_store_id']) ? $result['wx_store_id'] : '',
  399. 'storeOrderId' => isset($result['store_order_id']) ? $result['store_order_id'] : '',
  400. 'transOrderId' => isset($result['trans_order_id']) ? $result['trans_order_id'] : '',
  401. 'distance' => isset($result['distance']) ? $result['distance'] : 0,
  402. 'fee' => isset($result['fee']) ? $result['fee'] : 0,
  403. 'fetchCode' => isset($result['fetch_code']) ? $result['fetch_code'] : '',
  404. 'orderSeq' => isset($result['order_seq']) ? $result['order_seq'] : ''
  405. ];
  406. ExpressOrderClass::add($orderData);
  407. util::success(['fee' => $result['fee']], '订单创建成功');
  408. } else {
  409. noticeUtil::push("门店 -- " . $this->mainId . ",订单创建失败:" . json_encode($result));
  410. Yii::error("订单创建失败:" . ($result['errmsg'] ?? '未知错误'), 'intracity');
  411. util::fail( '订单创建失败: ' . $result['errcode']);
  412. }
  413. } catch (\Exception $e) {
  414. Yii::error("订单创建异常:" . $e->getMessage(), 'intracity');
  415. util::fail("系统出错");
  416. }
  417. }
  418. /**
  419. * 查询订单
  420. * GET /intra-city/get-order
  421. */
  422. public function actionGetOrder()
  423. {
  424. try {
  425. $shopExt = MainClass::getById($this->mainId, false, 'id, wxStoreId');
  426. if (empty($shopExt)) {
  427. util::fail("微信门店不存在");
  428. }
  429. $post = Yii::$app->request->post();
  430. $wxOrderId = isset($post['wx_order_id']) ? $post['wx_order_id'] : '';
  431. $orderId = $post['orderId'];
  432. $wxStoreId = isset($post['wx_store_id']) ? $post['wx_store_id'] : $shopExt['wxStoreId'];
  433. if (empty($wxOrderId) && (empty($orderId) || empty($wxStoreId))) {
  434. util::fail("参数不足:wx_order_id 或 (order_id + wx_store_id) 必须提供一组");
  435. }
  436. $order = OrderClass::getById($orderId, false, 'id, mainId, sendStatus');
  437. if ($order['mainId'] != $this->mainId) {
  438. util::fail('订单未找到');
  439. }
  440. // 检查是否存在重复的订单:如果存在,则使用最后一个订单的 store_order_id
  441. $storeOrderId = $orderId; // ------------------------------------- 注意区分:$storeOrderId , $orderId 。它们有可能不相同
  442. $query = (new Query())
  443. ->from('xhExpressOrder')
  444. ->where(['and',
  445. new \yii\db\Expression('storeOrderId LIKE CONCAT(:prefix, "%")', [':prefix' => $orderId . '_copy_']),
  446. ['status' => 1]
  447. ])->andWhere(['mainId' => $this->mainId])
  448. ->orderBy('addTime DESC');
  449. $esDatas = $query->all();
  450. $count = count($esDatas);
  451. if ($count > 0) {
  452. $es = $esDatas[0];
  453. $storeOrderId = $es['storeOrderId'];
  454. if ($count > 1) {
  455. noticeUtil::push('同城配送订单重复多于1个. store_order_id:' . $orderId . ',wx_store_id:' . $wxStoreId);
  456. }
  457. }
  458. $result = IntraCityExpress::queryOrder($wxStoreId, $storeOrderId, $wxOrderId);
  459. if (isset($result['errcode']) && $result['errcode'] === 0) {
  460. util::success($result, "查询成功");
  461. } else {
  462. Yii::error("订单查询失败:" . ($result['errmsg'] ?? '未知错误'), 'intracity');
  463. util::fail( '订单查询失败: ' . $result['errcode']);
  464. }
  465. } catch (\Exception $e) {
  466. Yii::error("订单查询异常:" . $e->getMessage(), 'intracity');
  467. util::fail("系统出错");
  468. }
  469. }
  470. /**
  471. * 取消订单
  472. * POST /intra-city/cancel-order
  473. */
  474. public function actionCancelOrder()
  475. {
  476. try {
  477. $post = Yii::$app->request->post();
  478. $shopExt = MainClass::getById($this->mainId, false, 'id, wxStoreId');
  479. if (empty($shopExt)) {
  480. util::fail("微信门店不存在");
  481. }
  482. $wxOrderId = isset($post['wx_order_id']) ? $post['wx_order_id'] : '';
  483. $orderId = $post['orderId']; //配送订单创建成功后,也会返回的 store_order_id。 即 orderId
  484. $wxStoreId = isset($post['wx_store_id']) ? $post['wx_store_id'] : $shopExt['wxStoreId'];
  485. if (empty($wxOrderId) && (empty($orderId) || empty($wxStoreId))) {
  486. util::fail("参数不足:wx_order_id 或 (store_order_id + wx_store_id) 必须提供一组");
  487. }
  488. $order = OrderClass::getById($orderId, false, 'id, mainId, sendStatus');
  489. if ($order['mainId'] != $this->mainId) {
  490. util::fail('订单未找到');
  491. }
  492. if ($order['sendStatus'] != 1) {
  493. util::fail('配送单状态不是配送中');
  494. }
  495. // 检查是否存在重复的订单,如果存在,则使用最后一个订单的 store_order_id
  496. $storeOrderId = $orderId; // ------------------------------------- 注意区分:$storeOrderId , $orderId 。它们有可能不相同
  497. $query = (new Query())
  498. ->from('xhExpressOrder')
  499. ->where(['and',
  500. new \yii\db\Expression('storeOrderId LIKE CONCAT(:prefix, "%")', [':prefix' => $orderId . '_copy_']),
  501. ['status' => 1]
  502. ])->andWhere(['mainId' => $this->mainId])
  503. ->orderBy('addTime DESC');
  504. // $command = $query->createCommand();
  505. // $sql = $command->sql;
  506. $esDatas = $query->all();
  507. $count = count($esDatas);
  508. if ($count > 0) {
  509. $es = $esDatas[0];
  510. $storeOrderId = $es['storeOrderId'];
  511. if ($count > 1) {
  512. noticeUtil::push('同城配送订单重复多于1个. store_order_id:' . $orderId . ',wx_store_id:' . $wxStoreId);
  513. }
  514. }
  515. $result = IntraCityExpress::cancelOrder($wxOrderId, $storeOrderId, $wxStoreId);
  516. if (isset($result['errcode']) && $result['errcode'] === 0) {
  517. // 更新订单状态
  518. OrderClass::updateById($orderId, ['sendStatus' => OrderClass::ORDER_STATUS_CANCEL]);//设置为取消状态
  519. $eo = ExpressOrderClass::getByCondition(['storeOrderId' => $storeOrderId, 'wxStoreId' => $wxStoreId], true);
  520. if (!empty($eo)) {
  521. $eo->status = 3;
  522. $eo->cancelTime = date('Y-m-d H:i:s');
  523. $eo->deductfee = $result['deductfee'];
  524. $eo->save();
  525. }
  526. util::success($result, "订单取消成功");
  527. } else {
  528. if ($result['errcode'] == 934018) { // 93401 -- 订单已取消,请勿重复操作
  529. // 更新订单状态
  530. OrderClass::updateById($orderId, ['sendStatus' => OrderClass::ORDER_STATUS_CANCEL]);//设置为取消状态
  531. util::success($result, "订单取消成功");
  532. }
  533. Yii::error("订单取消失败:" . ($result['errmsg'] ?? '未知错误'), 'intracity');
  534. util::fail('订单取消失败: ' . $result['errcode']);
  535. }
  536. } catch (\Exception $e) {
  537. Yii::error("订单取消异常:" . $e->getMessage(), 'intracity');
  538. util::fail("系统出错");
  539. }
  540. }
  541. }