package com.izouma.nineth.web;

import com.izouma.nineth.domain.WalletRenewOrder;
import com.izouma.nineth.dto.PageQuery;
import com.izouma.nineth.dto.PageWrapDTO;
import com.izouma.nineth.dto.pay.PayResult;
import com.izouma.nineth.exception.BusinessException;
import com.izouma.nineth.repo.WalletRenewOrderRepo;
import com.izouma.nineth.service.WalletRenewOrderService;
import com.izouma.nineth.utils.excel.ExcelUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
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("/walletRenewOrder")
@AllArgsConstructor
@Slf4j
public class WalletRenewOrderController {

    private WalletRenewOrderService walletRenewOrderService;

    private WalletRenewOrderRepo walletRenewOrderRepo;

    /**
     * 查询列表
     * @param pageQuery
     * @return
     */
    @PreAuthorize("hasRole('ADMIN')")
    @RequestMapping("/all")
    public PageWrapDTO<WalletRenewOrder> all(@RequestBody PageQuery pageQuery){
        return walletRenewOrderService.all(pageQuery);
    }

    @GetMapping("/get/{id}")
    public WalletRenewOrder get(@PathVariable Long id) {
        WalletRenewOrder walletRenewOrder = walletRenewOrderRepo.findById(id).orElseThrow(new BusinessException("续费无记录"));
        return walletRenewOrder;
    }


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


    /**
     * 检查提现结果
     * @return
     */
    @PreAuthorize("hasRole('ADMIN')")
    @RequestMapping("checkPayResult")
    @ResponseBody
    public PayResult checkPayResult(@RequestParam String orderNo){
        PayResult payResult = walletRenewOrderService.checkPayResult(orderNo);
        return payResult;
    }


}
