| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- <?php
- namespace App\Services\Common;
- use App\Models\BalanceLogModel;
- use App\Models\MemberModel;
- use App\Services\BaseService;
- use Illuminate\Support\Facades\DB;
- /**
- * 统一余额日志服务(充值/提现)
- * 支持会员、代理、商户的充值和提现记录
- */
- class BalanceLogsService extends BaseService
- {
- /**
- * 获取列表
- */
- public function getList()
- {
- // 获取参数
- $params = request()->all();
-
- $page = $params['page'] ?? 1;
- $limit = $params['limit'] ?? 20;
- $keyword = $params['keyword'] ?? '';
- $status = $params['status'] ?? '';
- $type = $params['type'] ?? ''; // 1-充值,2-提现
- $accountType = $params['account_type'] ?? ''; // 1-会员,2-代理,3-商户
- $startTime = $params['start_time'] ?? '';
- $endTime = $params['end_time'] ?? '';
- $query = BalanceLogModel::where('mark', 1);
- // 类型筛选
- if ($type !== '') {
- $query->where('type', $type);
- }
- // 账户类型筛选
- if ($accountType !== '') {
- $query->where('account_type', $accountType);
- }
- // 关键词搜索(订单号、姓名、手机号)
- if (!empty($keyword)) {
- $query->where(function($q) use ($keyword) {
- $q->where('order_no', 'like', "%{$keyword}%")
- ->orWhere('realname', 'like', "%{$keyword}%")
- ->orWhereHas('user', function($sq) use ($keyword) {
- $sq->where('realname', 'like', "%{$keyword}%")
- ->orWhere('nickname', 'like', "%{$keyword}%")
- ->orWhere('mobile', 'like', "%{$keyword}%");
- });
- });
- }
- // 状态筛选
- if ($status !== '') {
- $query->where('status', $status);
- }
- // 时间范围
- if (!empty($startTime)) {
- $query->where('create_time', '>=', strtotime($startTime));
- }
- if (!empty($endTime)) {
- $query->where('create_time', '<=', strtotime($endTime . ' 23:59:59'));
- }
- $total = $query->count();
- $list = $query->with(['user:id,realname,nickname,mobile,user_type'])
- ->orderBy('create_time', 'desc')
- ->offset(($page - 1) * $limit)
- ->limit($limit)
- ->get()
- ->toArray();
- // 格式化数据
- foreach ($list as &$item) {
- $item['money'] = number_format($item['money'], 2, '.', '');
- $item['actual_money'] = number_format($item['actual_money'], 2, '.', '');
- $item['create_time_text'] = date('Y-m-d H:i:s', $item['create_time']);
- $item['update_time_text'] = date('Y-m-d H:i:s', $item['update_time']);
- $item['status_text'] = $this->getStatusText($item['status']);
- $item['pay_type_text'] = $this->getPayTypeText($item['pay_type']);
- $item['account_type_text'] = $this->getAccountTypeText($item['account_type']);
- $item['type_text'] = $item['type'] == 1 ? '充值' : '提现';
-
- // 根据账户类型添加额外信息
- if (isset($item['user'])) {
- $item['user_realname'] = $item['user']['realname'] ?? '';
- $item['user_nickname'] = $item['user']['nickname'] ?? '';
- $item['user_mobile'] = $item['user']['mobile'] ?? '';
- $item['user_type'] = $item['user']['user_type'] ?? 0;
- }
- }
- return [
- 'code' => 0,
- 'msg' => '获取成功',
- 'data' => $list,
- 'count' => $total
- ];
- }
- /**
- * 获取详情
- */
- public function getInfo($id = null)
- {
- // 如果没有传入ID,从请求中获取
- if ($id === null) {
- $id = request()->input('id');
- }
-
- $info = BalanceLogModel::where('id', $id)
- ->where('mark', 1)
- ->with(['user:id,realname,nickname,mobile,user_type'])
- ->first();
- if (!$info) {
- return ['code' => 1, 'msg' => '记录不存在'];
- }
- $info = $info->toArray();
- $info['money'] = number_format($info['money'], 2, '.', '');
- $info['actual_money'] = number_format($info['actual_money'], 2, '.', '');
- $info['create_time_text'] = date('Y-m-d H:i:s', $info['create_time']);
- $info['update_time_text'] = date('Y-m-d H:i:s', $info['update_time']);
- $info['status_text'] = $this->getStatusText($info['status']);
- $info['pay_type_text'] = $this->getPayTypeText($info['pay_type']);
- $info['account_type_text'] = $this->getAccountTypeText($info['account_type']);
- $info['type_text'] = $info['type'] == 1 ? '充值' : '提现';
- // 根据账户类型添加额外信息
- if (isset($info['user'])) {
- $info['user_realname'] = $info['user']['realname'] ?? '';
- $info['user_nickname'] = $info['user']['nickname'] ?? '';
- $info['user_mobile'] = $info['user']['mobile'] ?? '';
- $info['user_type'] = $info['user']['user_type'] ?? 0;
- }
- return [
- 'code' => 0,
- 'msg' => '获取成功',
- 'data' => $info
- ];
- }
- /**
- * 审核(仅提现)
- */
- public function audit($id = null, $status = null, $remark = '', $payImg = '')
- {
- // 如果没有传入参数,从请求中获取
- if ($id === null) {
- $params = request()->all();
- $id = $params['id'] ?? null;
- $status = $params['status'] ?? null;
- $remark = $params['remark'] ?? '';
- $payImg = $params['pay_img'] ?? '';
- }
-
- DB::beginTransaction();
- try {
- $record = BalanceLogModel::where('id', $id)
- ->where('mark', 1)
- ->where('type', 2) // 必须是提现
- ->lockForUpdate()
- ->first();
- if (!$record) {
- throw new \Exception('提现记录不存在');
- }
- if ($record->status != 1) {
- throw new \Exception('该记录已处理');
- }
- // 更新状态
- $record->status = $status;
- $record->confirm_remark = $remark;
- $record->update_time = time();
-
- // 如果是通过,更新支付状态和打款凭证
- if ($status == 2) {
- $record->pay_status = 20; // 20-已支付
- $record->pay_at = date('Y-m-d H:i:s');
- if (!empty($payImg)) {
- $record->pay_img = $payImg;
- }
- }
-
- $record->save();
- // 如果是驳回,退回金额
- if ($status == 3) {
- $this->refundBalance($record);
- }
- DB::commit();
- return ['code' => 0, 'msg' => '操作成功'];
- } catch (\Exception $e) {
- DB::rollBack();
- return ['code' => 1, 'msg' => $e->getMessage()];
- }
- }
- /**
- * 退回余额
- */
- private function refundBalance($record)
- {
- // 所有用户(会员、代理、商户)都在 member 表中
- $user = MemberModel::find($record->user_id);
-
- if ($user) {
- // 退回余额
- $user->balance = bcadd($user->balance, $record->money, 2);
- $user->save();
- }
- }
- /**
- * 删除记录
- */
- public function delete()
- {
- // 获取参数
- $id = request()->input('id');
-
- $record = BalanceLogModel::where('id', $id)
- ->where('mark', 1)
- ->first();
- if (!$record) {
- return ['code' => 1, 'msg' => '记录不存在'];
- }
- if ($record->status == 1) {
- return ['code' => 1, 'msg' => '待审核的记录不能删除'];
- }
- $record->mark = 0;
- $record->save();
- return ['code' => 0, 'msg' => '删除成功'];
- }
- /**
- * 批量删除
- */
- public function deleteAll($ids = null)
- {
- // 如果没有传入参数,从请求中获取
- if ($ids === null) {
- $ids = request()->input('ids', []);
- }
-
- $count = BalanceLogModel::whereIn('id', $ids)
- ->where('mark', 1)
- ->where('status', '!=', 1)
- ->update(['mark' => 0]);
- return [
- 'code' => 0,
- 'msg' => "成功删除{$count}条记录"
- ];
- }
- /**
- * 获取状态文本
- */
- private function getStatusText($status)
- {
- $statusMap = [
- -1 => '已取消',
- 1 => '待审核',
- 2 => '已审核/到账',
- 3 => '审核失败'
- ];
- return $statusMap[$status] ?? '未知';
- }
- /**
- * 获取支付方式文本
- */
- private function getPayTypeText($payType)
- {
- $payTypeMap = [
- 10 => '微信',
- 20 => '支付宝',
- 50 => '银行卡'
- ];
- return $payTypeMap[$payType] ?? '未知';
- }
- /**
- * 获取账户类型文本
- */
- private function getAccountTypeText($accountType)
- {
- $accountTypeMap = [
- 1 => '会员',
- 2 => '代理',
- 3 => '商户'
- ];
- return $accountTypeMap[$accountType] ?? '未知';
- }
- /**
- * 获取代理等级文本
- */
- private function getAgentLevelText($level)
- {
- $levelMap = [
- 1 => '一级代理',
- 2 => '二级代理',
- 3 => '三级代理'
- ];
- return $levelMap[$level] ?? '未知';
- }
- }
|