package com.fs.store.service.channel;

import com.fs.common.core.domain.R;
import com.fs.common.exception.CustomException;
import com.fs.pay.pay.domain.CreateWxOrderResult;
import com.fs.pay.pay.domain.RefundResult;
import com.fs.pay.pay.dto.WxJspayDTO;
import com.fs.pay.pay.service.PayService;
import com.fs.store.domain.FsStorePayment;
import com.fs.store.service.channel.param.PayProcessContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.Date;

import static org.springframework.beans.factory.config.ConfigurableBeanFactory.SCOPE_PROTOTYPE;

/**
 * hf支付处理
 */
@Service
@Scope(value = SCOPE_PROTOTYPE)
@Slf4j
public class YbPaymentHandler extends PaymentHandler{
    @Autowired
    private PayService ybPayService;

    @Override
    protected R doCreateOrder(PayProcessContext context) {
        WxJspayDTO p = new WxJspayDTO();
        p.setPayMoney(fsStorePayment.getPayMoney().toString());
        p.setLowOrderId(context.getOrderPrefix()+context.getPayCode());
        p.setBody(context.getGoodsInfo());
        p.setIsMinipg("1");
        p.setReturnUrl(context.getFsPayConfig().getYbNotifyUrl());
        p.setOpenId(user.getMaOpenId());
        p.setAttach("");
        p.setStoreid("0");
        CreateWxOrderResult wxOrder = ybPayService.createWxOrder(p);
        log.info("yb支付：{}", wxOrder);
        if(wxOrder.getStatus().equals("100")){
            FsStorePayment ment=new FsStorePayment();
            ment.setPaymentId(context.getPaymentId());
            ment.setTradeNo(wxOrder.getUpOrderId());
            this.fsStorePaymentService.updateFsStorePayment(ment);
            return R.ok().put("payType",context.getPayType()).put("result",wxOrder.getPay_info());
        }
        else{
            throw new CustomException(String.format("支付出现异常: {}",wxOrder.getMessage()));
//            return R.error(wxOrder.getMessage());
        }
    }

    @Override
    protected void doRefundOrder(PayProcessContext context) {
        com.fs.pay.pay.dto.RefundDTO refundDTO = new com.fs.pay.pay.dto.RefundDTO();
        refundDTO.setRefundMoney(fsStorePayment.getPayMoney().toString());
        refundDTO.setLowRefundNo(context.getOrderPrefix()+fsStorePayment.getPayCode());
        refundDTO.setUpOrderId(fsStorePayment.getTradeNo());
        RefundResult result=ybPayService.refund(refundDTO);
        log.info("yb退款：{}",result);
        if(result.getState().equals("5")||result.getState().equals("6")){
            fsStorePayment.setRefundMoney(fsStorePayment.getPayMoney());
            fsStorePayment.setStatus(-1);
            fsStorePayment.setRefundTime(new Date());
            this.fsStorePaymentService.updateFsStorePayment(fsStorePayment);
        }
        else{
            throw new CustomException(result.getMessage());
        }
    }

    @Override
    public boolean support(String code) {
        return "yb".equals(code);
    }
}
