package com.izouma.nineth.web;

import com.izouma.nineth.domain.PaymentInfo;
import com.izouma.nineth.domain.WithdrawApply;
import com.izouma.nineth.dto.PageQuery;
import com.izouma.nineth.dto.PageWrapDTO;
import com.izouma.nineth.dto.pay.PayResult;
import com.izouma.nineth.dto.pay.WithdrawResult;
import com.izouma.nineth.dto.sumpay.Withdraw;
import com.izouma.nineth.exception.BusinessException;
import com.izouma.nineth.repo.WithdrawApplyRepo;
import com.izouma.nineth.service.WithdrawApplyService;
import com.izouma.nineth.utils.excel.ExcelUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 提现申请
 */
@RestController
@RequestMapping("/withdrawApply")
@AllArgsConstructor
@Slf4j
public class WithdrawApplyController extends BaseController{

    private WithdrawApplyRepo withdrawApplyRepo;

    private WithdrawApplyService withdrawApplyService;

    @PostMapping("/all")
    public PageWrapDTO<WithdrawApply> all(@RequestBody PageQuery pageQuery) {
        PageWrapDTO<WithdrawApply> page = withdrawApplyService.all(pageQuery);
        return  page;
    }

    @GetMapping("/get/{id}")
    public WithdrawApply get(@PathVariable Long id) {
        WithdrawApply withdrawApply = withdrawApplyRepo.findById(id).orElseThrow(new BusinessException("无记录"));
        return withdrawApply;
    }

    @PostMapping("/excel")
    @ResponseBody
    public void excel(HttpServletResponse response, @RequestBody PageQuery pageQuery) throws IOException {
        List<WithdrawApply> data = withdrawApplyService.all(pageQuery).getContent();
        ExcelUtils.export(response, data);
    }


    /**
     * 检查提现结果
     * @return
     */
    @PreAuthorize("hasRole('ADMIN')")
    @RequestMapping("checkWithdrawResult")
    @ResponseBody
    public WithdrawResult checkWithdrawResult(@RequestParam String orderNo){
        WithdrawResult withdrawResult = withdrawApplyService.checkWithdrawResult(orderNo);
        return withdrawResult;
    }


}
