MtController.php 13 KB

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