MtController.php 27 KB

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