Checkout.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\api\service\order;
  13. use app\api\model\User as UserModel;
  14. use app\api\model\Goods as GoodsModel;
  15. use app\api\model\Order as OrderModel;
  16. use app\api\model\Setting as SettingModel;
  17. use app\api\model\UserCoupon as UserCouponModel;
  18. use app\api\service\User as UserService;
  19. use app\api\service\user\Grade as UserGradeService;
  20. use app\api\service\coupon\GoodsDeduct as GoodsDeductService;
  21. use app\api\service\points\GoodsDeduct as PointsDeductService;
  22. use app\api\service\order\source\checkout\Factory as CheckoutFactory;
  23. use app\common\enum\order\OrderType;
  24. use app\common\enum\Setting as SettingEnum;
  25. use app\common\enum\order\OrderStatus as OrderStatusEnum;
  26. use app\common\enum\order\OrderSource as OrderSourceEnum;
  27. use app\common\enum\order\DeliveryType as DeliveryTypeEnum;
  28. use app\common\service\BaseService;
  29. use app\common\service\delivery\Express as ExpressService;
  30. use app\common\service\goods\source\Factory as StockFactory;
  31. use app\common\library\helper;
  32. use cores\exception\BaseException;
  33. /**
  34. * 订单结算台服务类
  35. * Class Checkout
  36. * @package app\api\service\order
  37. */
  38. class Checkout extends BaseService
  39. {
  40. /* $model OrderModel 订单模型 */
  41. public OrderModel $model;
  42. /* @var UserModel $user 当前用户信息 */
  43. private $user;
  44. // 订单结算商品列表
  45. private $goodsList;
  46. /**
  47. * 订单结算api参数
  48. * @var array
  49. */
  50. private array $param = [
  51. 'delivery' => null, // 配送方式
  52. 'couponId' => 0, // 用户的优惠券ID
  53. 'isUsePoints' => 0, // 是否使用积分抵扣
  54. 'remark' => '', // 买家留言
  55. ];
  56. /**
  57. * 订单结算的规则
  58. * @var array
  59. */
  60. private array $checkoutRule = [
  61. 'isUserGrade' => true, // 会员等级折扣
  62. 'isCoupon' => true, // 优惠券抵扣
  63. 'isUsePoints' => true, // 是否使用积分抵扣
  64. ];
  65. /**
  66. * 订单来源
  67. * @var array
  68. */
  69. private array $orderSource = [
  70. 'source' => OrderSourceEnum::MAIN,
  71. 'sourceId' => 0,
  72. 'sourceData' => [],
  73. ];
  74. /**
  75. * 订单结算数据
  76. * @var array
  77. */
  78. private array $orderData = [];
  79. /**
  80. * 构造函数
  81. * Checkout constructor.
  82. * @throws BaseException
  83. */
  84. public function __construct()
  85. {
  86. parent::__construct();
  87. $this->user = UserService::getCurrentLoginUser(true);
  88. $this->model = new OrderModel;
  89. $this->storeId = $this->getStoreId();
  90. }
  91. /**
  92. * 设置结算台请求的参数
  93. * @param $param
  94. * @return array
  95. */
  96. public function setParam($param): array
  97. {
  98. $this->param = array_merge($this->param, $param);
  99. return $this->getParam();
  100. }
  101. /**
  102. * 获取结算台请求的参数
  103. * @return array
  104. */
  105. public function getParam(): array
  106. {
  107. return $this->param;
  108. }
  109. /**
  110. * 订单结算的规则
  111. * @param $data
  112. * @return $this
  113. */
  114. public function setCheckoutRule($data): Checkout
  115. {
  116. $this->checkoutRule = array_merge($this->checkoutRule, $data);
  117. return $this;
  118. }
  119. /**
  120. * 设置订单来源(普通订单)
  121. * @param $data
  122. * @return $this
  123. */
  124. public function setOrderSource($data): Checkout
  125. {
  126. $this->orderSource = array_merge($this->orderSource, $data);
  127. return $this;
  128. }
  129. /**
  130. * 订单确认-结算台
  131. * @param $goodsList
  132. * @return array
  133. * @throws BaseException
  134. * @throws \think\db\exception\DataNotFoundException
  135. * @throws \think\db\exception\DbException
  136. * @throws \think\db\exception\ModelNotFoundException
  137. */
  138. public function onCheckout($goodsList): array
  139. {
  140. // 订单确认-立即购买
  141. $this->goodsList = $goodsList;
  142. return $this->checkout();
  143. }
  144. /**
  145. * 订单结算台
  146. * @return array
  147. * @throws BaseException
  148. * @throws \think\db\exception\DataNotFoundException
  149. * @throws \think\db\exception\DbException
  150. * @throws \think\db\exception\ModelNotFoundException
  151. */
  152. private function checkout(): array
  153. {
  154. // 整理订单数据
  155. $this->orderData = $this->getOrderData();
  156. // 验证商品状态, 是否允许购买
  157. $this->validateGoodsList();
  158. // 订单商品总数量
  159. $orderTotalNum = (int)helper::getArrayColumnSum($this->goodsList, 'total_num');
  160. // 设置订单商品会员折扣价
  161. $this->setOrderGoodsGradeMoney();
  162. // 设置订单商品总金额(不含优惠折扣)
  163. $this->setOrderTotalPrice();
  164. // 当前用户可用的优惠券列表
  165. $couponList = $this->getUserCouponList((float)$this->orderData['orderTotalPrice']);
  166. // 计算优惠券抵扣
  167. $this->setOrderCouponMoney($couponList, (int)$this->param['couponId']);
  168. // 计算可用积分抵扣
  169. $this->setOrderPoints();
  170. // 计算订单商品的实际付款金额
  171. $this->setOrderGoodsPayPrice();
  172. // 设置订单配送信息
  173. $this->setDelivery();
  174. if ($this->param['delivery'] == DeliveryTypeEnum::EXPRESS) {
  175. $this->setOrderExpress();
  176. }
  177. // 计算订单最终金额
  178. $this->setOrderPayPrice();
  179. // 计算订单积分赠送数量
  180. $this->setOrderPointsBonus();
  181. // 返回订单数据
  182. return array_merge([
  183. 'goodsList' => $this->goodsList, // 商品信息
  184. 'orderTotalNum' => $orderTotalNum, // 商品总数量
  185. 'couponList' => array_values($couponList), // 优惠券列表
  186. 'hasError' => $this->hasError(),
  187. 'errorMsg' => $this->getError(),
  188. ], $this->orderData);
  189. }
  190. /**
  191. * 设置订单配送信息
  192. * @throws \think\db\exception\DataNotFoundException
  193. * @throws \think\db\exception\DbException
  194. * @throws \think\db\exception\ModelNotFoundException
  195. */
  196. private function setDelivery()
  197. {
  198. // 设置默认配送方式
  199. if (!$this->param['delivery']) {
  200. $deliveryType = SettingModel::getItem(SettingEnum::DELIVERY)['delivery_type'];
  201. $this->param['delivery'] = current($deliveryType);
  202. }
  203. // 处理配送方式
  204. if ($this->param['delivery'] == DeliveryTypeEnum::EXPRESS) {
  205. $this->setOrderExpress();
  206. }
  207. // 判断商品是否支持配送
  208. $deliveryName = DeliveryTypeEnum::data()[$this->param['delivery']]['name'];
  209. foreach ($this->goodsList as $goods) {
  210. if ($goods['is_ind_delivery_type'] && !in_array($this->param['delivery'], $goods['delivery_type'])) {
  211. $this->setError("很抱歉,商品 [{$goods['goods_name']}] 不支持{$deliveryName}");
  212. }
  213. }
  214. }
  215. /**
  216. * 计算订单可用积分抵扣
  217. * @return void
  218. * @throws \think\db\exception\DataNotFoundException
  219. * @throws \think\db\exception\DbException
  220. * @throws \think\db\exception\ModelNotFoundException
  221. */
  222. private function setOrderPoints(): void
  223. {
  224. // 设置默认的商品积分抵扣信息
  225. $this->setDefaultGoodsPoints();
  226. // 积分设置
  227. $setting = SettingModel::getItem('points');
  228. // 条件:后台开启下单使用积分抵扣
  229. if (
  230. !$this->checkoutRule['isUsePoints']
  231. || !$setting['is_shopping_discount']
  232. ) {
  233. return;
  234. }
  235. // 条件:订单金额满足[?]元
  236. if (helper::bccomp($setting['discount']['full_order_price'], $this->orderData['orderTotalPrice']) === 1) {
  237. return;
  238. }
  239. // 计算订单商品最多可抵扣的积分数量
  240. $this->setOrderGoodsMaxPointsNum();
  241. // 订单最多可抵扣的积分总数量
  242. $maxPointsNumCount = (int)helper::getArrayColumnSum($this->goodsList, 'max_points_num');
  243. // 实际可抵扣的积分数量
  244. $actualPointsNum = min($maxPointsNumCount, $this->user['points']);
  245. if ($actualPointsNum < 1) {
  246. return;
  247. }
  248. // 计算订单商品实际抵扣的积分数量和金额
  249. $GoodsDeduct = new PointsDeductService($this->goodsList);
  250. $GoodsDeduct->setGoodsPoints($maxPointsNumCount, $actualPointsNum);
  251. // 积分抵扣总金额
  252. $orderPointsMoney = helper::getArrayColumnSum($this->goodsList, 'points_money');
  253. $this->orderData['pointsMoney'] = helper::number2($orderPointsMoney);
  254. // 积分抵扣总数量
  255. $this->orderData['pointsNum'] = $actualPointsNum;
  256. // 允许积分抵扣
  257. $this->orderData['isAllowPoints'] = true;
  258. }
  259. /**
  260. * 计算订单商品最多可抵扣的积分数量
  261. * @return void
  262. * @throws \think\db\exception\DataNotFoundException
  263. * @throws \think\db\exception\DbException
  264. * @throws \think\db\exception\ModelNotFoundException
  265. */
  266. private function setOrderGoodsMaxPointsNum(): void
  267. {
  268. // 积分设置
  269. $setting = SettingModel::getItem('points');
  270. foreach ($this->goodsList as &$goods) {
  271. // 商品不允许积分抵扣
  272. if (!$goods['is_points_discount']) continue;
  273. // 积分抵扣比例
  274. $deductionRatio = helper::bcdiv($setting['discount']['max_money_ratio'], 100);
  275. // 最多可抵扣的金额
  276. $totalPayPrice = helper::bcsub($goods['total_price'], $goods['coupon_money']);
  277. $maxPointsMoney = helper::bcmul($totalPayPrice, $deductionRatio);
  278. // 最多可抵扣的积分数量
  279. $goods['max_points_num'] = helper::bcdiv($maxPointsMoney, $setting['discount']['discount_ratio'], 0);
  280. }
  281. }
  282. /**
  283. * 设置默认的商品积分抵扣信息
  284. * @return void
  285. */
  286. private function setDefaultGoodsPoints(): void
  287. {
  288. foreach ($this->goodsList as &$goods) {
  289. // 最多可抵扣的积分数量
  290. $goods['max_points_num'] = 0;
  291. // 实际抵扣的积分数量
  292. $goods['pointsNum'] = 0;
  293. // 实际抵扣的金额
  294. $goods['points_money'] = 0.00;
  295. }
  296. }
  297. /**
  298. * 整理订单数据(结算台初始化)
  299. * @return array
  300. * @throws \think\db\exception\DataNotFoundException
  301. * @throws \think\db\exception\DbException
  302. * @throws \think\db\exception\ModelNotFoundException
  303. */
  304. private function getOrderData(): array
  305. {
  306. return [
  307. // 当前订单类型
  308. 'orderType' => $this->getOrderType(),
  309. // 当前配送方式
  310. 'delivery' => $this->getDeliveryType(),
  311. // 默认地址
  312. 'address' => $this->user['address_default'],
  313. // 是否存在收货地址
  314. 'existAddress' => $this->user['address_id'] > 0,
  315. // 配送费用
  316. 'expressPrice' => 0.00,
  317. // 当前用户收货城市是否存在配送规则中
  318. 'isIntraRegion' => true,
  319. // 是否允许使用积分抵扣
  320. 'isAllowPoints' => false,
  321. // 是否使用积分抵扣
  322. 'isUsePoints' => $this->param['isUsePoints'],
  323. // 积分抵扣金额
  324. 'pointsMoney' => 0.00,
  325. // 赠送的积分数量
  326. 'pointsBonus' => 0,
  327. ];
  328. }
  329. /**
  330. * 获取当前配送方式
  331. * @return int|mixed
  332. * @throws \think\db\exception\DataNotFoundException
  333. * @throws \think\db\exception\DbException
  334. * @throws \think\db\exception\ModelNotFoundException
  335. */
  336. private function getDeliveryType()
  337. {
  338. // 系统默认的配送方式
  339. $setting = SettingModel::getItem(SettingEnum::DELIVERY)['delivery_type'];
  340. // 配送方式:实物订单
  341. if ($this->getOrderType() == OrderType::PHYSICAL) {
  342. return $this->param['delivery'] > 0 ? $this->param['delivery'] : $setting[0];
  343. }
  344. // 配送方式:无需配送
  345. return DeliveryTypeEnum::NOTHING;
  346. }
  347. /**
  348. * 获取订单类型
  349. * @return mixed
  350. */
  351. private function getOrderType()
  352. {
  353. return $this->goodsList[0]['goods_type'];
  354. }
  355. /**
  356. * 获取订单页面中使用到的系统设置
  357. * @return array
  358. * @throws \think\db\exception\DataNotFoundException
  359. * @throws \think\db\exception\DbException
  360. * @throws \think\db\exception\ModelNotFoundException
  361. */
  362. public function getSetting(): array
  363. {
  364. // 系统支持的配送方式 (后台设置)
  365. $deliveryType = SettingModel::getItem(SettingEnum::DELIVERY)['delivery_type'];
  366. // 积分设置
  367. $pointsSetting = SettingModel::getItem(SettingEnum::POINTS);
  368. return [
  369. 'deliveryType' => $deliveryType, // 支持的配送方式
  370. 'points_name' => $pointsSetting['points_name'], // 积分名称
  371. 'points_describe' => $pointsSetting['describe'], // 积分说明
  372. ];
  373. }
  374. /**
  375. * 获取订单结算时的个人信息
  376. * @return array
  377. */
  378. public function getPersonal(): array
  379. {
  380. return [
  381. 'user_id' => $this->user['user_id'],
  382. 'balance' => $this->user['balance'],
  383. 'points' => $this->user['points'],
  384. 'address_id' => $this->user['address_id'],
  385. ];
  386. }
  387. /**
  388. * 当前用户可用的优惠券列表
  389. * @param float $orderTotalPrice 总金额
  390. * @return array
  391. * @throws \think\db\exception\DbException
  392. */
  393. private function getUserCouponList(float $orderTotalPrice): array
  394. {
  395. // 是否开启优惠券折扣
  396. if (!$this->checkoutRule['isCoupon']) {
  397. return [];
  398. }
  399. // 整理当前订单所有商品ID集
  400. $orderGoodsIds = helper::getArrayColumn($this->goodsList, 'goods_id');
  401. // 当前用户可用的优惠券列表
  402. $couponList = UserCouponModel::getUserCouponList($this->user['user_id'], $orderTotalPrice);
  403. // 判断当前优惠券是否满足订单使用条件 (优惠券适用范围)
  404. return UserCouponModel::couponListApplyRange($couponList, $orderGoodsIds);
  405. }
  406. /**
  407. * 验证订单商品的状态
  408. * @return void
  409. */
  410. private function validateGoodsList(): void
  411. {
  412. $Checkout = CheckoutFactory::getFactory(
  413. $this->user,
  414. $this->goodsList,
  415. $this->orderSource['source']
  416. );
  417. if (!$Checkout->validateGoodsList()) {
  418. $this->setError($Checkout->getError());
  419. }
  420. }
  421. /**
  422. * 设置订单的商品总金额(不含优惠折扣)
  423. */
  424. private function setOrderTotalPrice()
  425. {
  426. // 订单商品的总金额(不含优惠券折扣)
  427. $this->orderData['orderTotalPrice'] = helper::number2(helper::getArrayColumnSum($this->goodsList, 'total_price'));
  428. }
  429. /**
  430. * 设置订单的实际支付金额(含配送费)
  431. */
  432. private function setOrderPayPrice()
  433. {
  434. // 订单金额(含优惠折扣)
  435. $this->orderData['orderPrice'] = helper::number2(helper::getArrayColumnSum($this->goodsList, 'total_pay_price'));
  436. // 订单实付款金额(订单金额 + 运费)
  437. $this->orderData['orderPayPrice'] = helper::number2(helper::bcadd($this->orderData['orderPrice'], $this->orderData['expressPrice']));
  438. }
  439. /**
  440. * 计算订单积分赠送数量
  441. * @return void
  442. * @throws \think\db\exception\DataNotFoundException
  443. * @throws \think\db\exception\DbException
  444. * @throws \think\db\exception\ModelNotFoundException
  445. */
  446. private function setOrderPointsBonus(): void
  447. {
  448. // 初始化商品积分赠送数量
  449. foreach ($this->goodsList as &$goods) {
  450. $goods['points_bonus'] = 0;
  451. }
  452. // 积分设置
  453. $setting = SettingModel::getItem('points');
  454. // 条件:后台开启开启购物送积分
  455. if (!$setting['is_shopping_gift']) {
  456. return;
  457. }
  458. // 设置商品积分赠送数量
  459. foreach ($this->goodsList as &$goods) {
  460. // 积分赠送比例
  461. $ratio = helper::bcdiv($setting['gift_ratio'], 100);
  462. // 计算抵扣积分数量
  463. $goods['points_bonus'] = !$goods['is_points_gift'] ? 0 : helper::bcmul($goods['total_pay_price'], $ratio, 0);
  464. }
  465. // 订单积分赠送数量
  466. $this->orderData['pointsBonus'] = (int)helper::getArrayColumnSum($this->goodsList, 'points_bonus');
  467. }
  468. /**
  469. * 计算订单商品的实际付款金额
  470. * @return void
  471. */
  472. private function setOrderGoodsPayPrice(): void
  473. {
  474. // 商品总价 - 优惠抵扣
  475. foreach ($this->goodsList as &$goods) {
  476. // 减去优惠券抵扣金额
  477. $value = helper::bcsub($goods['total_price'], $goods['coupon_money']);
  478. // 减去积分抵扣金额
  479. if ($this->orderData['isAllowPoints'] && $this->orderData['isUsePoints']) {
  480. $value = helper::bcsub($value, $goods['points_money']);
  481. }
  482. $goods['total_pay_price'] = helper::number2($value);
  483. }
  484. }
  485. /**
  486. * 设置订单商品会员折扣价
  487. * @return void
  488. * @throws BaseException
  489. * @throws \think\db\exception\DataNotFoundException
  490. * @throws \think\db\exception\DbException
  491. * @throws \think\db\exception\ModelNotFoundException
  492. */
  493. private function setOrderGoodsGradeMoney(): void
  494. {
  495. // 设置默认数据
  496. helper::setDataAttribute($this->goodsList, [
  497. // 标记参与会员折扣
  498. 'is_user_grade' => false,
  499. // 会员等级抵扣的金额
  500. 'grade_ratio' => 0,
  501. // 会员折扣的商品单价
  502. 'grade_goods_price' => 0.00,
  503. // 会员折扣的总额差
  504. 'grade_total_money' => 0.00,
  505. ], true);
  506. // 是否开启会员等级折扣
  507. if (!$this->checkoutRule['isUserGrade']) {
  508. return;
  509. }
  510. // 获取当前登录用户的会员等级信息
  511. $gradeInfo = UserGradeService::getCurrentGradeInfo();
  512. // 判断商品是否参与会员折扣
  513. if (empty($gradeInfo)) {
  514. return;
  515. }
  516. // 计算抵扣金额
  517. foreach ($this->goodsList as &$goods) {
  518. // 判断商品是否参与会员折扣
  519. if (!$goods['is_enable_grade']) {
  520. continue;
  521. }
  522. // 折扣比例
  523. $discountRatio = $gradeInfo['equity']['discount'];
  524. // 商品单独设置了会员折扣
  525. if ($goods['is_alone_grade'] && isset($goods['alone_grade_equity'][$this->user['grade_id']])) {
  526. $discountRatio = $goods['alone_grade_equity'][$gradeInfo['grade_id']];
  527. }
  528. if (empty($discountRatio)) {
  529. continue;
  530. }
  531. // 会员折扣后的商品总金额
  532. $gradeTotalPrice = UserGradeService::getDiscountPrice($goods['total_price'], $discountRatio);
  533. helper::setDataAttribute($goods, [
  534. 'is_user_grade' => true,
  535. 'grade_ratio' => $discountRatio,
  536. 'grade_goods_price' => UserGradeService::getDiscountPrice($goods['goods_price'], $discountRatio),
  537. 'grade_total_money' => helper::bcsub($goods['total_price'], $gradeTotalPrice),
  538. 'total_price' => $gradeTotalPrice,
  539. ], false);
  540. }
  541. }
  542. /**
  543. * 设置订单优惠券抵扣信息
  544. * @param array $couponList 当前用户可用的优惠券列表
  545. * @param int $userCouponId 当前选择的优惠券ID
  546. * @return void
  547. * @throws BaseException
  548. */
  549. private function setOrderCouponMoney(array $couponList, int $userCouponId): void
  550. {
  551. // 设置默认数据:订单信息
  552. helper::setDataAttribute($this->orderData, [
  553. 'couponId' => 0, // 用户的优惠券ID
  554. 'couponMoney' => 0, // 优惠券抵扣金额
  555. ], false);
  556. // 设置默认数据:订单商品列表
  557. helper::setDataAttribute($this->goodsList, [
  558. 'coupon_money' => 0, // 优惠券抵扣金额
  559. ], true);
  560. // 验证选择的优惠券ID是否合法
  561. if (!$this->verifyOrderCouponId($userCouponId, $couponList)) {
  562. return;
  563. }
  564. // 获取优惠券信息
  565. $couponInfo = $this->getCouponInfo($userCouponId, $couponList);
  566. // 计算订单商品优惠券抵扣金额
  567. $goodsListTemp = helper::getArrayColumns($this->goodsList, ['goods_id', 'goods_sku_id', 'total_price']);
  568. $CouponMoney = new GoodsDeductService;
  569. $rangeGoodsList = $CouponMoney->setGoodsList($goodsListTemp)
  570. ->setCouponInfo($couponInfo)
  571. ->setGoodsCouponMoney()
  572. ->getRangeGoodsList();
  573. // 分配订单商品优惠券抵扣金额
  574. foreach ($this->goodsList as &$goods) {
  575. $goodsKey = "{$goods['goods_id']}-{$goods['goods_sku_id']}";
  576. if (isset($rangeGoodsList[$goodsKey])) {
  577. $goods['coupon_money'] = helper::bcdiv($rangeGoodsList[$goodsKey]['coupon_money'], 100);
  578. }
  579. }
  580. // 记录订单优惠券信息
  581. $this->orderData['couponId'] = $userCouponId;
  582. $this->orderData['couponMoney'] = helper::number2(helper::bcdiv($CouponMoney->getActualReducedMoney(), 100));
  583. }
  584. /**
  585. * 验证用户选择的优惠券ID是否合法
  586. * @param int $userCouponId
  587. * @param $couponList
  588. * @return bool
  589. * @throws BaseException
  590. */
  591. private function verifyOrderCouponId(int $userCouponId, $couponList): bool
  592. {
  593. // 是否开启优惠券折扣
  594. if (!$this->checkoutRule['isCoupon']) {
  595. return false;
  596. }
  597. // 如果没有可用的优惠券,直接返回
  598. if ($userCouponId <= 0 || empty($couponList)) {
  599. return false;
  600. }
  601. // 判断优惠券是否存在
  602. $couponInfo = $this->getCouponInfo($userCouponId, $couponList);
  603. if (!$couponInfo) {
  604. throwError('未找到优惠券信息');
  605. }
  606. // 判断优惠券适用范围是否合法
  607. if (!$couponInfo['is_apply']) {
  608. throwError($couponInfo['not_apply_info']);
  609. }
  610. return true;
  611. }
  612. /**
  613. * 查找指定的优惠券信息
  614. * @param int $userCouponId 用户优惠券ID
  615. * @param array $couponList 优惠券列表
  616. * @return false|mixed
  617. */
  618. private function getCouponInfo(int $userCouponId, array $couponList)
  619. {
  620. return helper::getArrayItemByColumn($couponList, 'user_coupon_id', $userCouponId);
  621. }
  622. /**
  623. * 订单配送-快递配送
  624. * @return void
  625. * @throws \think\db\exception\DataNotFoundException
  626. * @throws \think\db\exception\DbException
  627. * @throws \think\db\exception\ModelNotFoundException
  628. */
  629. private function setOrderExpress(): void
  630. {
  631. // 设置默认数据:配送费用
  632. helper::setDataAttribute($this->goodsList, [
  633. 'expressPrice' => 0,
  634. ], true);
  635. // 当前用户收货城市id
  636. $cityId = $this->user['address_default'] ? (int)$this->user['address_default']['city_id'] : 0;
  637. // 初始化配送服务类
  638. $ExpressService = new ExpressService($cityId, $this->goodsList);
  639. // 验证商品是否在配送范围
  640. $isIntraRegion = $ExpressService->isIntraRegion();
  641. if ($cityId > 0 && !$isIntraRegion) {
  642. $notInRuleGoodsName = $ExpressService->getNotInRuleGoodsName();
  643. $this->setError("很抱歉,您的收货地址不在商品 [{$notInRuleGoodsName}] 的配送范围内");
  644. }
  645. // 订单总运费金额
  646. $this->orderData['isIntraRegion'] = $isIntraRegion;
  647. $this->orderData['expressPrice'] = $ExpressService->getDeliveryFee();
  648. }
  649. /**
  650. * 创建新订单
  651. * @param array $order 订单信息
  652. * @return bool
  653. */
  654. public function createOrder(array $order): bool
  655. {
  656. // 表单验证
  657. if (!$this->validateOrderForm($order)) {
  658. return false;
  659. }
  660. // 创建新的订单
  661. return $this->model->transaction(function () use ($order) {
  662. return $this->createOrderEvent($order);
  663. });
  664. }
  665. /**
  666. * 创建订单事件
  667. * @param $order
  668. * @return bool
  669. * @throws BaseException
  670. * @throws \think\db\exception\DataNotFoundException
  671. * @throws \think\db\exception\DbException
  672. * @throws \think\db\exception\ModelNotFoundException
  673. */
  674. private function createOrderEvent($order): bool
  675. {
  676. // 新增订单记录
  677. $this->add($order, $this->param['remark']);
  678. // 保存订单数据 (根据订单类型)
  679. $order['orderType'] == 10 && $this->saveOrderByPhysical($order);
  680. // 保存订单商品信息
  681. $this->saveOrderGoods($order);
  682. // 更新商品库存 (针对下单减库存的商品)
  683. $this->updateGoodsStockNum($order);
  684. // 设置优惠券使用状态
  685. $order['couponId'] > 0 && UserCouponModel::setIsUse((int)$order['couponId']);
  686. // 积分抵扣情况下扣除用户积分
  687. if ($order['isAllowPoints'] && $order['isUsePoints'] && $order['pointsNum'] > 0) {
  688. $describe = "用户消费:{$this->model['order_no']}";
  689. UserModel::setIncPoints($this->user['user_id'], -$order['pointsNum'], $describe);
  690. }
  691. // 获取订单详情
  692. $detail = OrderModel::getDetail((int)$this->model['order_id'], ['goods']);
  693. return true;
  694. }
  695. /**
  696. * 保存订单数据 (实物商品)
  697. * @param $order
  698. * @return void
  699. */
  700. private function saveOrderByPhysical($order)
  701. {
  702. if ($order['delivery'] == DeliveryTypeEnum::EXPRESS) {
  703. // 记录收货地址
  704. $this->saveOrderAddress($order['address']);
  705. }
  706. }
  707. /**
  708. * 表单验证 (订单提交)
  709. * @param array $order 订单信息
  710. * @return bool
  711. */
  712. private function validateOrderForm(array $order): bool
  713. {
  714. if ($order['orderType'] == 10) {
  715. return $this->validateOrderFormByPhysical($order);
  716. }
  717. return true;
  718. }
  719. /**
  720. * 表单验证:实物订单
  721. * @param array $order
  722. * @return bool
  723. */
  724. private function validateOrderFormByPhysical(array $order): bool
  725. {
  726. if ($order['delivery'] == DeliveryTypeEnum::EXPRESS) {
  727. if (empty($order['address'])) {
  728. $this->error = '您还没有选择配送地址';
  729. return false;
  730. }
  731. }
  732. return true;
  733. }
  734. /**
  735. * 当前订单是否存在和使用积分抵扣
  736. * @param $order
  737. * @return bool
  738. */
  739. private function isExistPointsDeduction($order): bool
  740. {
  741. return $order['isAllowPoints'] && $order['isUsePoints'];
  742. }
  743. /**
  744. * 新增订单记录
  745. * @param $order
  746. * @param string $remark
  747. * @return void
  748. */
  749. private function add($order, string $remark = ''): void
  750. {
  751. // 当前订单是否存在和使用积分抵扣
  752. $isExistPointsDeduction = $this->isExistPointsDeduction($order);
  753. // 订单数据
  754. $data = [
  755. 'user_id' => $this->user['user_id'],
  756. 'order_no' => $this->model->orderNo(),
  757. 'order_type' => $order['orderType'],
  758. 'total_price' => $order['orderTotalPrice'],
  759. 'order_price' => $order['orderPrice'],
  760. 'coupon_id' => $order['couponId'],
  761. 'coupon_money' => $order['couponMoney'],
  762. 'points_money' => $isExistPointsDeduction ? $order['pointsMoney'] : 0.00,
  763. 'points_num' => $isExistPointsDeduction ? $order['pointsNum'] : 0,
  764. 'pay_price' => $order['orderPayPrice'],
  765. 'delivery_type' => $order['delivery'],
  766. 'buyer_remark' => trim($remark),
  767. 'order_source' => $this->orderSource['source'],
  768. 'order_source_id' => $this->orderSource['sourceId'],
  769. 'order_source_data' => $this->orderSource['sourceData'],
  770. 'points_bonus' => $order['pointsBonus'],
  771. 'order_status' => OrderStatusEnum::NORMAL,
  772. 'platform' => getPlatform(),
  773. 'store_id' => $this->storeId,
  774. ];
  775. if ($order['delivery'] == DeliveryTypeEnum::EXPRESS) {
  776. $data['express_price'] = $order['expressPrice'];
  777. }
  778. // 保存订单记录
  779. $this->model->save($data);
  780. }
  781. /**
  782. * 保存订单商品信息
  783. * @param $order
  784. * @return void
  785. */
  786. private function saveOrderGoods($order): void
  787. {
  788. // 当前订单是否存在和使用积分抵扣
  789. $isExistPointsDeduction = $this->isExistPointsDeduction($order);
  790. // 订单商品列表
  791. $goodsList = [];
  792. foreach ($order['goodsList'] as $goods) {
  793. /* @var GoodsModel $goods */
  794. $item = [
  795. 'user_id' => $this->user['user_id'],
  796. 'store_id' => $this->storeId,
  797. 'goods_id' => $goods['goods_id'],
  798. 'goods_type' => $goods['goods_type'],
  799. 'goods_name' => $goods['goods_name'],
  800. 'goods_no' => $goods['goods_no'] ?: '',
  801. 'image_id' => $this->getGoodsImageId($goods),
  802. 'deduct_stock_type' => $goods['deduct_stock_type'],
  803. 'spec_type' => $goods['spec_type'],
  804. 'goods_sku_id' => $goods['skuInfo']['goods_sku_id'],
  805. 'goods_props' => $goods['skuInfo']['goods_props'] ?: '',
  806. 'content' => $goods['content'] ?? '',
  807. 'goods_sku_no' => $goods['skuInfo']['goods_sku_no'] ?: '',
  808. 'goods_price' => $goods['skuInfo']['goods_price'],
  809. 'line_price' => $goods['skuInfo']['line_price'],
  810. 'goods_weight' => $goods['skuInfo']['goods_weight'],
  811. 'is_user_grade' => (int)$goods['is_user_grade'],
  812. 'grade_ratio' => $goods['grade_ratio'],
  813. 'grade_goods_price' => $goods['grade_goods_price'],
  814. 'grade_total_money' => $goods['grade_total_money'],
  815. 'coupon_money' => $goods['coupon_money'],
  816. 'points_money' => $isExistPointsDeduction ? $goods['points_money'] : 0.00,
  817. 'points_num' => $isExistPointsDeduction ? $goods['points_num'] : 0,
  818. 'points_bonus' => $goods['points_bonus'],
  819. 'total_num' => $goods['total_num'],
  820. 'total_price' => $goods['total_price'],
  821. 'total_pay_price' => $goods['total_pay_price'],
  822. ];
  823. // 记录订单商品来源ID
  824. $item['goods_source_id'] = isset($goods['goods_source_id']) ? $goods['goods_source_id'] : 0;
  825. $goodsList[] = $item;
  826. }
  827. $this->model->goods()->saveAll($goodsList) !== false;
  828. }
  829. /**
  830. * 获取订单商品的封面图(优先sku封面图)
  831. * @param $goods
  832. * @return int
  833. */
  834. private function getGoodsImageId($goods): int
  835. {
  836. return $goods['skuInfo']['image_id'] ?: (int)current($goods['goods_images'])['file_id'];
  837. }
  838. /**
  839. * 更新商品库存 (针对下单减库存的商品)
  840. * @param $order
  841. * @return void
  842. */
  843. private function updateGoodsStockNum($order): void
  844. {
  845. StockFactory::getFactory($this->model['order_source'])->updateGoodsStock($order['goodsList']);
  846. }
  847. /**
  848. * 记录收货地址
  849. * @param $address
  850. * @return void
  851. */
  852. private function saveOrderAddress($address): void
  853. {
  854. $this->model->address()->save([
  855. 'user_id' => $this->user['user_id'],
  856. 'store_id' => $this->storeId,
  857. 'name' => $address['name'],
  858. 'phone' => $address['phone'],
  859. 'province_id' => $address['province_id'],
  860. 'city_id' => $address['city_id'],
  861. 'region_id' => $address['region_id'],
  862. 'detail' => $address['detail'],
  863. ]);
  864. }
  865. }