package io.renren.service.impl;

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.utils.DateUtils;
import io.renren.common.utils.HttpClientUtil;
import io.renren.common.vo.acctserial.AcctserialDetailQueryResp;
import io.renren.common.vo.acctserial.AcctserialQueryPageReq;
import io.renren.common.vo.acctserial.AcctserialQueryPageResp;
import io.renren.service.AcctserialService;
import io.renren.vo.AcctserialDetailParams;
import io.renren.vo.AcctserialQueryPageParams;

@Service
public class AcctserialServiceImpl implements AcctserialService {
	/**
	 * INNERMERCHANT:商户 INNERUSER：个人用户 INNERCOMPANY：企业用户
	 */
	private final static String USER_TYPE = "INNERCOMPANY";
	
	@Autowired
	private LLianPayConstant constant;
	

	@Override
	public AcctserialQueryPageResp queryAcctserial(AcctserialQueryPageReq query) throws Exception {
		AcctserialQueryPageParams params = new AcctserialQueryPageParams();
        params.setTimestamp(DateUtils.getTimestamp());
        params.setOid_partner(constant.getOidPartner());
        params.setUser_id(query.getUserId());
        params.setUser_type(USER_TYPE);
        params.setAcct_type(query.getAcctType());
        params.setDate_start(query.getDateStart());
        params.setDate_end(query.getDateEnd());
        params.setPage_no(query.getPageNo());
        params.setPage_size(query.getPageSize());

        String resultJsonStr = HttpClientUtil.sendRequest(constant.getQueryAcctserial(), JSON.toJSONString(params),constant.getPrivateKey(),constant.getPublicKey());
        AcctserialQueryPageResp result = JSON.parseObject(resultJsonStr, AcctserialQueryPageResp.class);
        if(!LLianPayConstant.RET_CODE.equals(result.getRetCode())) {
        	throw new Exception("调用连连【资金流水列表查询】接口失败："+result.getRetMsg());
        }
        return result;
	}

	@Override
	public AcctserialDetailQueryResp queryAcctserialDetail(String userId,String jnoAcct) throws Exception {
		AcctserialDetailParams params = new AcctserialDetailParams();
	    params.setTimestamp(DateUtils.getTimestamp());
	    params.setOid_partner(constant.getOidPartner());
	    params.setUser_id(userId);
	    params.setUser_type(USER_TYPE);
	    params.setJno_acct(jnoAcct);

	    // 测试环境URL
	    String resultJsonStr = HttpClientUtil.sendRequest(constant.getQueryAcctserialdetail(), JSON.toJSONString(params),constant.getPrivateKey(),constant.getPublicKey());
	    AcctserialDetailQueryResp result = JSON.parseObject(resultJsonStr, AcctserialDetailQueryResp.class);
	    if(!LLianPayConstant.RET_CODE.equals(result.getRetCode())) {
        	throw new Exception("调用连连【资金流水列表查询】接口失败："+result.getRetMsg());
        }
        return result;
	}
	
	
	

}