StoreOrderTakeServices.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  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\jobs\integral\IntegralJob;
  14. use app\jobs\order\AutoTakeOrderJob;
  15. use app\jobs\notice\SmsAdminJob;
  16. use app\services\BaseServices;
  17. use app\services\message\service\StoreServiceServices;
  18. use app\services\message\sms\SmsSendServices;
  19. use app\services\user\member\MemberCardServices;
  20. use app\services\user\UserBillServices;
  21. use app\services\user\UserBrokerageServices;
  22. use app\services\user\level\UserLevelServices;
  23. use app\services\user\UserServices;
  24. use think\exception\ValidateException;
  25. use think\facade\Log;
  26. /**
  27. * 订单收货
  28. * Class StoreOrderTakeServices
  29. * @package app\services\order
  30. * @mixin StoreOrderDao
  31. */
  32. class StoreOrderTakeServices extends BaseServices
  33. {
  34. /**
  35. * 构造方法
  36. * StoreOrderTakeServices constructor.
  37. * @param StoreOrderDao $dao
  38. */
  39. public function __construct(StoreOrderDao $dao)
  40. {
  41. $this->dao = $dao;
  42. }
  43. /**
  44. * 小程序订单服务收货
  45. * @param $order_id
  46. * @return bool
  47. * @throws \think\db\exception\DataNotFoundException
  48. * @throws \think\db\exception\DbException
  49. * @throws \think\db\exception\ModelNotFoundException
  50. */
  51. public function miniOrderTakeOrder($order_id)
  52. {
  53. $orderArr = explode('_', $order_id);
  54. if (count($orderArr) == 2) {
  55. $order_id = $orderArr[1] ?? $order_id;
  56. }
  57. //查找订单信息
  58. $order = $this->dao->getOne(['order_id' => $order_id]);
  59. if (!$order) {
  60. return true;
  61. }
  62. if ($order['pid'] == -1) { // 有子订单
  63. // 查找待收货的子订单
  64. $son_order_list = $this->dao->getSubOrderNotSendList((int)$order['id']);
  65. foreach ($son_order_list as $son_order) {
  66. $this->takeOrder($son_order['order_id'], $son_order['uid']);
  67. }
  68. } else {
  69. $this->takeOrder($order_id, $order['uid']);
  70. }
  71. return true;
  72. }
  73. /**
  74. * 用户订单收货
  75. * @param $uni
  76. * @param $uid
  77. * @return bool
  78. */
  79. public function takeOrder(string $uni, int $uid)
  80. {
  81. $order = $this->dao->getUserOrderDetail($uni, $uid);
  82. if (!$order) {
  83. throw new ValidateException('订单不存在!');
  84. }
  85. /** @var StoreOrderServices $orderServices */
  86. $orderServices = app()->make(StoreOrderServices::class);
  87. $order = $orderServices->tidyOrder($order);
  88. if ($order['_status']['_type'] != 2) {
  89. throw new ValidateException('订单状态错误!');
  90. }
  91. //存在拆分发货 需要分开收货
  92. if ($this->dao->count(['pid' => $order['id']])) {
  93. throw new ValidateException('拆分发货,请去订单详情中包裹确认收货');
  94. }
  95. if ($order['type'] != 8) {
  96. $order->status = 2;
  97. } else {
  98. $order->status = 3;
  99. }
  100. /** @var StoreOrderStatusServices $statusService */
  101. $statusService = app()->make(StoreOrderStatusServices::class);
  102. $res = $order->save() && $statusService->save([
  103. 'oid' => $order['id'],
  104. 'change_type' => 'user_take_delivery',
  105. 'change_message' => '用户已收货',
  106. 'change_time' => time()
  107. ]);
  108. $res = $res && $this->storeProductOrderUserTakeDelivery($order);
  109. if (!$res) {
  110. throw new ValidateException('收货失败');
  111. }
  112. //核销订单 修改订单商品核销状态
  113. if ($order['shipping_type'] == 2 || (in_array($order['shipping_type'], [1, 3]) && $order['delivery_type'] == 'send')) {
  114. //修改原来订单商品信息
  115. $cartData['is_writeoff'] = 1;
  116. $cartData['write_surplus_times'] = 0;
  117. /** @var StoreOrderCartInfoServices $cartInfoServices */
  118. $cartInfoServices = app()->make(StoreOrderCartInfoServices::class);
  119. $cartInfoServices->update(['oid' => $order['id']], $cartData);
  120. }
  121. return $order;
  122. }
  123. /**
  124. * 订单确认收货
  125. * @param $order
  126. * @return bool
  127. */
  128. public function storeProductOrderUserTakeDelivery($order, bool $isTran = true)
  129. {
  130. $res = true;
  131. //获取购物车内的商品标题
  132. /** @var StoreOrderCartInfoServices $orderInfoServices */
  133. $orderInfoServices = app()->make(StoreOrderCartInfoServices::class);
  134. $storeName = $orderInfoServices->getCarIdByProductTitle((int)$order['id']);
  135. $storeTitle = substrUTf8($storeName, 20, 'UTF-8', '');
  136. if ($order['uid']) {
  137. /** @var UserServices $userServices */
  138. $userServices = app()->make(UserServices::class);
  139. $userInfo = $userServices->get((int)$order['uid']);
  140. $res = $this->transaction(function () use ($order, $userInfo, $storeTitle) {
  141. //赠送积分
  142. $res1 = $this->gainUserIntegral($order, $userInfo, $storeTitle);
  143. //返佣
  144. $res2 = $this->backOrderBrokerage($order, $userInfo);
  145. //返佣
  146. $res2 = $this->backAreaBrokerage($order, $userInfo);
  147. //经验
  148. $res3 = $this->gainUserExp($order, $userInfo);
  149. if (!($res1 && $res2 && $res3)) {
  150. throw new ValidateException('收货失败!');
  151. }
  152. return true;
  153. }, $isTran);
  154. }
  155. if ($res) {
  156. //订单收货事件
  157. event('order.take', [$order, $storeTitle]);
  158. return true;
  159. } else {
  160. return false;
  161. }
  162. }
  163. /**
  164. * 赠送积分
  165. * @param $order
  166. * @param $userInfo
  167. * @param $storeTitle
  168. * @return bool
  169. */
  170. public function gainUserIntegral($order, $userInfo, $storeTitle)
  171. {
  172. $res2 = true;
  173. if (!$userInfo) {
  174. return true;
  175. }
  176. // 营销产品送积分
  177. if (!isset($order['type']) || in_array($order['type'], [1, 2, 3, 5, 8])) {
  178. return true;
  179. }
  180. /** @var UserBillServices $userBillServices */
  181. $userBillServices = app()->make(UserBillServices::class);
  182. $balance = $userInfo['integral'];
  183. if ($order['gain_integral'] > 0) {
  184. $balance = bcadd((string)$balance, (string)$order['gain_integral']);
  185. $res2 = false != $userBillServices->income('pay_give_integral', $order['uid'], (int)$order['gain_integral'], (int)$balance, $order['id']);
  186. }
  187. $order_integral = 0;
  188. $res3 = true;
  189. $order_give_integral = sys_config('order_give_integral');
  190. if ($order['pay_price'] && $order_give_integral) {
  191. //会员消费返积分翻倍
  192. if ($userInfo['is_money_level'] > 0) {
  193. //看是否开启消费返积分翻倍奖励
  194. /** @var MemberCardServices $memberCardService */
  195. $memberCardService = app()->make(MemberCardServices::class);
  196. $integral_rule_number = $memberCardService->isOpenMemberCardCache('integral');
  197. if ($integral_rule_number) {
  198. $order_integral = bcmul((string)$order['pay_price'], (string)$integral_rule_number, 2);
  199. }
  200. }
  201. $order_integral = bcmul((string)$order_give_integral, (string)($order_integral ? $order_integral : $order['pay_price']), 0);
  202. $balance = bcadd((string)$balance, (string)$order_integral);
  203. $res3 = false != $userBillServices->income('order_give_integral', $order['uid'], (int)$order_integral, (int)$balance, $order['id']);
  204. }
  205. $give_integral = $order_integral + $order['gain_integral'];
  206. if ($give_integral > 0) {
  207. $integral = $userInfo['integral'] + $give_integral;
  208. $userInfo->integral = $integral;
  209. $res1 = false != $userInfo->save();
  210. $res = $res1 && $res2 && $res3;
  211. //发送消息
  212. event('notice.notice', [['order' => $order, 'storeTitle' => $storeTitle, 'give_integral' => $give_integral, 'integral' => $integral], 'integral_accout']);
  213. return $res;
  214. }
  215. return true;
  216. }
  217. /**
  218. * 地区返佣
  219. * @param $orderInfo
  220. * @param $userInfo
  221. * @return bool
  222. */
  223. public function backAreaBrokerage($orderInfo, $userInfo)
  224. {
  225. $storeOrderServices = app()->make(StoreOrderServices::class);
  226. // 当前订单|用户不存在 直接返回
  227. if (!$orderInfo || !$userInfo) {
  228. return $storeOrderServices->update($orderInfo['id'], ['send_area_award' => 1]);
  229. }
  230. if (!$orderInfo['province'] || $orderInfo['send_area_award'] == 1) {
  231. return $storeOrderServices->update($orderInfo['id'], ['send_area_award' => 1]);
  232. }
  233. //商城分销功能是否开启 0关闭1开启
  234. if (!sys_config('brokerage_func_status')) return $storeOrderServices->update($orderInfo['id'], ['send_area_award' => 1]);
  235. if (sys_config('area_award_time') > 0) return true;
  236. // 营销产品不返佣金
  237. if (!isset($orderInfo['type']) || in_array($orderInfo['type'], [1, 2, 3, 5, 8])) {
  238. return $storeOrderServices->update($orderInfo['id'], ['send_area_award' => 1]);
  239. }
  240. //冻结时间
  241. $broken_time = intval(sys_config('extract_time'));
  242. $frozen_time = time() + $broken_time * 86400;
  243. //订单中取出
  244. $order_price = $orderInfo['total_price'] ?? 0;
  245. $order_price = bcsub($order_price, $orderInfo['cost'], 2);
  246. // 一级返佣金额小于等于0
  247. if ($order_price <= 0) {//直接二级返佣
  248. return $storeOrderServices->update($orderInfo['id'], ['send_area_award' => 1]);
  249. }
  250. // 获取上级推广员信息
  251. /** @var UserServices $userServices */
  252. $userServices = app()->make(UserServices::class);
  253. $res = true;
  254. //找省代
  255. $admins = $userServices->search()->where('area_admin', 3)->where('area_admin_province', $orderInfo['order_province'])->select();
  256. if (count($admins) > 0) {
  257. $award = bcdiv(bcmul((string)sys_config('province_award'), (string)$order_price, 2), 100, 2);
  258. if ($award > 0) {
  259. $brokeragePrice = bcdiv($award, (string)count($admins), 2);
  260. if ($brokeragePrice > 0) {
  261. foreach ($admins as $v) {
  262. $spreadPrice = (string)$v['brokerage_price'];
  263. // 上级推广员返佣之后的金额
  264. $balance = bcadd($spreadPrice, $brokeragePrice, 2);
  265. // 添加佣金记录
  266. /** @var UserBrokerageServices $userBrokerageServices */
  267. $userBrokerageServices = app()->make(UserBrokerageServices::class);
  268. $type = 'get_area_brokerage';
  269. $res1 = $userBrokerageServices->income($type, $v['uid'], [
  270. 'nickname' => $userInfo['nickname'],
  271. 'pay_price' => floatval($orderInfo['pay_price']),
  272. 'area' => $orderInfo['order_province'] . '省级',
  273. 'number' => floatval($brokeragePrice),
  274. 'frozen_time' => $frozen_time
  275. ], $balance, $orderInfo['id']);
  276. // 添加用户佣金
  277. $res2 = $userServices->bcInc($v['uid'], 'brokerage_price', $brokeragePrice, 'uid');
  278. $res = $res1 && $res2 && $res;
  279. }
  280. }
  281. }
  282. }
  283. //找市代
  284. $admins = $userServices->search()->where('area_admin', 2)->where('area_admin_province', $orderInfo['order_province'])
  285. ->where('area_admin_city', $orderInfo['order_city'])->select();
  286. if (count($admins) > 0) {
  287. $award = bcdiv(bcmul((string)sys_config('city_award'), (string)$order_price, 2), 100, 2);
  288. if ($award > 0) {
  289. $brokeragePrice = bcdiv($award, (string)count($admins), 2);
  290. if ($brokeragePrice > 0) {
  291. foreach ($admins as $v) {
  292. $spreadPrice = (string)$v['brokerage_price'];
  293. // 上级推广员返佣之后的金额
  294. $balance = bcadd($spreadPrice, $brokeragePrice, 2);
  295. // 添加佣金记录
  296. /** @var UserBrokerageServices $userBrokerageServices */
  297. $userBrokerageServices = app()->make(UserBrokerageServices::class);
  298. $type = 'get_area_brokerage';
  299. $res1 = $userBrokerageServices->income($type, $v['uid'], [
  300. 'nickname' => $userInfo['nickname'],
  301. 'pay_price' => floatval($orderInfo['pay_price']),
  302. 'area' => $orderInfo['order_province'] . $orderInfo['order_city'] . '级',
  303. 'number' => floatval($brokeragePrice),
  304. 'frozen_time' => $frozen_time
  305. ], $balance, $orderInfo['id']);
  306. // 添加用户佣金
  307. $res2 = $userServices->bcInc($v['uid'], 'brokerage_price', $brokeragePrice, 'uid');
  308. $res = $res1 && $res2 && $res;
  309. }
  310. }
  311. }
  312. }
  313. //找区代
  314. $admins = $userServices->search()->where('area_admin', 1)
  315. ->where('area_admin_province', $orderInfo['order_province'])
  316. ->where('area_admin_city', $orderInfo['order_city'])
  317. ->where('area_admin_district', $orderInfo['order_district'])
  318. ->select();
  319. if (count($admins) > 0) {
  320. $award = bcdiv(bcmul((string)sys_config('district_award'), (string)$order_price, 2), 100, 2);
  321. if ($award > 0) {
  322. $brokeragePrice = bcdiv($award, (string)count($admins), 2);
  323. if ($brokeragePrice > 0) {
  324. foreach ($admins as $v) {
  325. $spreadPrice = (string)$v['brokerage_price'];
  326. // 上级推广员返佣之后的金额
  327. $balance = bcadd($spreadPrice, $brokeragePrice, 2);
  328. // 添加佣金记录
  329. /** @var UserBrokerageServices $userBrokerageServices */
  330. $userBrokerageServices = app()->make(UserBrokerageServices::class);
  331. $type = 'get_area_brokerage';
  332. $res1 = $userBrokerageServices->income($type, $v['uid'], [
  333. 'nickname' => $userInfo['nickname'],
  334. 'pay_price' => floatval($orderInfo['pay_price']),
  335. 'area' => $orderInfo['order_province'] . $orderInfo['order_city'] . $orderInfo['order_district'] . '级',
  336. 'number' => floatval($brokeragePrice),
  337. 'frozen_time' => $frozen_time
  338. ], $balance, $orderInfo['id']);
  339. // 添加用户佣金
  340. $res2 = $userServices->bcInc($v['uid'], 'brokerage_price', $brokeragePrice, 'uid');
  341. $res = $res1 && $res2 && $res;
  342. }
  343. }
  344. }
  345. }
  346. return $res && $storeOrderServices->update($orderInfo['id'], ['send_area_award' => 1]);
  347. }
  348. /**
  349. * 一级返佣
  350. * @param $orderInfo
  351. * @param $userInfo
  352. * @return bool
  353. */
  354. public function backOrderBrokerage($orderInfo, $userInfo)
  355. {
  356. // 当前订单|用户不存在 直接返回
  357. if (!$orderInfo || !$userInfo) {
  358. return true;
  359. }
  360. //商城分销功能是否开启 0关闭1开启
  361. if (!sys_config('brokerage_func_status')) return true;
  362. // 营销产品不返佣金
  363. if (!isset($orderInfo['type']) || in_array($orderInfo['type'], [1, 2, 3, 5, 8]) || $orderInfo['award_price'] > 0) {
  364. return true;
  365. }
  366. //绑定失效
  367. if (isset($orderInfo['spread_uid']) && $orderInfo['spread_uid'] == -1) {
  368. return true;
  369. }
  370. //是否开启自购返佣
  371. $isSelfBrokerage = sys_config('is_self_brokerage', 0);
  372. if (!isset($orderInfo['spread_uid']) || !$orderInfo['spread_uid']) {//兼容之前订单表没有spread_uid情况
  373. //没开启自购返佣 没有上级 或者 当用用户上级时自己 直接返回
  374. if (!$isSelfBrokerage && (!$userInfo['spread_uid'] || $userInfo['spread_uid'] == $orderInfo['uid'])) {
  375. return true;
  376. }
  377. $one_spread_uid = $isSelfBrokerage ? $userInfo['uid'] : $userInfo['spread_uid'];
  378. } else {
  379. $one_spread_uid = $orderInfo['spread_uid'];
  380. }
  381. $one_spread_uid = (int)$one_spread_uid;
  382. //冻结时间
  383. $broken_time = intval(sys_config('extract_time'));
  384. $frozen_time = time() + $broken_time * 86400;
  385. //订单中取出
  386. $brokeragePrice = $orderInfo['one_brokerage'] ?? 0;
  387. // 一级返佣金额小于等于0
  388. if ($brokeragePrice <= 0) {//直接二级返佣
  389. return $this->backOrderBrokerageTwo($orderInfo, $userInfo, $isSelfBrokerage, $frozen_time);
  390. }
  391. // 获取上级推广员信息
  392. /** @var UserServices $userServices */
  393. $userServices = app()->make(UserServices::class);
  394. $spreadPrice = $userServices->value(['uid' => $one_spread_uid], 'brokerage_price');
  395. // 上级推广员返佣之后的金额
  396. $balance = bcadd($spreadPrice, $brokeragePrice, 2);
  397. // 添加佣金记录
  398. /** @var UserBrokerageServices $userBrokerageServices */
  399. $userBrokerageServices = app()->make(UserBrokerageServices::class);
  400. //自购返佣 || 上级
  401. $type = $one_spread_uid == $orderInfo['uid'] ? 'get_self_brokerage' : 'get_brokerage';
  402. $res1 = $userBrokerageServices->income($type, $one_spread_uid, [
  403. 'nickname' => $userInfo['nickname'],
  404. 'pay_price' => floatval($orderInfo['pay_price']),
  405. 'number' => floatval($brokeragePrice),
  406. 'frozen_time' => $frozen_time
  407. ], $balance, $orderInfo['id']);
  408. // 添加用户佣金
  409. $res2 = $userServices->bcInc($one_spread_uid, 'brokerage_price', $brokeragePrice, 'uid');
  410. //给上级发送获得佣金的模板消息
  411. $this->sendBackOrderBrokerage($orderInfo, $one_spread_uid, $brokeragePrice);
  412. // 一级返佣成功 跳转二级返佣
  413. $res = $res1 && $res2 && $this->backOrderBrokerageTwo($orderInfo, $userInfo, $isSelfBrokerage, $frozen_time);
  414. return $res;
  415. }
  416. /**
  417. * 二级推广返佣
  418. * @param $orderInfo
  419. * @param $userInfo
  420. * @param int $isSelfbrokerage
  421. * @param int $frozenTime
  422. * @return bool
  423. */
  424. public function backOrderBrokerageTwo($orderInfo, $userInfo, $isSelfbrokerage = 0, $frozenTime = 0)
  425. {
  426. //绑定失效
  427. if (isset($orderInfo['spread_two_uid']) && $orderInfo['spread_two_uid'] == -1) {
  428. return true;
  429. }
  430. /** @var UserServices $userServices */
  431. $userServices = app()->make(UserServices::class);
  432. if (isset($orderInfo['spread_two_uid']) && $orderInfo['spread_two_uid']) {
  433. $spread_two_uid = $orderInfo['spread_two_uid'];
  434. } else {
  435. // 获取上推广人
  436. $userInfoTwo = $userServices->get((int)$userInfo['spread_uid']);
  437. // 订单|上级推广人不存在 直接返回
  438. if (!$orderInfo || !$userInfoTwo) {
  439. return true;
  440. }
  441. //没开启自购返佣 或者 上推广人没有上级 或者 当用用户上上级时自己 直接返回
  442. if (!$isSelfbrokerage && (!$userInfoTwo['spread_uid'] || $userInfoTwo['spread_uid'] == $orderInfo['uid'])) {
  443. return true;
  444. }
  445. $spread_two_uid = $isSelfbrokerage ? $userInfoTwo['uid'] : $userInfoTwo['spread_uid'];
  446. }
  447. $spread_two_uid = (int)$spread_two_uid;
  448. //订单中取出
  449. $brokeragePrice = $orderInfo['two_brokerage'] ?? 0;
  450. // 返佣金额小于等于0 直接返回不返佣金
  451. if ($brokeragePrice <= 0) {
  452. return true;
  453. }
  454. // 获取上上级推广员信息
  455. $spreadPrice = $userServices->value(['uid' => $spread_two_uid], 'brokerage_price');
  456. // 获取上上级推广员返佣之后余额
  457. $balance = bcadd($spreadPrice, $brokeragePrice, 2);
  458. // 添加佣金记录
  459. /** @var UserBrokerageServices $userBrokerageServices */
  460. $userBrokerageServices = app()->make(UserBrokerageServices::class);
  461. $res1 = $userBrokerageServices->income('get_two_brokerage', $spread_two_uid, [
  462. 'nickname' => $userInfo['nickname'],
  463. 'pay_price' => floatval($orderInfo['pay_price']),
  464. 'number' => floatval($brokeragePrice),
  465. 'frozen_time' => $frozenTime
  466. ], $balance, $orderInfo['id']);
  467. // 添加用户佣金
  468. $res2 = $userServices->bcInc($spread_two_uid, 'brokerage_price', $brokeragePrice, 'uid');
  469. //给上级发送获得佣金的模板消息
  470. $this->sendBackOrderBrokerage($orderInfo, $spread_two_uid, $brokeragePrice);
  471. return $res1 && $res2;
  472. }
  473. /**
  474. * 佣金到账发送模板消息
  475. * @param $orderInfo
  476. * @param $spread_uid
  477. * @param $brokeragePrice
  478. */
  479. public function sendBackOrderBrokerage($orderInfo, $spread_uid, $brokeragePrice, string $type = 'order')
  480. {
  481. /** @var UserServices $userServices */
  482. $userServices = app()->make(UserServices::class);
  483. $user = $userServices->getUserInfo($spread_uid, 'phone,user_type');
  484. if ($type == 'order') {
  485. /** @var StoreOrderCartInfoServices $storeOrderCartInfoService */
  486. $storeOrderCartInfoService = app()->make(StoreOrderCartInfoServices::class);
  487. $cartInfo = $storeOrderCartInfoService->getOrderCartInfo($orderInfo['id']);
  488. if ($cartInfo) {
  489. $cartInfo = array_column($cartInfo, 'cart_info');
  490. $goodsPrice = 0;
  491. $goodsName = "";
  492. foreach ($cartInfo as $k => $v) {
  493. $goodsName .= $v['productInfo']['store_name'];
  494. $price = $v['productInfo']['attrInfo']['price'] ?? $v['productInfo']['price'] ?? 0;
  495. $goodsPrice = bcadd((string)$goodsPrice, (string)$price, 2);
  496. }
  497. }
  498. } else {
  499. $goodsName = '推广用户获取佣金';
  500. $goodsPrice = $brokeragePrice;
  501. }
  502. //提醒推送
  503. event('notice.notice', [['spread_uid' => $spread_uid, 'phone' => $user['phone'], 'userType' => $user['user_type'], 'brokeragePrice' => $brokeragePrice, 'goodsName' => $goodsName, 'goodsPrice' => $goodsPrice, 'add_time' => $orderInfo['add_time'] ?? time()], 'order_brokerage']);
  504. return true;
  505. }
  506. /**
  507. * 发送短信
  508. * @param $order
  509. * @param $storeTitle
  510. */
  511. public function smsSend($order, $storeTitle)
  512. {
  513. /** @var SmsSendServices $smsServices */
  514. $smsServices = app()->make(SmsSendServices::class);
  515. $switch = (bool)sys_config('confirm_take_over_switch');
  516. //模板变量
  517. $store_name = $storeTitle;
  518. $order_id = $order['order_id'];
  519. $smsServices->send($switch, $order['user_phone'], compact('store_name', 'order_id'), 'TAKE_DELIVERY_CODE');
  520. }
  521. /**
  522. * 发送确认收货管理员短信
  523. * @param $order
  524. */
  525. public function smsSendTake($order)
  526. {
  527. $switch = (bool)sys_config('admin_confirm_take_over_switch');
  528. /** @var StoreServiceServices $services */
  529. $services = app()->make(StoreServiceServices::class);
  530. $adminList = $services->getStoreServiceOrderNotice();
  531. SmsAdminJob::dispatchDo('sendAdminConfirmTakeOver', [$switch, $adminList, $order]);
  532. return true;
  533. }
  534. /**
  535. * 赠送经验
  536. * @param $order
  537. * @param $userInfo
  538. * @return bool
  539. */
  540. public function gainUserExp($order, $userInfo)
  541. {
  542. if (!$userInfo) {
  543. return true;
  544. }
  545. //用户等级是否开启
  546. if (!sys_config('member_func_status', 1)) {
  547. return true;
  548. }
  549. /** @var UserBillServices $userBillServices */
  550. $userBillServices = app()->make(UserBillServices::class);
  551. $order_exp = 0;
  552. $res3 = true;
  553. $order_give_exp = sys_config('order_give_exp');
  554. $balance = $userInfo['exp'];
  555. if ($order['pay_price'] && $order_give_exp) {
  556. $order_exp = bcmul($order_give_exp, (string)$order['pay_price'], 2);
  557. $balance = bcadd((string)$balance, (string)$order_exp, 2);
  558. $res3 = false != $userBillServices->income('order_give_exp', $order['uid'], $order_exp, $balance, $order['id']);
  559. }
  560. $res = true;
  561. if ($order_exp > 0) {
  562. $userInfo->exp = $balance;
  563. $res1 = false != $userInfo->save();
  564. $res = $res1 && $res3;
  565. }
  566. /** @var UserLevelServices $levelServices */
  567. $levelServices = app()->make(UserLevelServices::class);
  568. $levelServices->detection((int)$order['uid']);
  569. return $res;
  570. }
  571. /**
  572. * 加入队列
  573. * @param array $where
  574. * @param int $count
  575. * @param int $maxLimit
  576. * @return bool
  577. */
  578. public function batchJoinJobs(array $where, int $count, int $maxLimit)
  579. {
  580. $page = ceil($count / $maxLimit);
  581. for ($i = 1; $i <= $page; $i++) {
  582. AutoTakeOrderJob::dispatch([$where, $i, $maxLimit]);
  583. }
  584. return true;
  585. }
  586. /**
  587. * 执行自动收货
  588. * @param array $where
  589. * @param int $page
  590. * @param int $maxLimit
  591. * @return bool
  592. */
  593. public function runAutoTakeOrder(array $where, int $page = 0, int $maxLimit = 0)
  594. {
  595. /** @var StoreOrderStoreOrderStatusServices $service */
  596. $service = app()->make(StoreOrderStoreOrderStatusServices::class);
  597. $orderList = $service->getOrderIds($where, $page, $maxLimit);
  598. /** @var StoreOrderRefundServices $storeOrderRefundServices */
  599. $storeOrderRefundServices = app()->make(StoreOrderRefundServices::class);
  600. foreach ($orderList as $order) {
  601. if ($order['status'] == 2) {
  602. continue;
  603. }
  604. if ($order['paid'] == 1 && $order['status'] == 1) {
  605. $data['status'] = 2;
  606. } else if ($order['pay_type'] == 'offline') {
  607. $data['status'] = 2;
  608. } else {
  609. continue;
  610. }
  611. if ($storeOrderRefundServices->count(['store_order_id' => $order['id'], 'refund_type' => [0, 1, 2, 4, 5], 'is_cancel' => 0, 'is_del' => 1])) {
  612. continue;
  613. }
  614. try {
  615. /** @var StoreOrderStatusServices $statusService */
  616. $statusService = app()->make(StoreOrderStatusServices::class);
  617. $res = $this->dao->update($order['id'], $data) && $statusService->save([
  618. 'oid' => $order['id'],
  619. 'change_type' => 'take_delivery',
  620. 'change_message' => '已收货[自动收货]',
  621. 'change_time' => time()
  622. ]);
  623. $res = $res && $this->storeProductOrderUserTakeDelivery($order);
  624. if (!$res) {
  625. throw new ValidateException('订单号' . $order['order_id'] . '自动收货失败');
  626. }
  627. } catch (\Throwable $e) {
  628. Log::error('自动收货失败,失败原因:' . $e->getMessage());
  629. }
  630. }
  631. return true;
  632. }
  633. /**
  634. * 自动收货
  635. * @return bool
  636. */
  637. public function autoTakeOrder()
  638. {
  639. //7天前时间戳
  640. $systemDeliveryTime = (int)sys_config('system_delivery_time', 0);
  641. //0为取消自动收货功能
  642. if ($systemDeliveryTime == 0) {
  643. return true;
  644. }
  645. $sevenDay = strtotime(date('Y-m-d H:i:s', strtotime('-' . $systemDeliveryTime . ' day')));
  646. /** @var StoreOrderStoreOrderStatusServices $service */
  647. $service = app()->make(StoreOrderStoreOrderStatusServices::class);
  648. $where = [
  649. 'change_time' => $sevenDay,
  650. 'is_del' => 0,
  651. 'paid' => 1,
  652. 'status' => 1,
  653. 'change_type' => ['delivery_goods', 'delivery_fictitious', 'delivery', 'city_delivery']
  654. ];
  655. $maxLimit = 20;
  656. $count = $service->getOrderCount($where);
  657. if ($count > $maxLimit) {
  658. return $this->batchJoinJobs($where, $count, $maxLimit);
  659. }
  660. return $this->runAutoTakeOrder($where);
  661. }
  662. }