package com.izouma.dingdong.web;

import com.izouma.dingdong.domain.WithdrawApply;
import com.izouma.dingdong.dto.PageQuery;
import com.izouma.dingdong.enums.WithdrawStatus;
import com.izouma.dingdong.exception.BusinessException;
import com.izouma.dingdong.repo.WithdrawApplyRepo;
import com.izouma.dingdong.service.WithdrawApplyService;
import com.izouma.dingdong.utils.ObjUtils;
import com.izouma.dingdong.utils.SecurityUtils;
import com.izouma.dingdong.utils.excel.ExcelUtils;

import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

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

@AllArgsConstructor
@RestController
@RequestMapping("/withdrawApply")
public class WithdrawApplyController extends BaseController {
    /*generatedStart*/
    private WithdrawApplyService withdrawApplyService;

    private WithdrawApplyRepo withdrawApplyRepo;
    /*generatedEnd*/

    /*generatedStart*/
//    @PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/save")
    public WithdrawApply save(@RequestBody WithdrawApply record) {
        if (record.getId() != null) {
            WithdrawApply orig = withdrawApplyRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录"));
            ObjUtils.merge(orig, record);
            return withdrawApplyRepo.save(orig);
        }
        return withdrawApplyRepo.save(record);
    }


    //    @PreAuthorize("hasRole('ADMIN')")
    @GetMapping("/all")
    public Page<WithdrawApply> all(PageQuery pageQuery) {
        return withdrawApplyRepo.findAll(toSpecification(pageQuery, WithdrawApply.class), toPageRequest(pageQuery));
    }

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

    @PostMapping("/del/{id}")
    public void del(@PathVariable Long id) {
        withdrawApplyRepo.deleteById(id);
    }

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

    @GetMapping("/audit")
    @ApiOperation("提现是否通过")
    public WithdrawApply success(@RequestParam Long id, @RequestParam Boolean consent) {
        return withdrawApplyService.audit(id, consent);
    }

    @PostMapping("/apply")
    @ApiOperation("申请提现")
    public WithdrawApply apply(@RequestParam BigDecimal amount, Long bankCardId) {
        return withdrawApplyService.apply(SecurityUtils.getAuthenticatedUser().getId(), amount, bankCardId);
    }

    @PostMapping("/my")
    @ApiOperation("提现未成功记录")
    public List<WithdrawApply> my() {
        return withdrawApplyRepo.findByUserIdAndConsentFalse(SecurityUtils.getAuthenticatedUser().getId());
    }

/*    @GetMapping("/name")
    public Page<WithdrawApply> getWithdrawApplyApply(String search, Pageable pageable) {
        return WithdrawApplyService.getWithdrawApplyApply(search, pageable);
    }*/
}

