package com.sptg.gateway.controller.estate;

import com.baomidou.mybatisplus.plugins.Page;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.common.core.constant.OperationConstant;
import com.sptg.common.security.annotation.Security;
import com.sptg.common.security.enums.DataFormatEnum;
import com.sptg.estate.api.constant.EstateSecurityToken;
import com.sptg.estate.api.dto.ApplyPayRefundDto;
import com.sptg.estate.api.entity.ApplyPayRefund;
import com.sptg.estate.api.feigns.ApplyPayRefundFeign;
import com.sptg.pay.api.feigns.WxPayFeign;
import lombok.RequiredArgsConstructor;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.io.FileNotFoundException;
import java.util.Map;

/**
 * @author xuanyan
 * @date 2020/6/23 17:01
 */

@RestController
@RequestMapping("/estate/applyPayRefund")
@RequiredArgsConstructor
public class ApplyPayRefundController {

    private final ApplyPayRefundFeign applyPayRefundFeign;
    private final WxPayFeign wxPayFeign;

    public final static String WXPAY = "wxPay";

    /**
     * 功能描述:退款申请管理页面
     *
     * @return ModelAndView
     */
    @GetMapping("/index")
    @Security(token = EstateSecurityToken.ESTATE_APPLY_PAY_REFUND_MANAGE)
    public ModelAndView index() {
        return new ModelAndView("estate/apply-pay-refund-manage");
    }

    @GetMapping(value = "/showApplyPayRefundPage")
    public ResultBean<Page<ApplyPayRefund>> showApplyPayRefundPage(ApplyPayRefundDto dto, Page<ApplyPayRefund> page) {
        dto.setPage(page);
        return this.applyPayRefundFeign.showApplyPayRefundPage(dto);
    }

    /**
     * 查看详情
     * @param id
     * @return
     */
    @GetMapping("/detail/{id}")
    @Security(token = EstateSecurityToken.ESTATE_APPLY_PAY_REFUND_MANAGE,format = DataFormatEnum.VIEW)
    public ModelAndView detail(@PathVariable(value = "id") Long id) {
        ApplyPayRefund applyPayRefund = this.applyPayRefundFeign.findApplyPayRefundById(id);
        return new ModelAndView("estate/apply-pay-refund-detail").addObject("applyPayRefund",applyPayRefund);
    }

    /**
     * 审核申请
     * @param id
     * @return
     */
    @GetMapping("/check/{id}")
    @Security(token = EstateSecurityToken.ESTATE_APPLY_PAY_REFUND_MANAGE,format = DataFormatEnum.VIEW)
    public ModelAndView check(@PathVariable(value = "id") Long id) {
        ApplyPayRefund applyPayRefund = this.applyPayRefundFeign.findApplyPayRefundById(id);
        return new ModelAndView("estate/apply-pay-refund-check").addObject("applyPayRefund",applyPayRefund);
    }

    /**
     * 审核
     * @param applyPayRefundDto{
     *                         id               主键
     *                         checkStatus      审核状态：0：申请待审核，1：申请审核通过，2：申请审核拒绝
     *                          }
     * @return
     */
    @PostMapping("/updateCheckState")
    @Security(token = EstateSecurityToken.ESTATE_APPLY_PAY_REFUND_CHECK,format = DataFormatEnum.JSON)
    public ResultBean<Boolean> updateCheckState(ApplyPayRefundDto applyPayRefundDto) throws FileNotFoundException {
        Integer checkStatus = applyPayRefundDto.getCheckStatus();
        Assert.isTrue(checkStatus.equals(OperationConstant.ONE), "支付申请未通过，不能发起退款申请");
        ApplyPayRefund applyPayRefund = this.applyPayRefundFeign.findApplyPayRefundById(applyPayRefundDto.getId());
        if(WXPAY.equals(applyPayRefund.getPayType())){
            BeanMap beanMap = BeanMap.create(applyPayRefund);
            Map refundResult = wxPayFeign.refund(beanMap);
            String third_refund_apply = (String)refundResult.get("third_refund_apply");
            applyPayRefundDto.setThirdRefundApply(third_refund_apply);
            return this.applyPayRefundFeign.auditApplyPayRefund(applyPayRefundDto);
        }
        return new ResultBean<>(ResultEnum.SUCCESS, true);
    }
}
