StoreOrderTakeServices.php 26 KB

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