StoreOrderBatch.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/12/20
  6. */
  7. namespace app\models\store;
  8. use crmeb\basic\BaseModel;
  9. use think\facade\Cache;
  10. use crmeb\traits\ModelTrait;
  11. use app\models\system\SystemStore;
  12. use app\models\user\{
  13. User, UserAddress, UserBill, WechatUser
  14. };
  15. use crmeb\repositories\{
  16. GoodsRepository, PaymentRepositories, OrderRepository, ShortLetterRepositories, UserRepository
  17. };
  18. use EasyWeChat\Store\Store;
  19. /**
  20. * 正常情況, 一次性下單購物車中多個商品,只產生一個訂單。 支付,積分,發貨,退貨,等等都是基於這一個訂單操作。
  21. * 一個明顯的問題是:當此訂單中多個商品不能在一個快遞中發貨時,訂單就變得不易管理
  22. *
  23. * 這個類的目的就是。已最小的侵入性來實現:一個訂單中包含多個商品時,自動按照一定的邏輯把這個訂單拆分爲若幹個訂單
  24. * 每個訂單中的商品可在同一個快遞中發貨。
  25. *
  26. * 有點類似一個電商平臺多個商家的情況,肯定是各個商家派發購物車中屬於自己家的商品。
  27. *
  28. * 實現:
  29. *
  30. * 單獨建一個表,此表只記錄一個大訂單號(用於支付接口傳遞訂單參數)和關聯的所有子訂單
  31. * 支付接口按大訂單號走,發貨等訂單管理按子訂單走。
  32. * 這樣有一個問題:子訂單退貨沒法執行三方支付退款(暫定)
  33. */
  34. class StoreOrderBatch extends BaseModel {
  35. /**
  36. * 数据表主键
  37. * @var string
  38. */
  39. protected $pk = 'id';
  40. /**
  41. * 模型名称
  42. * @var string
  43. */
  44. protected $name = 'store_order_batch';
  45. use ModelTrait;
  46. /**
  47. * 获取订单各项金额
  48. *
  49. */
  50. public static function getOrderPriceGroup($cartInfo, $addr)
  51. {
  52. return StoreOrder::getOrderPriceGroup($cartInfo, $addr);
  53. }
  54. /**
  55. * 缓存订单信息
  56. * @param $uid
  57. * @param $cartInfo
  58. * @param $priceGroup
  59. * @param array $other
  60. * @param int $cacheTime
  61. * @return string 总订单ID
  62. * @throws \Psr\SimpleCache\InvalidArgumentException
  63. */
  64. public static function cacheOrderInfo($uid, $cartInfo, $priceGroup, $other = [], $cacheTime = 600)
  65. {
  66. $key = md5(time() . $uid);
  67. Cache::set('user_order_' . $uid . $key, compact('cartInfo', 'priceGroup', 'other'), $cacheTime);
  68. return $key;
  69. }
  70. /**
  71. * 获取订单缓存信息
  72. * @param $uid
  73. * @param $key
  74. * @return mixed|null
  75. * @throws \Psr\SimpleCache\InvalidArgumentException
  76. */
  77. public static function getCacheOrderInfo($uid, $key)
  78. {
  79. $cacheName = 'user_order_' . $uid . $key;
  80. if (!Cache::has($cacheName)) return null;
  81. return Cache::get($cacheName);
  82. }
  83. /**
  84. * 生成总订单唯一id
  85. * @param $uid 用户uid
  86. * @return string
  87. */
  88. public static function getNewPOrderId()
  89. {
  90. $prefix = 'wxcn';
  91. list($msec, $sec) = explode(' ', microtime());
  92. $msectime = number_format((floatval($msec) + floatval($sec)) * 1000, 0, '', '');
  93. $porderId = $prefix . $msectime . mt_rand(10000, 99999);
  94. if (self::be(['porder_id' => $porderId])) {
  95. $porderId = $prefix . $msectime . mt_rand(10000, 99999);
  96. }
  97. return $porderId;
  98. }
  99. /**
  100. * 获取所有子订单
  101. * @param string $porderId: porder_id
  102. */
  103. public static function getAllSubOrders(string $porderId): Array
  104. {
  105. $subOrders = self::alias('b')->join('StoreOrder o', 'o.id=b.oid')
  106. ->where('b.porder_id', $porderId)->select()->toArray();
  107. foreach($subOrders as &$subOrder) {
  108. $subOrder['cart_id'] = json_decode($subOrder['cart_id'], true);
  109. }
  110. return $subOrders;
  111. }
  112. /**
  113. * 字段累加
  114. * @param Array $subOrders
  115. * @param Array $field_name_list
  116. * @return Array
  117. */
  118. public static function sumFields(Array $marr, Array $field_name_list): Array
  119. {
  120. $sums = [];
  121. foreach($marr as $row) {
  122. foreach($field_name_list as $fname) {
  123. $fv = $row[$fname] ?? 0; // field value
  124. $sums[$fname] = (float)bcadd($sums[$fname] ?? 0, $fv, 2);
  125. }
  126. }
  127. return $sums;
  128. }
  129. /**
  130. * 微信支付 为 0元时
  131. * @param $order_id
  132. * @param $uid
  133. * @param string $formId
  134. * @return bool
  135. * @throws \think\Exception
  136. * @throws \think\db\exception\DataNotFoundException
  137. * @throws \think\db\exception\ModelNotFoundException
  138. * @throws \think\exception\DbException
  139. */
  140. public static function jsPayPriceBatch($porder_id, $uid, $formId = '')
  141. {
  142. $subOrders = self::getAllSubOrders($porder_id);
  143. if (!$subOrders) return self::setErrorInfo('订单不存在!');
  144. $fieldValues = self::sumFields($subOrders, ['paid']);
  145. if ($fieldValues['paid']) return self::setErrorInfo('该订单已支付!');
  146. $userInfo = User::getUserInfo($uid);
  147. self::beginTrans();
  148. $res1 = $res2 = true;
  149. foreach($subOrders as $orderInfo) {
  150. $res1 = $res1 && UserBill::expend('购买商品', $uid, 'now_money', 'pay_product', $orderInfo['pay_price'], $orderInfo['id'], $userInfo['now_money'], '微信支付' . floatval($orderInfo['pay_price']) . '元购买商品');
  151. $res2 = $res2 && StoreOrder::paySuccess($orderInfo['order_id'], 'weixin', $formId);//微信支付为0时
  152. }
  153. $res = $res1 && $res2;
  154. self::checkTrans($res);
  155. return $res;
  156. }
  157. /**
  158. * 查找购物车里的所有产品标题
  159. * @param $cartId 购物车id
  160. * @return bool|string
  161. */
  162. public static function getProductTitleBatch($subOrders)
  163. {
  164. $title = '';
  165. $cartId = [];
  166. foreach($subOrders as $order) {
  167. $cartId = array_merge($cartId, $order['cart_id']);
  168. }
  169. try {
  170. $orderCart = StoreOrderCartInfo::where('cart_id', 'in', $cartId)->field('cart_info')->select();
  171. foreach ($orderCart as $item) {
  172. if (isset($item['cart_info']['productInfo']['store_name'])) {
  173. $title .= $item['cart_info']['productInfo']['store_name'] . '|';
  174. }
  175. }
  176. unset($item);
  177. if (!$title) {
  178. $productIds = StoreCart::where('id', 'in', $cartId)->column('product_id');
  179. $productlist = ($productlist = StoreProduct::getProductField($productIds, 'store_name')) ? $productlist->toArray() : [];
  180. foreach ($productlist as $item) {
  181. if (isset($item['store_name'])) $title .= $item['store_name'] . '|';
  182. }
  183. }
  184. if ($title) $title = substr($title, 0, strlen($title) - 1);
  185. unset($item);
  186. } catch (\Exception $e) {
  187. }
  188. return $title;
  189. }
  190. /**
  191. * 余额支付
  192. * @param $order_id
  193. * @param $uid
  194. * @param string $formId
  195. * @return bool
  196. * @throws \think\Exception
  197. * @throws \think\db\exception\DataNotFoundException
  198. * @throws \think\db\exception\ModelNotFoundException
  199. * @throws \think\exception\DbException
  200. */
  201. public static function yuePayBatch($porder_id, $uid, $formId = '')
  202. {
  203. $porderInfo = self::where('uid', $uid)->where('porder_id', $porder_id)->find();
  204. if (!$porderInfo) {
  205. return self::setErrorInfo('订单不存在!');
  206. }
  207. $subOrders = self::getAllSubOrders($porder_id);
  208. if (!$subOrders) {
  209. return self::setErrorInfo('订单不存在!');
  210. }
  211. $fieldValues = self::sumFields($subOrders, ['paid']);
  212. if ($fieldValues['paid']) {
  213. return self::setErrorInfo('该订单已支付!');
  214. }
  215. $userInfo = User::getUserInfo($uid);
  216. if ($userInfo['now_money'] < $porderInfo['total_price'])
  217. return self::setErrorInfo(['status' => 'pay_deficiency', 'msg' => '余额不足' . floatval($porderInfo['total_price'])]);
  218. self::beginTrans();
  219. $res1 = $res2 = $res3 = true;
  220. foreach($subOrders as $orderInfo) {
  221. $res1 = $res1 && (false !== User::bcDec($uid, 'now_money', $orderInfo['pay_price'], 'uid'));
  222. $res2 = $res2 && UserBill::expend('购买商品', $uid, 'now_money', 'pay_product', $orderInfo['pay_price'], $orderInfo['id'], $userInfo['now_money'], '余额支付' . floatval($orderInfo['pay_price']) . '元购买商品');
  223. $res3 = $res3 && StoreOrder::paySuccess($orderInfo['order_id'], 'yue', $formId);//余额支付成功
  224. try {
  225. PaymentRepositories::yuePayProduct($userInfo, $orderInfo);
  226. } catch (\Exception $e) {
  227. self::rollbackTrans();
  228. return self::setErrorInfo($e->getMessage());
  229. }
  230. }
  231. $res = $res1 && $res2 && $res3;
  232. self::checkTrans($res);
  233. return $res;
  234. }
  235. public static function paySuccessBatch($porderId, $paytype = 'weixin', $formId = '')
  236. {
  237. $subOrders = self::getAllSubOrders($porderId);
  238. $res = true;
  239. foreach($subOrders as $order) {
  240. $res = $res && StoreOrder::paySuccess($order['order_id'], $paytype, $formId);
  241. }
  242. return $res;
  243. }
  244. /**
  245. * 生成订单
  246. * @param $uid
  247. * @param $key orderKey 由客户端传入,总订单ID
  248. * @param $addressId
  249. * @param $payType weixin / yue
  250. * @param bool $useIntegral: 是否使用积分
  251. * @param int $couponId
  252. * @param string $mark: 订单留言
  253. * @param int $combinationId
  254. * @param int $pinkId
  255. * @param int $seckill_id
  256. * @param int $bargain_id
  257. * @param bool $test
  258. * @param int $isChannel 0, 1, 2 : routine, weixin, weixinh5
  259. * @param int $shipping_type
  260. * @param string $real_name
  261. * @param string $phone
  262. * @param int $storeId: 门店ID
  263. * @return StoreOrder|bool|\think\Model
  264. * @throws \think\Exception
  265. * @throws \think\db\exception\DataNotFoundException
  266. * @throws \think\db\exception\ModelNotFoundException
  267. * @throws \think\exception\DbException
  268. */
  269. public static function cacheKeyCreateOrderBatch($uid, $key, $addressId, $payType, $useIntegral = false, $couponId = 0,
  270. $mark = '', $combinationId = 0, $pinkId = 0, $seckill_id = 0, $bargain_id = 0, $test = false, $isChannel = 0,
  271. $shipping_type = 1, $real_name = '', $phone = '', $storeId = 0)
  272. {
  273. StoreOrder::beginTrans();
  274. try {
  275. $shipping_type = (int)$shipping_type;
  276. // 支付方式判断
  277. $offlinePayStatus = (int)sys_config('offline_pay_status') ?? (int)2; //线下支付状态 2 关闭
  278. if ($offlinePayStatus == 2) {
  279. unset(StoreOrder::$payType['offline']);
  280. }
  281. if (!array_key_exists($payType, StoreOrder::$payType)) {
  282. return self::setErrorInfo('选择支付方式有误!', true);
  283. }
  284. // 查总订单 sql-1
  285. if (self::be(['unique' => $key, 'uid' => $uid])) {
  286. return self::setErrorInfo('请勿重复提交订单', true);
  287. }
  288. // 用户信息 sql-2
  289. $userInfo = User::getUserInfo($uid);
  290. if (!$userInfo) return self::setErrorInfo('用户不存在!', true);
  291. // 取订单缓存
  292. $cartGroup = self::getCacheOrderInfo($uid, $key);
  293. if (!$cartGroup) return self::setErrorInfo('订单已过期,请刷新当前页面!', true);
  294. $cartInfo = $cartGroup['cartInfo'];
  295. $priceGroup = $cartGroup['priceGroup'];
  296. $other = $cartGroup['other'];
  297. // $payPrice = (float)$priceGroup['totalPrice'];
  298. // 收货地址 sql-3
  299. $addr = UserAddress::where('uid', $uid)->where('id', $addressId)->find();
  300. // if ($payType == 'offline' && sys_config('offline_postage') == 1) {
  301. // $payPostage = 0;
  302. // } else {
  303. // $payPostage = $priceGroup['storePostage'];
  304. // }
  305. if ($shipping_type === 1) {
  306. if (!$test && !$addressId) {
  307. return self::setErrorInfo('请选择收货地址!', true);
  308. }
  309. // sql-4,5
  310. if (!$test && (!UserAddress::be(['uid' => $uid, 'id' => $addressId, 'is_del' => 0]) || !($addressInfo = UserAddress::find($addressId))))
  311. return self::setErrorInfo('地址选择有误!', true);
  312. } else {
  313. if ((!$real_name || !$phone) && !$test) {
  314. return self::setErrorInfo('请填写姓名和电话', true);
  315. }
  316. $addressInfo['real_name'] = $real_name;
  317. $addressInfo['phone'] = $phone;
  318. $addressInfo['province'] = '';
  319. $addressInfo['city'] = '';
  320. $addressInfo['district'] = '';
  321. $addressInfo['detail'] = '';
  322. }
  323. $totalPrice = 0.0;
  324. $totalPayPrice = 0.0;
  325. $SurplusIntegral = 0;
  326. $totalPostage = 0.0;
  327. $totalDeductionPrice = 0.0;
  328. $totalCouponPrice = 0.0;
  329. $counter = 0;
  330. $subOrders = [];
  331. // 子订单逐个处理
  332. foreach ($cartInfo as $ci) {
  333. $counter += 1;
  334. // 一个订单的购物车
  335. $subOrderCart = [$ci];
  336. $subOrderId = StoreOrder::getNewOrderId();
  337. $subPriceGroup = StoreOrder::getOrderPriceGroup($subOrderCart, $addr);
  338. if (!$subPriceGroup) {
  339. return false;
  340. }
  341. $payPrice = (float)$subPriceGroup['totalPrice'];
  342. $totalPrice = (float)bcadd($totalPrice, $payPrice, 2);
  343. $cartIds = []; // 子订单购物车ID
  344. $totalNum = 0; // 总数量
  345. $gainIntegral = 0; // 总可得积分
  346. foreach ($subOrderCart as $cart){
  347. // sql-6 x n
  348. if (!$test && !StoreOrder::checkProductStock($uid, $cart['product_id'], $cart['cart_num'], $cart['product_attr_unique'], $cart['combination_id'], $cart['seckill_id'], $cart['bargain_id'])) {
  349. return false;
  350. }
  351. $cartIds[] = $cart['id'];
  352. $totalNum += $cart['cart_num'];
  353. if (!$seckill_id) $seckill_id = $cart['seckill_id'];
  354. if (!$bargain_id) $bargain_id = $cart['bargain_id'];
  355. if (!$combinationId) $combinationId = $cart['combination_id'];
  356. // 可赚积分
  357. $cartInfoGainIntegral = isset($cart['productInfo']['give_integral']) ? bcmul($cart['cart_num'], $cart['productInfo']['give_integral'], 2) : 0;
  358. // 汇总
  359. $gainIntegral = bcadd($gainIntegral, $cartInfoGainIntegral, 2);
  360. } // foreach
  361. // 活动
  362. $deduction = $seckill_id || $bargain_id || $combinationId;
  363. if ($deduction) {
  364. $couponId = 0;
  365. $useIntegral = false;
  366. if (!$test) {
  367. unset(StoreOrder::$payType['offline']);
  368. if (!array_key_exists($payType, StoreOrder::$payType)) {
  369. return self::setErrorInfo('营销产品不能使用线下支付!', true);
  370. }
  371. }
  372. }//
  373. //使用优惠劵
  374. $res1 = true;
  375. if ($couponId) {
  376. $couponInfo = StoreCouponUser::validAddressWhere()->where('id', $couponId)->where('uid', $uid)->find();
  377. if (!$couponInfo) return self::setErrorInfo('选择的优惠劵无效!', true);
  378. $coupons = StoreCouponUser::getUsableCouponList($uid, ['valid' => $subOrderCart], $payPrice);
  379. $flag = false;
  380. foreach ($coupons as $coupon) {
  381. if ($coupon['id'] == $couponId) {
  382. $flag = true;
  383. continue;
  384. }
  385. }
  386. if (!$flag)
  387. return self::setErrorInfo('不满足优惠劵的使用条件!', true);
  388. $payPrice = (float)bcsub($payPrice, $couponInfo['coupon_price'], 2);
  389. $res1 = StoreCouponUser::useCoupon($couponId);
  390. $couponPrice = $couponInfo['coupon_price'];
  391. } else {
  392. $couponId = 0;
  393. $couponPrice = 0;
  394. }
  395. if (!$res1) {
  396. return self::setErrorInfo('使用优惠劵失败!', true);
  397. }
  398. $totalCouponPrice = (float)bcadd($totalCouponPrice, $couponPrice, 2);
  399. //$shipping_type = 1 快递发货 $shipping_type = 2 门店自提
  400. $store_self_mention = sys_config('store_self_mention') ?? 0;
  401. if (!$store_self_mention) $shipping_type = 1;
  402. $payPostage = 0;
  403. if ($shipping_type === 1) {
  404. //是否包邮
  405. if ((isset($other['offlinePostage']) && $other['offlinePostage'] && $payType == 'offline')) {
  406. $payPostage = 0;
  407. }
  408. $payPrice = (float)bcadd($payPrice, $payPostage, 2);
  409. } else if ($shipping_type === 2) {
  410. //门店自提没有邮费支付
  411. $subPriceGroup['storePostage'] = 0;
  412. $payPostage = 0;
  413. if (!$storeId && !$test) {
  414. return self::setErrorInfo('请选择门店', true);
  415. }
  416. }
  417. $totalPostage = (float)bcadd($totalPostage, $payPostage, 2);
  418. //积分抵扣
  419. $res2 = true;
  420. $usedIntegral = 0.0;
  421. if ($useIntegral && $userInfo['integral'] > 0) {
  422. $deductionPrice = (float)bcmul($userInfo['integral'], $other['integralRatio'], 2);
  423. if ($deductionPrice < $payPrice) {
  424. $payPrice = bcsub($payPrice, $deductionPrice, 2);
  425. $usedIntegral = $userInfo['integral'];
  426. $SurplusIntegral = 0;
  427. // sql
  428. $res2 = false !== User::edit(['integral' => 0], $userInfo['uid'], 'uid');
  429. } else {
  430. $deductionPrice = $payPrice;
  431. $usedIntegral = (float)bcdiv($payPrice, $other['integralRatio'], 2);
  432. $SurplusIntegral = bcsub($userInfo['integral'], $usedIntegral, 2);
  433. $res2 = false !== User::bcDec($userInfo['uid'], 'integral', $usedIntegral, 'uid');
  434. $payPrice = 0;
  435. }
  436. $res2 = $res2 && false != UserBill::expend('积分抵扣', $uid, 'integral', 'deduction', $usedIntegral, $subOrderId, $userInfo['integral'], '购买商品使用' . floatval($usedIntegral) . '积分抵扣' . floatval($deductionPrice) . '元');
  437. } else {
  438. $deductionPrice = 0;
  439. $usedIntegral = 0;
  440. }
  441. if (!$res2) {
  442. return self::setErrorInfo('使用积分抵扣失败!', true);
  443. }
  444. // fix at runtime
  445. $userInfo['integral'] = (float)bcsub($userInfo['integral'], $usedIntegral, 2);
  446. if ($userInfo['integral'] < 0) {
  447. $userInfo['integral'] = 0;
  448. }
  449. $totalDeductionPrice = (float)bcadd($totalDeductionPrice, $deductionPrice, 2);
  450. $totalPayPrice = (float)bcadd($totalPayPrice, $payPrice, 2);
  451. if ($test) {
  452. continue;
  453. }
  454. $unique = mb_substr($key, 0, mb_strlen($key)-3) . $counter;
  455. // 增加子订单
  456. $orderInfo = [
  457. 'uid' => $uid,
  458. 'order_id' => $test ? 0 : $subOrderId,
  459. 'real_name' => $addressInfo['real_name'],
  460. 'user_phone' => $addressInfo['phone'],
  461. 'user_address' => $addressInfo['province'] . ' ' . $addressInfo['city'] . ' ' . $addressInfo['district'] . ' ' . $addressInfo['detail'],
  462. 'cart_id' => $cartIds,
  463. 'total_num' => $totalNum,
  464. 'total_price' => $subPriceGroup['totalPrice'],
  465. 'total_postage' => $subPriceGroup['storePostage'],
  466. 'coupon_id' => $couponId,
  467. 'coupon_price' => $couponPrice,
  468. 'pay_price' => $payPrice,
  469. 'pay_postage' => $payPostage,
  470. 'deduction_price' => $deductionPrice,
  471. 'paid' => 0,
  472. 'pay_type' => $payType,
  473. 'use_integral' => $usedIntegral,
  474. 'gain_integral' => $gainIntegral,
  475. 'mark' => htmlspecialchars($mark),
  476. 'combination_id' => $combinationId,
  477. 'pink_id' => $pinkId,
  478. 'seckill_id' => $seckill_id,
  479. 'bargain_id' => $bargain_id,
  480. 'cost' => $subPriceGroup['costPrice'],
  481. 'is_channel' => $isChannel,
  482. 'add_time' => time(),
  483. 'unique' => $unique,
  484. 'shipping_type' => $shipping_type,
  485. ];
  486. if ($shipping_type === 2) {
  487. $orderInfo['verify_code'] = StoreOrder::getStoreCode();
  488. $orderInfo['store_id'] = SystemStore::getStoreDispose($storeId, 'id');
  489. if (!$orderInfo['store_id']) return self::setErrorInfo('暂无门店无法选择门店自提!', true);
  490. }
  491. $order = StoreOrder::create($orderInfo);
  492. if (!$order) {
  493. return self::setErrorInfo('订单生成失败!', true);
  494. }
  495. $res5 = true;
  496. foreach ($subOrderCart as $cart) {
  497. //减库存加销量
  498. if ($combinationId) {
  499. $res5 = $res5 && StoreCombination::decCombinationStock($cart['cart_num'], $combinationId, isset($cart['productInfo']['attrInfo']) ? $cart['productInfo']['attrInfo']['unique'] : '');
  500. } else if ($seckill_id) {
  501. $res5 = $res5 && StoreSeckill::decSeckillStock($cart['cart_num'], $seckill_id, isset($cart['productInfo']['attrInfo']) ? $cart['productInfo']['attrInfo']['unique'] : '');
  502. } else if ($bargain_id) {
  503. $res5 = $res5 && StoreBargain::decBargainStock($cart['cart_num'], $bargain_id, isset($cart['productInfo']['attrInfo']) ? $cart['productInfo']['attrInfo']['unique'] : '');
  504. } else {
  505. $res5 = $res5 && StoreProduct::decProductStock($cart['cart_num'], $cart['productInfo']['id'], isset($cart['productInfo']['attrInfo']) ? $cart['productInfo']['attrInfo']['unique'] : '');
  506. }
  507. }
  508. //保存购物车商品信息
  509. $res4 = false !== StoreOrderCartInfo::setCartInfo($order['id'], $subOrderCart);
  510. //购物车状态修改
  511. $res6 = false !== StoreCart::where('id', 'IN', $cartIds)->update(['is_pay' => 1]);
  512. if (!$res4 || !$res5 || !$res6) {
  513. return self::setErrorInfo('订单生成失败!', true);
  514. }
  515. StoreOrderStatus::status($order['id'], 'cache_key_create_order', '订单生成');
  516. $subOrders[] = $order;
  517. } // foreach
  518. if ($totalPayPrice <= 0) $totalPayPrice = 0;
  519. if ($test) {
  520. StoreOrder::rollbackTrans();
  521. return [
  522. 'total_price' => $totalPrice,
  523. 'pay_price' => $totalPayPrice,
  524. 'pay_postage' => $totalPostage,
  525. 'coupon_price' => $totalCouponPrice,
  526. 'deduction_price' => $totalDeductionPrice,
  527. 'SurplusIntegral' => $SurplusIntegral,
  528. ];
  529. }
  530. $pOrderId = self::getNewPOrderId();
  531. foreach($subOrders as $subOrder) {
  532. $porderInfo = [
  533. 'porder_id' => $pOrderId,
  534. 'oid' => $subOrder['id'],
  535. 'total_price' => $totalPayPrice,
  536. 'unique' => $key,
  537. 'uid' => $uid,
  538. ];
  539. $porder = self::create($porderInfo);
  540. if(!$porder) {
  541. return self::setErrorInfo('订单生成失败!', true);
  542. }
  543. }
  544. //自动设置默认地址
  545. if (count($subOrders)) {
  546. UserRepository::storeProductOrderCreateEbApi($subOrders[0], compact('cartInfo', 'addressId'));
  547. }
  548. StoreOrder::clearCacheOrderInfo($uid, $key);
  549. StoreOrder::commitTrans();
  550. return $pOrderId;
  551. } catch (\PDOException $e) {
  552. StoreOrder::rollbackTrans();
  553. return self::setErrorInfo('生成订单时SQL执行错误错误原因:' . $e->getMessage());
  554. } catch (\Exception $e) {
  555. StoreOrder::rollbackTrans();
  556. return self::setErrorInfo('生成订单时系统错误错误原因:' . $e->getMessage());
  557. }
  558. }
  559. }