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. // 检查用户是否已在礼包关系表中存在(作为uid或pid)
  227. $existsAsUid = $storeProductGiftGroupService->getOne(['uid' => $uid]);
  228. $existsAsPid = $storeProductGiftGroupService->getOne(['pid' => $uid]);
  229. // 如果两者都不存在,说明用户还没参与礼包推荐关系,无法购买
  230. if (!$existsAsUid && !$existsAsPid) {
  231. throw new ApiException('没有推荐关系或者推荐人的用户无法购买,请重新扫描推荐二维码');
  232. }
  233. }
  234. if (count($cartInfo) == 1 && isset($cartInfo[0]['productInfo']['presale']) && $cartInfo[0]['productInfo']['presale'] == 1) {
  235. $advance_id = $cartInfo[0]['product_id'];
  236. } else {
  237. $advance_id = 0;
  238. }
  239. $deduction = $seckillId || $bargainId || $combinationId;
  240. if ($deduction) {
  241. $couponId = 0;
  242. $gainIntegral = 0;
  243. $useIntegral = false;
  244. }
  245. //$shipping_type = 1 快递发货 $shipping_type = 2 门店自提
  246. $storeSelfMention = sys_config('store_self_mention') ?? 0;
  247. if (!$storeSelfMention) $shippingType = 1;
  248. if ($is_gift == 1) $shippingType = 0;
  249. $orderInfo = [
  250. 'uid' => $uid,
  251. 'order_id' => $this->getNewOrderId('cp'),
  252. 'real_name' => $addressInfo['real_name'],
  253. 'user_phone' => $addressInfo['phone'],
  254. 'user_address' => $addressInfo['province'] . ' ' . $addressInfo['city'] . ' ' . $addressInfo['district'] . ' ' . $addressInfo['detail'],
  255. 'cart_id' => $cartIds,
  256. 'total_num' => $totalNum,
  257. 'total_price' => $priceGroup['totalPrice'],
  258. 'total_postage' => $shippingType == 1 ? $priceGroup['storePostage'] : 0,
  259. 'coupon_id' => $couponId,
  260. 'coupon_price' => $priceData['coupon_price'],
  261. 'pay_price' => $priceData['pay_price'],
  262. 'pay_postage' => $priceData['pay_postage'],
  263. 'deduction_price' => $priceData['deduction_price'],
  264. 'gift_price' => $priceData['gift_price'],
  265. 'paid' => 0,
  266. 'pay_type' => $payType,
  267. 'use_integral' => $priceData['usedIntegral'],
  268. 'gain_integral' => $gainIntegral,
  269. 'mark' => htmlspecialchars($mark),
  270. 'combination_id' => $combinationId,
  271. 'pink_id' => $pinkId,
  272. 'seckill_id' => $seckillId,
  273. 'bargain_id' => $bargainId,
  274. 'advance_id' => $advance_id,
  275. 'cost' => $priceGroup['costPrice'],
  276. 'add_time' => time(),
  277. 'unique' => $key,
  278. 'shipping_type' => $shippingType,
  279. 'channel_type' => $userInfo['user_type'],
  280. 'province' => strval($userInfo['user_type'] == 'wechat' || $userInfo['user_type'] == 'routine' ? $wechatServices->value(['uid' => $uid, 'user_type' => $userInfo['user_type']], 'province') : ''),
  281. 'spread_uid' => 0,
  282. 'spread_two_uid' => 0,
  283. 'virtual_type' => $virtual_type,
  284. 'is_gift' => $is_gift,
  285. 'gift_mark' => $gift_mark,
  286. 'pay_uid' => $uid,
  287. 'custom_form' => json_encode($customForm),
  288. 'division_id' => $userInfo['division_id'],
  289. 'agent_id' => $userInfo['agent_id'],
  290. 'staff_id' => $userInfo['staff_id'],
  291. 'lb_spread_uid' => $lb_spread_uid,
  292. 'is_lb' => $is_lb
  293. ];
  294. if ($shippingType == 2) {
  295. $orderInfo['verify_code'] = $this->getStoreCode();
  296. /** @var SystemStoreServices $storeServices */
  297. $storeServices = app()->make(SystemStoreServices::class);
  298. $orderInfo['store_id'] = $storeServices->getStoreDispose($storeId, 'id');
  299. if (!$orderInfo['store_id']) {
  300. throw new ApiException(410247);
  301. }
  302. }
  303. /** @var StoreOrderCartInfoServices $cartServices */
  304. $cartServices = app()->make(StoreOrderCartInfoServices::class);
  305. $priceData['coupon_id'] = $couponId;
  306. $order = $this->transaction(function () use ($cartIds, $orderInfo, $cartInfo, $key, $userInfo, $useIntegral, $priceData, $combinationId, $seckillId, $bargainId, $cartServices, $uid, $addressId, $advanceId) {
  307. //创建订单
  308. $order = $this->dao->save($orderInfo);
  309. if (!$order) {
  310. throw new ApiException(410200);
  311. }
  312. //记录自提人电话和姓名
  313. /** @var UserServices $userService */
  314. $userService = app()->make(UserServices::class);
  315. $realName = $userService->value(['uid' => $uid], 'real_name');
  316. if ($realName == '') $userService->update(['uid' => $uid], ['real_name' => $orderInfo['real_name'], 'record_phone' => $orderInfo['user_phone']]);
  317. //积分抵扣
  318. if ($priceData['usedIntegral'] > 0) {
  319. $this->deductIntegral($userInfo, $useIntegral, $priceData, (int)$userInfo['uid'], $order['id']);
  320. }
  321. //扣库存
  322. $this->decGoodsStock($cartInfo, $combinationId, $seckillId, $bargainId, $advanceId);
  323. //保存购物车商品信息
  324. $cartServices->setCartInfo($order['id'], $uid, $cartInfo);
  325. return $order;
  326. });
  327. //创建开票数据
  328. if ($invoice_id) {
  329. app()->make(StoreOrderInvoiceServices::class)->makeUp($uid, $order['order_id'], (int)$invoice_id);
  330. }
  331. // 订单创建成功后置事件
  332. event('OrderCreateAfterListener', [$order, compact('cartInfo', 'priceData', 'addressId', 'cartIds', 'news'), $uid, $key, $combinationId, $seckillId, $bargainId]);
  333. // 推送订单
  334. event('OutPushListener', ['order_create_push', ['order_id' => (int)$order['id']]]);
  335. //自定义事件-订单创建事件
  336. event('CustomEventListener', ['order_create', [
  337. 'uid' => $uid,
  338. 'id' => (int)$order['id'],
  339. 'order_id' => $order['order_id'],
  340. 'real_name' => $order['real_name'],
  341. 'user_phone' => $order['user_phone'],
  342. 'user_address' => $order['user_address'],
  343. 'total_num' => $order['total_num'],
  344. 'pay_price' => $order['pay_price'],
  345. 'pay_postage' => $order['pay_postage'],
  346. 'deduction_price' => $order['deduction_price'],
  347. 'coupon_price' => $order['coupon_price'],
  348. 'store_name' => app()->make(StoreOrderCartInfoServices::class)->getCarIdByProductTitle((int)$order['id']),
  349. 'add_time' => date('Y-m-d H:i:s', $order['add_time']),
  350. ]]);
  351. return $order;
  352. } catch (\Exception $e) {
  353. @file_put_contents('quanju4.txt', $e->getMessage() . "-创建订单报错内容\r\n", 8);
  354. @file_put_contents('quanju4.txt', $e->getFile() . "-文件\r\n", 8);
  355. @file_put_contents('quanju4.txt', $e->getLine() . "-位置\r\n", 8);
  356. }
  357. }
  358. /**
  359. * 抵扣积分
  360. * @param array $userInfo
  361. * @param bool $useIntegral
  362. * @param array $priceData
  363. * @param int $uid
  364. * @param string $key
  365. */
  366. public function deductIntegral(array $userInfo, bool $useIntegral, array $priceData, int $uid, $orderId)
  367. {
  368. $res2 = true;
  369. if ($useIntegral && $userInfo['integral'] > 0) {
  370. /** @var UserServices $userServices */
  371. $userServices = app()->make(UserServices::class);
  372. if (!$priceData['SurplusIntegral']) {
  373. $res2 = false !== $userServices->update($uid, ['integral' => 0]);
  374. } else {
  375. $res2 = false !== $userServices->bcDec($userInfo['uid'], 'integral', $priceData['usedIntegral'], 'uid');
  376. }
  377. /** @var UserBillServices $userBillServices */
  378. $userBillServices = app()->make(UserBillServices::class);
  379. $res3 = $userBillServices->income('deduction', $uid, [
  380. 'number' => $priceData['usedIntegral'],
  381. 'deductionPrice' => $priceData['deduction_price']
  382. ], $userInfo['integral'] - $priceData['usedIntegral'], $orderId);
  383. $res2 = $res2 && false != $res3;
  384. }
  385. if (!$res2) {
  386. throw new ApiException(410227);
  387. }
  388. }
  389. /**
  390. * 扣库存
  391. * @param array $cartInfo
  392. * @param int $combinationId
  393. * @param int $seckillId
  394. * @param int $bargainId
  395. */
  396. public function decGoodsStock(array $cartInfo, int $combinationId, int $seckillId, int $bargainId, int $advanceId)
  397. {
  398. $res5 = true;
  399. /** @var StoreProductServices $services */
  400. $services = app()->make(StoreProductServices::class);
  401. /** @var StoreSeckillServices $seckillServices */
  402. $seckillServices = app()->make(StoreSeckillServices::class);
  403. /** @var StoreCombinationServices $pinkServices */
  404. $pinkServices = app()->make(StoreCombinationServices::class);
  405. /** @var StoreBargainServices $bargainServices */
  406. $bargainServices = app()->make(StoreBargainServices::class);
  407. /** @var StoreAdvanceServices $advanceServices */
  408. $advanceServices = app()->make(StoreAdvanceServices::class);
  409. try {
  410. foreach ($cartInfo as $cart) {
  411. //减库存加销量
  412. if ($combinationId) $res5 = $res5 && $pinkServices->decCombinationStock((int)$cart['cart_num'], $combinationId, isset($cart['productInfo']['attrInfo']) ? $cart['productInfo']['attrInfo']['unique'] : '');
  413. else if ($seckillId) $res5 = $res5 && $seckillServices->decSeckillStock((int)$cart['cart_num'], $seckillId, isset($cart['productInfo']['attrInfo']) ? $cart['productInfo']['attrInfo']['unique'] : '');
  414. else if ($bargainId) $res5 = $res5 && $bargainServices->decBargainStock((int)$cart['cart_num'], $bargainId, isset($cart['productInfo']['attrInfo']) ? $cart['productInfo']['attrInfo']['unique'] : '');
  415. else if ($advanceId) $res5 = $res5 && $advanceServices->decAdvanceStock((int)$cart['cart_num'], $advanceId, isset($cart['productInfo']['attrInfo']) ? $cart['productInfo']['attrInfo']['unique'] : '');
  416. else $res5 = $res5 && $services->decProductStock((int)$cart['cart_num'], (int)$cart['productInfo']['id'], isset($cart['productInfo']['attrInfo']) ? $cart['productInfo']['attrInfo']['unique'] : '');
  417. }
  418. if (!$res5) {
  419. throw new ApiException(410238);
  420. }
  421. } catch (\Throwable $e) {
  422. throw new ApiException(410238);
  423. }
  424. }
  425. /**
  426. * 订单数据创建之后的商品实际金额计算,佣金计算,优惠折扣计算,设置默认地址,清理购物车
  427. * @param $order
  428. * @param array $group
  429. * @param $activity
  430. */
  431. public function orderCreateAfter($order, array $group, $activity)
  432. {
  433. /** @var UserAddressServices $addressServices */
  434. $addressServices = app()->make(UserAddressServices::class);
  435. //设置用户默认地址
  436. if (!$addressServices->be(['is_default' => 1, 'uid' => $order['uid']])) {
  437. $addressServices->setDefaultAddress($group['addressId'], $order['uid']);
  438. $province = $addressServices->value(['id' => $group['addressId']], 'province') ?? '';
  439. app()->make(WechatUserServices::class)->update(['uid' => $order['uid']], ['province' => $province]);
  440. }
  441. //删除购物车
  442. if ($group['news']) {
  443. array_map(function ($key) {
  444. CacheService::delete($key);
  445. }, $group['cartIds']);
  446. } else {
  447. /** @var StoreCartServices $cartServices */
  448. $cartServices = app()->make(StoreCartServices::class);
  449. $cartServices->deleteCartStatus($group['cartIds']);
  450. }
  451. $uid = (int)$order['uid'];
  452. $orderId = (int)$order['id'];
  453. try {
  454. $cartInfo = $group['cartInfo'] ?? [];
  455. $priceData = $group['priceData'] ?? [];
  456. $addressId = $group['addressId'] ?? 0;
  457. $spread_ids = [];
  458. /** @var StoreOrderCreateServices $createService */
  459. $createService = app()->make(StoreOrderCreateServices::class);
  460. if ($cartInfo && $priceData) {
  461. /** @var StoreOrderCartInfoServices $cartServices */
  462. $cartServices = app()->make(StoreOrderCartInfoServices::class);
  463. [$cartInfo, $spread_ids] = $createService->computeOrderProductTruePrice($cartInfo, $priceData, $addressId, $uid, $order);
  464. $cartServices->updateCartInfo($orderId, $cartInfo);
  465. }
  466. $orderData = [];
  467. $spread_uid = $spread_two_uid = 0;
  468. /** @var UserServices $userServices */
  469. $userServices = app()->make(UserServices::class);
  470. if ($spread_ids) {
  471. [$spread_uid, $spread_two_uid] = $spread_ids;
  472. $orderData['spread_uid'] = $spread_uid;
  473. $orderData['spread_two_uid'] = $spread_two_uid;
  474. } else {
  475. $spread_uid = $userServices->getSpreadUid($uid);
  476. $orderData = ['spread_uid' => 0, 'spread_two_uid' => 0];
  477. if ($spread_uid) {
  478. $orderData['spread_uid'] = $spread_uid;
  479. }
  480. if ($spread_uid > 0 && sys_config('brokerage_level') == 2) {
  481. $spread_two_uid = $userServices->getSpreadUid($spread_uid, [], false);
  482. if ($spread_two_uid) {
  483. $orderData['spread_two_uid'] = $spread_two_uid;
  484. }
  485. }
  486. }
  487. $isCommission = 0;
  488. if ($order['combination_id']) {
  489. //检测拼团是否参与返佣
  490. $isCommission = app()->make(StoreCombinationServices::class)->value(['id' => $order['combination_id']], 'is_commission');
  491. }
  492. if ($order['seckill_id']) {
  493. //检测秒杀是否参与返佣
  494. $isCommission = app()->make(StoreSeckillServices::class)->value(['id' => $order['seckill_id']], 'is_commission');
  495. }
  496. if ($order['bargain_id']) {
  497. //检测砍价是否参与返佣
  498. $isCommission = app()->make(StoreBargainServices::class)->value(['id' => $order['bargain_id']], 'is_commission');
  499. }
  500. if ($cartInfo && (!$activity || $isCommission)) {
  501. /** @var StoreOrderComputedServices $orderComputed */
  502. $orderComputed = app()->make(StoreOrderComputedServices::class);
  503. if ($userServices->checkUserPromoter($spread_uid)) $orderData['one_brokerage'] = $orderComputed->getOrderSumPrice($cartInfo, 'one_brokerage', false);
  504. if ($userServices->checkUserPromoter($spread_two_uid)) $orderData['two_brokerage'] = $orderComputed->getOrderSumPrice($cartInfo, 'two_brokerage', false);
  505. $orderData['staff_brokerage'] = $orderComputed->getOrderSumPrice($cartInfo, 'staff_brokerage', false);
  506. $orderData['agent_brokerage'] = $orderComputed->getOrderSumPrice($cartInfo, 'agent_brokerage', false);
  507. $orderData['division_brokerage'] = $orderComputed->getOrderSumPrice($cartInfo, 'division_brokerage', false);
  508. }
  509. $createService->update(['id' => $orderId], $orderData);
  510. } catch (\Throwable $e) {
  511. throw new ApiException('计算订单实际优惠、积分、邮费、佣金失败,原因:' . $e->getMessage());
  512. }
  513. }
  514. /**
  515. * 计算订单每个商品真实付款价格
  516. * @param array $cartInfo
  517. * @param array $priceData
  518. * @param $addressId
  519. * @param int $uid
  520. * @return array
  521. */
  522. public function computeOrderProductTruePrice(array $cartInfo, array $priceData, $addressId, int $uid, $orderInfo)
  523. {
  524. //统一放入默认数据
  525. foreach ($cartInfo as &$cart) {
  526. $cart['use_integral'] = 0;
  527. $cart['integral_price'] = 0.00;
  528. $cart['coupon_price'] = 0.00;
  529. }
  530. try {
  531. $cartInfo = $this->computeOrderProductCoupon($cartInfo, $priceData);
  532. $cartInfo = $this->computeOrderProductIntegral($cartInfo, $priceData);
  533. } catch (\Throwable $e) {
  534. Log::error('订单商品结算失败,File:' . $e->getFile() . ',Line:' . $e->getLine() . ',Message:' . $e->getMessage());
  535. throw new ApiException(410248);
  536. }
  537. //truePice实际支付单价(存在)
  538. //几件商品总体优惠 以及积分抵扣金额
  539. foreach ($cartInfo as &$cart) {
  540. $coupon_price = $cart['coupon_price'] ?? 0;
  541. $integral_price = $cart['integral_price'] ?? 0;
  542. $cart['sum_true_price'] = bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 2);
  543. if ($coupon_price) {
  544. $cart['sum_true_price'] = bcsub((string)$cart['sum_true_price'], (string)$coupon_price, 2);
  545. $uni_coupon_price = (string)bcdiv((string)$coupon_price, (string)$cart['cart_num'], 4);
  546. $cart['truePrice'] = $cart['truePrice'] > $uni_coupon_price ? bcsub((string)$cart['truePrice'], $uni_coupon_price, 2) : 0;
  547. }
  548. if ($integral_price) {
  549. $cart['sum_true_price'] = bcsub((string)$cart['sum_true_price'], (string)$integral_price, 2);
  550. $uni_integral_price = (string)bcdiv((string)$integral_price, (string)$cart['cart_num'], 4);
  551. $cart['truePrice'] = $cart['truePrice'] > $uni_integral_price ? bcsub((string)$cart['truePrice'], $uni_integral_price, 2) : 0;
  552. }
  553. if ($cart['sum_true_price'] < 0) $cart['sum_true_price'] = '0.00';
  554. }
  555. try {
  556. [$cartInfo, $spread_ids] = $this->computeOrderProductBrokerage($uid, $cartInfo);
  557. } catch (\Throwable $e) {
  558. Log::error('订单商品结算失败,File:' . $e->getFile() . ',Line:' . $e->getLine() . ',Message:' . $e->getMessage());
  559. throw new ApiException(410248);
  560. }
  561. return [$cartInfo, $spread_ids];
  562. }
  563. /**
  564. * 计算每个商品实际支付运费
  565. * @param array $cartInfo
  566. * @param array $priceData
  567. * @return array
  568. */
  569. public function computeOrderProductPostage(array $cartInfo, array $priceData, $addressId)
  570. {
  571. $storePostage = $priceData['pay_postage'] ?? 0;
  572. if ($storePostage) {
  573. /** @var UserAddressServices $addressServices */
  574. $addressServices = app()->make(UserAddressServices::class);
  575. $addr = $addressServices->getAddress($addressId);
  576. if ($addr) {
  577. $addr = $addr->toArray();
  578. //按照运费模板计算每个运费模板下商品的件数/重量/体积以及总金额 按照首重倒序排列
  579. $cityId = $addr['city_id'] ?? 0;
  580. $tempIds[] = 1;
  581. foreach ($cartInfo as $key_c => $item_c) {
  582. $tempIds[] = $item_c['productInfo']['temp_id'];
  583. }
  584. $tempIds = array_unique($tempIds);
  585. /** @var ShippingTemplatesServices $shippServices */
  586. $shippServices = app()->make(ShippingTemplatesServices::class);
  587. $temp = $shippServices->getShippingColumn(['id' => $tempIds], 'type,appoint', 'id');
  588. /** @var ShippingTemplatesRegionServices $regionServices */
  589. $regionServices = app()->make(ShippingTemplatesRegionServices::class);
  590. $regions = $regionServices->getTempRegionList($tempIds, [$cityId, 0], 'temp_id,first,first_price,continue,continue_price', 'temp_id');
  591. $temp_num = [];
  592. foreach ($cartInfo as $cart) {
  593. $tempId = $cart['productInfo']['temp_id'] ?? 1;
  594. $type = $temp[$tempId]['type'] ?? $temp[1]['type'];
  595. if ($type == 1) {
  596. $num = $cart['cart_num'];
  597. } elseif ($type == 2) {
  598. $num = $cart['cart_num'] * $cart['productInfo']['attrInfo']['weight'];
  599. } else {
  600. $num = $cart['cart_num'] * $cart['productInfo']['attrInfo']['volume'];
  601. }
  602. $region = $regions[$tempId] ?? $regions[1];
  603. if (!isset($temp_num[$cart['productInfo']['temp_id']])) {
  604. $temp_num[$cart['productInfo']['temp_id']]['cart_id'][] = $cart['id'];
  605. $temp_num[$cart['productInfo']['temp_id']]['number'] = $num;
  606. $temp_num[$cart['productInfo']['temp_id']]['type'] = $type;
  607. $temp_num[$cart['productInfo']['temp_id']]['price'] = bcmul($cart['cart_num'], $cart['truePrice'], 2);
  608. $temp_num[$cart['productInfo']['temp_id']]['first'] = $region['first'];
  609. $temp_num[$cart['productInfo']['temp_id']]['first_price'] = $region['first_price'];
  610. $temp_num[$cart['productInfo']['temp_id']]['continue'] = $region['continue'];
  611. $temp_num[$cart['productInfo']['temp_id']]['continue_price'] = $region['continue_price'];
  612. $temp_num[$cart['productInfo']['temp_id']]['temp_id'] = $cart['productInfo']['temp_id'];
  613. $temp_num[$cart['productInfo']['temp_id']]['city_id'] = $addr['city_id'];
  614. } else {
  615. $temp_num[$cart['productInfo']['temp_id']]['cart_id'][] = $cart['id'];
  616. $temp_num[$cart['productInfo']['temp_id']]['number'] += $num;
  617. $temp_num[$cart['productInfo']['temp_id']]['price'] += bcmul($cart['cart_num'], $cart['truePrice'], 2);
  618. }
  619. }
  620. $cartInfo = array_combine(array_column($cartInfo, 'id'), $cartInfo);
  621. /** @var ShippingTemplatesFreeServices $freeServices */
  622. $freeServices = app()->make(ShippingTemplatesFreeServices::class);
  623. foreach ($temp_num as $k => $v) {
  624. if (isset($temp[$v['temp_id']]['appoint']) && $temp[$v['temp_id']]['appoint']) {
  625. if ($freeServices->isFree($v['temp_id'], $v['city_id'], $v['number'], $v['price'], $v['type'])) {
  626. //免运费
  627. foreach ($v['cart_id'] as $c_id) {
  628. if (isset($cartInfo[$c_id])) $cartInfo[$c_id]['postage_price'] = 0.00;
  629. }
  630. }
  631. }
  632. }
  633. $count = 0;
  634. $compute_price = 0.00;
  635. $total_price = 0;
  636. $postage_price = 0.00;
  637. foreach ($cartInfo as &$cart) {
  638. if (isset($cart['postage_price'])) {//免运费
  639. continue;
  640. }
  641. $total_price = bcadd((string)$total_price, (string)bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 4), 2);
  642. $count++;
  643. }
  644. foreach ($cartInfo as &$cart) {
  645. if (isset($cart['postage_price'])) {//免运费
  646. continue;
  647. }
  648. if ($count > 1) {
  649. $postage_price = bcmul((string)bcdiv((string)bcmul((string)$cart['cart_num'], (string)$cart['truePrice'], 4), (string)$total_price, 4), (string)$storePostage, 2);
  650. $compute_price = bcadd((string)$compute_price, (string)$postage_price, 2);
  651. } else {
  652. $postage_price = bcsub((string)$storePostage, $compute_price, 2);
  653. }
  654. $cart['postage_price'] = $postage_price;
  655. $count--;
  656. }
  657. $cartInfo = array_merge($cartInfo);
  658. }
  659. }
  660. //保证不进运费模版计算的购物车商品postage_price字段有值
  661. foreach ($cartInfo as &$item) {
  662. if (!isset($item['postage_price'])) $item['postage_price'] = 0.00;
  663. }
  664. return $cartInfo;
  665. }
  666. /**
  667. * 计算订单商品积分实际抵扣金额
  668. * @param array $cartInfo
  669. * @param array $priceData
  670. * @return array
  671. */
  672. public function computeOrderProductIntegral(array $cartInfo, array $priceData)
  673. {
  674. $usedIntegral = $priceData['usedIntegral'] ?? 0;
  675. $deduction_price = $priceData['deduction_price'] ?? 0;
  676. if ($deduction_price) {
  677. $count = 0;
  678. $total_price = 0.00;
  679. $compute_price = 0.00;
  680. $integral_price = 0.00;
  681. $use_integral = 0;
  682. $compute_integral = 0;
  683. foreach ($cartInfo as $cart) {
  684. $total_price = bcadd((string)$total_price, (string)bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 4), 2);
  685. $count++;
  686. }
  687. foreach ($cartInfo as &$cart) {
  688. if ($count > 1) {
  689. $integral_price = bcmul((string)bcdiv((string)bcmul((string)$cart['cart_num'], (string)$cart['truePrice'], 4), (string)$total_price, 4), (string)$deduction_price, 2);
  690. $compute_price = bcadd((string)$compute_price, (string)$integral_price, 2);
  691. $use_integral = bcmul((string)bcdiv((string)bcmul((string)$cart['cart_num'], (string)$cart['truePrice'], 4), (string)$total_price, 4), (string)$usedIntegral, 0);
  692. $compute_integral = bcadd((string)$compute_integral, $use_integral, 0);
  693. } else {
  694. $integral_price = bcsub((string)$deduction_price, $compute_price, 2);
  695. $use_integral = bcsub((string)$usedIntegral, $compute_integral, 0);
  696. }
  697. $count--;
  698. $cart['integral_price'] = $integral_price;
  699. $cart['use_integral'] = $use_integral;
  700. }
  701. }
  702. return $cartInfo;
  703. }
  704. /**
  705. * 计算订单商品优惠券实际抵扣金额
  706. * @param array $cartInfo
  707. * @param array $priceData
  708. * @return array
  709. */
  710. public function computeOrderProductCoupon(array $cartInfo, array $priceData)
  711. {
  712. if ($priceData['coupon_id'] && $priceData['coupon_price'] ?? 0) {
  713. $count = 0;
  714. $total_price = 0.00;
  715. $compute_price = 0.00;
  716. $coupon_price = 0.00;
  717. /** @var StoreCouponUserServices $couponServices */
  718. $couponServices = app()->make(StoreCouponUserServices::class);
  719. $couponInfo = $couponServices->getOne(['id' => $priceData['coupon_id']], '*', ['issue']);
  720. if ($couponInfo) {
  721. $type = $couponInfo['applicable_type'] ?? 0;
  722. $counpon_id = $couponInfo['id'];
  723. switch ($type) {
  724. case 0:
  725. case 3:
  726. foreach ($cartInfo as $cart) {
  727. $total_price = bcadd((string)$total_price, (string)bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 4), 2);
  728. $count++;
  729. }
  730. foreach ($cartInfo as &$cart) {
  731. if ($count > 1) {
  732. $coupon_price = bcmul((string)bcdiv((string)bcmul((string)$cart['cart_num'], (string)$cart['truePrice'], 4), (string)$total_price, 4), (string)$couponInfo['coupon_price'], 2);
  733. $compute_price = bcadd((string)$compute_price, (string)$coupon_price, 2);
  734. } else {
  735. $coupon_price = bcsub((string)$couponInfo['coupon_price'], $compute_price, 2);
  736. }
  737. $cart['coupon_price'] = $coupon_price;
  738. $cart['coupon_id'] = $counpon_id;
  739. $count--;
  740. }
  741. break;
  742. case 1://品类券
  743. /** @var StoreCategoryServices $storeCategoryServices */
  744. $storeCategoryServices = app()->make(StoreCategoryServices::class);
  745. $coupon_category = explode(',', (string)$couponInfo['category_id']);
  746. $category_ids = $storeCategoryServices->getAllById($coupon_category);
  747. if ($category_ids) {
  748. $cateIds = array_column($category_ids, 'id');
  749. foreach ($cartInfo as $cart) {
  750. if (isset($cart['productInfo']['cate_id']) && array_intersect(explode(',', $cart['productInfo']['cate_id']), $cateIds)) {
  751. $total_price = bcadd((string)$total_price, (string)bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 4), 2);
  752. $count++;
  753. }
  754. }
  755. foreach ($cartInfo as &$cart) {
  756. $cart['coupon_id'] = 0;
  757. $cart['coupon_price'] = 0;
  758. if (isset($cart['productInfo']['cate_id']) && array_intersect(explode(',', $cart['productInfo']['cate_id']), $cateIds)) {
  759. if ($count > 1) {
  760. $coupon_price = bcmul((string)bcdiv((string)bcmul((string)$cart['cart_num'], (string)$cart['truePrice'], 4), (string)$total_price, 4), (string)$couponInfo['coupon_price'], 2);
  761. $compute_price = bcadd((string)$compute_price, (string)$coupon_price, 2);
  762. } else {
  763. $coupon_price = bcsub((string)$couponInfo['coupon_price'], $compute_price, 2);
  764. }
  765. $cart['coupon_id'] = $counpon_id;
  766. $cart['coupon_price'] = $coupon_price;
  767. $count--;
  768. }
  769. }
  770. }
  771. break;
  772. case 2://商品劵
  773. foreach ($cartInfo as $cart) {
  774. if (isset($cart['product_id']) && in_array($cart['product_id'], explode(',', $couponInfo['product_id']))) {
  775. $total_price = bcadd((string)$total_price, (string)bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 4), 2);
  776. $count++;
  777. }
  778. }
  779. foreach ($cartInfo as &$cart) {
  780. $cart['coupon_id'] = 0;
  781. $cart['coupon_price'] = 0;
  782. if (isset($cart['product_id']) && in_array($cart['product_id'], explode(',', $couponInfo['product_id']))) {
  783. if ($count > 1) {
  784. $coupon_price = bcmul((string)bcdiv((string)bcmul((string)$cart['cart_num'], (string)$cart['truePrice'], 4), (string)$total_price, 4), (string)$couponInfo['coupon_price'], 2);
  785. $compute_price = bcadd((string)$compute_price, (string)$coupon_price, 2);
  786. } else {
  787. $coupon_price = bcsub((string)$couponInfo['coupon_price'], $compute_price, 2);
  788. }
  789. $cart['coupon_id'] = $counpon_id;
  790. $cart['coupon_price'] = $coupon_price;
  791. $count--;
  792. }
  793. }
  794. break;
  795. }
  796. }
  797. }
  798. return $cartInfo;
  799. }
  800. /**
  801. * 计算实际佣金
  802. * @param int $uid
  803. * @param array $cartInfo
  804. * @return array
  805. * @throws \think\db\exception\DataNotFoundException
  806. * @throws \think\db\exception\DbException
  807. * @throws \think\db\exception\ModelNotFoundException
  808. */
  809. public function computeOrderProductBrokerage(int $uid, array $cartInfo)
  810. {
  811. [$storeBrokerageRatio, $storeBrokerageTwo, $spread_one_uid, $spread_two_uid] = $this->getSpreadDate($uid);
  812. /** @var DivisionServices $divisionService */
  813. $divisionService = app()->make(DivisionServices::class);
  814. [$storeBrokerageRatio, $storeBrokerageTwo, $staffPercent, $agentPercent, $divisionPercent] = $divisionService->getDivisionPercent($uid, $storeBrokerageRatio, $storeBrokerageTwo, sys_config('is_self_brokerage', 0));
  815. foreach ($cartInfo as &$cart) {
  816. $oneBrokerage = '0';//一级返佣金额
  817. $twoBrokerage = '0';//二级返佣金额
  818. $staffBrokerage = '0';//店员返佣金额
  819. $agentBrokerage = '0';//代理商返佣金额
  820. $divisionBrokerage = '0';//事业部返佣金额
  821. $cartNum = (string)$cart['cart_num'] ?? '0';
  822. if (isset($cart['productInfo'])) {
  823. $productInfo = $cart['productInfo'];
  824. //计算商品金额
  825. if (sys_config('user_brokerage_type') == 1) {
  826. //按照实际支付价格返佣
  827. $price = bcmul((string)bcadd((string)$cart['truePrice'], (string)$cart['postage_price'], 2), $cartNum, 4);
  828. } else {
  829. //按照商品价格返佣
  830. if (isset($productInfo['attrInfo'])) {
  831. $price = bcmul((string)($productInfo['attrInfo']['price'] ?? '0'), $cartNum, 4);
  832. } else {
  833. $price = bcmul((string)($productInfo['price'] ?? '0'), $cartNum, 4);
  834. }
  835. }
  836. //指定返佣金额
  837. if (isset($productInfo['is_sub']) && $productInfo['is_sub'] == 1) {
  838. $oneBrokerage = bcmul((string)($productInfo['attrInfo']['brokerage'] ?? '0'), $cartNum, 2);
  839. $twoBrokerage = bcmul((string)($productInfo['attrInfo']['brokerage_two'] ?? '0'), $cartNum, 2);
  840. } else {
  841. if ($price) {
  842. //一级返佣比例 小于等于零时直接返回 不返佣
  843. if ($storeBrokerageRatio > 0) {
  844. //计算获取一级返佣比例
  845. $brokerageRatio = bcdiv($storeBrokerageRatio, 100, 4);
  846. $oneBrokerage = bcmul((string)$price, (string)$brokerageRatio, 2);
  847. }
  848. //二级返佣比例小于等于0 直接返回
  849. if ($storeBrokerageTwo > 0) {
  850. //计算获取二级返佣比例
  851. $brokerageTwo = bcdiv($storeBrokerageTwo, 100, 4);
  852. $twoBrokerage = bcmul((string)$price, (string)$brokerageTwo, 2);
  853. }
  854. }
  855. $staffBrokerage = bcmul((string)$price, (string)bcdiv($staffPercent, 100, 4), 2);
  856. $agentBrokerage = bcmul((string)$price, (string)bcdiv($agentPercent, 100, 4), 2);
  857. $divisionBrokerage = bcmul((string)$price, (string)bcdiv($divisionPercent, 100, 4), 2);
  858. }
  859. }
  860. $cart['one_brokerage'] = $oneBrokerage;
  861. $cart['two_brokerage'] = $twoBrokerage;
  862. $cart['staff_brokerage'] = $staffBrokerage;
  863. $cart['agent_brokerage'] = $agentBrokerage;
  864. $cart['division_brokerage'] = $divisionBrokerage;
  865. }
  866. return [$cartInfo, [$spread_one_uid, $spread_two_uid]];
  867. }
  868. /**
  869. * 获取计算好的佣金比例以及返佣人员uid
  870. * @param int $uid
  871. * @return array|int[]
  872. * @throws \think\db\exception\DataNotFoundException
  873. * @throws \think\db\exception\DbException
  874. * @throws \think\db\exception\ModelNotFoundException
  875. * @author: 吴汐
  876. * @email: 442384644@qq.com
  877. * @date: 2023/10/8
  878. */
  879. public function getSpreadDate(int $uid)
  880. {
  881. //商城分销是否开启,用户uid是否存在,全部返回0
  882. if (!sys_config('brokerage_func_status') || !$uid) {
  883. return [0, 0, 0, 0];
  884. }
  885. //获取用户信息,获取不到全部返回0
  886. /** @var UserServices $userServices */
  887. $userServices = app()->make(UserServices::class);
  888. $userInfo = $userServices->getUserInfo($uid);
  889. if (!$userInfo) {
  890. return [0, 0, 0, 0];
  891. }
  892. //获取系统一二级分佣比例
  893. $storeBrokerageRatio = sys_config('store_brokerage_ratio') != '' ? sys_config('store_brokerage_ratio') : 0;
  894. $storeBrokerageTwo = sys_config('store_brokerage_two') != '' ? sys_config('store_brokerage_two') : 0;
  895. //获取上级和上上级的uid,开启自购获取自己和上级的uid
  896. $spread_one_uid = $userServices->getSpreadUid($uid, $userInfo);
  897. $spread_two_uid = 0;
  898. if ($spread_one_uid > 0 && $one_user_info = $userServices->getUserInfo($spread_one_uid)) {
  899. $spread_two_uid = $userServices->getSpreadUid($spread_one_uid, $one_user_info, false);
  900. }
  901. //计算分销等级之后的佣金比例
  902. [$storeBrokerageRatio, $storeBrokerageTwo] = app()->make(AgentLevelServices::class)->getAgentLevelBrokerage($storeBrokerageRatio, $storeBrokerageTwo, $spread_one_uid, $spread_two_uid);
  903. //判断返佣层级为一级时,将二级用户uid和二级分佣比例改为0
  904. if (sys_config('brokerage_level') == 1) {
  905. $storeBrokerageTwo = $spread_two_uid = 0;
  906. }
  907. return [$storeBrokerageRatio, $storeBrokerageTwo, $spread_one_uid, $spread_two_uid];
  908. }
  909. }