StoreOrderTakeServices.php 27 KB

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