StoreOrderTakeServices.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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\dao\order\StoreOrderDao;
  13. use app\services\activity\combination\StoreCombinationServices;
  14. use app\services\activity\combination\StorePinkServices;
  15. use app\services\BaseServices;
  16. use app\services\user\member\MemberCardServices;
  17. use app\services\user\UserBillServices;
  18. use app\services\user\UserBrokerageServices;
  19. use app\services\user\UserServices;
  20. use crmeb\exceptions\ApiException;
  21. use crmeb\utils\Str;
  22. use think\facade\Log;
  23. /**
  24. * 订单收货
  25. * Class StoreOrderTakeServices
  26. * @package app\services\order
  27. * @method get(int $id, ?array $field = []) 获取一条
  28. */
  29. class StoreOrderTakeServices extends BaseServices
  30. {
  31. /**
  32. * 构造方法
  33. * StoreOrderTakeServices constructor.
  34. * @param StoreOrderDao $dao
  35. */
  36. public function __construct(StoreOrderDao $dao)
  37. {
  38. $this->dao = $dao;
  39. }
  40. /**
  41. * 用户订单收货
  42. * @param $uni
  43. * @param $uid
  44. * @return bool
  45. */
  46. public function takeOrder(string $uni, int $uid)
  47. {
  48. $order = $this->dao->getUserOrderDetail($uni, $uid);
  49. if (!$order) {
  50. throw new ApiException(410173);
  51. }
  52. /** @var StoreOrderServices $orderServices */
  53. $orderServices = app()->make(StoreOrderServices::class);
  54. $order = $orderServices->tidyOrder($order);
  55. if ($order['_status']['_type'] != 2) {
  56. throw new ApiException(410266);
  57. }
  58. //存在拆分发货 需要分开收货
  59. if ($this->dao->count(['pid' => $order['id']])) {
  60. throw new ApiException(410266);
  61. }
  62. $order->status = 2;
  63. /** @var StoreOrderStatusServices $statusService */
  64. $statusService = app()->make(StoreOrderStatusServices::class);
  65. $res = $order->save() && $statusService->save([
  66. 'oid' => $order['id'],
  67. 'change_type' => 'user_take_delivery',
  68. 'change_message' => '用户已收货',
  69. 'change_time' => time()
  70. ]);
  71. $res = $res && $this->storeProductOrderUserTakeDelivery($order);
  72. if (!$res) {
  73. throw new ApiException(410205);
  74. }
  75. return $order;
  76. }
  77. /**
  78. * 订单确认收货
  79. * @param $order
  80. * @return bool
  81. */
  82. public function storeProductOrderUserTakeDelivery($order, bool $isTran = true)
  83. {
  84. /** @var UserServices $userServices */
  85. $userServices = app()->make(UserServices::class);
  86. $userInfo = $userServices->get((int)$order['uid']);
  87. //获取购物车内的商品标题
  88. /** @var StoreOrderCartInfoServices $orderInfoServices */
  89. $orderInfoServices = app()->make(StoreOrderCartInfoServices::class);
  90. $storeName = $orderInfoServices->getCarIdByProductTitle($order['id'], $order['cart_id']);
  91. $storeTitle = Str::substrUTf8($storeName, 20, 'UTF-8', '');
  92. $res = $this->transaction(function () use ($order, $userInfo, $storeTitle) {
  93. //赠送积分
  94. $res1 = $this->gainUserIntegral($order, $userInfo, $storeTitle);
  95. //返佣
  96. $res2 = $this->backOrderBrokerage($order, $userInfo);
  97. //经验
  98. $res3 = $this->gainUserExp($order, $userInfo);
  99. //事业部
  100. $res4 = $this->divisionBrokerage($order, $userInfo);
  101. if (!($res1 && $res2 && $res3 && $res4)) {
  102. throw new ApiException(410205);
  103. }
  104. return true;
  105. }, $isTran);
  106. if ($res) {
  107. try {
  108. // 收货成功后置队列
  109. event('order.orderTake', [$order, $userInfo, $storeTitle]);
  110. //收货给用户发送消息
  111. event('notice.notice', [['order' => $order, 'storeTitle' => $storeTitle], 'order_take']);
  112. //收货给客服发送消息
  113. event('notice.notice', [['order' => $order, 'storeTitle' => $storeTitle], 'send_admin_confirm_take_over']);
  114. } catch (\Throwable $exception) {
  115. }
  116. return true;
  117. } else {
  118. return false;
  119. }
  120. }
  121. /**
  122. * 赠送积分
  123. * @param $order
  124. * @param $userInfo
  125. * @param $storeTitle
  126. * @return bool
  127. */
  128. public function gainUserIntegral($order, $userInfo, $storeTitle)
  129. {
  130. $res1 = $res2 = $res3 = false;
  131. $integral = 0;
  132. if (!$userInfo) {
  133. return true;
  134. }
  135. // 营销产品送积分
  136. if (isset($order['combination_id']) && $order['combination_id']) {
  137. return true;
  138. }
  139. if (isset($order['seckill_id']) && $order['seckill_id']) {
  140. return true;
  141. }
  142. if (isset($order['bargain_id']) && $order['bargain_id']) {
  143. return true;
  144. }
  145. /** @var UserBillServices $userBillServices */
  146. $userBillServices = app()->make(UserBillServices::class);
  147. if ($order['gain_integral'] > 0) {
  148. $res2 = false != $userBillServices->income('pay_give_integral', $order['uid'], (int)$order['gain_integral'], $userInfo['integral'] + $order['gain_integral'], $order['id']);
  149. $integral = $userInfo['integral'] + $order['gain_integral'];
  150. $userInfo->integral = $integral;
  151. $res1 = false != $userInfo->save();
  152. }
  153. $order_integral = 0;
  154. $order_give_integral = sys_config('order_give_integral');
  155. if ($order['pay_price'] && $order_give_integral) {
  156. //会员消费返积分翻倍
  157. if ($userInfo['is_money_level'] > 0) {
  158. //看是否开启消费返积分翻倍奖励
  159. /** @var MemberCardServices $memberCardService */
  160. $memberCardService = app()->make(MemberCardServices::class);
  161. $integral_rule_number = $memberCardService->isOpenMemberCard('integral');
  162. if ($integral_rule_number) {
  163. $order_integral = bcmul((string)$order['pay_price'], (string)$integral_rule_number, 2);
  164. }
  165. }
  166. $order_integral = bcmul((string)$order_give_integral, (string)($order_integral ?: $order['pay_price']), 0);
  167. $res3 = false != $userBillServices->income('order_give_integral', $order['uid'], $order_integral, $userInfo['integral'] + $order_integral, $order['id']);
  168. $integral = $userInfo['integral'] + $order_integral;
  169. $userInfo->integral = $integral;
  170. $res1 = false != $userInfo->save();
  171. }
  172. $give_integral = $order_integral + $order['gain_integral'];
  173. if ($give_integral > 0 && $res1 && $res2 && $res3) {
  174. /** @var StoreOrderServices $orderServices */
  175. $orderServices = app()->make(StoreOrderServices::class);
  176. $orderServices->update($order['id'], ['gain_integral' => $give_integral], 'id');
  177. event('notice.notice', [['order' => $order, 'storeTitle' => $storeTitle, 'give_integral' => $give_integral, 'integral' => $integral], 'integral_accout']);
  178. return true;
  179. }
  180. return true;
  181. }
  182. /**
  183. * 事业部返佣
  184. * @param $orderInfo
  185. * @param $userInfo
  186. * @return bool
  187. */
  188. public function divisionBrokerage($orderInfo, $userInfo)
  189. {
  190. // 当前订单|用户不存在 直接返回
  191. if (!$orderInfo || !$userInfo) {
  192. return true;
  193. }
  194. // 营销产品不返佣金
  195. if (isset($orderInfo['combination_id']) && $orderInfo['combination_id']) {
  196. //检测拼团是否参与返佣
  197. /** @var StoreCombinationServices $combinationServices */
  198. $combinationServices = app()->make(StoreCombinationServices::class);
  199. $isCommission = $combinationServices->value(['id' => $orderInfo['combination_id']], 'is_commission');
  200. if (!$isCommission) {
  201. return true;
  202. }
  203. }
  204. if (isset($orderInfo['seckill_id']) && $orderInfo['seckill_id']) {
  205. return true;
  206. }
  207. if (isset($orderInfo['bargain_id']) && $orderInfo['bargain_id']) {
  208. return true;
  209. }
  210. /** @var UserServices $userServices */
  211. $userServices = app()->make(UserServices::class);
  212. if ($orderInfo['staff_id'] && $orderInfo['staff_brokerage'] > 0) {
  213. $spreadPrice = $userServices->value(['uid' => $orderInfo['staff_id']], 'brokerage_price');
  214. $balance = bcadd($spreadPrice, $orderInfo['staff_brokerage'], 2);
  215. $userServices->bcInc($orderInfo['staff_id'], 'brokerage_price', $orderInfo['staff_brokerage'], 'uid');
  216. //冻结时间
  217. $broken_time = intval(sys_config('extract_time'));
  218. $frozen_time = time() + $broken_time * 86400;
  219. // 添加佣金记录
  220. /** @var UserBrokerageServices $userBrokerageServices */
  221. $userBrokerageServices = app()->make(UserBrokerageServices::class);
  222. $userBrokerageServices->income('get_staff_brokerage', $orderInfo['staff_id'], [
  223. 'nickname' => $userInfo['nickname'],
  224. 'pay_price' => floatval($orderInfo['pay_price']),
  225. 'number' => floatval($orderInfo['staff_brokerage']),
  226. 'frozen_time' => $frozen_time
  227. ], $balance, $orderInfo['id']);
  228. }
  229. if ($orderInfo['agent_id'] && $orderInfo['agent_brokerage'] > 0) {
  230. $spreadPrice = $userServices->value(['uid' => $orderInfo['agent_id']], 'brokerage_price');
  231. $balance = bcadd($spreadPrice, $orderInfo['agent_brokerage'], 2);
  232. $userServices->bcInc($orderInfo['agent_id'], 'brokerage_price', $orderInfo['agent_brokerage'], 'uid');
  233. //冻结时间
  234. $broken_time = intval(sys_config('extract_time'));
  235. $frozen_time = time() + $broken_time * 86400;
  236. // 添加佣金记录
  237. /** @var UserBrokerageServices $userBrokerageServices */
  238. $userBrokerageServices = app()->make(UserBrokerageServices::class);
  239. $userBrokerageServices->income('get_agent_brokerage', $orderInfo['agent_id'], [
  240. 'nickname' => $userInfo['nickname'],
  241. 'pay_price' => floatval($orderInfo['pay_price']),
  242. 'number' => floatval($orderInfo['agent_brokerage']),
  243. 'frozen_time' => $frozen_time
  244. ], $balance, $orderInfo['id']);
  245. }
  246. if ($orderInfo['division_id'] && $orderInfo['division_brokerage'] > 0) {
  247. $spreadPrice = $userServices->value(['uid' => $orderInfo['division_id']], 'brokerage_price');
  248. $balance = bcadd($spreadPrice, $orderInfo['division_brokerage'], 2);
  249. $userServices->bcInc($orderInfo['division_id'], 'brokerage_price', $orderInfo['division_brokerage'], 'uid');
  250. //冻结时间
  251. $broken_time = intval(sys_config('extract_time'));
  252. $frozen_time = time() + $broken_time * 86400;
  253. // 添加佣金记录
  254. /** @var UserBrokerageServices $userBrokerageServices */
  255. $userBrokerageServices = app()->make(UserBrokerageServices::class);
  256. $userBrokerageServices->income('get_division_brokerage', $orderInfo['division_id'], [
  257. 'nickname' => $userInfo['nickname'],
  258. 'pay_price' => floatval($orderInfo['pay_price']),
  259. 'number' => floatval($orderInfo['division_brokerage']),
  260. 'frozen_time' => $frozen_time
  261. ], $balance, $orderInfo['id']);
  262. }
  263. return true;
  264. }
  265. /**
  266. * 一级返佣
  267. * @param $orderInfo
  268. * @param $userInfo
  269. * @return bool
  270. */
  271. public function backOrderBrokerage($orderInfo, $userInfo)
  272. {
  273. /** @var UserServices $userServices */
  274. $userServices = app()->make(UserServices::class);
  275. // 当前订单|用户不存在 直接返回
  276. if (!$orderInfo || !$userInfo) {
  277. return true;
  278. }
  279. //商城分销功能是否开启 0关闭1开启
  280. if (!sys_config('brokerage_func_status')) return true;
  281. // 营销产品不返佣金
  282. if (isset($orderInfo['combination_id']) && $orderInfo['combination_id']) {
  283. //检测拼团是否参与返佣
  284. /** @var StoreCombinationServices $combinationServices */
  285. $combinationServices = app()->make(StoreCombinationServices::class);
  286. $combinationInfo = $combinationServices->getOne(['id' => $orderInfo['combination_id']], 'is_commission,head_commission');
  287. if ($combinationInfo['head_commission']) {
  288. /** @var StorePinkServices $pinkServices */
  289. $pinkServices = app()->make(StorePinkServices::class);
  290. $pinkMasterUid = $pinkServices->value(['id' => $orderInfo['pink_id']], 'uid');
  291. if ($orderInfo['uid'] == $pinkMasterUid && $userServices->checkUserPromoter($pinkMasterUid)) {
  292. $pinkMasterPrice = bcmul((string)$orderInfo['pay_price'], bcdiv((string)$combinationInfo['head_commission'], 100, 2), 2);
  293. $userServices->bcInc($pinkMasterUid, 'brokerage_price', $pinkMasterPrice, 'uid');
  294. //冻结时间
  295. $broken_time = intval(sys_config('extract_time'));
  296. $frozen_time = time() + $broken_time * 86400;
  297. // 添加佣金记录
  298. /** @var UserBrokerageServices $userBrokerageServices */
  299. $userBrokerageServices = app()->make(UserBrokerageServices::class);
  300. //团长返佣
  301. $userBrokerageServices->income('get_pink_master_brokerage', $pinkMasterUid, [
  302. 'number' => floatval($pinkMasterPrice),
  303. 'frozen_time' => $frozen_time
  304. ], bcadd((string)$userInfo['brokerage_price'],$pinkMasterPrice,2), $orderInfo['id']);
  305. }
  306. }
  307. if (!$combinationInfo['is_commission']) {
  308. return true;
  309. }
  310. }
  311. if (isset($orderInfo['seckill_id']) && $orderInfo['seckill_id']) {
  312. return true;
  313. }
  314. if (isset($orderInfo['bargain_id']) && $orderInfo['bargain_id']) {
  315. return true;
  316. }
  317. //绑定失效
  318. if (isset($orderInfo['spread_uid']) && $orderInfo['spread_uid'] == -1) {
  319. return true;
  320. }
  321. //是否开启自购返佣
  322. $isSelfBrokerage = sys_config('is_self_brokerage', 0);
  323. if (!isset($orderInfo['spread_uid']) || !$orderInfo['spread_uid']) {//兼容之前订单表没有spread_uid情况
  324. //没开启自购返佣 没有上级 或者 当用用户上级时自己 直接返回
  325. if (!$isSelfBrokerage && (!$userInfo['spread_uid'] || $userInfo['spread_uid'] == $orderInfo['uid'])) {
  326. return true;
  327. }
  328. $one_spread_uid = $isSelfBrokerage ? $userInfo['uid'] : $userInfo['spread_uid'];
  329. } else {
  330. $one_spread_uid = $orderInfo['spread_uid'];
  331. }
  332. //检测是否是分销员
  333. if (!$userServices->checkUserPromoter($one_spread_uid)) {
  334. return $this->backOrderBrokerageTwo($orderInfo, $userInfo, $isSelfBrokerage);
  335. }
  336. $onebrokerage = $orderInfo['one_brokerage'] ?? 0;
  337. if ($onebrokerage) {//订单中取出
  338. $brokeragePrice = $onebrokerage;
  339. } else {
  340. $cartId = is_string($orderInfo['cart_id']) ? json_decode($orderInfo['cart_id'], true) : $orderInfo['cart_id'];
  341. /** @var StoreOrderCartInfoServices $cartServices */
  342. $cartServices = app()->make(StoreOrderCartInfoServices::class);
  343. $brokeragePrice = $cartServices->getProductBrokerage($cartId);
  344. }
  345. // 返佣金额小于等于0 直接返回不返佣金
  346. if ($brokeragePrice <= 0) {
  347. return true;
  348. }
  349. // 获取上级推广员信息
  350. $spreadPrice = $userServices->value(['uid' => $one_spread_uid], 'brokerage_price');
  351. // 上级推广员返佣之后的金额
  352. $balance = bcadd($spreadPrice, $brokeragePrice, 2);
  353. // 添加用户佣金
  354. $res1 = $userServices->bcInc($one_spread_uid, 'brokerage_price', $brokeragePrice, 'uid');
  355. if ($res1) {
  356. //冻结时间
  357. $broken_time = intval(sys_config('extract_time'));
  358. $frozen_time = time() + $broken_time * 86400;
  359. // 添加佣金记录
  360. /** @var UserBrokerageServices $userBrokerageServices */
  361. $userBrokerageServices = app()->make(UserBrokerageServices::class);
  362. //自购返佣 || 上级
  363. $type = $one_spread_uid == $orderInfo['uid'] ? 'get_self_brokerage' : 'get_brokerage';
  364. $userBrokerageServices->income($type, $one_spread_uid, [
  365. 'nickname' => $userInfo['nickname'],
  366. 'pay_price' => floatval($orderInfo['pay_price']),
  367. 'number' => floatval($brokeragePrice),
  368. 'frozen_time' => $frozen_time
  369. ], $balance, $orderInfo['id']);
  370. //给上级发送获得佣金的模板消息
  371. $this->sendBackOrderBrokerage($orderInfo, $one_spread_uid, $brokeragePrice);
  372. }
  373. // 一级返佣成功 跳转二级返佣
  374. $res = $res1 && $this->backOrderBrokerageTwo($orderInfo, $userInfo, $isSelfBrokerage, $frozen_time);
  375. return $res;
  376. }
  377. /**
  378. * 二级推广返佣
  379. * @param $orderInfo
  380. * @param $userInfo
  381. * @param $isSelfbrokerage
  382. * @param $frozenTime
  383. * @return bool
  384. */
  385. public function backOrderBrokerageTwo($orderInfo, $userInfo, $isSelfbrokerage = 0, $frozenTime = 0)
  386. {
  387. //绑定失效
  388. if (isset($orderInfo['spread_two_uid']) && $orderInfo['spread_two_uid'] == -1) {
  389. return true;
  390. }
  391. /** @var UserServices $userServices */
  392. $userServices = app()->make(UserServices::class);
  393. if (isset($orderInfo['spread_two_uid']) && $orderInfo['spread_two_uid']) {
  394. $spread_two_uid = $orderInfo['spread_two_uid'];
  395. } else {
  396. // 获取上推广人
  397. $userInfoTwo = $userServices->get((int)$userInfo['spread_uid']);
  398. // 订单|上级推广人不存在 直接返回
  399. if (!$orderInfo || !$userInfoTwo) {
  400. return true;
  401. }
  402. //没开启自购返佣 或者 上推广人没有上级 或者 当用用户上上级时自己 直接返回
  403. if (!$isSelfbrokerage && (!$userInfoTwo['spread_uid'] || $userInfoTwo['spread_uid'] == $orderInfo['uid'])) {
  404. return true;
  405. }
  406. $spread_two_uid = $isSelfbrokerage ? $userInfoTwo['uid'] : $userInfoTwo['spread_uid'];
  407. }
  408. // 获取后台分销类型 1 指定分销 2 人人分销
  409. if (!$userServices->checkUserPromoter($spread_two_uid)) {
  410. return true;
  411. }
  412. $twobrokerage = $orderInfo['two_brokerage'] ?? 0;
  413. if ($twobrokerage) {
  414. $brokeragePrice = $twobrokerage;
  415. } else {//兼容之前下单未计算佣金类订单 重新计算一次
  416. $cartId = is_string($orderInfo['cart_id']) ? json_decode($orderInfo['cart_id'], true) : $orderInfo['cart_id'];
  417. /** @var StoreOrderCartInfoServices $cartServices */
  418. $cartServices = app()->make(StoreOrderCartInfoServices::class);
  419. $brokeragePrice = $cartServices->getProductBrokerage($cartId, false);
  420. }
  421. // 返佣金额小于等于0 直接返回不返佣金
  422. if ($brokeragePrice <= 0) {
  423. return true;
  424. }
  425. // 获取上上级推广员信息
  426. $spreadPrice = $userServices->value(['uid' => $spread_two_uid], 'brokerage_price');
  427. // 获取上上级推广员返佣之后余额
  428. $balance = bcadd($spreadPrice, $brokeragePrice, 2);
  429. // 添加佣金记录
  430. /** @var UserBrokerageServices $userBrokerageServices */
  431. $userBrokerageServices = app()->make(UserBrokerageServices::class);
  432. $res1 = $userBrokerageServices->income('get_two_brokerage', $spread_two_uid, [
  433. 'nickname' => $userInfo['nickname'],
  434. 'pay_price' => floatval($orderInfo['pay_price']),
  435. 'number' => floatval($brokeragePrice),
  436. 'frozen_time' => $frozenTime
  437. ], $balance, $orderInfo['id']);
  438. // 添加用户余额
  439. $res2 = $userServices->bcInc($spread_two_uid, 'brokerage_price', $brokeragePrice, 'uid');
  440. //给上级发送获得佣金的模板消息
  441. $this->sendBackOrderBrokerage($orderInfo, $spread_two_uid, $brokeragePrice);
  442. return $res1 && $res2;
  443. }
  444. /**
  445. * 佣金到账发送模板消息
  446. * @param $orderInfo
  447. * @param $spread_uid
  448. * @param $brokeragePrice
  449. */
  450. public function sendBackOrderBrokerage($orderInfo, $spread_uid, $brokeragePrice, string $type = 'order')
  451. {
  452. /** @var UserServices $userServices */
  453. $userServices = app()->make(UserServices::class);
  454. $userType = $userServices->value(['uid' => $spread_uid], 'user_type');
  455. $goodsPrice = 0;
  456. $goodsName = '推广用户获取佣金';
  457. if ($type == 'order') {
  458. /** @var StoreOrderCartInfoServices $storeOrderCartInfoService */
  459. $storeOrderCartInfoService = app()->make(StoreOrderCartInfoServices::class);
  460. $cartInfo = $storeOrderCartInfoService->getOrderCartInfo($orderInfo['id']);
  461. if ($cartInfo) {
  462. $cartInfo = array_column($cartInfo, 'cart_info');
  463. $goodsPrice = 0;
  464. $goodsName = "";
  465. foreach ($cartInfo as $k => $v) {
  466. $goodsName .= $v['productInfo']['store_name'];
  467. $goodsPrice += $v['productInfo']['price'];
  468. }
  469. }
  470. } else {
  471. $goodsName = '推广用户获取佣金';
  472. $goodsPrice = $brokeragePrice;
  473. }
  474. //提醒推送
  475. event('notice.notice', [['spread_uid' => $spread_uid, 'userType' => $userType, 'brokeragePrice' => $brokeragePrice, 'goodsName' => $goodsName, 'goodsPrice' => $goodsPrice, 'add_time' => $orderInfo['add_time'] ?? time()], 'order_brokerage']);
  476. }
  477. /**
  478. * 赠送经验
  479. * @param $order
  480. * @param $userInfo
  481. * @return bool
  482. */
  483. public function gainUserExp($order, $userInfo)
  484. {
  485. if (!$userInfo) {
  486. return true;
  487. }
  488. //用户等级是否开启
  489. if (!sys_config('member_func_status', 1)) {
  490. return true;
  491. }
  492. /** @var UserBillServices $userBillServices */
  493. $userBillServices = app()->make(UserBillServices::class);
  494. $order_exp = 0;
  495. $res3 = true;
  496. $order_give_exp = sys_config('order_give_exp');
  497. if ($order['pay_price'] && $order_give_exp) {
  498. $order_exp = bcmul($order_give_exp, (string)$order['pay_price'], 2);
  499. $res3 = false != $userBillServices->income('order_give_exp', $order['uid'], $order_exp, bcadd((string)$userInfo['exp'], (string)$order_exp, 2), $order['id']);
  500. }
  501. $res = true;
  502. if ($order_exp > 0) {
  503. $exp = $userInfo['exp'] + $order_exp;
  504. $userInfo->exp = $exp;
  505. $res1 = false != $userInfo->save();
  506. $res = $res1 && $res3;
  507. }
  508. //用户升级事件
  509. event('user.userLevel', [$order['uid']]);
  510. return $res;
  511. }
  512. /**
  513. * 自动收货
  514. * @return bool
  515. */
  516. public function autoTakeOrder()
  517. {
  518. //7天前时间戳
  519. $systemDeliveryTime = (int)sys_config('system_delivery_time', 0);
  520. //0为取消自动收货功能
  521. if ($systemDeliveryTime == 0) {
  522. return true;
  523. }
  524. $sevenDay = strtotime(date('Y-m-d H:i:s', strtotime('-' . $systemDeliveryTime . ' day')));
  525. /** @var StoreOrderStoreOrderStatusServices $service */
  526. $service = app()->make(StoreOrderStoreOrderStatusServices::class);
  527. $orderList = $service->getTakeOrderIds([
  528. 'change_time' => $sevenDay,
  529. 'is_del' => 0,
  530. 'paid' => 1,
  531. 'status' => 1,
  532. 'change_type' => ['delivery_goods', 'delivery_fictitious', 'delivery']
  533. ]);
  534. foreach ($orderList as $order) {
  535. if ($order['status'] == 2) {
  536. continue;
  537. }
  538. if ($order['paid'] == 1 && $order['status'] == 1) {
  539. $data['status'] = 2;
  540. } else if ($order['pay_type'] == 'offline') {
  541. $data['status'] = 2;
  542. } else {
  543. continue;
  544. }
  545. try {
  546. $this->transaction(function () use ($order, $data) {
  547. /** @var StoreOrderStatusServices $statusService */
  548. $statusService = app()->make(StoreOrderStatusServices::class);
  549. $res = $this->dao->update($order['id'], $data) && $statusService->save([
  550. 'oid' => $order['id'],
  551. 'change_type' => 'take_delivery',
  552. 'change_message' => '已收货[自动收货]',
  553. 'change_time' => time()
  554. ]);
  555. $res = $res && $this->storeProductOrderUserTakeDelivery($order, false);
  556. if (!$res) {
  557. Log::error('订单号' . $order['order_id'] . '自动收货失败');
  558. }
  559. });
  560. } catch (\Throwable $e) {
  561. Log::error('自动收货失败,失败原因:' . $e->getMessage());
  562. }
  563. }
  564. }
  565. /**
  566. * 检查主订单是否需要修改状态
  567. * @param $pid
  568. * @throws \think\db\exception\DataNotFoundException
  569. * @throws \think\db\exception\DbException
  570. * @throws \think\db\exception\ModelNotFoundException
  571. */
  572. public function checkMaster($pid)
  573. {
  574. $p_order = $this->dao->get((int)$pid, ['id,pid,status']);
  575. //主订单全部发货 且子订单没有待收货 有待评价
  576. if ($p_order['status'] == 1 && !$this->dao->count(['pid' => $pid, 'status' => 2]) && $this->dao->count(['pid' => $pid, 'status' => 3])) {
  577. $this->dao->update($p_order['id'], ['status' => 2]);
  578. /** @var StoreOrderStatusServices $statusService */
  579. $statusService = app()->make(StoreOrderStatusServices::class);
  580. $statusService->save([
  581. 'oid' => $p_order['id'],
  582. 'change_type' => 'take_delivery',
  583. 'change_message' => '已收货',
  584. 'change_time' => time()
  585. ]);
  586. }
  587. }
  588. }