package com.sptg.estate.biz.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.constant.OperationConstant;
import com.sptg.estate.api.dto.ApplyPayRefundDto;
import com.sptg.estate.api.entity.ApplyPayRefund;
import com.sptg.estate.api.feigns.ApplyPayRefundFeign;
import com.sptg.estate.biz.service.ApplyPayRefundInterface;
import com.sptg.pay.api.entity.PayCertificate;
import com.sptg.pay.api.feigns.PayCertificateFeign;
import lombok.RequiredArgsConstructor;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.io.FileNotFoundException;
import java.util.Date;

/**
 * @author zwx
 * @create 2020/6/21 1:36
 */
@RestController
@RequiredArgsConstructor
public class ApplyPayRefundController implements ApplyPayRefundFeign {

    private final ApplyPayRefundInterface applyPayRefundInterface;
    private final PayCertificateFeign payCertificateFeign;

    @Override
    @PostMapping(value = "/applyPayRefund/showApplyPayRefundPage")
    public ResultBean<Page<ApplyPayRefund>> showApplyPayRefundPage(@RequestBody ApplyPayRefundDto applyPayRefundDto) {
        Assert.notNull(applyPayRefundDto, "支付退款申请传输对象不能为空");
        return this.applyPayRefundInterface.showApplyPayRefundList(applyPayRefundDto);
    }

    @Override
    @PostMapping(value = "/applyPayRefund/addApplyPayRefund")
    public ResultBean<Boolean> addApplyPayRefund(@RequestBody ApplyPayRefundDto applyPayRefundDto) {
        Long payCertificateId = applyPayRefundDto.getPayCertificateId();
        Assert.notNull(payCertificateId, "支付凭证ID不能为空");
        Long userId = applyPayRefundDto.getUserId();
        Assert.notNull(userId, "用户ID不能为空");
        ApplyPayRefund applyPayRefund = this.applyPayRefundInterface.getApplyPayRefundByCondition(applyPayRefundDto);
        Assert.isTrue(applyPayRefund == null, "支付退款申请记录已存在");
        PayCertificate payCertificate = payCertificateFeign.findPayCertificateById(payCertificateId);
        Assert.notNull(payCertificate, "支付凭证不能为空");
        Integer payStatus = payCertificate.getPayStatus();
        Integer refund = payCertificate.getRefund();
        Assert.isTrue(payStatus.equals(OperationConstant.TWO), "支付凭证未完成支付，无需退款");
        Assert.isTrue(!refund.equals(OperationConstant.ONE), "支付凭证已退款，请勿重复申请退款");
        return this.applyPayRefundInterface.addApplyPayRefund(applyPayRefundDto);
    }

    @Override
    @GetMapping(value = "/applyPayRefund/findApplyPayRefundById/{id}")
    public ApplyPayRefund findApplyPayRefundById(@PathVariable(value = "id") Long id) {
        ApplyPayRefund applyPayRefund = this.applyPayRefundInterface.getApplyPayRefundById(id);
        return applyPayRefund;
    }

    @Override
    @PostMapping(value = "/applyPayRefund/auditApplyPayRefund")
    public ResultBean<Boolean> auditApplyPayRefund(@RequestBody ApplyPayRefundDto applyPayRefundDto){
        Assert.notNull(applyPayRefundDto, "支付退款申请传输对象不能为空");
        Integer checkStatus = applyPayRefundDto.getCheckStatus();
        Assert.isTrue(checkStatus.equals(OperationConstant.ONE), "支付申请未通过，不能发起退款申请");
        Date today = new Date();
        applyPayRefundDto.setCheckDate(today);
        return this.applyPayRefundInterface.auditApplyPayRefund(applyPayRefundDto);
    }

    @Override
    @PostMapping(value = "/applyPayRefund/findApplyPayRefundByCondition")
    public ApplyPayRefund findApplyPayRefundByCondition(@RequestBody ApplyPayRefundDto applyPayRefundDto) {
        Assert.notNull(applyPayRefundDto, "支付退款申请传输对象不能为空");
        return this.applyPayRefundInterface.getApplyPayRefundByCondition(applyPayRefundDto);
    }

    @Override
    @PostMapping(value = "/applyPayRefund/updateApplyPayRefundByRefundNo")
    public Boolean updateApplyPayRefundByRefundNo(@RequestBody ApplyPayRefund applyPayRefund) {
        Assert.notNull(applyPayRefund, "修改退款申请传输对象不能为空");
        return this.applyPayRefundInterface.updateApplyPayRefundByRefundNo(applyPayRefund);
    }
}
