| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353 |
- <?php
- namespace app\admin\controller\order;
- use app\common\controller\Backend;
- use think\facade\Db;
- class Withdrawal extends Backend
- {
- protected array $noNeedLogin = ['*'];
- /**
- * 提现申请列表
- * @return void
- */
- public function index(): void
- {
- $where = [];
- // 申请人搜索
- if (!empty($this->request->get('applicant_name'))) {
- $applicantName = $this->request->get('applicant_name');
- $where[] = ['u.name', 'like', '%' . $applicantName . '%'];
- }
- // 状态筛选
- if ($this->request->get('status') !== '' && $this->request->get('status') !== null) {
- $where['wr.status'] = $this->request->get('status');
- }
- // 提现方式筛选
- if (!empty($this->request->get('withdrawal_method'))) {
- $where['wr.withdrawal_method'] = $this->request->get('withdrawal_method');
- }
- //查询时间范围
- $star_time = $this->request->get('star_time');
- $end_time = $this->request->get('end_time');
- if (!empty($star_time) && !empty($end_time)) {
- $where[] = ['wr.create_time', 'between', [$star_time, $end_time], 'AND'];
- }
- // 排序
- $order = [];
- if (!empty($this->request->get('order'))) {
- $order_a = $this->request->get('order');
- $order_a = explode(',', $order_a);
- $order[$order_a[0]] = $order_a[1];
- } else {
- $order['wr.create_time'] = 'desc';
- }
- // 分页
- $page = $this->request->get('page', 1);
- $limit = $this->request->get('limit', 20);
- // 查询数据
- $withdrawalList = Db::name('withdrawal_record')
- ->alias('wr')
- ->leftJoin('user_on u', 'wr.user_id = u.id')
- ->field('
- wr.id,
- u.name,
- wr.amount,
- wr.current_balance,
- wr.withdrawal_method,
- wr.status,
- wr.create_time,
- wr.audit_time,
- wr.audit_remark,
- wr.user_id,
- wr.withdrawal_no
- ')
- ->where($where)
- ->order($order)
- ->paginate([
- 'list_rows' => $limit,
- 'page' => $page
- ]);
- // 转换字段格式
- $list = $withdrawalList->items();
- foreach ($list as &$item) {
- // 提现方式转换
- $item['withdrawal_method'] = $this->getWithdrawalMethodText($item['withdrawal_method']);
- // 状态转换
- $item['status_text'] = $this->getStatusText($item['status']);
- // 格式化金额
- $item['amount'] = number_format($item['amount'], 2);
- $item['current_balance'] = number_format($item['current_balance'], 2);
- // 格式化时间
- if ($item['create_time']) {
- $item['create_time'] = date('Y-m-d H:i:s', strtotime($item['create_time']));
- }
- if ($item['audit_time']) {
- $item['audit_time'] = date('Y-m-d H:i:s', strtotime($item['audit_time']));
- }
- }
- $this->success('查询成功', [
- 'list' => $list,
- 'total' => $withdrawalList->total()
- ]);
- }
- /**
- * 审核通过
- * @return void
- */
- public function approve(): void
- {
- $id = $this->request->post('id');
- $auditRemark = $this->request->post('audit_remark', '');
- if (empty($id)) {
- $this->error(__('参数错误'));
- }
- try {
- Db::startTrans();
- // 获取提现记录
- $withdrawalRecord = Db::name('withdrawal_record')
- ->where('id', $id)
- ->lock(true)
- ->find();
- if (!$withdrawalRecord) {
- throw new \Exception('提现记录不存在');
- }
- if ($withdrawalRecord['status'] != 0) {
- throw new \Exception('只有待审核的记录才能审核');
- }
- // 检查用户佣金是否足够
- $userCommission = Db::name('user_commission')
- ->where('user_id', $withdrawalRecord['user_id'])
- ->lock(true)
- ->find();
- if (!$userCommission || $userCommission['current_commission'] < $withdrawalRecord['amount']) {
- throw new \Exception('用户佣金不足');
- }
- // 更新提现记录状态
- $updateData = [
- 'status' => 1, // 审核通过
- 'auditor_id' => $this->auth->id ?? 0,
- 'audit_time' => date('Y-m-d H:i:s'),
- 'audit_remark' => $auditRemark,
- 'balance_after' => Db::raw('current_balance - ' . $withdrawalRecord['amount']),
- 'update_time' => date('Y-m-d H:i:s')
- ];
- $result = Db::name('withdrawal_record')
- ->where('id', $id)
- ->update($updateData);
- if (!$result) {
- throw new \Exception('更新提现记录失败');
- }
- // 更新用户佣金(扣除已提现金额)
- Db::name('user_commission')
- ->where('user_id', $withdrawalRecord['user_id'])
- ->update([
- 'current_commission' => Db::raw('current_commission - ' . $withdrawalRecord['amount']),
- 'update_time' => date('Y-m-d H:i:s')
- ]);
- Db::commit();
- } catch (\Exception $e) {
- Db::rollback();
- $this->error('审核失败: ' . $e->getMessage());
- }
- $this->success('审核通过');
- }
- /**
- * 审核拒绝
- * @return void
- */
- public function reject(): void
- {
- $id = $this->request->post('id');
- $auditRemark = $this->request->post('audit_remark', '');
- if (empty($id)) {
- $this->error(__('参数错误'));
- }
- if (empty($auditRemark)) {
- $this->error(__('请填写拒绝原因'));
- }
- try {
- // 获取提现记录
- $withdrawalRecord = Db::name('withdrawal_record')
- ->where('id', $id)
- ->find();
- if (!$withdrawalRecord) {
- throw new \Exception ('提现记录不存在');
- }
- if ($withdrawalRecord['status'] != 0) {
- throw new \Exception ('只有待审核的记录才能审核');
- }
- // 更新提现记录状态
- $result = Db::name('withdrawal_record')
- ->where('id', $id)
- ->update([
- 'status' => 2, // 审核拒绝
- 'auditor_id' => $this->auth->id ?? 0,
- 'audit_time' => date('Y-m-d H:i:s'),
- 'audit_remark' => $auditRemark,
- 'update_time' => date('Y-m-d H:i:s')
- ]);
- } catch (\Exception $e) {
- $this->error('操作失败: ' . $e->getMessage());
- }
- if ($result) {
- $this->success('审核拒绝成功');
- } else {
- $this->error(__('审核拒绝失败'));
- }
- }
- /**
- * 获取提现方式文本
- * @param string $method
- * @return string
- */
- private function getWithdrawalMethodText(string $method): string
- {
- $map = [
- 'wechat' => '微信',
- 'alipay' => '支付宝',
- 'bank' => '银行卡'
- ];
- return $map[$method] ?? $method;
- }
- /**
- * 获取状态文本
- * @param int $status
- * @return string
- */
- private function getStatusText(int $status): string
- {
- $map = [
- 0 => '待审核',
- 1 => '审核通过',
- 2 => '审核拒绝',
- 3 => '已打款',
- 4 => '打款失败'
- ];
- return $map[$status] ?? '未知';
- }
- /**
- * 获取提现状态选项
- * @return void
- */
- public function statusOptions(): void
- {
- $options = [
- ['value' => 0, 'label' => '待审核'],
- ['value' => 1, 'label' => '审核通过'],
- ['value' => 2, 'label' => '审核拒绝'],
- ['value' => 3, 'label' => '已打款'],
- ['value' => 4, 'label' => '打款失败']
- ];
- $this->success('获取成功', $options);
- }
- /**
- * 获取提现方式选项
- * @return void
- */
- public function methodOptions(): void
- {
- $options = [
- ['value' => 'wechat', 'label' => '微信'],
- ['value' => 'alipay', 'label' => '支付宝'],
- ['value' => 'bank', 'label' => '银行卡']
- ];
- $this->success('获取成功', $options);
- }
- /**
- * 提现统计
- * @return void
- */
- public function statistics(): void
- {
- // 总提现申请数
- $totalCount = Db::name('withdrawal_record')->count();
- // 待审核数
- $pendingCount = Db::name('withdrawal_record')
- ->where('status', 0)
- ->count();
- // 已通过数
- $approvedCount = Db::name('withdrawal_record')
- ->where('status', 1)
- ->count();
- // 已拒绝数
- $rejectedCount = Db::name('withdrawal_record')
- ->where('status', 2)
- ->count();
- // 总提现金额
- $totalAmount = Db::name('withdrawal_record')
- ->where('status', 1) // 只计算审核通过的
- ->sum('amount');
- // 今日提现统计
- $todayStart = date('Y-m-d 00:00:00');
- $todayEnd = date('Y-m-d 23:59:59');
- $todayCount = Db::name('withdrawal_record')
- ->whereBetween('create_time', [$todayStart, $todayEnd])
- ->count();
- $todayAmount = Db::name('withdrawal_record')
- ->whereBetween('create_time', [$todayStart, $todayEnd])
- ->where('status', 1)
- ->sum('amount');
- $stats = [
- 'total_count' => $totalCount,
- 'pending_count' => $pendingCount,
- 'approved_count' => $approvedCount,
- 'rejected_count' => $rejectedCount,
- 'total_amount' => number_format($totalAmount ?? 0, 2),
- 'today_count' => $todayCount,
- 'today_amount' => number_format($todayAmount ?? 0, 2)
- ];
- $this->success('获取成功', $stats);
- }
- }
|