MtController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. namespace hd\controllers;
  3. use biz\shop\classes\ShopExtClass;
  4. use bizHd\custom\classes\CustomClass;
  5. use bizHd\goods\classes\CategoryClass;
  6. use bizHd\goods\classes\GoodsClass;
  7. use bizHd\meituan\classes\MtOrderClass;
  8. use bizHd\order\classes\OrderClass;
  9. use bizHd\order\services\OrderService;
  10. use bizHd\shop\classes\ShopClass;
  11. use bizHd\work\classes\WorkClass;
  12. use common\components\dict;
  13. use common\components\httpUtil;
  14. use common\components\mtUtil;
  15. use common\components\noticeUtil;
  16. use common\components\oss;
  17. use common\components\util;
  18. use Yii;
  19. use Shishaoqi\MeituanFlashPurchase\Application;
  20. use yii\helpers\Json;
  21. class MtController extends PublicController
  22. {
  23. public $guestAccess = [];
  24. //已确认订单通知
  25. public function actionConfirmOrder()
  26. {
  27. //error_reporting(E_ALL);
  28. $post = Yii::$app->request->post();
  29. $mtOrderId = $post['order_id'] ?? 0;
  30. $thirdSn = $post['day_seq'] ?? 0;
  31. //4秒内不允许第三方重复通知
  32. $cacheKey = 'mt_confirm_order_' . $mtOrderId;
  33. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  34. if (!empty($has)) {
  35. Yii::info("美团已确认订单通知,出现重复通知,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}");
  36. return Json::encode(['data' => 'ok']);
  37. }
  38. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 4, 'has']);
  39. // 无数据过来时,是美团对接口进行存活试探,请保留此段代码,好让请求在此被中断返回
  40. if (empty($post)) {
  41. return Json::encode(['data' => 'ok']);
  42. }
  43. Yii::info('confirmOrderData: ' . json_encode($post));
  44. $connection = Yii::$app->db;//事务处理
  45. $transaction = $connection->beginTransaction();
  46. try {
  47. $shopId = $post['app_poi_code'] ?? '';
  48. $shop = ShopClass::getById($shopId, true);
  49. if (empty($shop)) {
  50. noticeUtil::push("美团确认订单报错,没有找到门店,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  51. return Json::encode(['data' => 'ok']);
  52. }
  53. $shopExt = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  54. if (empty($shopExt)) {
  55. noticeUtil::push("美团确认订单报错,没有找到门店拓展信息,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  56. return Json::encode(['data' => 'ok']);
  57. }
  58. $mainId = $shop->mainId ?? 0;
  59. $sjId = $shop->sjId ?? 0;
  60. $customId = $shop->defaultCustomId ?? 0;
  61. if (empty($customId)) {
  62. noticeUtil::push("美团确认订单报错,没有找到客户哦,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  63. return Json::encode(['data' => 'ok']);
  64. }
  65. $custom = CustomClass::getById($customId);
  66. if (empty($custom)) {
  67. noticeUtil::push("美团确认订单报错,没有找到客户,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  68. return Json::encode(['data' => 'ok']);
  69. }
  70. foreach ($post as $k => $val) {
  71. $post[$k] = urldecode($val);
  72. }
  73. MtOrderClass::add($post);
  74. $detail = $post['detail'] ?? '';
  75. if (empty($detail)) {
  76. noticeUtil::push("美团确认订单报错,没有找到商品,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  77. return Json::encode(['data' => 'ok']);
  78. }
  79. $mtGoodsData = json_decode($detail, true);
  80. if (empty($mtGoodsData)) {
  81. noticeUtil::push("美团确认订单报错,没有找到商品哦,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  82. return Json::encode(['data' => 'ok']);
  83. }
  84. Yii::info('商品信息:' . json_encode($mtGoodsData));
  85. $productList = [];
  86. $mtCategory = CategoryClass::getByCondition(['mainId' => $mainId, 'style' => 1], true);
  87. if (empty($mtCategory)) {
  88. $mtCategory = CategoryClass::add(['style' => 1, 'categoryName' => '美团', 'mainId' => $mainId, 'sjId' => $sjId], true);
  89. }
  90. $catId = $mtCategory->id ?? 0;
  91. if (empty($catId)) {
  92. noticeUtil::push("美团确认订单报错,美团默认分类没有找到,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  93. return Json::encode(['data' => 'ok']);
  94. }
  95. $caution = isset($post['caution']) && !empty($post['caution']) ? $post['caution'] : '';
  96. $remark = $caution;
  97. $bookMobile = $post['order_phone_number'] ?? '';
  98. $receiveMobile = $post['recipient_phone'] ?? '';
  99. $receiveUserName = $post['recipient_name'] ?? '';
  100. $fullAddress = $post['recipient_address'] ?? '';
  101. $orderCover = '';
  102. $goodsNum = 0;
  103. foreach ($mtGoodsData as $mtKey => $mtGoods) {
  104. $mtGoodsSpu = $mtGoods['app_spu_code'] ?? '';
  105. if (empty($mtGoodsSpu)) {
  106. if (isset($mtGoods['app_food_code']) && !empty($mtGoods['app_food_code'])) {
  107. $mtGoodsSpu = $mtGoods['app_food_code'];
  108. }
  109. }
  110. $goodsPrice = $mtGoods['price'] ?? 0;
  111. $num = $mtGoods['quantity'] ?? 0;
  112. $goodsNum = bcadd($num, $goodsNum);
  113. $currentGoods = GoodsClass::getByCondition(['spu' => $mtGoodsSpu], true);
  114. if (empty($currentGoods)) {
  115. noticeUtil::push("美团确认订单报错,数据库没有相应商品,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  116. return Json::encode(['data' => 'ok']);
  117. }
  118. $id = $currentGoods->id ?? 0;
  119. $unitType = 0;
  120. $property = dict::getDict('property', 'goods');
  121. $goods = [['productId' => $id, 'num' => $num, 'unitType' => $unitType, 'unitPrice' => $goodsPrice, 'property' => $property]];
  122. $productList = array_merge($goods, $productList);
  123. }
  124. $poi_receive_detail_yuan = $post['poi_receive_detail_yuan'] ?? '';
  125. if (empty($poi_receive_detail_yuan)) {
  126. noticeUtil::push("美团确认订单报错,订单金额有问题,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  127. util::stop();
  128. }
  129. $poi_receive_detail_yuan_data = json_decode($poi_receive_detail_yuan, true);
  130. if (is_array($poi_receive_detail_yuan_data) == false) {
  131. noticeUtil::push("美团确认订单报错,订单金额有问题哦,美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  132. util::stop();
  133. }
  134. $onlinePayment = $poi_receive_detail_yuan_data['onlinePayment'] ?? 0;
  135. if ($onlinePayment == 0) {
  136. noticeUtil::push("美团确认订单报错,订单金额有问题!美团订单id:{$mtOrderId} 美团编号:{$thirdSn}", '15280215347');
  137. util::stop();
  138. }
  139. $modifyPrice = $onlinePayment;
  140. //技术服务费
  141. $foodShareFeeChargeByPoi = $poi_receive_detail_yuan_data['foodShareFeeChargeByPoi'] ?? 0;
  142. $reconciliationExtras = $poi_receive_detail_yuan_data['reconciliationExtras'] ?? '';
  143. //履约服务费
  144. $performanceServiceFee = 0;
  145. if (!empty($reconciliationExtras)) {
  146. $extArr = json_decode($reconciliationExtras, true);
  147. $performanceServiceFee = $extArr['performanceServiceFee'] ?? 0;
  148. }
  149. //运费
  150. $sendCost = $post['shipping_fee'] ?? 0;
  151. //打包费
  152. $labourCost = $post['package_bag_money_yuan'] ?? 0;
  153. //服务费和手续费
  154. $serviceFee = bcadd($foodShareFeeChargeByPoi, $performanceServiceFee, 2);
  155. $estimate_arrival_time = $post['estimate_arrival_time'] ?? 0;
  156. $reachPeriod = date("H:i", $estimate_arrival_time);
  157. $reachDate = date("Y-m-d", $estimate_arrival_time);
  158. $reachTime = $estimate_arrival_time;
  159. $orderData = [
  160. 'product' => $productList,
  161. 'fromType' => dict::getDict('fromType', 'mt'),
  162. 'labourCost' => $labourCost,
  163. 'sendCost' => $sendCost,
  164. 'shopId' => $shopId,
  165. 'mainId' => $mainId,
  166. 'sjId' => $sjId,
  167. 'modifyPrice' => $modifyPrice,
  168. 'serviceFee' => $serviceFee,
  169. 'thirdSn' => $thirdSn,
  170. 'cover' => $orderCover,
  171. 'reachTime' => $reachTime,
  172. 'reachDate' => $reachDate,
  173. 'reachPeriod' => $reachPeriod,
  174. 'remark' => $remark,
  175. 'goodsNum' => $goodsNum,
  176. 'bookMobile' => $bookMobile,
  177. 'receiveMobile' => $receiveMobile,
  178. 'receiveUserName' => $receiveUserName,
  179. 'fullAddress' => $fullAddress,
  180. 'thirdOrderId' => $mtOrderId,
  181. ];
  182. $hasPay = dict::getDict('hasPay', 'payed');
  183. $order = OrderService::createFinishOrder($orderData, $custom, $hasPay);
  184. $transaction->commit();
  185. //新制作单提示
  186. $shopExt = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  187. ShopExtClass::newWorkRemind($shopExt, $order);
  188. return Json::encode(['data' => 'ok']);
  189. } catch (\Exception $e) {
  190. $transaction->rollBack();
  191. $msg = $e->getMessage();
  192. noticeUtil::push("确认订单报错了,美团订单id:{$mtOrderId} 美团编号:{$thirdSn},错误信息:{$msg}", '15280215347');
  193. return Json::encode(['data' => 'ok']);
  194. }
  195. }
  196. //取消订单
  197. public function actionCancelOrder()
  198. {
  199. $post = Yii::$app->request->post();
  200. $mtOrderId = $post['order_id'] ?? 0;
  201. $shopId = $post['app_poi_code'] ?? '';
  202. if (empty($post)) {
  203. return Json::encode(['data' => 'ok']);
  204. }
  205. Yii::info('cancelOrderData: ' . json_encode($post));
  206. //4秒内不允许第三方重复通知
  207. $cacheKey = 'mt_cancel_order_' . $mtOrderId;
  208. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  209. if (!empty($has)) {
  210. Yii::info("美团取消订单通知,出现重复通知,orderId:{$mtOrderId}");
  211. return Json::encode(['data' => 'ok']);
  212. }
  213. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 4, 'has']);
  214. $mt = dict::getDict('fromType', 'mt');
  215. $order = OrderClass::getByCondition(['fromType' => $mt, 'thirdOrderId' => $mtOrderId], true);
  216. if (empty($order)) {
  217. //没有接单时取消订单
  218. return Json::encode(['data' => 'ok']);
  219. }
  220. //只要制作单没有完成,则走取消订单流程。如果有制作单完成,则订单状态和制作单暂时先不管,后面要兼容。
  221. $orderId = $order->id ?? 0;
  222. $workList = WorkClass::getAllByCondition(['orderId' => $orderId], null, '*', null, true);
  223. $hasCompleteWork = false;
  224. if (!empty($workList)) {
  225. foreach ($workList as $work) {
  226. if ($work->status == 1) {
  227. $hasCompleteWork = true;
  228. }
  229. }
  230. }
  231. //取消订单和制作单
  232. if ($hasCompleteWork == false) {
  233. $order->status = 5;
  234. $order->save();
  235. if (!empty($workList)) {
  236. foreach ($workList as $work) {
  237. $work->status = 2;
  238. $work->save();
  239. }
  240. }
  241. }
  242. //如果制作单完成了还会出现取消订单情况,这个等于全部退款,这个后面再考虑兼容!!!!!
  243. //制作单取消通知
  244. $shopExt = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  245. ShopExtClass::orderCancelRemind($shopExt, $order);
  246. return Json::encode(['data' => 'ok']);
  247. }
  248. //推送已支付订单
  249. public function actionNewOrder()
  250. {
  251. return Json::encode(['data' => 'ok']);
  252. }
  253. //部分退款
  254. public function actionPartRefund()
  255. {
  256. return Json::encode(['data' => 'ok']);
  257. }
  258. //全部退款
  259. public function actionAllRefund()
  260. {
  261. return Json::encode(['data' => 'ok']);
  262. }
  263. }