MtController.php 26 KB

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