package com.izouma.nineth.web;

import com.izouma.nineth.domain.RechargeOrder;
import com.izouma.nineth.domain.UserFundAccount;
import com.izouma.nineth.domain.WithdrawApply;
import com.izouma.nineth.dto.PageQuery;
import com.izouma.nineth.dto.PageWrapDTO;
import com.izouma.nineth.dto.UserDTO;
import com.izouma.nineth.dto.pay.PayResult;
import com.izouma.nineth.dto.pay.RechargeResult;
import com.izouma.nineth.dto.pay.WithdrawResult;
import com.izouma.nineth.exception.BusinessException;
import com.izouma.nineth.repo.RechargeOrderRepo;
import com.izouma.nineth.service.RechargeOrderService;
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("/rechargeOrder")
@AllArgsConstructor
@Slf4j
public class RechargeOrderController extends BaseController{

    private RechargeOrderRepo rechargeOrderRepo;

    private RechargeOrderService rechargeOrderService;

    @PostMapping("/all")
    public PageWrapDTO<RechargeOrder> all(@RequestBody PageQuery pageQuery) {
        //充值记录
        return rechargeOrderService.all(pageQuery);
    }


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


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

    /**
     * 检查提现结果
     * @return
     */
    @PreAuthorize("hasRole('ADMIN')")
    @RequestMapping("checkRechargeResult")
    @ResponseBody
    public RechargeResult checkRechargeResult(@RequestParam String orderNo){
        RechargeResult rechargeResult = rechargeOrderService.checkRechargeResult(orderNo);
        return rechargeResult;
    }



}
