package com.izouma.nineth.web;

import com.izouma.nineth.domain.DivMoney;
import com.izouma.nineth.domain.TransferMoneyInfo;
import com.izouma.nineth.domain.User;
import com.izouma.nineth.dto.PageQuery;
import com.izouma.nineth.dto.wallet.TransferDto;
import com.izouma.nineth.exception.BusinessException;
import com.izouma.nineth.repo.TransferMoneyInfoRepo;
import com.izouma.nineth.repo.UserRepo;
import com.izouma.nineth.service.TransferMoneyInfoService;
import com.izouma.nineth.service.UserFundAccountForLianlianpayService;
import com.izouma.nineth.utils.ApplicationContextUtil;
import com.izouma.nineth.utils.excel.ExcelUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
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("/transferMoneyInfo")
@AllArgsConstructor
@Slf4j
public class TransferMoneyInfoController extends BaseController{

    private TransferMoneyInfoRepo transferMoneyInfoRepo;

    private TransferMoneyInfoService transferMoneyInfoService;

    private UserFundAccountForLianlianpayService userFundAccountForLianlianpayService;

    private UserRepo userRepo;

    /**
     * 转账记录
     * @param pageQuery
     * @return
     */
    @PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/all")
    public Page<TransferMoneyInfo> all(@RequestBody PageQuery pageQuery) {
        //所有转账记录
        return transferMoneyInfoService.all(pageQuery);
    }

    @GetMapping("/get/{id}")
    public TransferMoneyInfo get(@PathVariable Long id){
        return transferMoneyInfoRepo.findById(id).orElseThrow(new BusinessException("未找到转账记录"));
    }


    /**
     * 创建转账交易
     * @return
     */
    @PreAuthorize("hasRole('ADMIN')")
    @PostMapping("createTransfer")
    public TransferMoneyInfo createTransfer(@RequestBody TransferDto transferDto){
        User user = currentUser();
        if(!ApplicationContextUtil.isHighestAdmin(user)){
            throw new BusinessException("只允许授权的管理员使用");
        }

        if(transferDto.getUserId() == null){
            throw new BusinessException("需要指定一个目标用户");
        }

        TransferMoneyInfo transferMoneyInfo = userFundAccountForLianlianpayService.createTransferToUser(transferDto.getUserId(),
                transferDto.getAmount(), transferDto.getReason());
        return transferMoneyInfo;
    }

    /**
     * 创建转账交易
     * @return
     */
    @PreAuthorize("hasRole('ADMIN')")
    @PostMapping("executeTransfer")
    public TransferMoneyInfo executeTransfer(@RequestParam String tradeNo){
        User user = currentUser();
        if(!ApplicationContextUtil.isHighestAdmin(user)){
            throw new BusinessException("只允许授权的管理员使用");
        }

        TransferMoneyInfo transferMoneyInfo = userFundAccountForLianlianpayService.executeTransfer(tradeNo);
        return transferMoneyInfo;
    }


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



}
