StoreOrderTakeServices.php 27 KB

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