123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693 |
- <?php
- // +----------------------------------------------------------------------
- // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
- // +----------------------------------------------------------------------
- // | Author: CRMEB Team <admin@crmeb.com>
- // +----------------------------------------------------------------------
- namespace app\services\order;
- use app\dao\order\StoreOrderDao;
- use app\jobs\integral\IntegralJob;
- use app\jobs\order\AutoTakeOrderJob;
- use app\jobs\notice\SmsAdminJob;
- use app\services\BaseServices;
- use app\services\message\service\StoreServiceServices;
- use app\services\message\sms\SmsSendServices;
- use app\services\user\member\MemberCardServices;
- use app\services\user\UserBillServices;
- use app\services\user\UserBrokerageServices;
- use app\services\user\level\UserLevelServices;
- use app\services\user\UserServices;
- use think\exception\ValidateException;
- use think\facade\Log;
- /**
- * 订单收货
- * Class StoreOrderTakeServices
- * @package app\services\order
- * @mixin StoreOrderDao
- */
- class StoreOrderTakeServices extends BaseServices
- {
- /**
- * 构造方法
- * StoreOrderTakeServices constructor.
- * @param StoreOrderDao $dao
- */
- public function __construct(StoreOrderDao $dao)
- {
- $this->dao = $dao;
- }
- /**
- * 小程序订单服务收货
- * @param $order_id
- * @return bool
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function miniOrderTakeOrder($order_id)
- {
- $orderArr = explode('_', $order_id);
- if (count($orderArr) == 2) {
- $order_id = $orderArr[1] ?? $order_id;
- }
- //查找订单信息
- $order = $this->dao->getOne(['order_id' => $order_id]);
- if (!$order) {
- return true;
- }
- if ($order['pid'] == -1) { // 有子订单
- // 查找待收货的子订单
- $son_order_list = $this->dao->getSubOrderNotSendList((int)$order['id']);
- foreach ($son_order_list as $son_order) {
- $this->takeOrder($son_order['order_id'], $son_order['uid']);
- }
- } else {
- $this->takeOrder($order_id, $order['uid']);
- }
- return true;
- }
- /**
- * 用户订单收货
- * @param $uni
- * @param $uid
- * @return bool
- */
- public function takeOrder(string $uni, int $uid)
- {
- $order = $this->dao->getUserOrderDetail($uni, $uid);
- if (!$order) {
- throw new ValidateException('订单不存在!');
- }
- /** @var StoreOrderServices $orderServices */
- $orderServices = app()->make(StoreOrderServices::class);
- $order = $orderServices->tidyOrder($order);
- if ($order['_status']['_type'] != 2) {
- throw new ValidateException('订单状态错误!');
- }
- //存在拆分发货 需要分开收货
- if ($this->dao->count(['pid' => $order['id']])) {
- throw new ValidateException('拆分发货,请去订单详情中包裹确认收货');
- }
- if ($order['type'] != 8) {
- $order->status = 2;
- } else {
- $order->status = 3;
- }
- /** @var StoreOrderStatusServices $statusService */
- $statusService = app()->make(StoreOrderStatusServices::class);
- $res = $order->save() && $statusService->save([
- 'oid' => $order['id'],
- 'change_type' => 'user_take_delivery',
- 'change_message' => '用户已收货',
- 'change_time' => time()
- ]);
- $res = $res && $this->storeProductOrderUserTakeDelivery($order);
- if (!$res) {
- throw new ValidateException('收货失败');
- }
- //核销订单 修改订单商品核销状态
- if ($order['shipping_type'] == 2 || (in_array($order['shipping_type'], [1, 3]) && $order['delivery_type'] == 'send')) {
- //修改原来订单商品信息
- $cartData['is_writeoff'] = 1;
- $cartData['write_surplus_times'] = 0;
- /** @var StoreOrderCartInfoServices $cartInfoServices */
- $cartInfoServices = app()->make(StoreOrderCartInfoServices::class);
- $cartInfoServices->update(['oid' => $order['id']], $cartData);
- }
- return $order;
- }
- /**
- * 订单确认收货
- * @param $order
- * @return bool
- */
- public function storeProductOrderUserTakeDelivery($order, bool $isTran = true)
- {
- $res = true;
- //获取购物车内的商品标题
- /** @var StoreOrderCartInfoServices $orderInfoServices */
- $orderInfoServices = app()->make(StoreOrderCartInfoServices::class);
- $storeName = $orderInfoServices->getCarIdByProductTitle((int)$order['id']);
- $storeTitle = substrUTf8($storeName, 20, 'UTF-8', '');
- if ($order['uid']) {
- /** @var UserServices $userServices */
- $userServices = app()->make(UserServices::class);
- $userInfo = $userServices->get((int)$order['uid']);
- $res = $this->transaction(function () use ($order, $userInfo, $storeTitle) {
- //赠送积分
- $res1 = $this->gainUserIntegral($order, $userInfo, $storeTitle);
- //返佣
- $res2 = $this->backOrderBrokerage($order, $userInfo);
- //返佣
- $res2 = $this->backAreaBrokerage($order, $userInfo);
- //经验
- $res3 = $this->gainUserExp($order, $userInfo);
- if (!($res1 && $res2 && $res3)) {
- throw new ValidateException('收货失败!');
- }
- return true;
- }, $isTran);
- }
- if ($res) {
- //订单收货事件
- event('order.take', [$order, $storeTitle]);
- return true;
- } else {
- return false;
- }
- }
- /**
- * 赠送积分
- * @param $order
- * @param $userInfo
- * @param $storeTitle
- * @return bool
- */
- public function gainUserIntegral($order, $userInfo, $storeTitle)
- {
- $res2 = true;
- if (!$userInfo) {
- return true;
- }
- // 营销产品送积分
- if (!isset($order['type']) || in_array($order['type'], [1, 2, 3, 5, 8])) {
- return true;
- }
- /** @var UserBillServices $userBillServices */
- $userBillServices = app()->make(UserBillServices::class);
- $balance = $userInfo['integral'];
- if ($order['gain_integral'] > 0) {
- $balance = bcadd((string)$balance, (string)$order['gain_integral']);
- $res2 = false != $userBillServices->income('pay_give_integral', $order['uid'], (int)$order['gain_integral'], (int)$balance, $order['id']);
- }
- $order_integral = 0;
- $res3 = true;
- $order_give_integral = sys_config('order_give_integral');
- if ($order['pay_price'] && $order_give_integral) {
- //会员消费返积分翻倍
- if ($userInfo['is_money_level'] > 0) {
- //看是否开启消费返积分翻倍奖励
- /** @var MemberCardServices $memberCardService */
- $memberCardService = app()->make(MemberCardServices::class);
- $integral_rule_number = $memberCardService->isOpenMemberCardCache('integral');
- if ($integral_rule_number) {
- $order_integral = bcmul((string)$order['pay_price'], (string)$integral_rule_number, 2);
- }
- }
- $order_integral = bcmul((string)$order_give_integral, (string)($order_integral ? $order_integral : $order['pay_price']), 0);
- $balance = bcadd((string)$balance, (string)$order_integral);
- $res3 = false != $userBillServices->income('order_give_integral', $order['uid'], (int)$order_integral, (int)$balance, $order['id']);
- }
- $give_integral = $order_integral + $order['gain_integral'];
- if ($give_integral > 0) {
- $integral = $userInfo['integral'] + $give_integral;
- $userInfo->integral = $integral;
- $res1 = false != $userInfo->save();
- $res = $res1 && $res2 && $res3;
- //发送消息
- event('notice.notice', [['order' => $order, 'storeTitle' => $storeTitle, 'give_integral' => $give_integral, 'integral' => $integral], 'integral_accout']);
- return $res;
- }
- return true;
- }
- /**
- * 地区返佣
- * @param $orderInfo
- * @param $userInfo
- * @return bool
- */
- public function backAreaBrokerage($orderInfo, $userInfo)
- {
- $storeOrderServices = app()->make(StoreOrderServices::class);
- // 当前订单|用户不存在 直接返回
- if (!$orderInfo || !$userInfo) {
- return $storeOrderServices->update($orderInfo['id'], ['send_area_award' => 1]);
- }
- if (!$orderInfo['province'] || $orderInfo['send_area_award'] == 1) {
- return $storeOrderServices->update($orderInfo['id'], ['send_area_award' => 1]);
- }
- //商城分销功能是否开启 0关闭1开启
- if (!sys_config('brokerage_func_status')) return $storeOrderServices->update($orderInfo['id'], ['send_area_award' => 1]);
- if (sys_config('area_award_time') > 0) return true;
- // 营销产品不返佣金
- if (!isset($orderInfo['type']) || in_array($orderInfo['type'], [1, 2, 3, 5, 8])) {
- return $storeOrderServices->update($orderInfo['id'], ['send_area_award' => 1]);
- }
- //冻结时间
- $broken_time = intval(sys_config('extract_time'));
- $frozen_time = time() + $broken_time * 86400;
- //订单中取出
- $order_price = $orderInfo['total_price'] ?? 0;
- $order_price = bcsub($order_price, $orderInfo['cost'], 2);
- // 一级返佣金额小于等于0
- if ($order_price <= 0) {//直接二级返佣
- return $storeOrderServices->update($orderInfo['id'], ['send_area_award' => 1]);
- }
- // 获取上级推广员信息
- /** @var UserServices $userServices */
- $userServices = app()->make(UserServices::class);
- $res = true;
- //找省代
- $admins = $userServices->search()->where('area_admin', 3)->where('area_admin_province', $orderInfo['order_province'])->select();
- if (count($admins) > 0) {
- $award = bcdiv(bcmul((string)sys_config('province_award'), (string)$order_price, 2), 100, 2);
- if ($award > 0) {
- $brokeragePrice = bcdiv($award, (string)count($admins), 2);
- if ($brokeragePrice > 0) {
- foreach ($admins as $v) {
- $spreadPrice = (string)$v['brokerage_price'];
- // 上级推广员返佣之后的金额
- $balance = bcadd($spreadPrice, $brokeragePrice, 2);
- // 添加佣金记录
- /** @var UserBrokerageServices $userBrokerageServices */
- $userBrokerageServices = app()->make(UserBrokerageServices::class);
- $type = 'get_area_brokerage';
- $res1 = $userBrokerageServices->income($type, $v['uid'], [
- 'nickname' => $userInfo['nickname'],
- 'pay_price' => floatval($orderInfo['pay_price']),
- 'area' => $orderInfo['order_province'] . '省级',
- 'number' => floatval($brokeragePrice),
- 'frozen_time' => $frozen_time
- ], $balance, $orderInfo['id']);
- // 添加用户佣金
- $res2 = $userServices->bcInc($v['uid'], 'brokerage_price', $brokeragePrice, 'uid');
- $res = $res1 && $res2 && $res;
- }
- }
- }
- }
- //找市代
- $admins = $userServices->search()->where('area_admin', 2)->where('area_admin_province', $orderInfo['order_province'])
- ->where('area_admin_city', $orderInfo['order_city'])->select();
- if (count($admins) > 0) {
- $award = bcdiv(bcmul((string)sys_config('city_award'), (string)$order_price, 2), 100, 2);
- if ($award > 0) {
- $brokeragePrice = bcdiv($award, (string)count($admins), 2);
- if ($brokeragePrice > 0) {
- foreach ($admins as $v) {
- $spreadPrice = (string)$v['brokerage_price'];
- // 上级推广员返佣之后的金额
- $balance = bcadd($spreadPrice, $brokeragePrice, 2);
- // 添加佣金记录
- /** @var UserBrokerageServices $userBrokerageServices */
- $userBrokerageServices = app()->make(UserBrokerageServices::class);
- $type = 'get_area_brokerage';
- $res1 = $userBrokerageServices->income($type, $v['uid'], [
- 'nickname' => $userInfo['nickname'],
- 'pay_price' => floatval($orderInfo['pay_price']),
- 'area' => $orderInfo['order_province'] . $orderInfo['order_city'] . '级',
- 'number' => floatval($brokeragePrice),
- 'frozen_time' => $frozen_time
- ], $balance, $orderInfo['id']);
- // 添加用户佣金
- $res2 = $userServices->bcInc($v['uid'], 'brokerage_price', $brokeragePrice, 'uid');
- $res = $res1 && $res2 && $res;
- }
- }
- }
- }
- //找区代
- $admins = $userServices->search()->where('area_admin', 1)
- ->where('area_admin_province', $orderInfo['order_province'])
- ->where('area_admin_city', $orderInfo['order_city'])
- ->where('area_admin_district', $orderInfo['order_district'])
- ->select();
- if (count($admins) > 0) {
- $award = bcdiv(bcmul((string)sys_config('district_award'), (string)$order_price, 2), 100, 2);
- if ($award > 0) {
- $brokeragePrice = bcdiv($award, (string)count($admins), 2);
- if ($brokeragePrice > 0) {
- foreach ($admins as $v) {
- $spreadPrice = (string)$v['brokerage_price'];
- // 上级推广员返佣之后的金额
- $balance = bcadd($spreadPrice, $brokeragePrice, 2);
- // 添加佣金记录
- /** @var UserBrokerageServices $userBrokerageServices */
- $userBrokerageServices = app()->make(UserBrokerageServices::class);
- $type = 'get_area_brokerage';
- $res1 = $userBrokerageServices->income($type, $v['uid'], [
- 'nickname' => $userInfo['nickname'],
- 'pay_price' => floatval($orderInfo['pay_price']),
- 'area' => $orderInfo['order_province'] . $orderInfo['order_city'] . $orderInfo['order_district'] . '级',
- 'number' => floatval($brokeragePrice),
- 'frozen_time' => $frozen_time
- ], $balance, $orderInfo['id']);
- // 添加用户佣金
- $res2 = $userServices->bcInc($v['uid'], 'brokerage_price', $brokeragePrice, 'uid');
- $res = $res1 && $res2 && $res;
- }
- }
- }
- }
- return $res && $storeOrderServices->update($orderInfo['id'], ['send_area_award' => 1]);
- }
- /**
- * 一级返佣
- * @param $orderInfo
- * @param $userInfo
- * @return bool
- */
- public function backOrderBrokerage($orderInfo, $userInfo)
- {
- // 当前订单|用户不存在 直接返回
- if (!$orderInfo || !$userInfo) {
- return true;
- }
- //商城分销功能是否开启 0关闭1开启
- if (!sys_config('brokerage_func_status')) return true;
- // 营销产品不返佣金
- if (!isset($orderInfo['type']) || in_array($orderInfo['type'], [1, 2, 3, 5, 8]) || $orderInfo['award_price'] > 0) {
- return true;
- }
- //绑定失效
- if (isset($orderInfo['spread_uid']) && $orderInfo['spread_uid'] == -1) {
- return true;
- }
- //是否开启自购返佣
- $isSelfBrokerage = sys_config('is_self_brokerage', 0);
- if (!isset($orderInfo['spread_uid']) || !$orderInfo['spread_uid']) {//兼容之前订单表没有spread_uid情况
- //没开启自购返佣 没有上级 或者 当用用户上级时自己 直接返回
- if (!$isSelfBrokerage && (!$userInfo['spread_uid'] || $userInfo['spread_uid'] == $orderInfo['uid'])) {
- return true;
- }
- $one_spread_uid = $isSelfBrokerage ? $userInfo['uid'] : $userInfo['spread_uid'];
- } else {
- $one_spread_uid = $orderInfo['spread_uid'];
- }
- $one_spread_uid = (int)$one_spread_uid;
- //冻结时间
- $broken_time = intval(sys_config('extract_time'));
- $frozen_time = time() + $broken_time * 86400;
- //订单中取出
- $brokeragePrice = $orderInfo['one_brokerage'] ?? 0;
- // 一级返佣金额小于等于0
- if ($brokeragePrice <= 0) {//直接二级返佣
- return $this->backOrderBrokerageTwo($orderInfo, $userInfo, $isSelfBrokerage, $frozen_time);
- }
- // 获取上级推广员信息
- /** @var UserServices $userServices */
- $userServices = app()->make(UserServices::class);
- $spreadPrice = $userServices->value(['uid' => $one_spread_uid], 'brokerage_price');
- // 上级推广员返佣之后的金额
- $balance = bcadd($spreadPrice, $brokeragePrice, 2);
- // 添加佣金记录
- /** @var UserBrokerageServices $userBrokerageServices */
- $userBrokerageServices = app()->make(UserBrokerageServices::class);
- //自购返佣 || 上级
- $type = $one_spread_uid == $orderInfo['uid'] ? 'get_self_brokerage' : 'get_brokerage';
- $res1 = $userBrokerageServices->income($type, $one_spread_uid, [
- 'nickname' => $userInfo['nickname'],
- 'pay_price' => floatval($orderInfo['pay_price']),
- 'number' => floatval($brokeragePrice),
- 'frozen_time' => $frozen_time
- ], $balance, $orderInfo['id']);
- // 添加用户佣金
- $res2 = $userServices->bcInc($one_spread_uid, 'brokerage_price', $brokeragePrice, 'uid');
- //给上级发送获得佣金的模板消息
- $this->sendBackOrderBrokerage($orderInfo, $one_spread_uid, $brokeragePrice);
- // 一级返佣成功 跳转二级返佣
- $res = $res1 && $res2 && $this->backOrderBrokerageTwo($orderInfo, $userInfo, $isSelfBrokerage, $frozen_time);
- return $res;
- }
- /**
- * 二级推广返佣
- * @param $orderInfo
- * @param $userInfo
- * @param int $isSelfbrokerage
- * @param int $frozenTime
- * @return bool
- */
- public function backOrderBrokerageTwo($orderInfo, $userInfo, $isSelfbrokerage = 0, $frozenTime = 0)
- {
- //绑定失效
- if (isset($orderInfo['spread_two_uid']) && $orderInfo['spread_two_uid'] == -1) {
- return true;
- }
- /** @var UserServices $userServices */
- $userServices = app()->make(UserServices::class);
- if (isset($orderInfo['spread_two_uid']) && $orderInfo['spread_two_uid']) {
- $spread_two_uid = $orderInfo['spread_two_uid'];
- } else {
- // 获取上推广人
- $userInfoTwo = $userServices->get((int)$userInfo['spread_uid']);
- // 订单|上级推广人不存在 直接返回
- if (!$orderInfo || !$userInfoTwo) {
- return true;
- }
- //没开启自购返佣 或者 上推广人没有上级 或者 当用用户上上级时自己 直接返回
- if (!$isSelfbrokerage && (!$userInfoTwo['spread_uid'] || $userInfoTwo['spread_uid'] == $orderInfo['uid'])) {
- return true;
- }
- $spread_two_uid = $isSelfbrokerage ? $userInfoTwo['uid'] : $userInfoTwo['spread_uid'];
- }
- $spread_two_uid = (int)$spread_two_uid;
- //订单中取出
- $brokeragePrice = $orderInfo['two_brokerage'] ?? 0;
- // 返佣金额小于等于0 直接返回不返佣金
- if ($brokeragePrice <= 0) {
- return true;
- }
- // 获取上上级推广员信息
- $spreadPrice = $userServices->value(['uid' => $spread_two_uid], 'brokerage_price');
- // 获取上上级推广员返佣之后余额
- $balance = bcadd($spreadPrice, $brokeragePrice, 2);
- // 添加佣金记录
- /** @var UserBrokerageServices $userBrokerageServices */
- $userBrokerageServices = app()->make(UserBrokerageServices::class);
- $res1 = $userBrokerageServices->income('get_two_brokerage', $spread_two_uid, [
- 'nickname' => $userInfo['nickname'],
- 'pay_price' => floatval($orderInfo['pay_price']),
- 'number' => floatval($brokeragePrice),
- 'frozen_time' => $frozenTime
- ], $balance, $orderInfo['id']);
- // 添加用户佣金
- $res2 = $userServices->bcInc($spread_two_uid, 'brokerage_price', $brokeragePrice, 'uid');
- //给上级发送获得佣金的模板消息
- $this->sendBackOrderBrokerage($orderInfo, $spread_two_uid, $brokeragePrice);
- return $res1 && $res2;
- }
- /**
- * 佣金到账发送模板消息
- * @param $orderInfo
- * @param $spread_uid
- * @param $brokeragePrice
- */
- public function sendBackOrderBrokerage($orderInfo, $spread_uid, $brokeragePrice, string $type = 'order')
- {
- /** @var UserServices $userServices */
- $userServices = app()->make(UserServices::class);
- $user = $userServices->getUserInfo($spread_uid, 'phone,user_type');
- if ($type == 'order') {
- /** @var StoreOrderCartInfoServices $storeOrderCartInfoService */
- $storeOrderCartInfoService = app()->make(StoreOrderCartInfoServices::class);
- $cartInfo = $storeOrderCartInfoService->getOrderCartInfo($orderInfo['id']);
- if ($cartInfo) {
- $cartInfo = array_column($cartInfo, 'cart_info');
- $goodsPrice = 0;
- $goodsName = "";
- foreach ($cartInfo as $k => $v) {
- $goodsName .= $v['productInfo']['store_name'];
- $price = $v['productInfo']['attrInfo']['price'] ?? $v['productInfo']['price'] ?? 0;
- $goodsPrice = bcadd((string)$goodsPrice, (string)$price, 2);
- }
- }
- } else {
- $goodsName = '推广用户获取佣金';
- $goodsPrice = $brokeragePrice;
- }
- //提醒推送
- 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']);
- return true;
- }
- /**
- * 发送短信
- * @param $order
- * @param $storeTitle
- */
- public function smsSend($order, $storeTitle)
- {
- /** @var SmsSendServices $smsServices */
- $smsServices = app()->make(SmsSendServices::class);
- $switch = (bool)sys_config('confirm_take_over_switch');
- //模板变量
- $store_name = $storeTitle;
- $order_id = $order['order_id'];
- $smsServices->send($switch, $order['user_phone'], compact('store_name', 'order_id'), 'TAKE_DELIVERY_CODE');
- }
- /**
- * 发送确认收货管理员短信
- * @param $order
- */
- public function smsSendTake($order)
- {
- $switch = (bool)sys_config('admin_confirm_take_over_switch');
- /** @var StoreServiceServices $services */
- $services = app()->make(StoreServiceServices::class);
- $adminList = $services->getStoreServiceOrderNotice();
- SmsAdminJob::dispatchDo('sendAdminConfirmTakeOver', [$switch, $adminList, $order]);
- return true;
- }
- /**
- * 赠送经验
- * @param $order
- * @param $userInfo
- * @return bool
- */
- public function gainUserExp($order, $userInfo)
- {
- if (!$userInfo) {
- return true;
- }
- //用户等级是否开启
- if (!sys_config('member_func_status', 1)) {
- return true;
- }
- /** @var UserBillServices $userBillServices */
- $userBillServices = app()->make(UserBillServices::class);
- $order_exp = 0;
- $res3 = true;
- $order_give_exp = sys_config('order_give_exp');
- $balance = $userInfo['exp'];
- if ($order['pay_price'] && $order_give_exp) {
- $order_exp = bcmul($order_give_exp, (string)$order['pay_price'], 2);
- $balance = bcadd((string)$balance, (string)$order_exp, 2);
- $res3 = false != $userBillServices->income('order_give_exp', $order['uid'], $order_exp, $balance, $order['id']);
- }
- $res = true;
- if ($order_exp > 0) {
- $userInfo->exp = $balance;
- $res1 = false != $userInfo->save();
- $res = $res1 && $res3;
- }
- /** @var UserLevelServices $levelServices */
- $levelServices = app()->make(UserLevelServices::class);
- $levelServices->detection((int)$order['uid']);
- return $res;
- }
- /**
- * 加入队列
- * @param array $where
- * @param int $count
- * @param int $maxLimit
- * @return bool
- */
- public function batchJoinJobs(array $where, int $count, int $maxLimit)
- {
- $page = ceil($count / $maxLimit);
- for ($i = 1; $i <= $page; $i++) {
- AutoTakeOrderJob::dispatch([$where, $i, $maxLimit]);
- }
- return true;
- }
- /**
- * 执行自动收货
- * @param array $where
- * @param int $page
- * @param int $maxLimit
- * @return bool
- */
- public function runAutoTakeOrder(array $where, int $page = 0, int $maxLimit = 0)
- {
- /** @var StoreOrderStoreOrderStatusServices $service */
- $service = app()->make(StoreOrderStoreOrderStatusServices::class);
- $orderList = $service->getOrderIds($where, $page, $maxLimit);
- /** @var StoreOrderRefundServices $storeOrderRefundServices */
- $storeOrderRefundServices = app()->make(StoreOrderRefundServices::class);
- foreach ($orderList as $order) {
- if ($order['status'] == 2) {
- continue;
- }
- if ($order['paid'] == 1 && $order['status'] == 1) {
- $data['status'] = 2;
- } else if ($order['pay_type'] == 'offline') {
- $data['status'] = 2;
- } else {
- continue;
- }
- if ($storeOrderRefundServices->count(['store_order_id' => $order['id'], 'refund_type' => [0, 1, 2, 4, 5], 'is_cancel' => 0, 'is_del' => 1])) {
- continue;
- }
- try {
- /** @var StoreOrderStatusServices $statusService */
- $statusService = app()->make(StoreOrderStatusServices::class);
- $res = $this->dao->update($order['id'], $data) && $statusService->save([
- 'oid' => $order['id'],
- 'change_type' => 'take_delivery',
- 'change_message' => '已收货[自动收货]',
- 'change_time' => time()
- ]);
- $res = $res && $this->storeProductOrderUserTakeDelivery($order);
- if (!$res) {
- throw new ValidateException('订单号' . $order['order_id'] . '自动收货失败');
- }
- } catch (\Throwable $e) {
- Log::error('自动收货失败,失败原因:' . $e->getMessage());
- }
- }
- return true;
- }
- /**
- * 自动收货
- * @return bool
- */
- public function autoTakeOrder()
- {
- //7天前时间戳
- $systemDeliveryTime = (int)sys_config('system_delivery_time', 0);
- //0为取消自动收货功能
- if ($systemDeliveryTime == 0) {
- return true;
- }
- $sevenDay = strtotime(date('Y-m-d H:i:s', strtotime('-' . $systemDeliveryTime . ' day')));
- /** @var StoreOrderStoreOrderStatusServices $service */
- $service = app()->make(StoreOrderStoreOrderStatusServices::class);
- $where = [
- 'change_time' => $sevenDay,
- 'is_del' => 0,
- 'paid' => 1,
- 'status' => 1,
- 'change_type' => ['delivery_goods', 'delivery_fictitious', 'delivery', 'city_delivery']
- ];
- $maxLimit = 20;
- $count = $service->getOrderCount($where);
- if ($count > $maxLimit) {
- return $this->batchJoinJobs($where, $count, $maxLimit);
- }
- return $this->runAutoTakeOrder($where);
- }
- }
|