StoreOrderCreateServices.php 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\order;
  12. use app\services\activity\advance\StoreAdvanceServices;
  13. use app\services\activity\combination\StorePinkServices;
  14. use app\services\agent\AgentLevelServices;
  15. use app\services\activity\coupon\StoreCouponUserServices;
  16. use app\services\agent\DivisionServices;
  17. use app\services\product\product\StoreCategoryServices;
  18. use app\services\product\product\StoreProductGiftGroupService;
  19. use app\services\shipping\ShippingTemplatesFreeServices;
  20. use app\services\shipping\ShippingTemplatesRegionServices;
  21. use app\services\shipping\ShippingTemplatesServices;
  22. use app\services\user\UserInvoiceServices;
  23. use app\services\wechat\WechatUserServices;
  24. use app\services\BaseServices;
  25. use crmeb\exceptions\ApiException;
  26. use crmeb\exceptions\ApiStatusException;
  27. use crmeb\services\CacheService;
  28. use app\dao\order\StoreOrderDao;
  29. use app\services\user\UserServices;
  30. use app\services\user\UserBillServices;
  31. use app\services\user\UserAddressServices;
  32. use app\services\activity\bargain\StoreBargainServices;
  33. use app\services\activity\seckill\StoreSeckillServices;
  34. use app\services\system\store\SystemStoreServices;
  35. use app\services\activity\combination\StoreCombinationServices;
  36. use app\services\product\product\StoreProductServices;
  37. use think\facade\Cache;
  38. use think\facade\Config;
  39. use think\facade\Log;
  40. /**
  41. * 订单创建
  42. * Class StoreOrderCreateServices
  43. * @package app\services\order
  44. */
  45. class StoreOrderCreateServices extends BaseServices
  46. {
  47. /**
  48. * StoreOrderCreateServices constructor.
  49. * @param StoreOrderDao $dao
  50. */
  51. public function __construct(StoreOrderDao $dao)
  52. {
  53. $this->dao = $dao;
  54. }
  55. /**
  56. * 使用雪花算法生成订单ID
  57. * @return string
  58. * @throws \Exception
  59. */
  60. public function getNewOrderId(string $prefix = 'wx')
  61. {
  62. $snowflake = new \Godruoyi\Snowflake\Snowflake();
  63. if (Config::get('cache.default') == 'file') {
  64. //32位
  65. if (PHP_INT_SIZE == 4) {
  66. $id = abs($snowflake->id());
  67. } else {
  68. $id = $snowflake->setStartTimeStamp(strtotime('2022-01-01') * 1000)->id();
  69. }
  70. $replace = '';
  71. $chars = '0123456789';
  72. for ($i = 0; $i < 6; $i++) {
  73. $replace .= $chars[mt_rand(0, strlen($chars) - 1)];
  74. }
  75. $id = substr_replace($id, $replace, -6);
  76. } else {
  77. $is_callable = function ($currentTime) {
  78. $redis = Cache::store('redis');
  79. $swooleSequenceResolver = new \Godruoyi\Snowflake\RedisSequenceResolver($redis->handler());
  80. return $swooleSequenceResolver->sequence($currentTime);
  81. };
  82. //32位
  83. if (PHP_INT_SIZE == 4) {
  84. $id = abs($snowflake->setSequenceResolver($is_callable)->id());
  85. } else {
  86. $id = $snowflake->setStartTimeStamp(strtotime('2022-01-01') * 1000)->setSequenceResolver($is_callable)->id();
  87. }
  88. }
  89. return $prefix . $id;
  90. }
  91. /**
  92. * 核销订单生成核销码
  93. * @return false|string
  94. */
  95. public function getStoreCode()
  96. {
  97. list($msec, $sec) = explode(' ', microtime());
  98. $num = time() + mt_rand(10, 999999) . '' . substr($msec, 2, 3);//生成随机数
  99. if (strlen($num) < 12)
  100. $num = str_pad((string)$num, 12, 0, STR_PAD_RIGHT);
  101. else
  102. $num = substr($num, 0, 12);
  103. if ($this->dao->count(['verify_code' => $num])) {
  104. return $this->getStoreCode();
  105. }
  106. return $num;
  107. }
  108. /**
  109. * 创建订单
  110. * @param $uid
  111. * @param $key
  112. * @param $userInfo
  113. * @param $addressId
  114. * @param $payType
  115. * @param false $useIntegral
  116. * @param int $couponId
  117. * @param string $mark
  118. * @param int $combinationId
  119. * @param int $pinkId
  120. * @param int $seckillId
  121. * @param int $bargainId
  122. * @param int $shippingType
  123. * @param string $real_name
  124. * @param string $phone
  125. * @param int $storeId
  126. * @param false $news
  127. * @param int $advanceId
  128. * @param array $customForm
  129. * @param int $invoice_id
  130. * @return mixed
  131. * @throws \think\db\exception\DataNotFoundException
  132. * @throws \think\db\exception\DbException
  133. * @throws \think\db\exception\ModelNotFoundException
  134. * @author 吴汐
  135. * @email 442384644@qq.com
  136. * @date 2023/03/01
  137. */
  138. public function createOrder($uid, $key, $userInfo, $addressId, $payType, $useIntegral = false, $couponId = 0, $mark = '', $combinationId = 0, $pinkId = 0, $seckillId = 0, $bargainId = 0, $shippingType = 1, $real_name = '', $phone = '', $storeId = 0, $news = false, $advanceId = 0, $customForm = [], $invoice_id = 0, $is_gift = 0, $gift_mark = '', $lb_spread_uid = 0)
  139. {
  140. try {
  141. /** @var StoreOrderServices $orderService */
  142. $storeOrderServices = app()->make(StoreOrderServices::class);
  143. $bargainServices = app()->make(StoreBargainServices::class);
  144. $cartGroup = $storeOrderServices->getCacheOrderInfo($uid, $key);
  145. if (!$cartGroup) {
  146. throw new ApiException(410208);
  147. }
  148. //下单前砍价验证
  149. if ($bargainId) {
  150. $bargainServices->checkBargainUser((int)$bargainId, $uid);
  151. }
  152. if ($pinkId) {
  153. $pinkId = (int)$pinkId;
  154. /** @var StorePinkServices $pinkServices */
  155. $pinkServices = app()->make(StorePinkServices::class);
  156. if ($pinkServices->isPink($pinkId, $uid))
  157. throw new ApiStatusException('ORDER_EXIST', 410210, ['orderId' => $storeOrderServices->getStoreIdPink($pinkId, $uid)]);
  158. if ($storeOrderServices->getIsOrderPink($pinkId, $uid))
  159. throw new ApiStatusException('ORDER_EXIST', 410211, ['orderId' => $storeOrderServices->getStoreIdPink($pinkId, $uid)]);
  160. }
  161. $virtual_type = $cartGroup['cartInfo'][0]['productInfo']['virtual_type'] ?? 0;
  162. //下单前发票验证
  163. if ($invoice_id) {
  164. app()->make(UserInvoiceServices::class)->checkInvoice((int)$invoice_id, $uid);
  165. }
  166. /** @var StoreOrderComputedServices $computedServices */
  167. $computedServices = app()->make(StoreOrderComputedServices::class);
  168. $priceData = $computedServices->computedOrder($uid, $userInfo, $cartGroup, $addressId, $payType, $useIntegral, $couponId, true, $shippingType, $is_gift);
  169. /** @var WechatUserServices $wechatServices */
  170. $wechatServices = app()->make(WechatUserServices::class);
  171. /** @var UserAddressServices $addressServices */
  172. $addressServices = app()->make(UserAddressServices::class);
  173. if ($is_gift == 0) {
  174. if ($shippingType == 1 && $virtual_type == 0) {
  175. if (!$addressId) {
  176. throw new ApiException(410045);
  177. }
  178. if (!$addressInfo = $addressServices->getOne(['uid' => $uid, 'id' => $addressId, 'is_del' => 0]))
  179. throw new ApiException(410046);
  180. $addressInfo = $addressInfo->toArray();
  181. } else {
  182. if ((!$real_name || !$phone) && $virtual_type == 0) {
  183. throw new ApiException(410245);
  184. }
  185. $addressInfo['real_name'] = $real_name;
  186. $addressInfo['phone'] = $phone;
  187. $addressInfo['province'] = '';
  188. $addressInfo['city'] = '';
  189. $addressInfo['district'] = '';
  190. $addressInfo['detail'] = '';
  191. }
  192. } else {
  193. $addressInfo['real_name'] = '';
  194. $addressInfo['phone'] = '';
  195. $addressInfo['province'] = '';
  196. $addressInfo['city'] = '';
  197. $addressInfo['district'] = '';
  198. $addressInfo['detail'] = '';
  199. }
  200. $cartInfo = $cartGroup['cartInfo'];
  201. $priceGroup = $cartGroup['priceGroup'];
  202. $cartIds = [];
  203. $totalNum = 0;
  204. $gainIntegral = 0;
  205. $is_lb = 0;
  206. foreach ($cartInfo as $cart) {
  207. $cartIds[] = $cart['id'];
  208. $totalNum += $cart['cart_num'];
  209. if (!$seckillId) $seckillId = $cart['seckill_id'];
  210. if (!$bargainId) $bargainId = $cart['bargain_id'];
  211. if (!$combinationId) $combinationId = $cart['combination_id'];
  212. if (!$advanceId) $advanceId = $cart['advance_id'];
  213. $cartInfoGainIntegral = isset($cart['productInfo']['give_integral']) ? bcmul((string)$cart['cart_num'], (string)$cart['productInfo']['give_integral'], 0) : 0;
  214. $gainIntegral = bcadd((string)$gainIntegral, (string)$cartInfoGainIntegral, 0);
  215. if ($cart['productInfo']['is_lb']==1){
  216. $is_lb = 1;
  217. }
  218. // @file_put_contents('quanju.txt', json_encode($cart) . "-购物车\r\n", 8);
  219. }
  220. // if ($is_lb == 1){
  221. // if ($lb_spread_uid == 0){
  222. // throw new ApiException('没有推荐关系或者推荐人的用户无法购买,请重新扫描推荐二维码');
  223. // }
  224. // /** @var StoreProductGiftGroupService $storeProductGiftGroupService */
  225. // $storeProductGiftGroupService = app()->make(StoreProductGiftGroupService::class);
  226. //
  227. // // 检查用户是否已在礼包关系表中存在(作为uid或pid)
  228. // $existsAsUid = $storeProductGiftGroupService->getOne(['uid' => $uid]);
  229. // $existsAsPid = $storeProductGiftGroupService->getOne(['pid' => $uid]);
  230. //
  231. // // 如果两者都不存在,说明用户还没参与礼包推荐关系,无法购买
  232. // if (!$existsAsUid && !$existsAsPid) {
  233. // throw new ApiException('没有推荐关系或者推荐人的用户无法购买,请重新扫描推荐二维码');
  234. // }
  235. //
  236. // }
  237. if (count($cartInfo) == 1 && isset($cartInfo[0]['productInfo']['presale']) && $cartInfo[0]['productInfo']['presale'] == 1) {
  238. $advance_id = $cartInfo[0]['product_id'];
  239. } else {
  240. $advance_id = 0;
  241. }
  242. $deduction = $seckillId || $bargainId || $combinationId;
  243. if ($deduction) {
  244. $couponId = 0;
  245. $gainIntegral = 0;
  246. $useIntegral = false;
  247. }
  248. //$shipping_type = 1 快递发货 $shipping_type = 2 门店自提
  249. $storeSelfMention = sys_config('store_self_mention') ?? 0;
  250. if (!$storeSelfMention) $shippingType = 1;
  251. if ($is_gift == 1) $shippingType = 0;
  252. $orderInfo = [
  253. 'uid' => $uid,
  254. 'order_id' => $this->getNewOrderId('cp'),
  255. 'real_name' => $addressInfo['real_name'],
  256. 'user_phone' => $addressInfo['phone'],
  257. 'user_address' => $addressInfo['province'] . ' ' . $addressInfo['city'] . ' ' . $addressInfo['district'] . ' ' . $addressInfo['detail'],
  258. 'cart_id' => $cartIds,
  259. 'total_num' => $totalNum,
  260. 'total_price' => $priceGroup['totalPrice'],
  261. 'total_postage' => $shippingType == 1 ? $priceGroup['storePostage'] : 0,
  262. 'coupon_id' => $couponId,
  263. 'coupon_price' => $priceData['coupon_price'],
  264. 'pay_price' => $priceData['pay_price'],
  265. 'pay_postage' => $priceData['pay_postage'],
  266. 'deduction_price' => $priceData['deduction_price'],
  267. 'gift_price' => $priceData['gift_price'],
  268. 'paid' => 0,
  269. 'pay_type' => $payType,
  270. 'use_integral' => $priceData['usedIntegral'],
  271. 'gain_integral' => $gainIntegral,
  272. 'mark' => htmlspecialchars($mark),
  273. 'combination_id' => $combinationId,
  274. 'pink_id' => $pinkId,
  275. 'seckill_id' => $seckillId,
  276. 'bargain_id' => $bargainId,
  277. 'advance_id' => $advance_id,
  278. 'cost' => $priceGroup['costPrice'],
  279. 'add_time' => time(),
  280. 'unique' => $key,
  281. 'shipping_type' => $shippingType,
  282. 'channel_type' => $userInfo['user_type'],
  283. 'province' => strval($userInfo['user_type'] == 'wechat' || $userInfo['user_type'] == 'routine' ? $wechatServices->value(['uid' => $uid, 'user_type' => $userInfo['user_type']], 'province') : ''),
  284. 'spread_uid' => 0,
  285. 'spread_two_uid' => 0,
  286. 'virtual_type' => $virtual_type,
  287. 'is_gift' => $is_gift,
  288. 'gift_mark' => $gift_mark,
  289. 'pay_uid' => $uid,
  290. 'custom_form' => json_encode($customForm),
  291. 'division_id' => $userInfo['division_id'],
  292. 'agent_id' => $userInfo['agent_id'],
  293. 'staff_id' => $userInfo['staff_id'],
  294. 'lb_spread_uid' => $lb_spread_uid,
  295. 'is_lb' => $is_lb
  296. ];
  297. if ($shippingType == 2) {
  298. $orderInfo['verify_code'] = $this->getStoreCode();
  299. /** @var SystemStoreServices $storeServices */
  300. $storeServices = app()->make(SystemStoreServices::class);
  301. $orderInfo['store_id'] = $storeServices->getStoreDispose($storeId, 'id');
  302. if (!$orderInfo['store_id']) {
  303. throw new ApiException(410247);
  304. }
  305. }
  306. /** @var StoreOrderCartInfoServices $cartServices */
  307. $cartServices = app()->make(StoreOrderCartInfoServices::class);
  308. $priceData['coupon_id'] = $couponId;
  309. $order = $this->transaction(function () use ($cartIds, $orderInfo, $cartInfo, $key, $userInfo, $useIntegral, $priceData, $combinationId, $seckillId, $bargainId, $cartServices, $uid, $addressId, $advanceId) {
  310. //创建订单
  311. $order = $this->dao->save($orderInfo);
  312. if (!$order) {
  313. throw new ApiException(410200);
  314. }
  315. //记录自提人电话和姓名
  316. /** @var UserServices $userService */
  317. $userService = app()->make(UserServices::class);
  318. $realName = $userService->value(['uid' => $uid], 'real_name');
  319. if ($realName == '') $userService->update(['uid' => $uid], ['real_name' => $orderInfo['real_name'], 'record_phone' => $orderInfo['user_phone']]);
  320. //积分抵扣
  321. if ($priceData['usedIntegral'] > 0) {
  322. $this->deductIntegral($userInfo, $useIntegral, $priceData, (int)$userInfo['uid'], $order['id']);
  323. }
  324. //扣库存
  325. $this->decGoodsStock($cartInfo, $combinationId, $seckillId, $bargainId, $advanceId);
  326. //保存购物车商品信息
  327. $cartServices->setCartInfo($order['id'], $uid, $cartInfo);
  328. return $order;
  329. });
  330. //创建开票数据
  331. if ($invoice_id) {
  332. app()->make(StoreOrderInvoiceServices::class)->makeUp($uid, $order['order_id'], (int)$invoice_id);
  333. }
  334. // 订单创建成功后置事件
  335. event('OrderCreateAfterListener', [$order, compact('cartInfo', 'priceData', 'addressId', 'cartIds', 'news'), $uid, $key, $combinationId, $seckillId, $bargainId]);
  336. // 推送订单
  337. event('OutPushListener', ['order_create_push', ['order_id' => (int)$order['id']]]);
  338. //自定义事件-订单创建事件
  339. event('CustomEventListener', ['order_create', [
  340. 'uid' => $uid,
  341. 'id' => (int)$order['id'],
  342. 'order_id' => $order['order_id'],
  343. 'real_name' => $order['real_name'],
  344. 'user_phone' => $order['user_phone'],
  345. 'user_address' => $order['user_address'],
  346. 'total_num' => $order['total_num'],
  347. 'pay_price' => $order['pay_price'],
  348. 'pay_postage' => $order['pay_postage'],
  349. 'deduction_price' => $order['deduction_price'],
  350. 'coupon_price' => $order['coupon_price'],
  351. 'store_name' => app()->make(StoreOrderCartInfoServices::class)->getCarIdByProductTitle((int)$order['id']),
  352. 'add_time' => date('Y-m-d H:i:s', $order['add_time']),
  353. ]]);
  354. return $order;
  355. } catch (\Exception $e) {
  356. @file_put_contents('quanju4.txt', $e->getMessage() . "-创建订单报错内容\r\n", 8);
  357. @file_put_contents('quanju4.txt', $e->getFile() . "-文件\r\n", 8);
  358. @file_put_contents('quanju4.txt', $e->getLine() . "-位置\r\n", 8);
  359. }
  360. }
  361. /**
  362. * 抵扣积分
  363. * @param array $userInfo
  364. * @param bool $useIntegral
  365. * @param array $priceData
  366. * @param int $uid
  367. * @param string $key
  368. */
  369. public function deductIntegral(array $userInfo, bool $useIntegral, array $priceData, int $uid, $orderId)
  370. {
  371. $res2 = true;
  372. if ($useIntegral && $userInfo['integral'] > 0) {
  373. /** @var UserServices $userServices */
  374. $userServices = app()->make(UserServices::class);
  375. if (!$priceData['SurplusIntegral']) {
  376. $res2 = false !== $userServices->update($uid, ['integral' => 0]);
  377. } else {
  378. $res2 = false !== $userServices->bcDec($userInfo['uid'], 'integral', $priceData['usedIntegral'], 'uid');
  379. }
  380. /** @var UserBillServices $userBillServices */
  381. $userBillServices = app()->make(UserBillServices::class);
  382. $res3 = $userBillServices->income('deduction', $uid, [
  383. 'number' => $priceData['usedIntegral'],
  384. 'deductionPrice' => $priceData['deduction_price']
  385. ], $userInfo['integral'] - $priceData['usedIntegral'], $orderId);
  386. $res2 = $res2 && false != $res3;
  387. }
  388. if (!$res2) {
  389. throw new ApiException(410227);
  390. }
  391. }
  392. /**
  393. * 扣库存
  394. * @param array $cartInfo
  395. * @param int $combinationId
  396. * @param int $seckillId
  397. * @param int $bargainId
  398. */
  399. public function decGoodsStock(array $cartInfo, int $combinationId, int $seckillId, int $bargainId, int $advanceId)
  400. {
  401. $res5 = true;
  402. /** @var StoreProductServices $services */
  403. $services = app()->make(StoreProductServices::class);
  404. /** @var StoreSeckillServices $seckillServices */
  405. $seckillServices = app()->make(StoreSeckillServices::class);
  406. /** @var StoreCombinationServices $pinkServices */
  407. $pinkServices = app()->make(StoreCombinationServices::class);
  408. /** @var StoreBargainServices $bargainServices */
  409. $bargainServices = app()->make(StoreBargainServices::class);
  410. /** @var StoreAdvanceServices $advanceServices */
  411. $advanceServices = app()->make(StoreAdvanceServices::class);
  412. try {
  413. foreach ($cartInfo as $cart) {
  414. //减库存加销量
  415. if ($combinationId) $res5 = $res5 && $pinkServices->decCombinationStock((int)$cart['cart_num'], $combinationId, isset($cart['productInfo']['attrInfo']) ? $cart['productInfo']['attrInfo']['unique'] : '');
  416. else if ($seckillId) $res5 = $res5 && $seckillServices->decSeckillStock((int)$cart['cart_num'], $seckillId, isset($cart['productInfo']['attrInfo']) ? $cart['productInfo']['attrInfo']['unique'] : '');
  417. else if ($bargainId) $res5 = $res5 && $bargainServices->decBargainStock((int)$cart['cart_num'], $bargainId, isset($cart['productInfo']['attrInfo']) ? $cart['productInfo']['attrInfo']['unique'] : '');
  418. else if ($advanceId) $res5 = $res5 && $advanceServices->decAdvanceStock((int)$cart['cart_num'], $advanceId, isset($cart['productInfo']['attrInfo']) ? $cart['productInfo']['attrInfo']['unique'] : '');
  419. else $res5 = $res5 && $services->decProductStock((int)$cart['cart_num'], (int)$cart['productInfo']['id'], isset($cart['productInfo']['attrInfo']) ? $cart['productInfo']['attrInfo']['unique'] : '');
  420. }
  421. if (!$res5) {
  422. throw new ApiException(410238);
  423. }
  424. } catch (\Throwable $e) {
  425. throw new ApiException(410238);
  426. }
  427. }
  428. /**
  429. * 订单数据创建之后的商品实际金额计算,佣金计算,优惠折扣计算,设置默认地址,清理购物车
  430. * @param $order
  431. * @param array $group
  432. * @param $activity
  433. */
  434. public function orderCreateAfter($order, array $group, $activity)
  435. {
  436. /** @var UserAddressServices $addressServices */
  437. $addressServices = app()->make(UserAddressServices::class);
  438. //设置用户默认地址
  439. if (!$addressServices->be(['is_default' => 1, 'uid' => $order['uid']])) {
  440. $addressServices->setDefaultAddress($group['addressId'], $order['uid']);
  441. $province = $addressServices->value(['id' => $group['addressId']], 'province') ?? '';
  442. app()->make(WechatUserServices::class)->update(['uid' => $order['uid']], ['province' => $province]);
  443. }
  444. //删除购物车
  445. if ($group['news']) {
  446. array_map(function ($key) {
  447. CacheService::delete($key);
  448. }, $group['cartIds']);
  449. } else {
  450. /** @var StoreCartServices $cartServices */
  451. $cartServices = app()->make(StoreCartServices::class);
  452. $cartServices->deleteCartStatus($group['cartIds']);
  453. }
  454. $uid = (int)$order['uid'];
  455. $orderId = (int)$order['id'];
  456. try {
  457. $cartInfo = $group['cartInfo'] ?? [];
  458. $priceData = $group['priceData'] ?? [];
  459. $addressId = $group['addressId'] ?? 0;
  460. $spread_ids = [];
  461. /** @var StoreOrderCreateServices $createService */
  462. $createService = app()->make(StoreOrderCreateServices::class);
  463. if ($cartInfo && $priceData) {
  464. /** @var StoreOrderCartInfoServices $cartServices */
  465. $cartServices = app()->make(StoreOrderCartInfoServices::class);
  466. [$cartInfo, $spread_ids] = $createService->computeOrderProductTruePrice($cartInfo, $priceData, $addressId, $uid, $order);
  467. $cartServices->updateCartInfo($orderId, $cartInfo);
  468. }
  469. $orderData = [];
  470. $spread_uid = $spread_two_uid = 0;
  471. /** @var UserServices $userServices */
  472. $userServices = app()->make(UserServices::class);
  473. if ($spread_ids) {
  474. [$spread_uid, $spread_two_uid] = $spread_ids;
  475. $orderData['spread_uid'] = $spread_uid;
  476. $orderData['spread_two_uid'] = $spread_two_uid;
  477. } else {
  478. $spread_uid = $userServices->getSpreadUid($uid);
  479. $orderData = ['spread_uid' => 0, 'spread_two_uid' => 0];
  480. if ($spread_uid) {
  481. $orderData['spread_uid'] = $spread_uid;
  482. }
  483. if ($spread_uid > 0 && sys_config('brokerage_level') == 2) {
  484. $spread_two_uid = $userServices->getSpreadUid($spread_uid, [], false);
  485. if ($spread_two_uid) {
  486. $orderData['spread_two_uid'] = $spread_two_uid;
  487. }
  488. }
  489. }
  490. $isCommission = 0;
  491. if ($order['combination_id']) {
  492. //检测拼团是否参与返佣
  493. $isCommission = app()->make(StoreCombinationServices::class)->value(['id' => $order['combination_id']], 'is_commission');
  494. }
  495. if ($order['seckill_id']) {
  496. //检测秒杀是否参与返佣
  497. $isCommission = app()->make(StoreSeckillServices::class)->value(['id' => $order['seckill_id']], 'is_commission');
  498. }
  499. if ($order['bargain_id']) {
  500. //检测砍价是否参与返佣
  501. $isCommission = app()->make(StoreBargainServices::class)->value(['id' => $order['bargain_id']], 'is_commission');
  502. }
  503. if ($cartInfo && (!$activity || $isCommission)) {
  504. /** @var StoreOrderComputedServices $orderComputed */
  505. $orderComputed = app()->make(StoreOrderComputedServices::class);
  506. if ($userServices->checkUserPromoter($spread_uid)) $orderData['one_brokerage'] = $orderComputed->getOrderSumPrice($cartInfo, 'one_brokerage', false);
  507. if ($userServices->checkUserPromoter($spread_two_uid)) $orderData['two_brokerage'] = $orderComputed->getOrderSumPrice($cartInfo, 'two_brokerage', false);
  508. $orderData['staff_brokerage'] = $orderComputed->getOrderSumPrice($cartInfo, 'staff_brokerage', false);
  509. $orderData['agent_brokerage'] = $orderComputed->getOrderSumPrice($cartInfo, 'agent_brokerage', false);
  510. $orderData['division_brokerage'] = $orderComputed->getOrderSumPrice($cartInfo, 'division_brokerage', false);
  511. }
  512. $createService->update(['id' => $orderId], $orderData);
  513. } catch (\Throwable $e) {
  514. throw new ApiException('计算订单实际优惠、积分、邮费、佣金失败,原因:' . $e->getMessage());
  515. }
  516. }
  517. /**
  518. * 计算订单每个商品真实付款价格
  519. * @param array $cartInfo
  520. * @param array $priceData
  521. * @param $addressId
  522. * @param int $uid
  523. * @return array
  524. */
  525. public function computeOrderProductTruePrice(array $cartInfo, array $priceData, $addressId, int $uid, $orderInfo)
  526. {
  527. //统一放入默认数据
  528. foreach ($cartInfo as &$cart) {
  529. $cart['use_integral'] = 0;
  530. $cart['integral_price'] = 0.00;
  531. $cart['coupon_price'] = 0.00;
  532. }
  533. try {
  534. $cartInfo = $this->computeOrderProductCoupon($cartInfo, $priceData);
  535. $cartInfo = $this->computeOrderProductIntegral($cartInfo, $priceData);
  536. } catch (\Throwable $e) {
  537. Log::error('订单商品结算失败,File:' . $e->getFile() . ',Line:' . $e->getLine() . ',Message:' . $e->getMessage());
  538. throw new ApiException(410248);
  539. }
  540. //truePice实际支付单价(存在)
  541. //几件商品总体优惠 以及积分抵扣金额
  542. foreach ($cartInfo as &$cart) {
  543. $coupon_price = $cart['coupon_price'] ?? 0;
  544. $integral_price = $cart['integral_price'] ?? 0;
  545. $cart['sum_true_price'] = bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 2);
  546. if ($coupon_price) {
  547. $cart['sum_true_price'] = bcsub((string)$cart['sum_true_price'], (string)$coupon_price, 2);
  548. $uni_coupon_price = (string)bcdiv((string)$coupon_price, (string)$cart['cart_num'], 4);
  549. $cart['truePrice'] = $cart['truePrice'] > $uni_coupon_price ? bcsub((string)$cart['truePrice'], $uni_coupon_price, 2) : 0;
  550. }
  551. if ($integral_price) {
  552. $cart['sum_true_price'] = bcsub((string)$cart['sum_true_price'], (string)$integral_price, 2);
  553. $uni_integral_price = (string)bcdiv((string)$integral_price, (string)$cart['cart_num'], 4);
  554. $cart['truePrice'] = $cart['truePrice'] > $uni_integral_price ? bcsub((string)$cart['truePrice'], $uni_integral_price, 2) : 0;
  555. }
  556. if ($cart['sum_true_price'] < 0) $cart['sum_true_price'] = '0.00';
  557. }
  558. try {
  559. [$cartInfo, $spread_ids] = $this->computeOrderProductBrokerage($uid, $cartInfo);
  560. } catch (\Throwable $e) {
  561. Log::error('订单商品结算失败,File:' . $e->getFile() . ',Line:' . $e->getLine() . ',Message:' . $e->getMessage());
  562. throw new ApiException(410248);
  563. }
  564. return [$cartInfo, $spread_ids];
  565. }
  566. /**
  567. * 计算每个商品实际支付运费
  568. * @param array $cartInfo
  569. * @param array $priceData
  570. * @return array
  571. */
  572. public function computeOrderProductPostage(array $cartInfo, array $priceData, $addressId)
  573. {
  574. $storePostage = $priceData['pay_postage'] ?? 0;
  575. if ($storePostage) {
  576. /** @var UserAddressServices $addressServices */
  577. $addressServices = app()->make(UserAddressServices::class);
  578. $addr = $addressServices->getAddress($addressId);
  579. if ($addr) {
  580. $addr = $addr->toArray();
  581. //按照运费模板计算每个运费模板下商品的件数/重量/体积以及总金额 按照首重倒序排列
  582. $cityId = $addr['city_id'] ?? 0;
  583. $tempIds[] = 1;
  584. foreach ($cartInfo as $key_c => $item_c) {
  585. $tempIds[] = $item_c['productInfo']['temp_id'];
  586. }
  587. $tempIds = array_unique($tempIds);
  588. /** @var ShippingTemplatesServices $shippServices */
  589. $shippServices = app()->make(ShippingTemplatesServices::class);
  590. $temp = $shippServices->getShippingColumn(['id' => $tempIds], 'type,appoint', 'id');
  591. /** @var ShippingTemplatesRegionServices $regionServices */
  592. $regionServices = app()->make(ShippingTemplatesRegionServices::class);
  593. $regions = $regionServices->getTempRegionList($tempIds, [$cityId, 0], 'temp_id,first,first_price,continue,continue_price', 'temp_id');
  594. $temp_num = [];
  595. foreach ($cartInfo as $cart) {
  596. $tempId = $cart['productInfo']['temp_id'] ?? 1;
  597. $type = $temp[$tempId]['type'] ?? $temp[1]['type'];
  598. if ($type == 1) {
  599. $num = $cart['cart_num'];
  600. } elseif ($type == 2) {
  601. $num = $cart['cart_num'] * $cart['productInfo']['attrInfo']['weight'];
  602. } else {
  603. $num = $cart['cart_num'] * $cart['productInfo']['attrInfo']['volume'];
  604. }
  605. $region = $regions[$tempId] ?? $regions[1];
  606. if (!isset($temp_num[$cart['productInfo']['temp_id']])) {
  607. $temp_num[$cart['productInfo']['temp_id']]['cart_id'][] = $cart['id'];
  608. $temp_num[$cart['productInfo']['temp_id']]['number'] = $num;
  609. $temp_num[$cart['productInfo']['temp_id']]['type'] = $type;
  610. $temp_num[$cart['productInfo']['temp_id']]['price'] = bcmul($cart['cart_num'], $cart['truePrice'], 2);
  611. $temp_num[$cart['productInfo']['temp_id']]['first'] = $region['first'];
  612. $temp_num[$cart['productInfo']['temp_id']]['first_price'] = $region['first_price'];
  613. $temp_num[$cart['productInfo']['temp_id']]['continue'] = $region['continue'];
  614. $temp_num[$cart['productInfo']['temp_id']]['continue_price'] = $region['continue_price'];
  615. $temp_num[$cart['productInfo']['temp_id']]['temp_id'] = $cart['productInfo']['temp_id'];
  616. $temp_num[$cart['productInfo']['temp_id']]['city_id'] = $addr['city_id'];
  617. } else {
  618. $temp_num[$cart['productInfo']['temp_id']]['cart_id'][] = $cart['id'];
  619. $temp_num[$cart['productInfo']['temp_id']]['number'] += $num;
  620. $temp_num[$cart['productInfo']['temp_id']]['price'] += bcmul($cart['cart_num'], $cart['truePrice'], 2);
  621. }
  622. }
  623. $cartInfo = array_combine(array_column($cartInfo, 'id'), $cartInfo);
  624. /** @var ShippingTemplatesFreeServices $freeServices */
  625. $freeServices = app()->make(ShippingTemplatesFreeServices::class);
  626. foreach ($temp_num as $k => $v) {
  627. if (isset($temp[$v['temp_id']]['appoint']) && $temp[$v['temp_id']]['appoint']) {
  628. if ($freeServices->isFree($v['temp_id'], $v['city_id'], $v['number'], $v['price'], $v['type'])) {
  629. //免运费
  630. foreach ($v['cart_id'] as $c_id) {
  631. if (isset($cartInfo[$c_id])) $cartInfo[$c_id]['postage_price'] = 0.00;
  632. }
  633. }
  634. }
  635. }
  636. $count = 0;
  637. $compute_price = 0.00;
  638. $total_price = 0;
  639. $postage_price = 0.00;
  640. foreach ($cartInfo as &$cart) {
  641. if (isset($cart['postage_price'])) {//免运费
  642. continue;
  643. }
  644. $total_price = bcadd((string)$total_price, (string)bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 4), 2);
  645. $count++;
  646. }
  647. foreach ($cartInfo as &$cart) {
  648. if (isset($cart['postage_price'])) {//免运费
  649. continue;
  650. }
  651. if ($count > 1) {
  652. $postage_price = bcmul((string)bcdiv((string)bcmul((string)$cart['cart_num'], (string)$cart['truePrice'], 4), (string)$total_price, 4), (string)$storePostage, 2);
  653. $compute_price = bcadd((string)$compute_price, (string)$postage_price, 2);
  654. } else {
  655. $postage_price = bcsub((string)$storePostage, $compute_price, 2);
  656. }
  657. $cart['postage_price'] = $postage_price;
  658. $count--;
  659. }
  660. $cartInfo = array_merge($cartInfo);
  661. }
  662. }
  663. //保证不进运费模版计算的购物车商品postage_price字段有值
  664. foreach ($cartInfo as &$item) {
  665. if (!isset($item['postage_price'])) $item['postage_price'] = 0.00;
  666. }
  667. return $cartInfo;
  668. }
  669. /**
  670. * 计算订单商品积分实际抵扣金额
  671. * @param array $cartInfo
  672. * @param array $priceData
  673. * @return array
  674. */
  675. public function computeOrderProductIntegral(array $cartInfo, array $priceData)
  676. {
  677. $usedIntegral = $priceData['usedIntegral'] ?? 0;
  678. $deduction_price = $priceData['deduction_price'] ?? 0;
  679. if ($deduction_price) {
  680. $count = 0;
  681. $total_price = 0.00;
  682. $compute_price = 0.00;
  683. $integral_price = 0.00;
  684. $use_integral = 0;
  685. $compute_integral = 0;
  686. foreach ($cartInfo as $cart) {
  687. $total_price = bcadd((string)$total_price, (string)bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 4), 2);
  688. $count++;
  689. }
  690. foreach ($cartInfo as &$cart) {
  691. if ($count > 1) {
  692. $integral_price = bcmul((string)bcdiv((string)bcmul((string)$cart['cart_num'], (string)$cart['truePrice'], 4), (string)$total_price, 4), (string)$deduction_price, 2);
  693. $compute_price = bcadd((string)$compute_price, (string)$integral_price, 2);
  694. $use_integral = bcmul((string)bcdiv((string)bcmul((string)$cart['cart_num'], (string)$cart['truePrice'], 4), (string)$total_price, 4), (string)$usedIntegral, 0);
  695. $compute_integral = bcadd((string)$compute_integral, $use_integral, 0);
  696. } else {
  697. $integral_price = bcsub((string)$deduction_price, $compute_price, 2);
  698. $use_integral = bcsub((string)$usedIntegral, $compute_integral, 0);
  699. }
  700. $count--;
  701. $cart['integral_price'] = $integral_price;
  702. $cart['use_integral'] = $use_integral;
  703. }
  704. }
  705. return $cartInfo;
  706. }
  707. /**
  708. * 计算订单商品优惠券实际抵扣金额
  709. * @param array $cartInfo
  710. * @param array $priceData
  711. * @return array
  712. */
  713. public function computeOrderProductCoupon(array $cartInfo, array $priceData)
  714. {
  715. if ($priceData['coupon_id'] && $priceData['coupon_price'] ?? 0) {
  716. $count = 0;
  717. $total_price = 0.00;
  718. $compute_price = 0.00;
  719. $coupon_price = 0.00;
  720. /** @var StoreCouponUserServices $couponServices */
  721. $couponServices = app()->make(StoreCouponUserServices::class);
  722. $couponInfo = $couponServices->getOne(['id' => $priceData['coupon_id']], '*', ['issue']);
  723. if ($couponInfo) {
  724. $type = $couponInfo['applicable_type'] ?? 0;
  725. $counpon_id = $couponInfo['id'];
  726. switch ($type) {
  727. case 0:
  728. case 3:
  729. foreach ($cartInfo as $cart) {
  730. $total_price = bcadd((string)$total_price, (string)bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 4), 2);
  731. $count++;
  732. }
  733. foreach ($cartInfo as &$cart) {
  734. if ($count > 1) {
  735. $coupon_price = bcmul((string)bcdiv((string)bcmul((string)$cart['cart_num'], (string)$cart['truePrice'], 4), (string)$total_price, 4), (string)$couponInfo['coupon_price'], 2);
  736. $compute_price = bcadd((string)$compute_price, (string)$coupon_price, 2);
  737. } else {
  738. $coupon_price = bcsub((string)$couponInfo['coupon_price'], $compute_price, 2);
  739. }
  740. $cart['coupon_price'] = $coupon_price;
  741. $cart['coupon_id'] = $counpon_id;
  742. $count--;
  743. }
  744. break;
  745. case 1://品类券
  746. /** @var StoreCategoryServices $storeCategoryServices */
  747. $storeCategoryServices = app()->make(StoreCategoryServices::class);
  748. $coupon_category = explode(',', (string)$couponInfo['category_id']);
  749. $category_ids = $storeCategoryServices->getAllById($coupon_category);
  750. if ($category_ids) {
  751. $cateIds = array_column($category_ids, 'id');
  752. foreach ($cartInfo as $cart) {
  753. if (isset($cart['productInfo']['cate_id']) && array_intersect(explode(',', $cart['productInfo']['cate_id']), $cateIds)) {
  754. $total_price = bcadd((string)$total_price, (string)bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 4), 2);
  755. $count++;
  756. }
  757. }
  758. foreach ($cartInfo as &$cart) {
  759. $cart['coupon_id'] = 0;
  760. $cart['coupon_price'] = 0;
  761. if (isset($cart['productInfo']['cate_id']) && array_intersect(explode(',', $cart['productInfo']['cate_id']), $cateIds)) {
  762. if ($count > 1) {
  763. $coupon_price = bcmul((string)bcdiv((string)bcmul((string)$cart['cart_num'], (string)$cart['truePrice'], 4), (string)$total_price, 4), (string)$couponInfo['coupon_price'], 2);
  764. $compute_price = bcadd((string)$compute_price, (string)$coupon_price, 2);
  765. } else {
  766. $coupon_price = bcsub((string)$couponInfo['coupon_price'], $compute_price, 2);
  767. }
  768. $cart['coupon_id'] = $counpon_id;
  769. $cart['coupon_price'] = $coupon_price;
  770. $count--;
  771. }
  772. }
  773. }
  774. break;
  775. case 2://商品劵
  776. foreach ($cartInfo as $cart) {
  777. if (isset($cart['product_id']) && in_array($cart['product_id'], explode(',', $couponInfo['product_id']))) {
  778. $total_price = bcadd((string)$total_price, (string)bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 4), 2);
  779. $count++;
  780. }
  781. }
  782. foreach ($cartInfo as &$cart) {
  783. $cart['coupon_id'] = 0;
  784. $cart['coupon_price'] = 0;
  785. if (isset($cart['product_id']) && in_array($cart['product_id'], explode(',', $couponInfo['product_id']))) {
  786. if ($count > 1) {
  787. $coupon_price = bcmul((string)bcdiv((string)bcmul((string)$cart['cart_num'], (string)$cart['truePrice'], 4), (string)$total_price, 4), (string)$couponInfo['coupon_price'], 2);
  788. $compute_price = bcadd((string)$compute_price, (string)$coupon_price, 2);
  789. } else {
  790. $coupon_price = bcsub((string)$couponInfo['coupon_price'], $compute_price, 2);
  791. }
  792. $cart['coupon_id'] = $counpon_id;
  793. $cart['coupon_price'] = $coupon_price;
  794. $count--;
  795. }
  796. }
  797. break;
  798. }
  799. }
  800. }
  801. return $cartInfo;
  802. }
  803. /**
  804. * 计算实际佣金
  805. * @param int $uid
  806. * @param array $cartInfo
  807. * @return array
  808. * @throws \think\db\exception\DataNotFoundException
  809. * @throws \think\db\exception\DbException
  810. * @throws \think\db\exception\ModelNotFoundException
  811. */
  812. public function computeOrderProductBrokerage(int $uid, array $cartInfo)
  813. {
  814. [$storeBrokerageRatio, $storeBrokerageTwo, $spread_one_uid, $spread_two_uid] = $this->getSpreadDate($uid);
  815. /** @var DivisionServices $divisionService */
  816. $divisionService = app()->make(DivisionServices::class);
  817. [$storeBrokerageRatio, $storeBrokerageTwo, $staffPercent, $agentPercent, $divisionPercent] = $divisionService->getDivisionPercent($uid, $storeBrokerageRatio, $storeBrokerageTwo, sys_config('is_self_brokerage', 0));
  818. foreach ($cartInfo as &$cart) {
  819. $oneBrokerage = '0';//一级返佣金额
  820. $twoBrokerage = '0';//二级返佣金额
  821. $staffBrokerage = '0';//店员返佣金额
  822. $agentBrokerage = '0';//代理商返佣金额
  823. $divisionBrokerage = '0';//事业部返佣金额
  824. $cartNum = (string)$cart['cart_num'] ?? '0';
  825. if (isset($cart['productInfo'])) {
  826. $productInfo = $cart['productInfo'];
  827. //计算商品金额
  828. if (sys_config('user_brokerage_type') == 1) {
  829. //按照实际支付价格返佣
  830. $price = bcmul((string)bcadd((string)$cart['truePrice'], (string)$cart['postage_price'], 2), $cartNum, 4);
  831. } else {
  832. //按照商品价格返佣
  833. if (isset($productInfo['attrInfo'])) {
  834. $price = bcmul((string)($productInfo['attrInfo']['price'] ?? '0'), $cartNum, 4);
  835. } else {
  836. $price = bcmul((string)($productInfo['price'] ?? '0'), $cartNum, 4);
  837. }
  838. }
  839. //指定返佣金额
  840. if (isset($productInfo['is_sub']) && $productInfo['is_sub'] == 1) {
  841. $oneBrokerage = bcmul((string)($productInfo['attrInfo']['brokerage'] ?? '0'), $cartNum, 2);
  842. $twoBrokerage = bcmul((string)($productInfo['attrInfo']['brokerage_two'] ?? '0'), $cartNum, 2);
  843. } else {
  844. if ($price) {
  845. //一级返佣比例 小于等于零时直接返回 不返佣
  846. if ($storeBrokerageRatio > 0) {
  847. //计算获取一级返佣比例
  848. $brokerageRatio = bcdiv($storeBrokerageRatio, 100, 4);
  849. $oneBrokerage = bcmul((string)$price, (string)$brokerageRatio, 2);
  850. }
  851. //二级返佣比例小于等于0 直接返回
  852. if ($storeBrokerageTwo > 0) {
  853. //计算获取二级返佣比例
  854. $brokerageTwo = bcdiv($storeBrokerageTwo, 100, 4);
  855. $twoBrokerage = bcmul((string)$price, (string)$brokerageTwo, 2);
  856. }
  857. }
  858. $staffBrokerage = bcmul((string)$price, (string)bcdiv($staffPercent, 100, 4), 2);
  859. $agentBrokerage = bcmul((string)$price, (string)bcdiv($agentPercent, 100, 4), 2);
  860. $divisionBrokerage = bcmul((string)$price, (string)bcdiv($divisionPercent, 100, 4), 2);
  861. }
  862. }
  863. $cart['one_brokerage'] = $oneBrokerage;
  864. $cart['two_brokerage'] = $twoBrokerage;
  865. $cart['staff_brokerage'] = $staffBrokerage;
  866. $cart['agent_brokerage'] = $agentBrokerage;
  867. $cart['division_brokerage'] = $divisionBrokerage;
  868. }
  869. return [$cartInfo, [$spread_one_uid, $spread_two_uid]];
  870. }
  871. /**
  872. * 获取计算好的佣金比例以及返佣人员uid
  873. * @param int $uid
  874. * @return array|int[]
  875. * @throws \think\db\exception\DataNotFoundException
  876. * @throws \think\db\exception\DbException
  877. * @throws \think\db\exception\ModelNotFoundException
  878. * @author: 吴汐
  879. * @email: 442384644@qq.com
  880. * @date: 2023/10/8
  881. */
  882. public function getSpreadDate(int $uid)
  883. {
  884. //商城分销是否开启,用户uid是否存在,全部返回0
  885. if (!sys_config('brokerage_func_status') || !$uid) {
  886. return [0, 0, 0, 0];
  887. }
  888. //获取用户信息,获取不到全部返回0
  889. /** @var UserServices $userServices */
  890. $userServices = app()->make(UserServices::class);
  891. $userInfo = $userServices->getUserInfo($uid);
  892. if (!$userInfo) {
  893. return [0, 0, 0, 0];
  894. }
  895. //获取系统一二级分佣比例
  896. $storeBrokerageRatio = sys_config('store_brokerage_ratio') != '' ? sys_config('store_brokerage_ratio') : 0;
  897. $storeBrokerageTwo = sys_config('store_brokerage_two') != '' ? sys_config('store_brokerage_two') : 0;
  898. //获取上级和上上级的uid,开启自购获取自己和上级的uid
  899. $spread_one_uid = $userServices->getSpreadUid($uid, $userInfo);
  900. $spread_two_uid = 0;
  901. if ($spread_one_uid > 0 && $one_user_info = $userServices->getUserInfo($spread_one_uid)) {
  902. $spread_two_uid = $userServices->getSpreadUid($spread_one_uid, $one_user_info, false);
  903. }
  904. //计算分销等级之后的佣金比例
  905. [$storeBrokerageRatio, $storeBrokerageTwo] = app()->make(AgentLevelServices::class)->getAgentLevelBrokerage($storeBrokerageRatio, $storeBrokerageTwo, $spread_one_uid, $spread_two_uid);
  906. //判断返佣层级为一级时,将二级用户uid和二级分佣比例改为0
  907. if (sys_config('brokerage_level') == 1) {
  908. $storeBrokerageTwo = $spread_two_uid = 0;
  909. }
  910. return [$storeBrokerageRatio, $storeBrokerageTwo, $spread_one_uid, $spread_two_uid];
  911. }
  912. }