StoreOrderTakeServices.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\order;
  12. use app\dao\order\StoreOrderDao;
  13. use app\services\BaseServices;
  14. use app\services\user\MemberCardServices;
  15. use app\services\user\UserBillServices;
  16. use app\services\user\UserBrokerageFrozenServices;
  17. use app\services\user\UserServices;
  18. use crmeb\utils\Str;
  19. use think\exception\ValidateException;
  20. use think\facade\Log;
  21. /**
  22. * 订单收货
  23. * Class StoreOrderTakeServices
  24. * @package app\services\order
  25. * @method get(int $id, ?array $field = []) 获取一条
  26. */
  27. class StoreOrderTakeServices extends BaseServices
  28. {
  29. /**
  30. * 构造方法
  31. * StoreOrderTakeServices constructor.
  32. * @param StoreOrderDao $dao
  33. */
  34. public function __construct(StoreOrderDao $dao)
  35. {
  36. $this->dao = $dao;
  37. }
  38. /**
  39. * 用户订单收货
  40. * @param $uni
  41. * @param $uid
  42. * @return bool
  43. */
  44. public function takeOrder(string $uni, int $uid)
  45. {
  46. $order = $this->dao->getUserOrderDetail($uni, $uid);
  47. if (!$order) {
  48. throw new ValidateException('订单不存在!');
  49. }
  50. /** @var StoreOrderServices $orderServices */
  51. $orderServices = app()->make(StoreOrderServices::class);
  52. $order = $orderServices->tidyOrder($order);
  53. if ($order['_status']['_type'] != 2) {
  54. throw new ValidateException('订单状态错误!');
  55. }
  56. //存在拆分发货 需要分开收货
  57. if ($this->dao->count(['pid' => $order['id']])) {
  58. throw new ValidateException('拆分发货,请去订单详情中包裹确认收货');
  59. }
  60. $order->status = 2;
  61. /** @var StoreOrderStatusServices $statusService */
  62. $statusService = app()->make(StoreOrderStatusServices::class);
  63. $res = $order->save() && $statusService->save([
  64. 'oid' => $order['id'],
  65. 'change_type' => 'user_take_delivery',
  66. 'change_message' => '用户已收货',
  67. 'change_time' => time()
  68. ]);
  69. $res = $res && $this->storeProductOrderUserTakeDelivery($order);
  70. if (!$res) {
  71. throw new ValidateException('收货失败');
  72. }
  73. return $order;
  74. }
  75. /**
  76. * 订单确认收货
  77. * @param $order
  78. * @return bool
  79. */
  80. public function storeProductOrderUserTakeDelivery($order, bool $isTran = true)
  81. {
  82. /** @var UserServices $userServices */
  83. $userServices = app()->make(UserServices::class);
  84. $userInfo = $userServices->get((int)$order['uid']);
  85. //获取购物车内的商品标题
  86. /** @var StoreOrderCartInfoServices $orderInfoServices */
  87. $orderInfoServices = app()->make(StoreOrderCartInfoServices::class);
  88. $storeName = $orderInfoServices->getCarIdByProductTitle($order['id'], $order['cart_id']);
  89. $storeTitle = Str::substrUTf8($storeName, 20, 'UTF-8', '');
  90. $res = $this->transaction(function () use ($order, $userInfo, $storeTitle) {
  91. //赠送积分
  92. $res1 = $this->gainUserIntegral($order, $userInfo, $storeTitle);
  93. //返佣
  94. $res2 = $this->backOrderBrokerage($order, $userInfo);
  95. //经验
  96. $res3 = $this->gainUserExp($order, $userInfo);
  97. if (!($res1 && $res2 && $res3)) {
  98. throw new ValidateException('收货失败!');
  99. }
  100. return true;
  101. }, $isTran);
  102. if ($res) {
  103. try {
  104. // 收货成功后置队列
  105. event('order.orderTake', [$order, $userInfo, $storeTitle]);
  106. //收货给用户发送消息
  107. event('notice.notice', [['order' => $order, 'storeTitle' => $storeTitle], 'order_take']);
  108. //收货给客服发送消息
  109. event('notice.notice', [['order' => $order, 'storeTitle' => $storeTitle], 'send_admin_confirm_take_over']);
  110. } catch (\Throwable $exception) {
  111. }
  112. return true;
  113. } else {
  114. return false;
  115. }
  116. }
  117. /**
  118. * 赠送积分
  119. * @param $order
  120. * @param $userInfo
  121. * @param $storeTitle
  122. * @return bool
  123. */
  124. public function gainUserIntegral($order, $userInfo, $storeTitle)
  125. {
  126. $res2 = true;
  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'], $order['gain_integral'], $userInfo['integral'], $order['id']);
  144. }
  145. $order_integral = 0;
  146. $res3 = true;
  147. $order_give_integral = sys_config('order_give_integral');
  148. if ($order['pay_price'] && $order_give_integral) {
  149. //会员消费返积分翻倍
  150. if ($userInfo['is_money_level'] > 0) {
  151. //看是否开启消费返积分翻倍奖励
  152. /** @var MemberCardServices $memberCardService */
  153. $memberCardService = app()->make(MemberCardServices::class);
  154. $integral_rule_number = $memberCardService->isOpenMemberCard('integral');
  155. if ($integral_rule_number) {
  156. $order_integral = bcmul((string)$order['pay_price'], (string)$integral_rule_number, 2);
  157. }
  158. }
  159. $order_integral = bcmul((string)$order_give_integral, (string)($order_integral ? $order_integral : $order['pay_price']), 0);
  160. $res3 = false != $userBillServices->income('order_give_integral', $order['uid'], $order_integral, $userInfo['integral'], $order['id']);
  161. }
  162. $give_integral = $order_integral + $order['gain_integral'];
  163. if ($give_integral > 0) {
  164. $integral = $userInfo['integral'] + $give_integral;
  165. $userInfo->integral = $integral;
  166. $res1 = false != $userInfo->save();
  167. $res = $res1 && $res2 && $res3;
  168. event('notice.notice', [['order' => $order, 'storeTitle' => $storeTitle, 'give_integral' => $give_integral, 'integral' => $integral], 'integral_accout']);
  169. return $res;
  170. }
  171. return true;
  172. }
  173. /**
  174. * 一级返佣
  175. * @param $orderInfo
  176. * @param $userInfo
  177. * @return bool
  178. */
  179. public function backOrderBrokerage($orderInfo, $userInfo)
  180. {
  181. // 当前订单|用户不存在 直接返回
  182. if (!$orderInfo || !$userInfo) {
  183. return true;
  184. }
  185. //商城分销功能是否开启 0关闭1开启
  186. if (!sys_config('brokerage_func_status')) return true;
  187. // 营销产品不返佣金
  188. if (isset($orderInfo['combination_id']) && $orderInfo['combination_id']) {
  189. return true;
  190. }
  191. if (isset($orderInfo['seckill_id']) && $orderInfo['seckill_id']) {
  192. return true;
  193. }
  194. if (isset($orderInfo['bargain_id']) && $orderInfo['bargain_id']) {
  195. return true;
  196. }
  197. //绑定失效
  198. if (isset($orderInfo['spread_uid']) && $orderInfo['spread_uid'] == -1) {
  199. return true;
  200. }
  201. //是否开启自购返佣
  202. $isSelfBrokerage = sys_config('is_self_brokerage', 0);
  203. if (!isset($orderInfo['spread_uid']) || !$orderInfo['spread_uid']) {//兼容之前订单表没有spread_uid情况
  204. //没开启自购返佣 没有上级 或者 当用用户上级时自己 直接返回
  205. if (!$isSelfBrokerage && (!$userInfo['spread_uid'] || $userInfo['spread_uid'] == $orderInfo['uid'])) {
  206. return true;
  207. }
  208. $one_spread_uid = $isSelfBrokerage ? $userInfo['uid'] : $userInfo['spread_uid'];
  209. } else {
  210. $one_spread_uid = $orderInfo['spread_uid'];
  211. }
  212. //检测是否是分销员
  213. /** @var UserServices $userServices */
  214. $userServices = app()->make(UserServices::class);
  215. if (!$userServices->checkUserPromoter($one_spread_uid)) {
  216. return $this->backOrderBrokerageTwo($orderInfo, $userInfo, $isSelfBrokerage);
  217. }
  218. $onebrokerage = $orderInfo['one_brokerage'] ?? 0;
  219. if ($onebrokerage) {//订单中取出
  220. $brokeragePrice = $onebrokerage;
  221. } else {
  222. $cartId = is_string($orderInfo['cart_id']) ? json_decode($orderInfo['cart_id'], true) : $orderInfo['cart_id'];
  223. /** @var StoreOrderCartInfoServices $cartServices */
  224. $cartServices = app()->make(StoreOrderCartInfoServices::class);
  225. $brokeragePrice = $cartServices->getProductBrokerage($cartId);
  226. }
  227. // 返佣金额小于等于0 直接返回不返佣金
  228. if ($brokeragePrice <= 0) {
  229. return true;
  230. }
  231. //TODO 获取上级推广员信息
  232. $spreadPrice = $userServices->value(['uid' => $one_spread_uid], 'brokerage_price');
  233. // 上级推广员返佣之后的金额
  234. $balance = bcadd($spreadPrice, $brokeragePrice, 2);
  235. // 添加推广记录
  236. /** @var UserBillServices $userBillServices */
  237. $userBillServices = app()->make(UserBillServices::class);
  238. $res1 = $userBillServices->income('get_brokerage', $one_spread_uid, [
  239. 'nickname' => $userInfo['nickname'],
  240. 'pay_price' => floatval($orderInfo['pay_price']),
  241. 'number' => floatval($brokeragePrice)
  242. ], $balance, $orderInfo['id']);
  243. // 添加用户余额
  244. $res2 = $userServices->bcInc($one_spread_uid, 'brokerage_price', $brokeragePrice, 'uid');
  245. if ($res2) {
  246. /** @var UserBrokerageFrozenServices $frozenService */
  247. $frozenService = app()->make(UserBrokerageFrozenServices::class);
  248. $res2 = $frozenService->saveBrokage($one_spread_uid, $brokeragePrice, $res1->id, $orderInfo['order_id']);
  249. //给上级发送获得佣金的模板消息
  250. $this->sendBackOrderBrokerage($orderInfo, $one_spread_uid, $brokeragePrice);
  251. }
  252. // 一级返佣成功 跳转二级返佣
  253. $res = $res1 && $res2 && $this->backOrderBrokerageTwo($orderInfo, $userInfo, $isSelfBrokerage);
  254. return $res;
  255. }
  256. /**
  257. * 二级推广返佣
  258. * @param $orderInfo
  259. * @param $userInfo
  260. * @param int $isSelfbrokerage
  261. * @return bool
  262. */
  263. public function backOrderBrokerageTwo($orderInfo, $userInfo, $isSelfbrokerage = 0)
  264. {
  265. //绑定失效
  266. if (isset($orderInfo['spread_two_uid']) && $orderInfo['spread_two_uid'] == -1) {
  267. return true;
  268. }
  269. /** @var UserServices $userServices */
  270. $userServices = app()->make(UserServices::class);
  271. if (isset($orderInfo['spread_two_uid']) && $orderInfo['spread_two_uid']) {
  272. $spread_two_uid = $orderInfo['spread_two_uid'];
  273. } else {
  274. // 获取上推广人
  275. $userInfoTwo = $userServices->get((int)$userInfo['spread_uid']);
  276. // 订单|上级推广人不存在 直接返回
  277. if (!$orderInfo || !$userInfoTwo) {
  278. return true;
  279. }
  280. //没开启自购返佣 或者 上推广人没有上级 或者 当用用户上上级时自己 直接返回
  281. if (!$isSelfbrokerage && (!$userInfoTwo['spread_uid'] || $userInfoTwo['spread_uid'] == $orderInfo['uid'])) {
  282. return true;
  283. }
  284. $spread_two_uid = $isSelfbrokerage ? $userInfoTwo['uid'] : $userInfoTwo['spread_uid'];
  285. }
  286. // 获取后台分销类型 1 指定分销 2 人人分销
  287. if (!$userServices->checkUserPromoter($spread_two_uid)) {
  288. return true;
  289. }
  290. $twobrokerage = $orderInfo['two_brokerage'] ?? 0;
  291. if ($twobrokerage) {
  292. $brokeragePrice = $twobrokerage;
  293. } else {//兼容之前下单未计算佣金类订单 重新计算一次
  294. $cartId = is_string($orderInfo['cart_id']) ? json_decode($orderInfo['cart_id'], true) : $orderInfo['cart_id'];
  295. /** @var StoreOrderCartInfoServices $cartServices */
  296. $cartServices = app()->make(StoreOrderCartInfoServices::class);
  297. $brokeragePrice = $cartServices->getProductBrokerage($cartId, false);
  298. }
  299. // 返佣金额小于等于0 直接返回不返佣金
  300. if ($brokeragePrice <= 0) {
  301. return true;
  302. }
  303. // 获取上上级推广员信息
  304. $spreadPrice = $userServices->value(['uid' => $spread_two_uid], 'brokerage_price');
  305. // 获取上上级推广员返佣之后余额
  306. $balance = bcadd($spreadPrice, $brokeragePrice, 2);
  307. // 添加返佣记录
  308. /** @var UserBillServices $userBillServices */
  309. $userBillServices = app()->make(UserBillServices::class);
  310. $res1 = $userBillServices->income('get_two_brokerage', $spread_two_uid, [
  311. 'nickname' => $userInfo['nickname'],
  312. 'pay_price' => floatval($orderInfo['pay_price']),
  313. 'number' => floatval($brokeragePrice)
  314. ], $balance, $orderInfo['id']);
  315. if ($res1) {
  316. /** @var UserBrokerageFrozenServices $frozenService */
  317. $frozenService = app()->make(UserBrokerageFrozenServices::class);
  318. $res1 = $frozenService->saveBrokage($spread_two_uid, $brokeragePrice, $res1->id, $orderInfo['order_id']);
  319. }
  320. // 添加用户余额
  321. $res2 = $userServices->bcInc($spread_two_uid, 'brokerage_price', $brokeragePrice, 'uid');
  322. //给上级发送获得佣金的模板消息
  323. $this->sendBackOrderBrokerage($orderInfo, $spread_two_uid, $brokeragePrice);
  324. return $res1 && $res2;
  325. }
  326. /**
  327. * 佣金到账发送模板消息
  328. * @param $orderInfo
  329. * @param $spread_uid
  330. * @param $brokeragePrice
  331. */
  332. public function sendBackOrderBrokerage($orderInfo, $spread_uid, $brokeragePrice, string $type = 'order')
  333. {
  334. /** @var UserServices $userServices */
  335. $userServices = app()->make(UserServices::class);
  336. $userType = $userServices->value(['uid' => $spread_uid], 'user_type');
  337. if ($type == 'order') {
  338. /** @var StoreOrderCartInfoServices $storeOrderCartInfoService */
  339. $storeOrderCartInfoService = app()->make(StoreOrderCartInfoServices::class);
  340. $cartInfo = $storeOrderCartInfoService->getOrderCartInfo($orderInfo['id']);
  341. if ($cartInfo) {
  342. $cartInfo = array_column($cartInfo, 'cart_info');
  343. $goodsPrice = 0;
  344. $goodsName = "";
  345. foreach ($cartInfo as $k => $v) {
  346. $goodsName .= $v['productInfo']['store_name'];
  347. $goodsPrice += $v['productInfo']['price'];
  348. }
  349. }
  350. } else {
  351. $goodsName = '推广用户获取佣金';
  352. $goodsPrice = $brokeragePrice;
  353. }
  354. //提醒推送
  355. event('notice.notice', [['spread_uid' => $spread_uid, 'userType' => $userType, 'brokeragePrice' => $brokeragePrice, 'goodsName' => $goodsName, 'goodsPrice' => $goodsPrice, 'add_time' => $orderInfo['add_time'] ?? time()], 'order_brokerage']);
  356. }
  357. /**
  358. * 赠送经验
  359. * @param $order
  360. * @param $userInfo
  361. * @return bool
  362. */
  363. public function gainUserExp($order, $userInfo)
  364. {
  365. if (!$userInfo) {
  366. return true;
  367. }
  368. //用户等级是否开启
  369. if (!sys_config('member_func_status', 1)) {
  370. return true;
  371. }
  372. /** @var UserBillServices $userBillServices */
  373. $userBillServices = app()->make(UserBillServices::class);
  374. $order_exp = 0;
  375. $res3 = true;
  376. $order_give_exp = sys_config('order_give_exp');
  377. if ($order['pay_price'] && $order_give_exp) {
  378. $order_exp = bcmul($order_give_exp, (string)$order['pay_price'], 2);
  379. $res3 = false != $userBillServices->income('order_give_exp', $order['uid'], $order_exp, $userInfo['exp'], $order['id']);
  380. }
  381. $res = true;
  382. if ($order_exp > 0) {
  383. $exp = $userInfo['exp'] + $order_exp;
  384. $userInfo->exp = $exp;
  385. $res1 = false != $userInfo->save();
  386. $res = $res1 && $res3;
  387. }
  388. //用户升级事件
  389. event('user.userLevel', [$order['uid']]);
  390. return $res;
  391. }
  392. /**
  393. * 自动收货
  394. * @return bool
  395. */
  396. public function autoTakeOrder()
  397. {
  398. //7天前时间戳
  399. $systemDeliveryTime = (int)sys_config('system_delivery_time', 0);
  400. //0为取消自动收货功能
  401. if ($systemDeliveryTime == 0) {
  402. return true;
  403. }
  404. $sevenDay = strtotime(date('Y-m-d H:i:s', strtotime('-' . $systemDeliveryTime . ' day')));
  405. /** @var StoreOrderStoreOrderStatusServices $service */
  406. $service = app()->make(StoreOrderStoreOrderStatusServices::class);
  407. $orderList = $service->getTakeOrderIds([
  408. 'change_time' => $sevenDay,
  409. 'is_del' => 0,
  410. 'paid' => 1,
  411. 'status' => 1,
  412. 'change_type' => ['delivery_goods', 'delivery_fictitious', 'delivery']
  413. ]);
  414. foreach ($orderList as $order) {
  415. if ($order['status'] == 2) {
  416. continue;
  417. }
  418. if ($order['paid'] == 1 && $order['status'] == 1) {
  419. $data['status'] = 2;
  420. } else if ($order['pay_type'] == 'offline') {
  421. $data['status'] = 2;
  422. } else {
  423. continue;
  424. }
  425. try {
  426. $this->transaction(function () use ($order, $data) {
  427. /** @var StoreOrderStatusServices $statusService */
  428. $statusService = app()->make(StoreOrderStatusServices::class);
  429. $res = $this->dao->update($order['id'], $data) && $statusService->save([
  430. 'oid' => $order['id'],
  431. 'change_type' => 'take_delivery',
  432. 'change_message' => '已收货[自动收货]',
  433. 'change_time' => time()
  434. ]);
  435. $res = $res && $this->storeProductOrderUserTakeDelivery($order, false);
  436. if (!$res) {
  437. throw new ValidateException('订单号' . $order['order_id'] . '自动收货失败');
  438. }
  439. });
  440. } catch (\Throwable $e) {
  441. Log::error('自动收货失败,失败原因:' . $e->getMessage());
  442. }
  443. }
  444. }
  445. }