MtController.php 13 KB

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