package com.fs.store.service.channel;

import com.fs.common.core.domain.R;
import com.fs.common.exception.CustomException;
import com.fs.huifuPay.domain.HuiFuCreateOrder;
import com.fs.huifuPay.domain.HuiFuRefundResult;
import com.fs.huifuPay.domain.HuifuCreateOrderResult;
import com.fs.huifuPay.sdk.opps.core.request.V2TradePaymentScanpayRefundRequest;
import com.fs.huifuPay.service.HuiFuService;
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.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

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

/**
 * hf支付处理
 */
@Service
@Scope(value = SCOPE_PROTOTYPE)
@Slf4j
public class HfPaymentHandler extends PaymentHandler{
    @Autowired
    protected HuiFuService huiFuService;

    @Override
    protected R doCreateOrder(PayProcessContext context) {
        HuiFuCreateOrder o = new HuiFuCreateOrder();
        o.setTradeType("T_MINIAPP");
        o.setOpenid(user.getMaOpenId());
        o.setReqSeqId(context.getOrderPrefix()+ this.fsStorePayment.getPayCode());
        o.setTransAmt(this.fsStorePayment.getPayMoney().toString());
        o.setGoodsDesc(context.getGoodsInfo());
        HuifuCreateOrderResult result = huiFuService.createOrder(o);
        //创建订单
        if(result.getResp_code()!=null&&(result.getResp_code().equals("00000000")||result.getResp_code().equals("00000100"))){
            FsStorePayment mt=new FsStorePayment();
            mt.setPaymentId(context.getPaymentId());
            mt.setTradeNo(result.getHf_seq_id());
            this.fsStorePaymentService.updateFsStorePayment(mt);
            return R.ok().put("payType", context.getPayType()).put("result", result.getPay_info());
        }
        else{
            return R.error(result.getResp_desc());
        }
    }

    @Override
    protected void doRefundOrder(PayProcessContext context) {
        V2TradePaymentScanpayRefundRequest request = new V2TradePaymentScanpayRefundRequest();
        request.setOrdAmt(this.fsStorePayment.getPayMoney().toString());
        request.setOrgReqDate(new SimpleDateFormat("yyyyMMdd").format(fsStorePayment.getCreateTime()));
        request.setReqSeqId(context.getOrderPrefix()+fsStorePayment.getPayCode());
        Map<String, Object> extendInfoMap = new HashMap<>();
        extendInfoMap.put("org_party_order_id", fsStorePayment.getBankSerialNo());
        request.setExtendInfo(extendInfoMap);
        HuiFuRefundResult refund = huiFuService.refund(request);
        log.info("退款：{}",refund);
        if((refund.getResp_code().equals("00000000")||refund.getResp_code().equals("00000100"))&&(refund.getTrans_stat().equals("S")||refund.getTrans_stat().equals("P"))){
            fsStorePayment.setRefundMoney(fsStorePayment.getPayMoney());
            fsStorePayment.setStatus(-1);
            fsStorePayment.setRefundTime(new Date());
            this.fsStorePaymentService.updateFsStorePayment(fsStorePayment);
        }else {
            throw new CustomException(refund.getResp_desc());
        }
    }

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