MtController.php 27 KB

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