MtController.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. <?php
  2. namespace hd\controllers;
  3. use biz\shop\classes\ShopExtClass;
  4. use biz\stock\classes\GoodsStockRecordClass;
  5. use bizHd\custom\classes\CustomClass;
  6. use bizHd\goods\classes\CategoryClass;
  7. use bizHd\goods\classes\GoodsCategoryClass;
  8. use bizHd\goods\classes\GoodsClass;
  9. use bizHd\meituan\classes\MtOrderClass;
  10. use bizHd\order\classes\OrderClass;
  11. use bizHd\order\classes\OrderGoodsClass;
  12. use bizHd\order\classes\OrderItemClass;
  13. use bizHd\order\services\OrderService;
  14. use bizHd\refund\classes\HdRefundClass;
  15. use bizHd\refund\services\HdRefundService;
  16. use bizHd\shop\classes\ShopClass;
  17. use bizHd\work\classes\WorkClass;
  18. use common\components\dict;
  19. use common\components\httpUtil;
  20. use common\components\mtUtil;
  21. use common\components\noticeUtil;
  22. use common\components\oss;
  23. use common\components\util;
  24. use Yii;
  25. use Shishaoqi\MeituanFlashPurchase\Application;
  26. use yii\helpers\Json;
  27. class MtController extends PublicController
  28. {
  29. public $guestAccess = [];
  30. public function actionGoodsAdd()
  31. {
  32. $post = Yii::$app->request->post();
  33. $connection = Yii::$app->db;//事务处理
  34. $transaction = $connection->beginTransaction();
  35. try {
  36. if (isset($post['retail_data']) == false) {
  37. noticeUtil::push("美团添加商品,通知我们时出错了,没有获取到请求的具体信息", '15280215347');
  38. return Json::encode(['data' => 'ok']);
  39. }
  40. $retailData = $post['retail_data'];
  41. $retail = urldecode(urldecode($retailData));
  42. $info = json_decode($retail);
  43. $obj = $info[0] ?? null;
  44. $shopId = $obj->app_poi_code ?? 0;
  45. $shop = ShopClass::getById($shopId, true);
  46. if (empty($shop)) {
  47. noticeUtil::push("美团添加商品,通知我们时出错了,没有找到门店", '15280215347');
  48. return Json::encode(['data' => 'ok']);
  49. }
  50. $sjId = $shop->sjId ?? 0;
  51. $ext = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  52. if (empty($ext)) {
  53. noticeUtil::push("美团添加商品,通知我们时出错了,没有找到门店拓展信息", '15280215347');
  54. return Json::encode(['data' => 'ok']);
  55. }
  56. $mainId = $shop->mainId ?? 0;
  57. if (empty($mainId)) {
  58. noticeUtil::push("美团添加商品,通知我们时出错了,没有main信息", '15280215347');
  59. return Json::encode(['data' => 'ok']);
  60. }
  61. $mtCategory = CategoryClass::getByCondition(['mainId' => $mainId, 'style' => 1], true);
  62. if (empty($mtCategory)) {
  63. $mtCategory = CategoryClass::add(['style' => 1, 'categoryName' => '美团', 'mainId' => $mainId, 'sjId' => $sjId], true);
  64. }
  65. $catId = $mtCategory->id ?? 0;
  66. if (empty($catId)) {
  67. noticeUtil::push("美团添加商品,通知我们时出错了,没有美团的分类id", '15280215347');
  68. return Json::encode(['data' => 'ok']);
  69. }
  70. $config = dict::getDict('mtConfig');
  71. $app = new Application($config);
  72. $accessToken = mtUtil::getAccessToken($ext);
  73. $appSpuCode = $obj->app_spu_code ?? '';
  74. $hasGoods = GoodsClass::getByCondition(['mainId' => $mainId, 'spu' => $appSpuCode], true);
  75. if (!empty($hasGoods)) {
  76. noticeUtil::push("美团添加商品,通知我们时,发现已经有这个商品了,mainId:{$mainId} spu:{$appSpuCode}", '15280215347');
  77. return Json::encode(['data' => 'ok']);
  78. }
  79. $params = ['app_poi_code' => $shopId, 'app_spu_code' => $appSpuCode, 'access_token' => $accessToken];
  80. $goodsInfo = $app->retail->getDetail($params);
  81. Yii::info('获取商品详情:' . $goodsInfo . ' ' . json_encode($params));
  82. $goodsInfo = json_decode($goodsInfo, true);
  83. if (isset($goodsInfo['error'])) {
  84. noticeUtil::push("美团添加商品,通知我们时出错了,没有找到商品!", '15280215347');
  85. //return Json::encode(['data' => 'ok']);
  86. return '';
  87. }
  88. $imgString = $goodsInfo['data']['picture'] ?? '';
  89. if (empty($imgString)) {
  90. noticeUtil::push("美团添加商品,通知我们时出错了,没有商品图片...", '15280215347');
  91. //return Json::encode(['data' => 'ok']);
  92. return '';
  93. }
  94. noticeUtil::push("美团添加商品,通知我们,商品图片:" . $imgString, '15280215347');
  95. $goodsName = $goodsInfo['data']['name'] ?? '';
  96. $goodsPrice = $goodsInfo['data']['price'] ?? '';
  97. $description = $goodsInfo['data']['description'] ?? '';
  98. $sh = strstr($description, '【鲜花】') !== false ? 1 : 0;
  99. $shopImg = [];
  100. $cover = '';
  101. $imgArr = explode(',', $imgString);
  102. if (!empty($imgArr)) {
  103. foreach ($imgArr as $imgUrl) {
  104. $shortUrl = httpUtil::downloadRemoteImg($sjId, $shopId, $imgUrl);
  105. $shopImg[] = $shortUrl;
  106. if ($cover == '') {
  107. $cover = $shortUrl;
  108. }
  109. }
  110. }
  111. $catIds = [$catId];
  112. $addData = [
  113. 'name' => $goodsName,
  114. 'flower' => 1,
  115. 'property' => 0,
  116. 'price' => $goodsPrice,
  117. 'sjId' => $sjId,
  118. 'mainId' => $mainId,
  119. 'shopId' => $shopId,
  120. 'status' => 0,
  121. 'delStatus' => 0,
  122. 'shopImg' => $shopImg,
  123. 'catIds' => $catIds,
  124. 'cover' => $cover,
  125. 'spu' => $appSpuCode,
  126. 'sh' => $sh,
  127. ];
  128. GoodsClass::addGoods($addData);
  129. $transaction->commit();
  130. return Json::encode(['data' => 'ok']);
  131. } catch (\Exception $e) {
  132. $transaction->rollBack();
  133. $msg = $e->getMessage();
  134. noticeUtil::push("美团添加商品,通知我们时出错了,错误信息:{$msg}", '15280215347');
  135. return Json::encode(['data' => 'ok']);
  136. }
  137. }
  138. //删除商品 ssh 20220914
  139. public function actionGoodsDel()
  140. {
  141. $post = Yii::$app->request->post();
  142. if (isset($post['retail_data']) == false) {
  143. noticeUtil::push("美团删除商品,通知我们时出错了,没有获取到相应信息", '15280215347');
  144. return Json::encode(['data' => 'ok']);
  145. }
  146. $retailData = $post['retail_data'];
  147. $retail = urldecode(urldecode($retailData));
  148. $info = json_decode($retail);
  149. $obj = $info[0] ?? null;
  150. $shopId = $obj->app_poi_code ?? 0;
  151. $shop = ShopClass::getById($shopId, true);
  152. if (empty($shop)) {
  153. noticeUtil::push("美团删除商品,通知我们时出错了,没有找到门店", '15280215347');
  154. return Json::encode(['data' => 'ok']);
  155. }
  156. $sjId = $shop->sjId ?? 0;
  157. $ext = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  158. if (empty($ext)) {
  159. noticeUtil::push("美团删除商品,通知我们时出错了,没有找到门店拓展信息", '15280215347');
  160. return Json::encode(['data' => 'ok']);
  161. }
  162. $mainId = $shop->mainId ?? 0;
  163. if (empty($mainId)) {
  164. noticeUtil::push("美团删除商品,通知我们时出错了,没有main信息", '15280215347');
  165. return Json::encode(['data' => 'ok']);
  166. }
  167. $mtCategory = CategoryClass::getByCondition(['mainId' => $mainId, 'style' => 1], true);
  168. if (empty($mtCategory)) {
  169. $mtCategory = CategoryClass::add(['style' => 1, 'categoryName' => '美团', 'mainId' => $mainId, 'sjId' => $sjId], true);
  170. }
  171. $catId = $mtCategory->id ?? 0;
  172. if (empty($catId)) {
  173. noticeUtil::push("美团删除商品,通知我们时出错了,没有美团的分类id", '15280215347');
  174. return Json::encode(['data' => 'ok']);
  175. }
  176. $appSpuCode = $obj->app_spu_code ?? '';
  177. $hasGoods = GoodsClass::getByCondition(['mainId' => $mainId, 'spu' => $appSpuCode], true);
  178. if (empty($hasGoods)) {
  179. return Json::encode(['data' => 'ok']);
  180. }
  181. $hasGoods->delStatus = 1;
  182. $hasGoods->save();
  183. $goodsId = $hasGoods->id ?? 0;
  184. GoodsCategoryClass::updateByCondition(['gId' => $goodsId], ['delStatus' => 1]);
  185. return Json::encode(['data' => 'ok']);
  186. }
  187. //修改商品 ssh 20220914
  188. public function actionGoodsUpdate()
  189. {
  190. $post = Yii::$app->request->post();
  191. $connection = Yii::$app->db;//事务处理
  192. $transaction = $connection->beginTransaction();
  193. try {
  194. if (isset($post['retail_data']) == false) {
  195. noticeUtil::push("美团修改商品,通知我们时出错了,没有获取到相应信息", '15280215347');
  196. return Json::encode(['data' => 'ok']);
  197. }
  198. $retailData = $post['retail_data'];
  199. $retail = urldecode(urldecode($retailData));
  200. $info = json_decode($retail);
  201. $obj = $info[0] ?? null;
  202. $shopId = $obj->app_poi_code ?? 0;
  203. $shop = ShopClass::getById($shopId, true);
  204. if (empty($shop)) {
  205. noticeUtil::push("美团修改商品,通知我们时出错了,没有找到门店", '15280215347');
  206. return Json::encode(['data' => 'ok']);
  207. }
  208. $sjId = $shop->sjId ?? 0;
  209. $ext = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  210. if (empty($ext)) {
  211. noticeUtil::push("美团修改商品,通知我们时出错了,没有找到门店拓展信息", '15280215347');
  212. return Json::encode(['data' => 'ok']);
  213. }
  214. $mainId = $shop->mainId ?? 0;
  215. if (empty($mainId)) {
  216. noticeUtil::push("美团修改商品,通知我们时出错了,没有main信息", '15280215347');
  217. return Json::encode(['data' => 'ok']);
  218. }
  219. $mtCategory = CategoryClass::getByCondition(['mainId' => $mainId, 'style' => 1], true);
  220. if (empty($mtCategory)) {
  221. $mtCategory = CategoryClass::add(['style' => 1, 'categoryName' => '美团', 'mainId' => $mainId, 'sjId' => $sjId], true);
  222. }
  223. $catId = $mtCategory->id ?? 0;
  224. if (empty($catId)) {
  225. noticeUtil::push("美团修改商品,通知我们时出错了,没有美团的分类id", '15280215347');
  226. return Json::encode(['data' => 'ok']);
  227. }
  228. $config = dict::getDict('mtConfig');
  229. $app = new Application($config);
  230. $accessToken = mtUtil::getAccessToken($ext);
  231. $appSpuCode = $obj->app_spu_code ?? '';
  232. $hasGoods = GoodsClass::getByCondition(['mainId' => $mainId, 'spu' => $appSpuCode], true);
  233. if (empty($hasGoods)) {
  234. noticeUtil::push("美团修改商品,通知我们时,没有发现这个商品,mainId:{$mainId} spu:{$appSpuCode}", '15280215347');
  235. return Json::encode(['data' => 'ok']);
  236. }
  237. $hasGoodsId = $hasGoods->id ?? 0;
  238. $params = ['app_poi_code' => $shopId, 'app_spu_code' => $appSpuCode, 'access_token' => $accessToken];
  239. $goodsInfo = $app->retail->getDetail($params);
  240. Yii::info('获取商品详情:' . $goodsInfo . ' ' . json_encode($params));
  241. $goodsInfo = json_decode($goodsInfo, true);
  242. if (isset($goodsInfo['error'])) {
  243. noticeUtil::push("美团修改商品,通知我们时出错了,没有找到商品图片", '15280215347');
  244. return Json::encode(['data' => 'ok']);
  245. }
  246. $imgString = $goodsInfo['data']['picture'] ?? '';
  247. if (empty($imgString)) {
  248. noticeUtil::push("美团修改商品,通知我们时出错了,没有商品图片...", '15280215347');
  249. return Json::encode(['data' => 'ok']);
  250. }
  251. $goodsName = $goodsInfo['data']['name'] ?? '';
  252. $goodsPrice = $goodsInfo['data']['price'] ?? '';
  253. $description = $goodsInfo['data']['description'] ?? '';
  254. $sh = strstr($description, '【鲜花】') !== false ? 1 : 0;
  255. $currentStr = md5($goodsName . '_' . $goodsPrice . '_' . $description . '_' . $sh . '_' . $imgString);
  256. $cacheKey = 'mt_goods_id_' . $hasGoodsId;
  257. $cacheStr = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  258. if ($cacheStr == $currentStr) {
  259. //商品没有更新
  260. return Json::encode(['data' => 'ok']);
  261. } else {
  262. Yii::$app->redis->executeCommand('SET', [$cacheKey, $currentStr]);
  263. }
  264. noticeUtil::push("美团修改商品,通知我们,商品图片:" . $imgString, '15280215347');
  265. $shopImg = [];
  266. $cover = '';
  267. $imgArr = explode(',', $imgString);
  268. if (!empty($imgArr)) {
  269. foreach ($imgArr as $imgUrl) {
  270. $shortUrl = httpUtil::downloadRemoteImg($sjId, $shopId, $imgUrl);
  271. $shopImg[] = $shortUrl;
  272. if ($cover == '') {
  273. $cover = $shortUrl;
  274. }
  275. }
  276. }
  277. $hasGoods->price = $goodsPrice;
  278. $hasGoods->name = $goodsName;
  279. $hasGoods->cover = $cover;
  280. $hasGoods->sh = $sh;
  281. $hasGoods->shopImg = json_encode($shopImg);
  282. $hasGoods->save();
  283. $transaction->commit();
  284. return Json::encode(['data' => 'ok']);
  285. } catch (\Exception $e) {
  286. $transaction->rollBack();
  287. $msg = $e->getMessage();
  288. noticeUtil::push("美团修改商品,通知我们时出错了,错误信息:{$msg}", '15280215347');
  289. return Json::encode(['data' => 'ok']);
  290. }
  291. }
  292. //已确认订单通知
  293. public function actionConfirmOrder()
  294. {
  295. //error_reporting(E_ALL);
  296. $post = Yii::$app->request->post();
  297. $mtOrderId = $post['order_id'] ?? 0;
  298. $thirdSn = $post['day_seq'] ?? 0;
  299. //4秒内不允许第三方重复通知
  300. $cacheKey = 'mt_confirm_order_' . $mtOrderId;
  301. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  302. if (!empty($has)) {
  303. Yii::info("美团已确认订单通知,出现重复通知,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}");
  304. return Json::encode(['data' => 'ok']);
  305. }
  306. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 4, 'has']);
  307. // 无数据过来时,是美团对接口进行存活试探,请保留此段代码,好让请求在此被中断返回
  308. if (empty($post)) {
  309. return Json::encode(['data' => 'ok']);
  310. }
  311. Yii::info('confirmOrderData: ' . json_encode($post));
  312. $connection = Yii::$app->db;//事务处理
  313. $transaction = $connection->beginTransaction();
  314. try {
  315. $shopId = $post['app_poi_code'] ?? '';
  316. $shop = ShopClass::getById($shopId, true);
  317. if (empty($shop)) {
  318. noticeUtil::push("美团确认订单报错,没有找到门店,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  319. return Json::encode(['data' => 'ok']);
  320. }
  321. $shopExt = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  322. if (empty($shopExt)) {
  323. noticeUtil::push("美团确认订单报错,没有找到门店拓展信息,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  324. return Json::encode(['data' => 'ok']);
  325. }
  326. $mainId = $shop->mainId ?? 0;
  327. $sjId = $shop->sjId ?? 0;
  328. $customId = $shop->defaultCustomId ?? 0;
  329. if (empty($customId)) {
  330. noticeUtil::push("美团确认订单报错,没有找到客户哦,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  331. return Json::encode(['data' => 'ok']);
  332. }
  333. $custom = CustomClass::getById($customId);
  334. if (empty($custom)) {
  335. noticeUtil::push("美团确认订单报错,没有找到客户,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  336. return Json::encode(['data' => 'ok']);
  337. }
  338. foreach ($post as $k => $val) {
  339. $post[$k] = urldecode($val);
  340. }
  341. MtOrderClass::add($post);
  342. $detail = $post['detail'] ?? '';
  343. if (empty($detail)) {
  344. noticeUtil::push("美团确认订单报错,没有找到商品,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  345. return Json::encode(['data' => 'ok']);
  346. }
  347. $mtGoodsData = json_decode($detail, true);
  348. if (empty($mtGoodsData)) {
  349. noticeUtil::push("美团确认订单报错,没有找到商品哦,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  350. return Json::encode(['data' => 'ok']);
  351. }
  352. Yii::info('商品信息:' . json_encode($mtGoodsData));
  353. $productList = [];
  354. $mtCategory = CategoryClass::getByCondition(['mainId' => $mainId, 'style' => 1], true);
  355. if (empty($mtCategory)) {
  356. $mtCategory = CategoryClass::add(['style' => 1, 'categoryName' => '美团', 'mainId' => $mainId, 'sjId' => $sjId], true);
  357. }
  358. $catId = $mtCategory->id ?? 0;
  359. if (empty($catId)) {
  360. noticeUtil::push("美团确认订单报错,美团默认分类没有找到,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  361. return Json::encode(['data' => 'ok']);
  362. }
  363. $caution = isset($post['caution']) && !empty($post['caution']) ? $post['caution'] : '';
  364. $remark = $caution;
  365. $bookMobile = $post['order_phone_number'] ?? '';
  366. $receiveMobile = $post['recipient_phone'] ?? '';
  367. $receiveUserName = $post['recipient_name'] ?? '';
  368. $fullAddress = $post['recipient_address'] ?? '';
  369. $cover = '';
  370. $goodsNum = 0;
  371. foreach ($mtGoodsData as $mtKey => $mtGoods) {
  372. $mtGoodsSpu = $mtGoods['app_spu_code'] ?? '';
  373. if (empty($mtGoodsSpu)) {
  374. if (isset($mtGoods['app_food_code']) && !empty($mtGoods['app_food_code'])) {
  375. $mtGoodsSpu = $mtGoods['app_food_code'];
  376. }
  377. }
  378. $goodsPrice = $mtGoods['price'] ?? 0;
  379. $num = $mtGoods['quantity'] ?? 0;
  380. $goodsNum = bcadd($num, $goodsNum);
  381. $currentGoods = GoodsClass::getByCondition(['spu' => $mtGoodsSpu], true);
  382. if (empty($currentGoods)) {
  383. noticeUtil::push("美团确认订单报错,数据库没有相应商品,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  384. return Json::encode(['data' => 'ok']);
  385. }
  386. $id = $currentGoods->id ?? 0;
  387. $unitType = 0;
  388. $property = dict::getDict('property', 'goods');
  389. $goods = [['productId' => $id, 'num' => $num, 'unitType' => $unitType, 'unitPrice' => $goodsPrice, 'property' => $property]];
  390. $productList = array_merge($goods, $productList);
  391. }
  392. $poi_receive_detail_yuan = $post['poi_receive_detail_yuan'] ?? '';
  393. if (empty($poi_receive_detail_yuan)) {
  394. noticeUtil::push("美团确认订单报错,订单金额有问题,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  395. util::stop();
  396. }
  397. $poi_receive_detail_yuan_data = json_decode($poi_receive_detail_yuan, true);
  398. if (is_array($poi_receive_detail_yuan_data) == false) {
  399. noticeUtil::push("美团确认订单报错,订单金额有问题哦,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  400. util::stop();
  401. }
  402. $onlinePayment = $poi_receive_detail_yuan_data['onlinePayment'] ?? 0;
  403. if ($onlinePayment == 0) {
  404. noticeUtil::push("美团确认订单报错,订单金额有问题!美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  405. util::stop();
  406. }
  407. $modifyPrice = $onlinePayment;
  408. //技术服务费
  409. $foodShareFeeChargeByPoi = $poi_receive_detail_yuan_data['foodShareFeeChargeByPoi'] ?? 0;
  410. $reconciliationExtras = $poi_receive_detail_yuan_data['reconciliationExtras'] ?? '';
  411. //履约服务费
  412. $performanceServiceFee = 0;
  413. if (!empty($reconciliationExtras)) {
  414. $extArr = json_decode($reconciliationExtras, true);
  415. $performanceServiceFee = $extArr['performanceServiceFee'] ?? 0;
  416. }
  417. //运费
  418. $sendCost = $post['shipping_fee'] ?? 0;
  419. //打包费
  420. $labourCost = $post['package_bag_money_yuan'] ?? 0;
  421. //服务费和手续费
  422. $serviceFee = bcadd($foodShareFeeChargeByPoi, $performanceServiceFee, 2);
  423. $estimate_arrival_time = $post['estimate_arrival_time'] ?? 0;
  424. $reachPeriod = date("H:i", $estimate_arrival_time);
  425. $reachDate = date("Y-m-d", $estimate_arrival_time);
  426. $reachTime = $estimate_arrival_time;
  427. $orderData = [
  428. 'product' => $productList,
  429. 'fromType' => dict::getDict('fromType', 'mt'),
  430. 'labourCost' => $labourCost,
  431. 'sendCost' => $sendCost,
  432. 'shopId' => $shopId,
  433. 'mainId' => $mainId,
  434. 'sjId' => $sjId,
  435. 'modifyPrice' => $modifyPrice,
  436. 'serviceFee' => $serviceFee,
  437. 'thirdSn' => $thirdSn,
  438. 'cover' => $cover,
  439. 'reachTime' => $reachTime,
  440. 'reachDate' => $reachDate,
  441. 'reachPeriod' => $reachPeriod,
  442. 'remark' => $remark,
  443. 'goodsNum' => $goodsNum,
  444. 'bookMobile' => $bookMobile,
  445. 'receiveMobile' => $receiveMobile,
  446. 'receiveUserName' => $receiveUserName,
  447. 'fullAddress' => $fullAddress,
  448. 'thirdOrderId' => $mtOrderId,
  449. ];
  450. $hasPay = dict::getDict('hasPay', 'payed');
  451. $order = OrderService::createFinishOrder($orderData, $custom, $hasPay);
  452. $transaction->commit();
  453. //新制作单提示
  454. $shopExt = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  455. ShopExtClass::newWorkRemind($shopExt, $order);
  456. return Json::encode(['data' => 'ok']);
  457. } catch (\Exception $e) {
  458. $transaction->rollBack();
  459. $msg = $e->getMessage();
  460. noticeUtil::push("确认订单报错了,美团订单id:{$mtOrderId} 美团编号:{$thirdSn},错误信息:{$msg}", '15280215347');
  461. return Json::encode(['data' => 'ok']);
  462. }
  463. }
  464. //取消订单
  465. public function actionCancelOrder()
  466. {
  467. $post = Yii::$app->request->post();
  468. $mtOrderId = $post['order_id'] ?? 0;
  469. $shopId = $post['app_poi_code'] ?? '';
  470. if (empty($post)) {
  471. return Json::encode(['data' => 'ok']);
  472. }
  473. Yii::info('cancelOrderData: ' . json_encode($post));
  474. $connection = Yii::$app->db;//事务处理
  475. $transaction = $connection->beginTransaction();
  476. try {
  477. //4秒内不允许第三方重复通知
  478. $cacheKey = 'mt_cancel_order_' . $mtOrderId;
  479. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  480. if (!empty($has)) {
  481. Yii::info("美团取消订单通知,出现重复通知,orderId:{$mtOrderId}");
  482. return Json::encode(['data' => 'ok']);
  483. }
  484. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 4, 'has']);
  485. $mt = dict::getDict('fromType', 'mt');
  486. $order = OrderClass::getByCondition(['fromType' => $mt, 'thirdOrderId' => $mtOrderId], true);
  487. if (empty($order)) {
  488. //没有接单时取消订单
  489. return Json::encode(['data' => 'ok']);
  490. }
  491. //订单取消,金额全部退还
  492. $shop = ShopClass::getById($shopId, true);
  493. if (empty($shop)) {
  494. return Json::encode(['data' => 'ok']);
  495. }
  496. $sjId = $shop->sjId ?? 0;
  497. $mainId = $shop->mainId ?? 0;
  498. $post = [];
  499. $post['price'] = $order->actPrice ?? 0;
  500. $post['refundType'] = HdRefundClass::REFUND_TYPE_MONEY_GOOD;
  501. $post['id'] = $order->id ?? 0;
  502. $post['shopId'] = $shopId;
  503. $post['sjId'] = $sjId;
  504. $post['shopAdminId'] = 0;
  505. $post['mainId'] = $mainId;
  506. $post['shopAdminName'] = '美团APP';
  507. $orderSn = $order->orderSn ?? '';
  508. $orderGoods = OrderGoodsClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  509. $productData = [];
  510. if (!empty($orderGoods)) {
  511. foreach ($orderGoods as $goodsItem) {
  512. $productData[] = ['productId' => $goodsItem->goodsId, 'num' => $goodsItem->num, 'unitPrice' => $goodsItem->unitPrice, 'property' => 0,];
  513. }
  514. }
  515. $orderItem = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  516. if (!empty($orderItem)) {
  517. foreach ($orderItem as $singe) {
  518. $productData[] = ['productId' => $singe->itemId, 'num' => $singe->num, 'unitPrice' => $singe->unitPrice, 'property' => 1,];
  519. }
  520. }
  521. $post['product'] = $productData;
  522. HdRefundService::addRefund($post, $order);
  523. $transaction->commit();
  524. //制作单取消通知
  525. $shopExt = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  526. ShopExtClass::orderCancelRemind($shopExt, $order);
  527. return Json::encode(['data' => 'ok']);
  528. } catch (\Exception $e) {
  529. $transaction->rollBack();
  530. $msg = $e->getMessage();
  531. noticeUtil::push("取消报错了,美团订单id:{$mtOrderId} ,错误信息:{$msg}", '15280215347');
  532. return Json::encode(['data' => 'ok']);
  533. }
  534. }
  535. //推送已支付订单
  536. public function actionNewOrder()
  537. {
  538. return Json::encode(['data' => 'ok']);
  539. }
  540. //部分退款
  541. public function actionPartRefund()
  542. {
  543. return Json::encode(['data' => 'ok']);
  544. }
  545. //全部退款
  546. public function actionAllRefund()
  547. {
  548. return Json::encode(['data' => 'ok']);
  549. }
  550. }