package com.fs.his.service.impl;

import com.fs.common.exception.CustomException;
import com.fs.his.domain.*;
import com.fs.his.service.IFsPayService;
import com.fs.tzBankPay.TzBankService.TzBankServiceImpl.TzBankServiceImpl;
import com.fs.tzBankPay.doman.*;
import com.fs.tzBankPay.util.TzBankSendUtil;
import com.fs.ybPay.domain.CreateWxOrderResult;
import com.fs.ybPay.domain.OrderResult;
import com.fs.ybPay.dto.OrderQueryDTO;
import com.fs.ybPay.dto.RefundDTO;
import com.fs.ybPay.dto.WxJspayDTO;
import com.fs.ybPay.service.impl.PayServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class FsPayServiceImpl implements IFsPayService {
    @Autowired
    PayServiceImpl payService;
    @Autowired
    TzBankSendUtil tzBankSendUtil;
    @Override
    public FsPayCreateOrderResult createOrder(FsPayCreateOrder order) {

        FsPayCreateOrderResult result = new FsPayCreateOrderResult();

        if (order.getType().equals("yb")){
            WxJspayDTO p = new WxJspayDTO();
            p.setPayMoney(order.getPayMoney());
            p.setLowOrderId(order.getOrderNo());
            p.setBody(order.getGoodsInfo());
            p.setOpenId(order.getOpenId());
            p.setAttach("附加信息");
            CreateWxOrderResult wxOrder = payService.createWxOrder(p);
            if (!wxOrder.getStatus().equals(100)){
                throw new CustomException(wxOrder.getMessage());
            }
            result.setType("yb");
            result.setUpOrderId(wxOrder.getUpOrderId());
            result.setPay_info(wxOrder.getPay_info());
            result.setMessage(wxOrder.getMessage());

        }else if (order.getType().equals("tz")){
            TzBankServiceImpl tzBankService = new TzBankServiceImpl();
            PayCreateOrder o = new PayCreateOrder();
            o.setOrderNo(order.getOrderNo()); // 业务系统订单号
            o.setTrxAmt(Double.parseDouble(order.getPayMoney())); // 交易金额
            o.setBusinessCstNo(order.getBusinessCstNo()); // 业务平台客户号
            o.setPayerName(order.getPayerName());
            o.setGoodsInfo(order.getGoodsInfo()); // 订单信息
            TzBankResult<PayCreateOrderResult> tzOrder = tzBankService.createOrder(o);
            if (!tzOrder.getRetType().equals("S")){
                throw new CustomException(tzOrder.getRetMsg());
            }
            result.setType("tz");
            result.setUpOrderId(tzOrder.getBody().getOrderFlowNo());
            result.setTrxAmt(tzOrder.getBody().getTrxAmt());
            result.setPlatMerCstNo(tzOrder.getBody().getPlatMerCstNo());
            result.setUrl(tzOrder.getBody().getUrl());
            result.setOrderNo(tzOrder.getBody().getOrderNo());
            result.setMessage(tzOrder.getRetMsg());

        }else {
            throw new CustomException("支付类型错误");
        }
        return result;
    }

    @Override
    public FsQueryOrderResult queryOrder(FsPayQueryOrder payQueryOrder) {
        FsQueryOrderResult fsQueryOrderResult = new FsQueryOrderResult();
        if (payQueryOrder.getType().equals("yb")){
            PayServiceImpl payService = new PayServiceImpl();
            OrderQueryDTO o = new OrderQueryDTO();
            o.setUpOrderId(payQueryOrder.getUpOrderId());
            OrderResult order = payService.getOrder(o);
            if (!order.getStatus().equals("100")){
                throw new CustomException(order.getMessage());
            }
            fsQueryOrderResult.setPayType("16");
            fsQueryOrderResult.setBankTrxId(order.getBankTrxId());
            fsQueryOrderResult.setPayMoney(order.getPayMoney());
            fsQueryOrderResult.setBankOrderId(order.getBankOrderId());
            fsQueryOrderResult.setStatus(order.getStatus());
            fsQueryOrderResult.setState(order.getState());
        }else if (payQueryOrder.getType().equals("tz")){
            TzBankServiceImpl tzBankService = new TzBankServiceImpl();
            payQueryOrder queryOrder = new payQueryOrder();
            queryOrder.setOrderFlowNo(payQueryOrder.getUpOrderId());
            TzBankResult<QueryOrderResult> result = tzBankService.queryOrder(queryOrder);
            if (!result.getRetType().equals("S")){
                throw new CustomException(result.getRetMsg());
            }
            QueryOrderResult body = result.getBody();
            fsQueryOrderResult.setPayType(body.getPayType());
            fsQueryOrderResult.setStatus(body.getStatus());
        }else {
            throw new CustomException("支付类型错误");
        }
        return  fsQueryOrderResult;
    }

    @Override
    public FsRefundResult refund(FsRefundOrder order) {

        FsRefundResult fsRefundResult = new FsRefundResult();
        if (order.getType().equals("yb")){
            PayServiceImpl payService = new PayServiceImpl();
            RefundDTO refundDTO = new RefundDTO();
            refundDTO.setLowRefundNo(order.getRefundOrderNo());
            refundDTO.setUpOrderId(order.getUpOrderId());
            refundDTO.setRefundMoney(order.getRefundMoney());
            com.fs.ybPay.domain.RefundResult refund = payService.refund(refundDTO);
            if (!refund.getStatus().equals("100")&&(refund.getState().equals("5")||refund.getState().equals("6"))){
                throw new CustomException(refund.getMessage());
            }

            fsRefundResult.setMessage(refund.getMessage());
            fsRefundResult.setUpOrderId(refund.getUpOrderId());
            fsRefundResult.setLowRefundNo(refund.getLowRefundNo());
            fsRefundResult.setState(refund.getState());
        }else if (order.getType().equals("tz")){
            TzBankServiceImpl tzBankService = new TzBankServiceImpl();
            RefundParam tzBankResult = new RefundParam();
            tzBankResult.setOldPayOutOrderNo(order.getOldPayOutOrderNo());
            tzBankResult.setRefundOrderNo(order.getRefundOrderNo());
            tzBankResult.setRefundOrdTransAmt(Double.parseDouble(order.getRefundMoney()));
            TzBankResult<RefundResult> result = tzBankService.refund(tzBankResult);
            if (!result.getRetType().equals("S")){
                throw new CustomException(result.getRetMsg());
            }
            RefundResult body = result.getBody();
            fsRefundResult.setRefundOrderFlowNo(body.getRefundOrderFlowNo());
            fsRefundResult.setMessage(result.getRetMsg());
            fsRefundResult.setRefundOrderNo(body.getRefundOrderNo());
            fsRefundResult.setState("5");
        }else {
            throw new CustomException("支付类型错误");
        }
        return fsRefundResult;
    }
}
