Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

StoreOrderTakeServices.php 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. <?php
  2. namespace app\services\order;
  3. use app\dao\order\StoreOrderDao;
  4. use app\jobs\order\AutoTakeOrderJob;
  5. use app\jobs\notice\SmsAdminJob;
  6. use app\services\BaseServices;
  7. use app\services\message\service\StoreServiceServices;
  8. use app\services\message\sms\SmsSendServices;
  9. use app\services\user\member\MemberCardServices;
  10. use app\services\user\UserBillServices;
  11. use app\services\user\UserBrokerageServices;
  12. use app\services\user\level\UserLevelServices;
  13. use app\services\user\UserServices;
  14. use think\exception\ValidateException;
  15. use think\facade\Log;
  16. /**
  17. * 订单收货
  18. * Class StoreOrderTakeServices
  19. * @package app\services\order
  20. * @method get(int $id, ?array $field = []) 获取一条
  21. */
  22. class StoreOrderTakeServices extends BaseServices
  23. {
  24. /**
  25. * 构造方法
  26. * StoreOrderTakeServices constructor.
  27. * @param StoreOrderDao $dao
  28. */
  29. public function __construct(StoreOrderDao $dao)
  30. {
  31. $this->dao = $dao;
  32. }
  33. /**
  34. * 用户订单收货
  35. * @param $uni
  36. * @param $uid
  37. * @return bool
  38. */
  39. public function takeOrder(string $uni, int $uid)
  40. {
  41. $order = $this->dao->getUserOrderDetail($uni, $uid);
  42. if (!$order) {
  43. throw new ValidateException('订单不存在!');
  44. }
  45. /** @var StoreOrderServices $orderServices */
  46. $orderServices = app()->make(StoreOrderServices::class);
  47. $order = $orderServices->tidyOrder($order);
  48. if ($order['_status']['_type'] != 2) {
  49. throw new ValidateException('订单状态错误!');
  50. }
  51. //存在拆分发货 需要分开收货
  52. if ($this->dao->count(['pid' => $order['id']])) {
  53. throw new ValidateException('拆分发货,请去订单详情中包裹确认收货');
  54. }
  55. $order->status = 2;
  56. /** @var StoreOrderStatusServices $statusService */
  57. $statusService = app()->make(StoreOrderStatusServices::class);
  58. $res = $order->save() && $statusService->save([
  59. 'oid' => $order['id'],
  60. 'change_type' => 'user_take_delivery',
  61. 'change_message' => '用户已收货',
  62. 'change_time' => time()
  63. ]);
  64. $res = $res && $this->storeProductOrderUserTakeDelivery($order);
  65. if (!$res) {
  66. throw new ValidateException('收货失败');
  67. }
  68. //核销订单 修改订单商品核销状态
  69. if ($order['shipping_type'] == 2 || (in_array($order['shipping_type'], [1, 3]) && $order['delivery_type'] == 'send')) {
  70. //修改原来订单商品信息
  71. $cartData['is_writeoff'] = 1;
  72. $cartData['surplus_num'] = 0;
  73. /** @var StoreOrderCartInfoServices $cartInfoServices */
  74. $cartInfoServices = app()->make(StoreOrderCartInfoServices::class);
  75. $cartInfoServices->update(['oid' => $order['id']], $cartData);
  76. }
  77. return $order;
  78. }
  79. /**
  80. * 订单确认收货
  81. * @param $order
  82. * @return bool
  83. */
  84. public function storeProductOrderUserTakeDelivery($order, bool $isTran = true)
  85. {
  86. $res = true;
  87. //获取购物车内的商品标题
  88. /** @var StoreOrderCartInfoServices $orderInfoServices */
  89. $orderInfoServices = app()->make(StoreOrderCartInfoServices::class);
  90. $storeName = $orderInfoServices->getCarIdByProductTitle((int)$order['id'], $order['cart_id']);
  91. $storeTitle = substrUTf8($storeName, 20, 'UTF-8', '');
  92. if ($order['uid']) {
  93. /** @var UserServices $userServices */
  94. $userServices = app()->make(UserServices::class);
  95. $userInfo = $userServices->get((int)$order['uid']);
  96. $res = $this->transaction(function () use ($order, $userInfo, $storeTitle) {
  97. //赠送积分
  98. $res1 = $this->gainUserIntegral($order, $userInfo, $storeTitle);
  99. //返佣
  100. $res2 = $this->backOrderBrokerage($order, $userInfo);
  101. //经验
  102. $res3 = $this->gainUserExp($order, $userInfo);
  103. if (!($res1 && $res2 && $res3)) {
  104. throw new ValidateException('收货失败!');
  105. }
  106. return true;
  107. }, $isTran);
  108. }
  109. if ($res) {
  110. //订单收货事件
  111. event('order.take', [$order, $storeTitle]);
  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['type']) || in_array($order['type'], [1, 2, 3, 5])) {
  132. return true;
  133. }
  134. /** @var UserBillServices $userBillServices */
  135. $userBillServices = app()->make(UserBillServices::class);
  136. if ($order['gain_integral'] > 0) {
  137. $balance = $userInfo['integral'];
  138. $res2 = false != $userBillServices->income('pay_give_integral', $order['uid'], $order['gain_integral'], $balance, $order['id']);
  139. }
  140. $order_integral = 0;
  141. $res3 = true;
  142. $order_give_integral = sys_config('order_give_integral');
  143. if ($order['pay_price'] && $order_give_integral) {
  144. //会员消费返积分翻倍
  145. if ($userInfo['is_money_level'] > 0) {
  146. //看是否开启消费返积分翻倍奖励
  147. /** @var MemberCardServices $memberCardService */
  148. $memberCardService = app()->make(MemberCardServices::class);
  149. $integral_rule_number = $memberCardService->isOpenMemberCardCache('integral');
  150. if ($integral_rule_number) {
  151. $order_integral = bcmul((string)$order['pay_price'], (string)$integral_rule_number, 2);
  152. }
  153. }
  154. $order_integral = bcmul((string)$order_give_integral, (string)($order_integral ? $order_integral : $order['pay_price']), 0);
  155. $balance = bcadd((string)$userInfo['integral'], (string)$order['gain_integral'], 0);
  156. $res3 = false != $userBillServices->income('order_give_integral', $order['uid'], $order_integral, $balance, $order['id']);
  157. }
  158. $give_integral = $order_integral + $order['gain_integral'];
  159. if ($give_integral > 0) {
  160. $integral = $userInfo['integral'] + $give_integral;
  161. $userInfo->integral = $integral;
  162. $res1 = false != $userInfo->save();
  163. $res = $res1 && $res2 && $res3;
  164. //发送消息
  165. event('notice.notice', [['order' => $order, 'storeTitle' => $storeTitle, 'give_integral' => $give_integral, 'integral' => $integral], 'integral_accout']);
  166. return $res;
  167. }
  168. return true;
  169. }
  170. /**
  171. * 一级返佣
  172. * @param $orderInfo
  173. * @param $userInfo
  174. * @return bool
  175. */
  176. public function backOrderBrokerage($orderInfo, $userInfo)
  177. {
  178. // 当前订单|用户不存在 直接返回
  179. if (!$orderInfo || !$userInfo) {
  180. return true;
  181. }
  182. //商城分销功能是否开启 0关闭1开启
  183. if (!sys_config('brokerage_func_status')) return true;
  184. // 营销产品不返佣金
  185. if (!isset($orderInfo['type']) || in_array($orderInfo['type'], [1, 2, 3, 5])) {
  186. return true;
  187. }
  188. //绑定失效
  189. if (isset($orderInfo['spread_uid']) && $orderInfo['spread_uid'] == -1) {
  190. return true;
  191. }
  192. //是否开启自购返佣
  193. $isSelfBrokerage = sys_config('is_self_brokerage', 0);
  194. if (!isset($orderInfo['spread_uid']) || !$orderInfo['spread_uid']) {//兼容之前订单表没有spread_uid情况
  195. //没开启自购返佣 没有上级 或者 当用用户上级时自己 直接返回
  196. if (!$isSelfBrokerage && (!$userInfo['spread_uid'] || $userInfo['spread_uid'] == $orderInfo['uid'])) {
  197. return true;
  198. }
  199. $one_spread_uid = $isSelfBrokerage ? $userInfo['uid'] : $userInfo['spread_uid'];
  200. } else {
  201. $one_spread_uid = $orderInfo['spread_uid'];
  202. }
  203. $one_spread_uid = (int)$one_spread_uid;
  204. //冻结时间
  205. $broken_time = intval(sys_config('extract_time'));
  206. $frozen_time = time() + $broken_time * 86400;
  207. //检测是否是分销员
  208. /** @var UserServices $userServices */
  209. $userServices = app()->make(UserServices::class);
  210. if (!$userServices->checkUserPromoter($one_spread_uid)) {//一级不是分销员 直接二级返佣
  211. return $this->backOrderBrokerageTwo($orderInfo, $userInfo, $isSelfBrokerage, $frozen_time);
  212. }
  213. //订单中取出
  214. $brokeragePrice = $orderInfo['one_brokerage'] ?? 0;
  215. // 返佣金额小于等于0 直接返回不返佣金
  216. if ($brokeragePrice <= 0) {
  217. return true;
  218. }
  219. // 获取上级推广员信息
  220. $spreadPrice = $userServices->value(['uid' => $one_spread_uid], 'brokerage_price');
  221. // 上级推广员返佣之后的金额
  222. $balance = bcadd($spreadPrice, $brokeragePrice, 2);
  223. // 添加佣金记录
  224. /** @var UserBrokerageServices $userBrokerageServices */
  225. $userBrokerageServices = app()->make(UserBrokerageServices::class);
  226. //自购返佣 || 上级
  227. $type = $one_spread_uid == $orderInfo['uid'] ? 'get_self_brokerage' : 'get_brokerage';
  228. $res1 = $userBrokerageServices->income($type, $one_spread_uid, [
  229. 'nickname' => $userInfo['nickname'],
  230. 'pay_price' => floatval($orderInfo['pay_price']),
  231. 'number' => floatval($brokeragePrice),
  232. 'frozen_time' => $frozen_time
  233. ], $balance, $orderInfo['id']);
  234. // 添加用户佣金
  235. $res2 = $userServices->bcInc($one_spread_uid, 'brokerage_price', $brokeragePrice, 'uid');
  236. //给上级发送获得佣金的模板消息
  237. $this->sendBackOrderBrokerage($orderInfo, $one_spread_uid, $brokeragePrice);
  238. // 一级返佣成功 跳转二级返佣
  239. $res = $res1 && $res2 && $this->backOrderBrokerageTwo($orderInfo, $userInfo, $isSelfBrokerage, $frozen_time);
  240. return $res;
  241. }
  242. /**
  243. * 二级推广返佣
  244. * @param $orderInfo
  245. * @param $userInfo
  246. * @param int $isSelfbrokerage
  247. * @param int $frozenTime
  248. * @return bool
  249. */
  250. public function backOrderBrokerageTwo($orderInfo, $userInfo, $isSelfbrokerage = 0, $frozenTime = 0)
  251. {
  252. //绑定失效
  253. if (isset($orderInfo['spread_two_uid']) && $orderInfo['spread_two_uid'] == -1) {
  254. return true;
  255. }
  256. /** @var UserServices $userServices */
  257. $userServices = app()->make(UserServices::class);
  258. if (isset($orderInfo['spread_two_uid']) && $orderInfo['spread_two_uid']) {
  259. $spread_two_uid = $orderInfo['spread_two_uid'];
  260. } else {
  261. // 获取上推广人
  262. $userInfoTwo = $userServices->get((int)$userInfo['spread_uid']);
  263. // 订单|上级推广人不存在 直接返回
  264. if (!$orderInfo || !$userInfoTwo) {
  265. return true;
  266. }
  267. //没开启自购返佣 或者 上推广人没有上级 或者 当用用户上上级时自己 直接返回
  268. if (!$isSelfbrokerage && (!$userInfoTwo['spread_uid'] || $userInfoTwo['spread_uid'] == $orderInfo['uid'])) {
  269. return true;
  270. }
  271. $spread_two_uid = $isSelfbrokerage ? $userInfoTwo['uid'] : $userInfoTwo['spread_uid'];
  272. }
  273. $spread_two_uid = (int)$spread_two_uid;
  274. // 获取后台分销类型 1 指定分销 2 人人分销
  275. if (!$userServices->checkUserPromoter($spread_two_uid)) {
  276. return true;
  277. }
  278. //订单中取出
  279. $brokeragePrice = $orderInfo['two_brokerage'] ?? 0;
  280. // 返佣金额小于等于0 直接返回不返佣金
  281. if ($brokeragePrice <= 0) {
  282. return true;
  283. }
  284. // 获取上上级推广员信息
  285. $spreadPrice = $userServices->value(['uid' => $spread_two_uid], 'brokerage_price');
  286. // 获取上上级推广员返佣之后余额
  287. $balance = bcadd($spreadPrice, $brokeragePrice, 2);
  288. // 添加佣金记录
  289. /** @var UserBrokerageServices $userBrokerageServices */
  290. $userBrokerageServices = app()->make(UserBrokerageServices::class);
  291. $res1 = $userBrokerageServices->income('get_two_brokerage', $spread_two_uid, [
  292. 'nickname' => $userInfo['nickname'],
  293. 'pay_price' => floatval($orderInfo['pay_price']),
  294. 'number' => floatval($brokeragePrice),
  295. 'frozen_time' => $frozenTime
  296. ], $balance, $orderInfo['id']);
  297. // 添加用户佣金
  298. $res2 = $userServices->bcInc($spread_two_uid, 'brokerage_price', $brokeragePrice, 'uid');
  299. //给上级发送获得佣金的模板消息
  300. $this->sendBackOrderBrokerage($orderInfo, $spread_two_uid, $brokeragePrice);
  301. return $res1 && $res2;
  302. }
  303. /**
  304. * 佣金到账发送模板消息
  305. * @param $orderInfo
  306. * @param $spread_uid
  307. * @param $brokeragePrice
  308. */
  309. public function sendBackOrderBrokerage($orderInfo, $spread_uid, $brokeragePrice, string $type = 'order')
  310. {
  311. /** @var UserServices $userServices */
  312. $userServices = app()->make(UserServices::class);
  313. $userType = $userServices->value(['uid' => $spread_uid], 'user_type');
  314. if ($type == 'order') {
  315. /** @var StoreOrderCartInfoServices $storeOrderCartInfoService */
  316. $storeOrderCartInfoService = app()->make(StoreOrderCartInfoServices::class);
  317. $cartInfo = $storeOrderCartInfoService->getOrderCartInfo($orderInfo['id']);
  318. if ($cartInfo) {
  319. $cartInfo = array_column($cartInfo, 'cart_info');
  320. $goodsPrice = 0;
  321. $goodsName = "";
  322. foreach ($cartInfo as $k => $v) {
  323. $goodsName .= $v['productInfo']['store_name'];
  324. $price = $v['productInfo']['attrInfo']['price'] ?? $v['productInfo']['price'] ?? 0;
  325. $goodsPrice = bcadd((string)$goodsPrice, (string)$price, 2);
  326. }
  327. }
  328. } else {
  329. $goodsName = '推广用户获取佣金';
  330. $goodsPrice = $brokeragePrice;
  331. }
  332. //提醒推送
  333. event('notice.notice', [['spread_uid' => $spread_uid, 'userType' => $userType, 'brokeragePrice' => $brokeragePrice, 'goodsName' => $goodsName, 'goodsPrice' => $goodsPrice, 'add_time' => $orderInfo['add_time'] ?? time()], 'order_brokerage']);
  334. return true;
  335. }
  336. /**
  337. * 发送短信
  338. * @param $order
  339. * @param $storeTitle
  340. */
  341. public function smsSend($order, $storeTitle)
  342. {
  343. /** @var SmsSendServices $smsServices */
  344. $smsServices = app()->make(SmsSendServices::class);
  345. $switch = sys_config('confirm_take_over_switch') ? true : false;
  346. //模板变量
  347. $store_name = $storeTitle;
  348. $order_id = $order['order_id'];
  349. $smsServices->send($switch, $order['user_phone'], compact('store_name', 'order_id'), 'TAKE_DELIVERY_CODE');
  350. }
  351. /**
  352. * 发送确认收货管理员短信
  353. * @param $order
  354. */
  355. public function smsSendTake($order)
  356. {
  357. $switch = sys_config('admin_confirm_take_over_switch') ? true : false;
  358. /** @var StoreServiceServices $services */
  359. $services = app()->make(StoreServiceServices::class);
  360. $adminList = $services->getStoreServiceOrderNotice();
  361. SmsAdminJob::dispatchDo('sendAdminConfirmTakeOver', [$switch, $adminList, $order]);
  362. return true;
  363. }
  364. /**
  365. * 赠送经验
  366. * @param $order
  367. * @param $userInfo
  368. * @return bool
  369. */
  370. public function gainUserExp($order, $userInfo)
  371. {
  372. if (!$userInfo) {
  373. return true;
  374. }
  375. //用户等级是否开启
  376. if (!sys_config('member_func_status', 1)) {
  377. return true;
  378. }
  379. /** @var UserBillServices $userBillServices */
  380. $userBillServices = app()->make(UserBillServices::class);
  381. $order_exp = 0;
  382. $res3 = true;
  383. $order_give_exp = sys_config('order_give_exp');
  384. if ($order['pay_price'] && $order_give_exp) {
  385. $order_exp = bcmul($order_give_exp, (string)$order['pay_price'], 2);
  386. $res3 = false != $userBillServices->income('order_give_exp', $order['uid'], $order_exp, $userInfo['exp'], $order['id']);
  387. }
  388. $res = true;
  389. if ($order_exp > 0) {
  390. $exp = $userInfo['exp'] + $order_exp;
  391. $userInfo->exp = $exp;
  392. $res1 = false != $userInfo->save();
  393. $res = $res1 && $res3;
  394. }
  395. /** @var UserLevelServices $levelServices */
  396. $levelServices = app()->make(UserLevelServices::class);
  397. $levelServices->detection((int)$order['uid']);
  398. return $res;
  399. }
  400. /**
  401. * 加入队列
  402. * @param array $where
  403. * @param int $count
  404. * @param int $maxLimit
  405. * @return bool
  406. */
  407. public function batchJoinJobs(array $where, int $count, int $maxLimit)
  408. {
  409. $page = ceil($maxLimit / $count);
  410. for ($i = 1; $i <= $page; $i++) {
  411. AutoTakeOrderJob::dispatch([$where, $i, $maxLimit]);
  412. }
  413. return true;
  414. }
  415. /**
  416. * 执行自动收货
  417. * @param array $where
  418. * @param int $page
  419. * @param int $maxLimit
  420. * @return bool
  421. */
  422. public function runAutoTakeOrder(array $where, int $page = 0, int $maxLimit = 0)
  423. {
  424. /** @var StoreOrderStoreOrderStatusServices $service */
  425. $service = app()->make(StoreOrderStoreOrderStatusServices::class);
  426. $orderList = $service->getTakeOrderIds($where, $page, $maxLimit);
  427. foreach ($orderList as $order) {
  428. if ($order['status'] == 2) {
  429. continue;
  430. }
  431. if ($order['paid'] == 1 && $order['status'] == 1) {
  432. $data['status'] = 2;
  433. } else if ($order['pay_type'] == 'offline') {
  434. $data['status'] = 2;
  435. } else {
  436. continue;
  437. }
  438. try {
  439. /** @var StoreOrderStatusServices $statusService */
  440. $statusService = app()->make(StoreOrderStatusServices::class);
  441. $res = $this->dao->update($order['id'], $data) && $statusService->save([
  442. 'oid' => $order['id'],
  443. 'change_type' => 'take_delivery',
  444. 'change_message' => '已收货[自动收货]',
  445. 'change_time' => time()
  446. ]);
  447. $res = $res && $this->storeProductOrderUserTakeDelivery($order);
  448. if (!$res) {
  449. throw new ValidateException('订单号' . $order['order_id'] . '自动收货失败');
  450. }
  451. } catch (\Throwable $e) {
  452. Log::error('自动收货失败,失败原因:' . $e->getMessage());
  453. }
  454. }
  455. return true;
  456. }
  457. /**
  458. * 自动收货
  459. * @return bool
  460. */
  461. public function autoTakeOrder()
  462. {
  463. //7天前时间戳
  464. $systemDeliveryTime = (int)sys_config('system_delivery_time', 0);
  465. //0为取消自动收货功能
  466. if ($systemDeliveryTime == 0) {
  467. return true;
  468. }
  469. $sevenDay = strtotime(date('Y-m-d H:i:s', strtotime('-' . $systemDeliveryTime . ' day')));
  470. /** @var StoreOrderStoreOrderStatusServices $service */
  471. $service = app()->make(StoreOrderStoreOrderStatusServices::class);
  472. $where = [
  473. 'change_time' => $sevenDay,
  474. 'is_del' => 0,
  475. 'paid' => 1,
  476. 'status' => 1,
  477. 'change_type' => ['delivery_goods', 'delivery_fictitious', 'delivery']
  478. ];
  479. $maxLimit = 100;
  480. $count = $service->getTakeOrderCount($where);
  481. if ($count > $maxLimit) {
  482. return $this->batchJoinJobs($where, $count, $maxLimit);
  483. }
  484. return $this->runAutoTakeOrder($where);
  485. }
  486. }