package io.renren.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;

import io.renren.common.constant.LLianPayConstant;
import io.renren.common.security.RSASign;
import io.renren.common.utils.DateUtils;
import io.renren.common.utils.HttpClientUtil;
import io.renren.common.vo.trans.TransferMorepyeeReq;
import io.renren.common.vo.trans.TransferMorepyeeResp;
import io.renren.common.vo.trans.TransferReq;
import io.renren.common.vo.trans.TransferResp;
import io.renren.service.TransService;
import io.renren.vo.TransferMorepyeeParams;
import io.renren.vo.TransferParams;
import io.renren.vo.TransferMorepyeeParams.OrderInfo;
import io.renren.vo.TransferMorepyeeParams.PayeeInfo;
import io.renren.vo.TransferMorepyeeParams.PayerInfo;

@Service
public class TransServiceImpl implements TransService {
	
	/**
	 * 代发交易用途。
	 *  服务费
	 *  信息费
	 *  修理费
	 *  佣金支付
	 *  贷款
	 *  其他
	 */
	private static final String TXN_PURPOSE = "信息费";
	
	@Autowired
	private LLianPayConstant constant;

	@Override
	public TransferMorepyeeResp transferMorepyee(TransferMorepyeeReq req) throws Exception {
		String timestamp = DateUtils.getTimestamp();
    	
    	TransferMorepyeeParams params = new TransferMorepyeeParams();
        params.setTimestamp(timestamp);
        params.setOid_partner(constant.getOidPartner());
        params.setNotify_url(constant.getNotifyUrl());
        
        OrderInfo orderInfo = params.new OrderInfo();
        orderInfo.setTxn_seqno(DateUtils.getCurrentTime());
        orderInfo.setTxn_time(timestamp);
        orderInfo.setTotal_amount(new BigDecimal(req.getAmount()));
        orderInfo.setTxn_purpose(TXN_PURPOSE);
        params.setOrderInfo(orderInfo);
        
        TransferMorepyeeReq.PayerInfo payer = req.getPayerInfo();
        
        PayerInfo payerInfo = params.new PayerInfo();
        payerInfo.setPayer_id(payer.getPayerId());
        payerInfo.setPassword(RSASign.getInstance().encrypt(payer.getPassword(),constant.getPublicKey()));
        payerInfo.setPayer_type(payer.getPayerType());
        payerInfo.setPayer_accttype(payer.getPayerAccttype());
        params.setPayerInfo(payerInfo);
        
        List<TransferMorepyeeReq.PayeeInfo> payeeList = req.getPayeeInfo();
        List<PayeeInfo> list = new ArrayList<>(payeeList.size());
        payeeList.forEach(payee->{
        	PayeeInfo payeeInfo = params.new PayeeInfo();
        	payeeInfo.setPayee_id(payee.getPayeeId());
            payeeInfo.setPayee_type(payee.getPayeeType());
            payeeInfo.setPayee_accttype(payee.getPayeeAccttype());
            payeeInfo.setPayee_amount(payee.getPayeeAmount());
            list.add(payeeInfo);
        });
        params.setPayeeInfo(list);

		
        String resultJsonStr = HttpClientUtil.sendRequest(constant.getTransferMorepyee(), JSON.toJSONString(params),constant.getPrivateKey(),constant.getPublicKey());
        TransferMorepyeeResp result = JSON.parseObject(resultJsonStr, TransferMorepyeeResp.class);
        if(!LLianPayConstant.RET_CODE.equals(result.getRetCode())) {
        	throw new Exception("调用连连【内部代发】接口失败："+result.getRetMsg());
        }
        return result;
	}

	@Override
	public TransferResp transfer(TransferReq req) throws Exception {
		String timestamp = DateUtils.getTimestamp();
    	
    	TransferParams params = new TransferParams();
        params.setTimestamp(timestamp);
        params.setOid_partner(constant.getOidPartner());
        params.setNotify_url(constant.getNotifyUrl());
        
        //订单信息
        TransferParams.OrderInfo orderInfo = params.new OrderInfo();
        orderInfo.setTxn_seqno(DateUtils.getCurrentTime());
        orderInfo.setTxn_time(timestamp);
        orderInfo.setTotal_amount(new BigDecimal(req.getAmount()));
        orderInfo.setFee_amount(new BigDecimal(req.getAmount()));
        orderInfo.setTxn_purpose(TXN_PURPOSE);
        orderInfo.setPostscript(req.getPostscript());
        params.setOrderInfo(orderInfo);
        
        //付款方信息
        TransferReq.PayerInfo payer = req.getPayerInfo();
        TransferParams.PayerInfo payerInfo = params.new PayerInfo();
        payerInfo.setPayer_type(payer.getPayerType());
        payerInfo.setPayer_id(payer.getPayerId());
        if(StringUtils.isNotBlank(payer.getPayerAccttype())) {
        	payerInfo.setPayer_accttype(payer.getPayerAccttype());
        }
        if(StringUtils.isNotBlank(payer.getPassword())) {
        	payerInfo.setPassword(RSASign.getInstance().encrypt(payer.getPassword(),constant.getPublicKey()));
        }
        if(StringUtils.isNotBlank(payer.getRandomKey())) {
        	payerInfo.setRandom_key(payer.getRandomKey());
        }
        if(StringUtils.isNotBlank(payer.getPapAgreeNo())) {
        	payerInfo.setPap_agree_no(RSASign.getInstance().encrypt(payer.getPapAgreeNo(),constant.getPublicKey()));
        }
        params.setPayerInfo(payerInfo);
        
        //收款方信息
        TransferReq.PayeeInfo payee = req.getPayeeInfo();
        TransferParams.PayeeInfo payeeInfo = params.new PayeeInfo();
        payeeInfo.setPayee_type(payee.getPayeeType());
        payeeInfo.setBank_acctno(payee.getBankAcctno());
        payeeInfo.setBank_acctname(payee.getBankAcctname());
        payeeInfo.setBank_code(payee.getBankCode());
        payeeInfo.setCnaps_code(payee.getCnapsCode());
        params.setPayeeInfo(payeeInfo);

        String resultJsonStr = HttpClientUtil.sendRequest(constant.getTransfer(), JSON.toJSONString(params),constant.getPrivateKey(),constant.getPublicKey());
        TransferResp result = JSON.parseObject(resultJsonStr, TransferResp.class);
        if(!LLianPayConstant.RET_CODE.equals(result.getRetCode())) {
        	throw new Exception("调用连连【外部代发】接口失败："+result.getRetMsg());
        }
        
        return result;
	}

	
	

}