AdminRiderWithdrawController.java 3.97 KB
package com.diligrp.rider.controller;

import com.diligrp.rider.common.result.Result;
import com.diligrp.rider.dto.WithdrawAuditDTO;
import com.diligrp.rider.dto.WithdrawMarkPaidDTO;
import com.diligrp.rider.service.RiderWithdrawService;
import com.diligrp.rider.vo.PageResultVO;
import com.diligrp.rider.vo.RiderWithdrawApplyVO;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/admin/rider/withdraw")
@RequiredArgsConstructor
public class AdminRiderWithdrawController {

    private final RiderWithdrawService withdrawService;

    @GetMapping("/list")
    public Result<PageResultVO<RiderWithdrawApplyVO>> list(@RequestParam(required = false) Long cityId,
                                                           @RequestParam(required = false) Integer status,
                                                           @RequestParam(required = false) String keyword,
                                                           @RequestParam(required = false) String startDate,
                                                           @RequestParam(required = false) String endDate,
                                                           @RequestParam(defaultValue = "1") int page,
                                                           HttpServletRequest request) {
        return Result.success(withdrawService.adminList(resolveQueryCityId(request, cityId), status, keyword, startDate, endDate, page));
    }

    @GetMapping("/{id}")
    public Result<RiderWithdrawApplyVO> detail(@PathVariable Long id, HttpServletRequest request) {
        return Result.success(withdrawService.adminDetail(id, resolveScopedCityId(request)));
    }

    @PostMapping("/{id}/approve")
    public Result<Void> approve(@PathVariable Long id,
                                @RequestBody(required = false) WithdrawAuditDTO dto,
                                HttpServletRequest request) {
        withdrawService.approve(id, dto, resolveScopedCityId(request), currentAdminId(request), currentRole(request));
        return Result.success();
    }

    @PostMapping("/{id}/reject")
    public Result<Void> reject(@PathVariable Long id,
                               @RequestBody(required = false) WithdrawAuditDTO dto,
                               HttpServletRequest request) {
        withdrawService.reject(id, dto, resolveScopedCityId(request), currentAdminId(request), currentRole(request));
        return Result.success();
    }

    @PostMapping("/{id}/mark-paid")
    public Result<Void> markPaid(@PathVariable Long id,
                                 @Valid @RequestBody WithdrawMarkPaidDTO dto,
                                 HttpServletRequest request) {
        withdrawService.markPaid(id, dto, resolveScopedCityId(request), currentAdminId(request), currentRole(request));
        return Result.success();
    }

    private Long resolveScopedCityId(HttpServletRequest request) {
        return "substation".equals(request.getAttribute("role")) ? (Long) request.getAttribute("cityId") : null;
    }

    private Long resolveQueryCityId(HttpServletRequest request, Long queryCityId) {
        Long scopedCityId = resolveScopedCityId(request);
        return scopedCityId != null ? scopedCityId : queryCityId;
    }

    private Long currentAdminId(HttpServletRequest request) {
        return (Long) request.getAttribute("adminId");
    }

    private String currentRole(HttpServletRequest request) {
        Object role = request.getAttribute("role");
        return role == null ? "" : role.toString();
    }
}