123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974 |
- <?php
- namespace App\Http\Controllers\Admin;
- use App\Filters\GroupSendSmsFilter;
- use App\Filters\RefundBalanceOrderFilter;
- use App\Filters\StudentFilter;
- use App\Filters\UserFilter;
- use App\Http\Requests\GroupSendSmsRequest;
- use App\Http\Requests\StudentRequest;
- use App\Http\Requests\UserRequest;
- use App\Http\Resources\GroupSendSmsResource;
- use App\Http\Resources\RefundBalanceOrderResource;
- use App\Http\Resources\StudentResource;
- use App\Http\Resources\UserResource;
- use App\Http\Resources\WalletResource;
- use App\Jobs\SendSMSJob;
- use App\Models\AdminMerchant;
- use App\Models\AdminUser;
- use App\Models\AdminUserArea;
- use App\Models\Area;
- use App\Models\Auth;
- use App\Models\Config;
- use App\Models\DepositOrder;
- use App\Models\DepositRefund;
- use App\Models\GroupSendSms;
- use App\Models\Order;
- use App\Models\OrderRent;
- use App\Models\RechargeOrder;
- use App\Models\RefundBalanceOrder;
- use App\Models\RefundLog;
- use App\Models\Student;
- use App\Models\User;
- use App\Models\WalletLog;
- use App\Utils\Admin;
- use App\Utils\RedisKeys;
- use Carbon\Carbon;
- use Illuminate\Database\Eloquent\Builder;
- use Illuminate\Http\Request;
- use App\Http\Controllers\Controller;
- use Illuminate\Support\Facades\Cache;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- /**
- * Class UserController
- * @package App\Http\Controllers\Admin
- */
- class UserController extends Controller
- {
- /**
- * index 用户列表
- *
- * @param UserFilter $filter
- * @return \Illuminate\Http\JsonResponse
- * @author Fx
- *
- */
- public function index(UserFilter $filter)
- {
- $admin_id = Admin::user()->id;
- $users = User::query()
- ->filter($filter)
- ->where(AdminMerchant::getMerchantWhere())
- ->where('is_register', User::REGISTER_OK)
- ->orderByDesc('id');
- if (!Admin::isAdministrator() && !Admin::isNormalAdministrator()) {
- $area_ids = AdminUser::getAreaIdsByAdminId($admin_id);
- if (count($area_ids) !== 0) {
- $users = $users->where(function ($q) use ($area_ids) {
- $q->whereIn('register_area_id', $area_ids)->orWhere('register_area_id', 0);
- });
- } else {
- $area_id = AdminUserArea::query()->where('admin_id', $admin_id)->first('area_id');
- $area_id = $area_id->area_id ?? 0;
- $users = $users->where(function ($q) use ($area_id) {
- $q->where('register_area_id', $area_id)->orWhere('register_area_id', 0);
- });
- }
- }
- $users = $users->paginate();
- return $this->ok(UserResource::collection($users));
- }
- /**
- * update 更新用户信息
- *
- * @param UserRequest $request
- * @param User $user
- * @return \Illuminate\Http\JsonResponse
- * @author Fx
- *
- */
- public function update(UserRequest $request, User $user)
- {
- $inputs = $request->validated();
- $user->update($inputs);
- return $this->created(UserResource::make($user));
- }
- /**
- * userNumber总用户数 总有效用户数
- *
- * @return \Illuminate\Http\JsonResponse
- * @author Fx
- *
- */
- public function userNumber()
- {
- $admin_id = Admin::user()->id;
- $data['user_total'] = User::query()->where(AdminMerchant::getMerchantWhere());
- $data['user_true_total'] = User::query()->where('is_card_certified', User::CARD_OK)->where(AdminMerchant::getMerchantWhere());
- if (!Admin::isAdministrator()) {
- $area_ids = AdminUser::getAreaIdsByAdminId($admin_id);
- if (count($area_ids) !== 0) {
- $data['user_total'] = $data['user_total']->whereIn('register_area_id', $area_ids);
- $data['user_true_total'] = $data['user_true_total']->whereIn('register_area_id', $area_ids);
- } else {
- $area_id = AdminUserArea::query()->where('admin_id', $admin_id)->first('area_id');
- $area_id = $area_id->area_id ?? 0;
- $data['user_total'] = $data['user_total']->where('register_area_id', $area_id);
- $data['user_true_total'] = $data['user_true_total']->where('register_area_id', $area_id);
- }
- }
- $data['user_total'] = $data['user_total']->count('id');
- $data['user_true_total'] = $data['user_true_total']->count('id');
- return $this->ok($data);
- }
- /**
- * moneySum 总充值数 总余额数
- * @return \Illuminate\Http\JsonResponse
- * @author Fx
- *
- */
- public function moneySum()
- {
- $admin_id = Admin::user()->id;
- $data['recharge_total'] = User::query()->where(AdminMerchant::getMerchantWhere());
- $data['wallet_money_total'] = User::query()->where(AdminMerchant::getMerchantWhere());;
- if (!Admin::isAdministrator()) {
- $area_ids = AdminUser::getAreaIdsByAdminId($admin_id);
- if (count($area_ids) !== 0) {
- $data['recharge_total'] = $data['recharge_total']->whereIn('register_area_id', $area_ids);
- $data['wallet_money_total'] = $data['wallet_money_total']->whereIn('register_area_id', $area_ids);
- } else {
- $area_id = AdminUserArea::query()->where('admin_id', $admin_id)->first('area_id');
- $area_id = $area_id->area_id ?? 0;
- $data['recharge_total'] = $data['recharge_total']->where('register_area_id', $area_id);
- $data['wallet_money_total'] = $data['wallet_money_total']->where('register_area_id', $area_id);
- }
- }
- $data['recharge_total'] = $data['recharge_total']->sum('recharge');
- $data['wallet_money_total'] = $data['wallet_money_total']->sum('wallet_money');
- return $this->ok($data);
- }
- /**
- * wallet 钱包列表
- *
- * @param Request $request
- * @return \Illuminate\Http\JsonResponse *@author Fx
- *
- */
- public function wallet(Request $request)
- {
- $user_id = $request->get('user_id') ?? '';
- if (empty($user_id)) return $this->error('参数错误');
- if (AdminMerchant::isUserToMch($user_id)) return $this->error('参数错误请刷新后重试!');
- $wallet = WalletLog::query()
- ->where('user_id', $user_id)
- ->orderByDesc('id')
- ->paginate();
- return $this->ok(WalletResource::collection($wallet));
- }
- /**
- * changeStatus 改变用户status
- *
- * @param Request $request
- * @return \Illuminate\Http\JsonResponse *@author Fx
- *
- */
- public function changeStatus(Request $request)
- {
- $user_id = $request->get('user_id') ?? '';
- $status = $request->get('status') ?? 0;
- $is_card_certified = $request->get('is_card_certified') ?? 0;
- $is_bind_mobile = $request->get('is_bind_mobile') ?? 0;
- $is_match_ride_age = $request->get('is_match_ride_age') ?? 0;
- $truename = $request->get('truename') ?? '';
- $card_id = $request->get('card_id') ?? '';
- $mobile = $request->get('mobile') ?? '';
- // $reason = $request->get('reason') ?? ''; //封号原因
- if (empty($user_id)) return $this->error('参数错误');
- try {
- $bool = User::query()->where('id', $user_id)->update([
- 'truename' => $truename,
- 'card_id' => $card_id,
- 'mobile' => $mobile,
- 'status' => $status,
- 'is_bind_mobile' => $is_bind_mobile,
- 'is_card_certified' => $is_card_certified,
- 'is_match_ride_age' => $is_match_ride_age,
- ]);
- } catch (\Exception $e) {
- Log::info($e->getMessage());
- return $this->error("修改失败,请联系管理员");
- }
- if ($bool) {
- return $this->created();
- } else {
- return $this->error("修改失败,请联系管理员");
- }
- }
- /**
- * returnDeposit 退还押金
- *
- * @param Request $request
- * @return \Illuminate\Http\JsonResponse
- * @author Fx
- *
- */
- public function returnDeposit(Request $request)
- {
- $user_id = $request->get('user_id') ?? '';
- if (empty($user_id)) return $this->error('参数错误');
- $user = User::find($user_id);
- if (empty($user)) return $this->error('找不到该用户信息');
- $order = Order::query()->where('user_id', $user_id)
- ->whereIn('status', [Order::STATUS_RIDE_BIKE, Order::STATUS_PAUSE_BIKE, Order::STATUS_CLOSE_BIKE])
- ->first();
- if (!empty($order)) return $this->error('该用户还有未完成得订单');
- $orderRent = OrderRent::query()->where('user_id', $user_id)
- ->whereIn('status', [OrderRent::STATUS_RENT_BIKE, OrderRent::STATUS_CLOSE_RENT_BIKE])
- ->first();
- if (!empty($orderRent)) return $this->error('该用户还有未完成得日租订单');
- // 此验证暂时不验证 防止交两次押金无法退还
- // if($user->is_deposit == User::DEPOSIT_NO) return $this->error('此用户还没缴纳押金');
- $depositOrder = DepositOrder::where('user_id', $user_id)->where('pay_status', DepositOrder::PAY_STATUS_OK)->where('is_refund', DepositOrder::REFUND_NO)->orderBy('id', 'desc')->first();
- if (empty($depositOrder)) return $this->error('此用户还没缴纳押金');
- if (DepositRefund::where('deposit_id', $depositOrder->id)->where('pay_status', DepositRefund::PAY_STATUS_OK)->exists()) {
- $depositOrder->is_refund = DepositOrder::REFUND_OK;
- $depositOrder->save();
- return $this->ok('此押金已退还');
- }
- if ($refund = DepositRefund::where('deposit_id', $depositOrder->id)->where('pay_status', DepositRefund::PAY_STATUS_NO)->first()) {
- $url = config('systemConfig.api_url') . '/api/payments/wechat-refund-api' . '?no=' . $depositOrder->no . '&is-order-no=1';
- $data = [
- 'no' => $depositOrder->no,
- 'is-order-no' => 1
- ];
- $res = curl_request($url, 'get', $data);
- $res = json_decode($res, true);
- if ($res['status']) {
- return $this->ok('退款已成功');
- }
- //需要重新发起退款
- if ($res['code'] === 'REFUNDNOTEXIST') {
- $payment = app('wechat.payment'); // 微信支付
- $result = $payment->refund->byOutTradeNumber($depositOrder->no, $refund->no, wechat_fee($depositOrder->pay_money), wechat_fee($refund->pay_money), [
- // 可在此处传入其他参数,详细参数见微信支付文档
- 'refund_desc' => '退押金',
- 'notify_url' => config('wechat.payment.refund_deposit_notify_url')
- ]);
- if ($result['return_code'] === 'SUCCESS') {
- DB::commit();
- return $this->ok('退还发起成功,请耐心等待');
- } else {
- Log::error(php2js($result));
- DB::rollBack();
- return $this->error('退还失败,请联系管理员' . php2js($result));
- }
- }
- return $this->error($res['msg']);
- }
- // if ($user->deposit_type == User::DEPOSIT_CARD) return $this->error('此用户是押金卡,还没缴纳押金');
- try {
- DB::beginTransaction();
- // 1.添加记录
- $refund = DepositRefund::firstOrCreate([
- 'deposit_id' => $depositOrder->id,
- 'user_id' => $user_id,
- ], [
- 'no' => DepositRefund::makeNo(),
- 'money' => $depositOrder->pay_money,
- 'type' => DepositRefund::TYPE_USER,
- 'is_check_status' => DepositRefund::CHECK_STATUS_OK,
- 'area_id' => $depositOrder->area_id,
- 'pay_type' => $depositOrder->pay_type,
- 'pay_money' => $depositOrder->pay_money,
- 'pay_status' => DepositRefund::PAY_STATUS_NO
- ]);
- //2.修改用户押金
- $deposit_expire_time = Carbon::parse($user->deposit_expire_time);
- if (Carbon::now()->gte($deposit_expire_time)) {
- // 免押金卡已经过期
- User::where('id', $user_id)->update([
- 'deposit_money' => 0,
- 'is_deposit' => User::DEPOSIT_NO,
- 'deposit_type' => User::DEPOSIT_TYPE_NO
- ]);
- } else {
- // 免押金卡未过期
- User::where('id', $user_id)->update([
- 'deposit_money' => 0,
- 'is_deposit' => User::DEPOSIT_OK,
- 'deposit_type' => User::DEPOSIT_CARD
- ]);
- }
- // User::where('id', $user_id)->update([
- // 'deposit_money' => 0,
- // 'is_deposit' => User::DEPOSIT_NO
- // ]);
- //3.微信退款
- $payment = app('wechat.payment'); // 微信支付
- $result = $payment->refund->byOutTradeNumber($depositOrder->no, $refund->no, wechat_fee($depositOrder->pay_money), wechat_fee($refund->pay_money), [
- // 可在此处传入其他参数,详细参数见微信支付文档
- 'refund_desc' => '退押金',
- 'notify_url' => config('wechat.payment.refund_deposit_notify_url')
- ]);
- $refund->save();
- if ($result['return_code'] === 'SUCCESS') {
- DB::commit();
- return $this->ok('退还发起成功,请耐心等待');
- } else {
- Log::error(php2js($result));
- DB::rollBack();
- return $this->error('退还失败,请联系管理员' . php2js($result));
- }
- } catch (\Exception $e) {
- Log::error($e->getMessage());
- DB::rollBack();
- return $this->error('退还失败,请联系管理员');
- }
- }
- /**
- * 重新发起退还押金
- *
- * @param Request $request
- * @return \Illuminate\Http\JsonResponse
- * @author Fx
- *
- */
- public function reReturnDeposit(Request $request)
- {
- $refund_no = $request->get('refund_no') ?? '';
- if (empty($refund_no)) return $this->error('参数错误');
- $refundLog = RefundLog::query()->where('no', $refund_no)->first();
- if (empty($refundLog)) return $this->error('找不到退款记录');
- $depositOrder = DepositOrder::query()->find($refundLog->deposit_id);
- if (empty($depositOrder)) return $this->error('找不到押金卡记录');
- $payment = app('wechat.payment'); // 微信支付
- $result = $payment->refund->byOutTradeNumber($depositOrder->no, $refundLog->no, wechat_fee($depositOrder->pay_money), wechat_fee($refundLog->pay_money), [
- // 可在此处传入其他参数,详细参数见微信支付文档
- 'refund_desc' => '退押金',
- 'notify_url' => config('wechat.payment.refund_deposit_notify_url')
- ]);
- if ($result['return_code'] === 'SUCCESS') {
- return $this->ok('退还发起成功,请耐心等待');
- } else {
- Log::error(php2js($result));
- return $this->error('退还失败,请联系管理员' . php2js($result));
- }
- }
- /**
- * groupSendSms 群发短信表单
- *
- * @param GroupSendSmsRequest $groupSendSmsRequest
- * @return \Illuminate\Http\JsonResponse
- * @author Fx
- *
- */
- public function groupSendSms(GroupSendSmsRequest $groupSendSmsRequest)
- {
- $template_id = $groupSendSmsRequest->get('template_id');
- $area_id = $groupSendSmsRequest->get('area_id');
- $is_deposit = $groupSendSmsRequest->get('is_deposit');
- $params = $groupSendSmsRequest->get('domains');
- $is_card_certified = $groupSendSmsRequest->get('is_card_certified');
- $ids = $groupSendSmsRequest->get('ids');
- $is_search = $groupSendSmsRequest->get('is_search');
- $users = User::query()
- ->where('is_deposit', $is_deposit)
- ->where('register_area_id', $area_id)
- ->where('is_card_certified', $is_card_certified)
- ->where('is_bind_mobile', User::BIND_MOBILE_OK);
- // 判断是否使用高级勾选
- if ($is_search == '1') {
- if (count($ids) == 0) {
- return $this->error('没有勾选人员');
- }
- $users = $users->whereIn('id', $ids);
- }
- $users = $users->get();
- // Log::info($params);
- try {
- foreach ($users as $v) {
- $data = [];
- foreach ($params as $k => $param) {
- switch ($param['value']) {
- case 'now':
- $value = Carbon::now()->format('Y-m-d H:i:s');
- break;
- case 'truename':
- $value = $v->truename;
- break;
- default:
- $value = $param['value'];
- }
- $data[$k] = $value;
- }
- SendSMSJob::dispatch($v->mobile, $template_id, $data);
- }
- GroupSendSms::create([
- 'area_id' => $area_id,
- 'template_id' => $template_id,
- 'is_deposit' => $is_deposit,
- 'is_card_certified' => $is_card_certified,
- 'is_search' => $is_search,
- 'ids' => empty($ids) ? "" : json_encode($ids),
- 'params' => empty($params) ? "" : json_encode($params),
- ]);
- return $this->ok('操作成功');
- } catch (\Exception $e) {
- Log::error($e->getMessage());
- return $this->error('出现错误,请联系管理员');
- }
- }
- /**
- * groupSendSmsList 群发短信列表
- *
- * @param GroupSendSmsFilter $filter
- * @return \Illuminate\Http\JsonResponse
- * @author Fx
- *
- */
- public function groupSendSmsList(GroupSendSmsFilter $filter)
- {
- $list = GroupSendSms::query()
- ->filter($filter)
- ->orderByDesc('id')
- ->where(AdminMerchant::getMerchantWhere())
- ->paginate();
- return $this->ok(GroupSendSmsResource::collection($list));
- }
- /**
- * groupSmsTemplate 短信模板
- *
- * @return \Illuminate\Http\JsonResponse
- * @author Fx
- *
- */
- public function groupSmsTemplate()
- {
- $smsConfigSlug = config('systemConfig.sort.sms_template.slug');
- $template = Config::query()->whereHas('category', function ($q) use ($smsConfigSlug) {
- $q->where('slug', $smsConfigSlug);
- })->get()->toArray();
- return $this->ok($template);
- }
- /**
- * getUserByMobile 根据手机获得用户
- *
- * @param Request $request
- * @return \Illuminate\Http\JsonResponse
- * @author Fx
- *
- */
- public function getUserByMobile(Request $request)
- {
- $mobile = $request->get('mobile') ?? '';
- if (empty($mobile)) return $this->error('参数错误');
- $user = User::where('mobile', $mobile)->first();
- if (empty($user)) return $this->error('找不到该用户');
- return $this->ok($user->toArray());
- }
- /**
- * rechargeBalance 充值余额
- *
- * @param Request $request
- * @return \Illuminate\Http\JsonResponse
- * @author Fx
- *
- */
- public function rechargeBalance(Request $request)
- {
- $mobile = $request->get('mobile') ?? '';
- $money = $request->get('money') ?? '';
- $remark = $request->get('remark') ?? '';
- if (empty($mobile) || empty($money) || empty($remark)) return $this->error('参数错误');
- if ($money == 0) return $this->error('金额不能为0');
- $users = User::query()
- ->where('mobile', $mobile)
- ->first();
- if (empty($users)) return $this->error('找不到该用户,请检查手机号是否正确');
- try {
- DB::beginTransaction();
- // 1.生成充值订单
- $data = [
- 'area_id' => $users->register_area_id,
- 'recharge_money' => $money,
- 'preferential_money' => 0.00,
- 'total_money' => 0.00,
- 'pay_money' => 0.00,
- 'pay_time' => Carbon::now(),
- 'user_id' => $users->id,
- 'no' => RechargeOrder::makeNo(),
- 'pay_status' => RechargeOrder::PAY_STATUS_OK,
- 'pay_type' => RechargeOrder::PAY_TYPE_SYSTERM,
- 'recharge_config' => '',
- 'is_admin' => Admin::user()->id,
- 'remark' => $remark,
- ];
- $order = RechargeOrder::create($data);
- // 2.钱包记录
- WalletLog::log(WalletLog::OPERATE_TYPE_ADD, $order->recharge_money, $users->id, WalletLog::TYPE_ADD_ADMIN_TO_WALLET, $order->area_id, $order->id, RechargeOrder::class);
- // 3.增加用户余额 (上边已做)
- DB::commit();
- return $this->ok('充值成功');
- } catch (\Exception $exception) {
- DB::rollBack();
- Log::error($exception->getMessage());
- return $this->error($exception->getMessage());
- }
- }
- /**
- * refundBalance 退还余额(附带查询)
- *
- * @param Request $request
- * @return \Illuminate\Http\JsonResponse
- * @author Fx
- *
- */
- public function refundBalance(Request $request)
- {
- $id = $request->get('id') ?? '';
- $type = $request->get('type') ?? '';
- if (empty($id) || empty($type)) return $this->error('参数错误');
- $user = User::find($id);
- if (empty($user)) return $this->error('找不到该用户');
- $refundBalanceOrder = RefundBalanceOrder::query()->where('user_id', $id)->orderByDesc('id')->first();
- // 用户现有余额
- $balance_now = $user->wallet_money;
- // 实际充值支付金额
- $total_pay_recharge = RechargeOrder::query()
- ->where('user_id', $id)
- ->where('pay_status', RechargeOrder::PAY_STATUS_OK);
- if (empty($refundBalanceOrder)) {
- $total_pay_recharge = $total_pay_recharge->sum('pay_money');
- } else {
- $total_pay_recharge = $total_pay_recharge
- ->where('created_at', '>', Carbon::parse($refundBalanceOrder->created_at)->format('Y-m-d H:i:s'))
- ->sum('pay_money');
- }
- //
- $rechargeOrders = RechargeOrder::query()
- ->where('user_id', $id)
- ->where('pay_status', RechargeOrder::PAY_STATUS_OK);
- if (empty($refundBalanceOrder)) {
- $rechargeOrders = $rechargeOrders->get();
- } else {
- $rechargeOrders = $rechargeOrders
- ->where('created_at', '>', Carbon::parse($refundBalanceOrder->created_at)->format('Y-m-d H:i:s'))
- ->get();
- }
- // 充值订单总充值金额
- $total_recharge = 0;
- if (!empty($rechargeOrders)) {
- foreach ($rechargeOrders as $v) {
- if (empty($v->recharge_config)) {
- $total_recharge = bcadd($v->recharge_money, $total_recharge, 2);
- } else {
- // Log::info($v->recharge_config);
- $rechargeConfig = json_decode($v->recharge_config, true);
- if (empty($rechargeConfig)) {
- $total_recharge = bcadd($v->recharge_money, $total_recharge, 2);
- } else {
- $total_recharge = bcadd(bcadd($rechargeConfig['recharge_money'], $rechargeConfig['give_money'], 2), $total_recharge, 2);
- }
- }
- }
- }
- // 已消费
- // $consumed = bcsub($total_recharge, $balance_now, 2) < 0 ? 0 : bcsub($total_recharge, $balance_now, 2);
- $consumed = WalletLog::query()->whereIn('type', [WalletLog::TYPE_SUB_WALLET_BIKE_ORDER, WalletLog::TYPE_ADMIN_SUB_BIKE_ORDER, WalletLog::TYPE_SUB_WALLET_RENT_ORDER])->where('user_id', $user->id);
- if (!empty($refundBalanceOrder)) {
- $consumed = $consumed
- ->where('created_at', '>', Carbon::parse($refundBalanceOrder->created_at)->format('Y-m-d H:i:s'));
- }
- $consumed = $consumed->sum('money');
- $consumed = -$consumed;
- $consumed = $consumed < 0 ? 0 : $consumed;
- // 订单返还得钱可退
- $order_refund_money = WalletLog::query()->where('type', WalletLog::TYPE_ADMIN_ADD_TO_WALLET)->where('user_id', $user->id);
- if (!empty($refundBalanceOrder)) {
- $order_refund_money = $order_refund_money->where('created_at', '>', Carbon::parse($refundBalanceOrder->created_at)->format('Y-m-d H:i:s'));
- }
- $order_refund_money = $order_refund_money->sum('money');
- // Log::info($order_refund_money);
- // 可退还
- $money = bcadd(bcsub($total_pay_recharge, $consumed, 2), $order_refund_money, 2);
- $money = $user->wallet_money < $money ? $user->wallet_money : $money;
- // 如果退款 必须从余额减去得钱 实际从余额扣除得
- $subBalance = bcadd($total_recharge, $order_refund_money, 2);
- $userBalance = $user->wallet_money < $subBalance ? $user->wallet_money : $subBalance;
- switch ($type) {
- case 'query_refund':
- return $this->ok([
- 'balance_now' => $balance_now,
- 'total_pay_recharge' => $total_pay_recharge,
- 'total_recharge' => $total_recharge,
- 'order_refund_money' => $order_refund_money,
- 'consumed' => $consumed,
- 'money' => $money < 0 ? 0 : $money,
- 'subBalance' => $userBalance
- ]);
- break;
- case 'refund_balance':
- if (Cache::has(sprintf(RedisKeys::REFUND_BALANCE, $user->id))) {
- return $this->error('此操作5分钟仅可操作一次');
- } else {
- Cache::add(sprintf(RedisKeys::REFUND_BALANCE, $user->id), 1, Carbon::now()->addMinutes(5));
- }
- if ($money <= 0) return $this->error('不可退款');
- // 新增订单
- $order = RefundBalanceOrder::create(
- [
- 'no' => RefundBalanceOrder::makeNo(),
- 'user_id' => $user->id,
- 'area_id' => $user->register_area_id,
- 'admin_id' => Admin::user()->id,
- 'balance_now' => $balance_now,
- 'total_pay_recharge' => $total_pay_recharge,
- 'total_recharge' => $total_recharge,
- 'consumed' => $consumed,
- 'sub_balance' => $userBalance,
- 'refund_money' => $money < 0 ? 0 : $money,
- 'pay_money' => $money < 0 ? 0 : $money,
- ]
- );
- $payment = app('wechat.payment'); // 微信支付
- $res = $payment->transfer->toBalance([
- 'partner_trade_no' => $order->no, // 商户订单号,需保持唯一性(只能是字母或者数字,不能包含有符号)
- 'openid' => $user->auth->credential,
- 'check_name' => 'FORCE_CHECK', // NO_CHECK:不校验真实姓名, FORCE_CHECK:强校验真实姓名
- 're_user_name' => $user->truename, // 如果 check_name 设置为FORCE_CHECK,则必填用户真实姓名
- 'amount' => wechat_fee($order->pay_money), // 企业付款金额,单位为分
- 'desc' => '余额退款', // 企业付款操作说明信息。必填
- ]);
- // 更新钱包记录
- WalletLog::log(WalletLog::OPERATE_TYPE_SUB, $userBalance, $user->id, WalletLog::TYPE_SUB_WALLET, $order->area_id, $order->id, RefundBalanceOrder::class);
- if ($res['return_code'] === 'SUCCESS') {
- if ($res['result_code'] === 'SUCCESS') {
- // 更新订单
- $order->pay_time = Carbon::now();
- $order->pay_status = RefundBalanceOrder::PAY_STATUS_OK;
- $order->save();
- return $this->ok('操作成功');
- } else {
- $order->err_code = $res['err_code'];
- $order->save();
- Log::error($res);
- return $this->error('退款失败,错误码为' . $res['err_code'] . ';具体原因请联系管理员查看');
- }
- } else {
- $order->err_code = $res['err_code'];
- $order->save();
- Log::error($res);
- return $this->error('退款失败,错误码为' . $res['err_code'] . ';具体原因请联系管理员查看');
- }
- break;
- default:
- return $this->error('类型参数错误');
- }
- }
- /**
- * refundBalanceOrderList 退余额订单
- *
- * @param RefundBalanceOrderFilter $filter
- * @return \Illuminate\Http\JsonResponse
- * @author Fx
- *
- */
- public function refundBalanceOrderList(RefundBalanceOrderFilter $filter)
- {
- $list = RefundBalanceOrder::filter($filter)->where(AdminMerchant::getMerchantWhere())->orderByDesc('id');
- $area_ids = AdminUser::getAreaIdsByAdminId(Admin::user()->id);
- $list = $list->whereIn('area_id', $area_ids)->paginate();
- return $this->ok(RefundBalanceOrderResource::collection($list));
- }
- /**
- * refundBalanceAgain 重试退余额订单
- *
- * @param Request $request
- * @return \Illuminate\Http\JsonResponse
- * @author Fx
- *
- */
- public function refundBalanceAgain(Request $request)
- {
- $no = $request->get('no') ?? '';
- if (empty($no)) return $this->error('参数错误');
- $order = RefundBalanceOrder::query()->where('no', $no)->first();
- if (empty($order)) return $this->error('参数错误,找不到该订单');
- $user = User::find($order->user_id);
- if (Cache::has(sprintf(RedisKeys::REFUND_BALANCE_AGAIN, $user->id))) {
- return $this->error('此操作1分钟仅可操作一次');
- } else {
- Cache::add(sprintf(RedisKeys::REFUND_BALANCE_AGAIN, $user->id), 1, Carbon::now()->addMinutes(1));
- }
- $payment = app('wechat.payment'); // 微信支付
- $res = $payment->transfer->toBalance([
- 'partner_trade_no' => $order->no, // 商户订单号,需保持唯一性(只能是字母或者数字,不能包含有符号)
- 'openid' => $user->auth->credential,
- 'check_name' => 'FORCE_CHECK', // NO_CHECK:不校验真实姓名, FORCE_CHECK:强校验真实姓名
- 're_user_name' => $user->truename, // 如果 check_name 设置为FORCE_CHECK,则必填用户真实姓名
- 'amount' => wechat_fee($order->pay_money), // 企业付款金额,单位为分
- 'desc' => '余额退款', // 企业付款操作说明信息。必填
- ]);
- if ($res['return_code'] === 'SUCCESS') {
- if ($res['result_code'] === 'SUCCESS') {
- // 更新订单
- $order->pay_time = Carbon::now();
- $order->pay_status = RefundBalanceOrder::PAY_STATUS_OK;
- $order->save();
- // 更新用户余额
- // $userBalance = $user->wallet_money < $subBalance ? $user->wallet_money : $subBalance ;
- // 更新钱包记录
- WalletLog::log(WalletLog::OPERATE_TYPE_SUB, $order->sub_balance, $user->id, WalletLog::TYPE_SUB_WALLET, $order->area_id, $order->id, RefundBalanceOrder::class);
- return $this->ok('操作成功');
- } else {
- $order->err_code = $res['err_code'];
- $order->save();
- Log::error($res);
- return $this->error('退款失败,错误码为' . $res['err_code'] . ';具体原因请联系管理员查看');
- }
- } else {
- $order->err_code = $res['err_code'];
- $order->save();
- Log::error($res);
- return $this->error('退款失败,错误码为' . $res['err_code'] . ';具体原因请联系管理员查看');
- }
- }
- /**
- * queryRefundBalance 查询退余额是否成功
- *
- * @param Request $request
- * @return \Illuminate\Http\JsonResponse
- * @author Fx
- *
- */
- public function queryRefundBalance(Request $request)
- {
- $no = $request->get('no') ?? '';
- if (empty($no)) return $this->error('参数错误');
- $order = RefundBalanceOrder::query()->where('no', $no)->first();
- if (empty($order)) return $this->error('找不到订单');
- $payment = app('wechat.payment'); // 微信支付
- $res = $payment->transfer->queryBalanceOrder($no);
- if ($res['return_code'] === 'SUCCESS') {
- if ($res['result_code'] === 'SUCCESS') {
- if ($res['status'] === 'SUCCESS') {
- // 更新订单
- if ($order->pay_status == RefundBalanceOrder::PAY_STATUS_NO) {
- $order->pay_time = Carbon::now();
- $order->pay_status = RefundBalanceOrder::PAY_STATUS_OK;
- $order->save();
- }
- $comments = WalletLog::query()->where('log_type', RefundBalanceOrder::class)->where('log_id', $order->id)->first();
- if (empty($comments)) {
- // 更新钱包记录
- WalletLog::log(WalletLog::OPERATE_TYPE_SUB, $order->sub_balance, $order->user_id, WalletLog::TYPE_SUB_WALLET, $order->area_id, $order->id, RefundBalanceOrder::class);
- }
- return $this->ok('已成功退款');
- } elseif ($res['status'] === 'FAILED') {
- Log::error($res);
- return $this->error('转账失败,具体原因请联系管理员');
- } elseif ($res['status'] === 'PROCESSING') {
- Log::info($res);
- return $this->ok('转账处理中,请耐心等待');
- }
- } else {
- Log::error($res);
- return $this->error('查询失败,具体原因请联系管理员');
- }
- } else {
- return $this->error('请求失败');
- }
- }
- /**
- * 认证列表
- * Author: Mead
- */
- public function students(StudentFilter $filter)
- {
- $admin_id = Admin::user()->id;
- $students = Student::query()
- ->filter($filter)
- ->with(['user', 'resource'])
- ->orderByDesc('id');
- $students = $students->paginate();
- return $this->ok(StudentResource::collection($students));
- }
- /**
- * 待处理认证信息
- * @return mixed
- * Author: Mead
- */
- public function studentsNumber()
- {
- return Student::where('auth_status', Student::AUTH_STATUS_WAIT)->count();
- }
- /**
- * 调整成功
- * @param StudentRequest $request
- * @return \Illuminate\Http\JsonResponse
- * Author: Mead
- */
- public function updateStudents(StudentRequest $request)
- {
- $data = $request->only(['id', 'auth_status', 'auth_date', 'error_msg']);
- $data['admin_id'] = Admin::user()->id;
- if (Student::where('id', $data['id'])->where('auth_status', Student::AUTH_STATUS_OK)->where('auth_date', '>', date("Y-m"))->exists()) {
- return $this->error('已经认证');
- }
- $student = Student::where('id', $data['id'])->orderByDesc('id')->first();
- if ($data['auth_status'] === Student::AUTH_STATUS_OK) {
- //认证成功处理
- $user = User::where('id', $student->user_id)->first();
- if ((int)$user->deposit_type === User::DEPOSIT_CARD) {
- $deposit_expire_time = Carbon::parse($user->deposit_expire_time);
- if (Carbon::parse($data['auth_date'])->gt($deposit_expire_time)) {
- //判断第一个日期是否比第二个日期大
- $user->deposit_expire_time = Carbon::parse($data['auth_date']);
- }
- } else {
- $user->deposit_expire_time = Carbon::parse($data['auth_date']);
- }
- if ((int)$user->is_deposit === User::DEPOSIT_NO) {
- $user->deposit_type = User::DEPOSIT_CARD;
- $user->is_deposit = User::DEPOSIT_OK;
- }
- $user->is_student = User::IS_STUDENT_OK;
- $user->save();
- }
- Student::where('id', $student->id)->update($data);
- return $this->ok();
- }
- }
|