StoreOrderTakeServices.php 26 KB

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