package com.yihu.jw.order.pay.ylz;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yihu.jw.entity.ylzinfo.OauthYlzConfigDO;
import com.yihu.jw.hospital.dict.WlyyHospitalSysDictDao;
import com.yihu.jw.hospital.prescription.dao.OauthYlzConfigDao;
import com.yihu.jw.order.pay.utils.PayLogService;
import com.yihu.jw.util.common.QrcodeUtil;
import com.yihu.jw.utils.ByteToInputStream;
import com.ylzinfo.onepay.sdk.HisOnepayClient;
import com.ylzinfo.onepay.sdk.OnepayClient;
import com.ylzinfo.onepay.sdk.OnepayDefaultClient;
import com.ylzinfo.onepay.sdk.domain.RequestParams;
import com.ylzinfo.onepay.sdk.domain.ResponseParams;
import com.ylzinfo.onepay.sdk.domain.ext.*;
import com.ylzinfo.onepay.sdk.utils.DateUtil;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by wangzhinan on 2020/5/9.
 */
@Service
public class YlzPayService {

    @Autowired
    private OauthYlzConfigDao oauthYlzConfigDao;

    @Autowired
    private PayLogService logService;
    @Autowired
    private WlyyHospitalSysDictDao hospitalSysDictDao;


    /**
     private String termNo;
     private String operatorId; 操作人员id
     private String operatorName;操作人员型
     private String cardType;卡类型 ---01 社保卡 06 临时卡
     private String cardNo;---卡号
     private String accountType;---帐户类型
     private String idNo;--身份证号
     private String depositType;---充值方式WX_MIN 微信小程序 WX_PUB 微信公众号 ALI_WAP 支付宝生活号
     private String rechargeAmount;--单位：元充值金额(自费金额)
     private String consumeAmount;---消费金额
     private String outTradeNo;---商户扣费流水号
     private String tradeKind;----消费种类门诊自费结算 长处方自费结算 互联网医院自费结算
     private String hisCustId;---HIS 患者唯一 ID若无，请传卡号
     private String rechargeHis;---是否往 HIS 充值0:不往 HIS 充值 1:往 HIS 充值 默认为 0
     private String userName;----用户姓名
     private String notes;---备注
     openId---openid
     * @return
     */
    public String rechargeConsume(String operatorId,String operatorName,String cardType,String cardNo,String accountType,String idNo,String depositType,String rechargeAmount,
                                  String consumeAmount,String outTradeNo,String hisCustId,String rechargeHis,String userName,String notes,String openid,String returnUrl,String notifyUrl,String wxMinId){
        String object  = null;
        Boolean isSuccess = true;
        String error = null;
        OauthYlzConfigDO oauthYlzConfigDO = oauthYlzConfigDao.findById("ylz_pay_config");
        if(oauthYlzConfigDO==null){
            return "未找到支付配置文件";
        }
        String appId = oauthYlzConfigDO.getAppId();
        String appSecret = oauthYlzConfigDO.getAppKey();
        String onepayUrl = oauthYlzConfigDO.getUrl();
        String signType=oauthYlzConfigDO.getSignType();
        String encryptType = oauthYlzConfigDO.getEncType();
        HisOnepayClient onepayClient = new HisOnepayClient(onepayUrl, appId, appSecret, signType, encryptType);
        RechargeConsumeParams param = new RechargeConsumeParams();
        try {
            param.setRechargeHis(rechargeHis);//HIS 充值
            param.setAccountType(accountType);
            param.setOutTradeNo(outTradeNo);
            param.setCardNo(cardNo);
            param.setCardType(cardType);
            param.setDepositType(depositType);
            param.setRechargeAmount(rechargeAmount);
            param.setConsumeAmount(consumeAmount);
            param.setIdNo(idNo);
            param.setHisCustId(hisCustId);
            param.setUserName(userName);
            param.setTermNo("123456");
            param.setNotes(notes);
            param.setOperatorId(operatorId);
            param.setOperatorName(operatorName);
            Map<String,String> extra = new HashMap<String,String>();
            extra.put("openId", openid);
            extra.put("returnUrl",returnUrl);
            extra.put("notifyUrl",notifyUrl);
            if (StringUtils.isNoneBlank(wxMinId)){
                extra.put("wxMinId",wxMinId);
            }
            param.setExtra(extra);
            ResponseParams<RechargeConsumeResult> respnse = onepayClient.rechargeConsume(param);
            if (respnse.getRespCode().equals("000000")){
                object = JSON.toJSONString(respnse);
            }else {
                isSuccess = false;
                object = JSON.toJSONString(respnse);
                error = "请求失败,返回结果:" + respnse.getRespCode() + "," + respnse.getRespMsg();
            }
        } catch (Exception e) {
            e.printStackTrace();
            isSuccess = false;
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            e.printStackTrace(pw);
            error = sw.toString();
        }
        logService.saveHttpLog(isSuccess, "hop.trade.recharge", "互联网医院充值", "POST", null,JSON.toJSONString(param), object, error,logService.onepayType);
        return object;
    }


    /**
     * termNo 终端编号 String(50) Y
     * operatorId 操作员编号 String(30) N
     * operatorName 操作员姓名 String(50) N
     * cardType 卡类型 String(2) N
     * cardNo 卡号 String(32) N
     * accountType 帐户类型 String(2) N 01:门诊,02:住院
     * amount 退款金额 String(12) N
     * withdrawMode 退款方式 String(1) N 0 现金退回 1 原路退回 2 银企转账 3 医保自费退回
     * outChargeNo 商户原充值订单号 String(30) N 二选一,两者同时存在 时,
     * traceNo>outCharge No,两者不能同时为空
     * traceNo 平台交易跟踪号(调 用平台充值接口后返 回的 traceNo) String(30)
     * outRefundNo 商户退款订单号 String(30) N
     * idNo 身份证号 String(32) Y
     * hisCustId HIS 系统唯一性 患者 String(30) N 没有则传卡号
     ID userName 用户姓名 String(30) Y extra 扩展参数 JSON Y checkCash String 当 withdrawMode=0 时，checkCash=1 校验 现金充值流水号及可退 金额是否合法，放空或 其他值不校验

     * @return
     */
    public String refund(String hisCustId,String operatorId,String operatorName,String cardType,String cardNo,String accountType,String amount,
                         String withdrawMode,String outChargeNo,String outRefundNo,String idNo){
        String object  = null;
        Boolean isSuccess = true;
        String error = null;
        OauthYlzConfigDO oauthYlzConfigDO = oauthYlzConfigDao.findById("ylz_pay_config");
        if(oauthYlzConfigDO==null){
            return "未找到支付配置文件";
        }
        String appId = oauthYlzConfigDO.getAppId();
        String appSecret = oauthYlzConfigDO.getAppKey();
        String onepayUrl = oauthYlzConfigDO.getUrl();
        String signType=oauthYlzConfigDO.getSignType();
        String encryptType = oauthYlzConfigDO.getEncType();
        HisOnepayClient onepayClient = new HisOnepayClient(onepayUrl, appId, appSecret, signType, encryptType);
        RefundParams param = new RefundParams();
        try {
            param.setTermNo("123456");
            param.setHisCustId(hisCustId);
            param.setOperatorId(operatorId);
            param.setOperatorName(operatorName);
            param.setCardType(cardType);
            param.setCardNo(cardNo);
            param.setAccountType(accountType);
            param.setAmount(amount);
            param.setWithdrawMode(withdrawMode);
            param.setOutChargeNo(outChargeNo);
            param.setOutRefundNo(outRefundNo);
            param.setIdNo(idNo);
            ResponseParams<RefundResult> response = onepayClient.refund(param);
            if (response.getRespCode().equals("000000")){
                object = JSON.toJSONString(response);
            }else {
                isSuccess = false;
                object = JSON.toJSONString(response);
                error = "请求失败,返回结果:" + response.getRespCode() + "," + response.getRespMsg();
            }
        } catch (Exception e) {
            e.printStackTrace();
            isSuccess = false;
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            e.printStackTrace(pw);
            error = sw.toString();
        }
        logService.saveHttpLog(isSuccess, "hop.trade.refund", "互联网医院退款", "POST", null,JSON.toJSONString(param), object, error,logService.onepayType);
        return object;
    }


    public String tradeQuery(String termNo,String operatorId,String operatorName,String outTradeNo,String tradeType,String notes){
        String object  = null;
        Boolean isSuccess = true;
        String error = null;
        OauthYlzConfigDO oauthYlzConfigDO = oauthYlzConfigDao.findById("ylz_pay_config");
        if(oauthYlzConfigDO==null){
            return "未找到支付配置文件";
        }
        String appId = oauthYlzConfigDO.getAppId();
        String appSecret = oauthYlzConfigDO.getAppKey();
        String onepayUrl = oauthYlzConfigDO.getUrl();
        String signType=oauthYlzConfigDO.getSignType();
        String encryptType = oauthYlzConfigDO.getEncType();
        HisOnepayClient onepayClient = new HisOnepayClient(onepayUrl, appId, appSecret, signType, encryptType);
        TradeQueryParams param = new TradeQueryParams();
        try {
            param.setTermNo("123456");
            param.setOperatorId("123456");
            param.setOperatorName(operatorName);
            param.setTradeType(tradeType);
            param.setOutTradeNo(outTradeNo);
            param.setNotes(notes);
            ResponseParams<TradeQueryResult> response = onepayClient.tradeQuery(param);
            if (response.getRespCode().equals("000000")){
                object = JSON.toJSONString(response);
            }else {
                isSuccess = false;
                object = JSON.toJSONString(response);
                error = "请求失败,返回结果:" + response.getRespCode() + "," + response.getRespMsg();
            }
        } catch (Exception e) {
            e.printStackTrace();
            isSuccess = false;
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            e.printStackTrace(pw);
            error = sw.toString();
        }
        logService.saveHttpLog(isSuccess, "hop.trade.tradeQuery", "互联网医院查询订单", "POST", null,JSON.toJSONString(param), object, error,logService.onepayType);
        return object;
    }


    /**
     * 统一平台推送消息
     * @param cardType
     * @param cardNo
     * @param accountType
     * @param contactNumber
     * @param feeType
     * @param userNo
     * @param userName
     * @param idNo
     * @param voucherNo
     * @param applyDepaName
     * @param applyDoctorName
     * @param recipeTime
     * @param fee
     * @param recipeNum
     * @param pushChannel
     * @return
     */
    public String msgPush(String cardType,String cardNo,String accountType,String contactNumber,String feeType,String userNo,String userName,String idNo,String voucherNo,
                          String applyDepaName,String applyDoctorName,String recipeTime,String fee,String recipeNum,String pushChannel){
        String object  = null;
        Boolean isSuccess = true;
        String error = null;
        OauthYlzConfigDO oauthYlzConfigDO = oauthYlzConfigDao.findById("ylz_balance_config");
        if(oauthYlzConfigDO==null){
            return "未找到支付配置文件";
        }
        String appId = oauthYlzConfigDO.getAppId();
        String appSecret = oauthYlzConfigDO.getAppKey();
        String onepayUrl = oauthYlzConfigDO.getUrl();
        String signType=oauthYlzConfigDO.getSignType();
        String encryptType = oauthYlzConfigDO.getEncType();
        HisOnepayClient onepayClient = new HisOnepayClient(onepayUrl, appId, appSecret, signType, encryptType);
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("cardType",cardType);//卡类型01 社保卡 06 临时卡
            jsonObject.put("cardNo",cardNo);//卡号
            jsonObject.put("accountType",accountType);//帐户类型
            jsonObject.put("contactNumber",contactNumber);//手机号码
            jsonObject.put("feeType",feeType);//费用类型(病人身份)
            jsonObject.put("userNo",userNo);//用户编号
            jsonObject.put("userName",userName);//用户姓名
            jsonObject.put("idNo",idNo);//身份证号
            jsonObject.put("voucherNo",voucherNo);//单据号
            jsonObject.put("applyDepaName",applyDepaName);//申请科室名称
            jsonObject.put("applyDoctorName",applyDoctorName);//申请医生姓名
            jsonObject.put("recipeTime",recipeTime);//处方时间yyyyMMddHHmmss
            if (StringUtils.isNoneBlank(pushChannel)){
                jsonObject.put("pushChannel",pushChannel);//推送渠道
            }else {
                jsonObject.put("pushChannel","WX_PUB");//推送渠道
            }

            if (StringUtils.isNoneBlank(fee)){
                Double free = Double.parseDouble(fee);
                jsonObject.put("fee",free);//总费用
            }
            if (StringUtils.isNoneBlank(recipeNum)){
                jsonObject.put("recipeNum",recipeNum);//处方数
            }
            System.out.println("推送模板消息参数"+jsonObject.toJSONString());
            //jsonObject.put("extra")
            ResponseParams<MmpPushResult> response = onepayClient.mmpMsgPush(jsonObject);
            if (response.getRespCode().equals("000000")){
                object = JSON.toJSONString(response);
            }else {
                isSuccess = false;
                object = JSON.toJSONString(response);
                error = "请求失败,返回结果:" + response.getRespCode() + "," + response.getRespMsg();
            }
        } catch (Exception e) {
            e.printStackTrace();
            isSuccess = false;
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            e.printStackTrace(pw);
            error = sw.toString();
        }
        logService.saveHttpLog(isSuccess, "hop.mmpay.msg.push", "互联网医院消息推送", "POST", null,JSON.toJSONString(jsonObject), object, error,logService.pushType);
        return object;
    }


    /**
     * 生成门诊结算url
     * @param accId
     * @param openId
     * @param cardNo
     * @param channel
     * @return
     */
    public String createSicardPayUrl(String accId,String openId,String cardNo,String channel){
        String object  = null;
        Boolean isSuccess = true;
        String error = null;
        OauthYlzConfigDO oauthYlzConfigDO = oauthYlzConfigDao.findById("ylz_balance_config");
        if(oauthYlzConfigDO==null){
            return "未找到支付配置文件";
        }
        String appId = oauthYlzConfigDO.getAppId();
        String appSecret = oauthYlzConfigDO.getAppKey();
        String onepayUrl = oauthYlzConfigDO.getUrl();
        String signType=oauthYlzConfigDO.getSignType();
        String encryptType = oauthYlzConfigDO.getEncType();
        OnepayClient onepayClient = new OnepayClient(onepayUrl, appId, appSecret, signType, encryptType);
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("accId", accId);// 第三方应用原始ID微信公众号(支付宝)原始ID市民卡APP商户(医院)Id平安APP商户(医院)Id银联商户(医院)Id如：gh_54ec946bd3cd
            jsonObject.put("openId", openId);// openid
            jsonObject.put("cardNo",cardNo);//就诊卡或社保卡
            jsonObject.put("channel", channel);//WX: 微信 (默认)ALI: 支付宝PA: 平安SMK:市民卡APP
            //jsonObject.put("extra")
            ResponseParams<JSONObject> response = onepayClient.createSicardPayUrl(jsonObject);
            if (response.getRespCode().equals("000000")){
                JSONObject object1 = response.getParam();
                String url = object1.getString("payUrl");
                InputStream qrcode = QrcodeUtil.createQrcode(url,300,"jpg");
                ByteToInputStream byteToInputStream = new ByteToInputStream();
                String QRCodeImg = byteToInputStream.getBase64FromInputStream(qrcode);
                object1.put("qrcodeUrl","data:image/png;base64,"+ QRCodeImg);
                response.setParam(object1);
                object = JSON.toJSONString(response);
            }else {
                isSuccess = false;
                object = JSON.toJSONString(response);
                error = "请求失败,返回结果:" + response.getRespCode() + "," + response.getRespMsg();
            }
        } catch (Exception e) {
            e.printStackTrace();
            isSuccess = false;
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            e.printStackTrace(pw);
            error = sw.toString();
        }
        logService.saveHttpLog(isSuccess, "hop.mmpay.sicard.pay.url", "互联网医院支付地址", "POST", null,JSON.toJSONString(jsonObject), object, error,logService.pushType);
        return object;
    }

//============================================预交金充值 start======================================================
    /**
     * 预交金充值支付
     *
     * @param operatorId 操作员编码
     * @param operatorName  操作员姓名
     * @param cardType 卡类型
     * @param accountType 账户类型
     * @param cardNo 卡号
     * @param idNo 身份证号
     * @param depositType 充值方式 WX_PUB
     * @param amount 充值金额
     * @param outChargeNo 商户充值业务流水号
     * @param userName 用户姓名
     * @param openId 微信公共号支付需入参openId
     * @return
     */
    public String recharge(String operatorId,String operatorName,String cardType,String accountType,String cardNo,String idNo,String depositType,String amount,String outChargeNo,String openId,String userName,String notifyUrl){
        Boolean isSuccess = true;
        String jsonObject = null;
        RechargeParams param = new RechargeParams();
        String error = null;
        try {
            OauthYlzConfigDO oauthYlzConfigDO = oauthYlzConfigDao.findById("ylz_pay_config");
            if(oauthYlzConfigDO==null){
                return "未找到支付配置文件";
            }
            String appId = oauthYlzConfigDO.getAppId();
            String appSecret = oauthYlzConfigDO.getAppKey();
            String onepayUrl = oauthYlzConfigDO.getUrl();
            String signType=oauthYlzConfigDO.getSignType();
            String encryptType = oauthYlzConfigDO.getEncType();
            HisOnepayClient onepayClient = new HisOnepayClient(onepayUrl, appId, appSecret, signType, encryptType);
            param.setOperatorId(operatorId);
            param.setOperatorName(operatorName);
            param.setCardType(cardType);
            param.setAccountType(accountType);
            param.setCardNo(cardNo);
            param.setIdNo(idNo);
            param.setDepositType(depositType);
            param.setAmount(amount);
            param.setOutChargeNo(outChargeNo);
            param.setUserName(userName);
            Map<String, String> extra = new HashedMap();
            if(StringUtils.isNoneBlank(openId)){
                extra.put("openId",openId);
            }


		/*	String value = systemDictDao.findByDictNameAndCode("RETURN_URL","RETURN_URL");
			extra.put("returnUrl",value);*/
            extra.put("notifyUrl", notifyUrl);
            if (depositType.equalsIgnoreCase("WX_WAP")){
                extra.put("spbill_create_ip",getServerIp());
                extra.put("returnUrl","https://www1.yanketong.com");
            }
            param.setExtra(extra);
            ResponseParams<RechargeResult> respnse = onepayClient.recharge(param);
            if (respnse.getRespCode().equals("000000")){
                jsonObject = JSON.toJSONString(respnse);
            }else {
                isSuccess = false;
                jsonObject = JSON.toJSONString(respnse);
                error = "请求失败,返回结果:" + respnse.getRespCode() + "," + respnse.getRespMsg();
            }
            if (respnse!=null){

            }
        } catch (Exception e) {
            isSuccess = false;
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            e.printStackTrace(pw);
            error = sw.toString();
        }

        logService.saveHttpLog(isSuccess, "hop.trade.recharge", "互联网预交金充值", "POST", null,JSON.toJSONString(param), jsonObject, error,logService.shoppatType);
        return jsonObject;
    }

    /**
     * 获取服务器ip地址
     *
     * @return
     * @throws UnknownHostException
     */
    public String getServerIp() throws UnknownHostException {
        InetAddress address = InetAddress.getLocalHost();//获取的是本地的IP地址 //PC-20140317PXKX/192.168.0.121
        String hostAddress = address.getHostAddress();//192.168.0.121
        return hostAddress;
    }
    /**
     *
     * 预交金退款
     *
     * @param operatorId 操作者id
     * @param operatorName 操作者名字
     * @param cardType 卡类型
     * @param accountType 账号类型
     * @param cardNo 卡号
     * @param idNo 身份证号
     * @param amount 退款金额
     * @param outChargeNo 原充值订单号
     * @param userName 用户名字
     * @param withdrawMode 退款方式         0 现金退回1 原路退回2 银企转账3 医保自费退回
     * @param traceNo 平台交易跟踪号
     * @param outRefundNo 退款订单号
     * @return
     */
    public String refund(String operatorId,String operatorName,String cardType,String accountType,String cardNo,String idNo,String amount,String outChargeNo,String userName,String withdrawMode,String traceNo,String outRefundNo,String hisCustId){
        Boolean isSuccess = true;
        String jsonObject = null;
        RefundParams param = new RefundParams();
        String error = null;
        try {
            OauthYlzConfigDO oauthYlzConfigDO = oauthYlzConfigDao.findById("ylz_pay_config");
            if(oauthYlzConfigDO==null){
                return "未找到支付配置文件";
            }
            String appId = oauthYlzConfigDO.getAppId();
            String appSecret = oauthYlzConfigDO.getAppKey();
            String onepayUrl = oauthYlzConfigDO.getUrl();
            String signType=oauthYlzConfigDO.getSignType();
            String encryptType = oauthYlzConfigDO.getEncType();
            HisOnepayClient onepayClient = new HisOnepayClient(onepayUrl, appId, appSecret, signType, encryptType);
            param.setOperatorId(operatorId);
            param.setOperatorName(operatorName);
            param.setOutChargeNo(outChargeNo);
            param.setCardType(cardType);
            param.setCardNo(cardNo);
            param.setAccountType(accountType);
            param.setAmount(amount);
            param.setWithdrawMode(withdrawMode);
            param.setOutChargeNo(outChargeNo);
            param.setTraceNo(traceNo);
            param.setOutRefundNo(outRefundNo);
            param.setHisCustId(hisCustId);
            param.setIdNo(idNo);
            param.setUserName(userName);
            ResponseParams<RefundResult> respnse = onepayClient.refund(param);
            if (respnse.getRespCode().equals("000000")){
                jsonObject = JSON.toJSONString(respnse);
            }else {
                jsonObject = JSON.toJSONString(respnse);
                isSuccess = false;
                error = "请求失败,返回结果:" + respnse.getRespCode() + "," + respnse.getRespMsg();
            }
        } catch (Exception e) {
            isSuccess = false;
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            e.printStackTrace(pw);
            error = sw.toString();
        }
        logService.saveHttpLog(isSuccess, "hop.trade.refund", "互联网预交金退款", "POST", null,JSON.toJSONString(param), jsonObject, error, logService.shoppatType);
        return jsonObject;
    }


    /**
     * 预交金查询余额
     *
     * @param operatorId
     * @param operatorName
     * @param termNo
     * @param tradeType
     * @param outTradeNo
     * @param traceNo
     * @return
     */
    public String ylzTradeQuery(String operatorId,String operatorName,String termNo,String tradeType,String outTradeNo,String traceNo){
        Boolean isSuccess = true;
        String jsonObject = null;
        TradeQueryParams param = new TradeQueryParams();
        String error = null;
        try {
            OauthYlzConfigDO oauthYlzConfigDO = oauthYlzConfigDao.findById("ylz_pay_config");
            if(oauthYlzConfigDO==null){
                return "未找到支付配置文件";
            }
            String appId = oauthYlzConfigDO.getAppId();
            String appSecret = oauthYlzConfigDO.getAppKey();
            String onepayUrl = oauthYlzConfigDO.getUrl();
            String signType=oauthYlzConfigDO.getSignType();
            String encryptType = oauthYlzConfigDO.getEncType();
            HisOnepayClient onepayClient = new HisOnepayClient(onepayUrl, appId, appSecret, signType, encryptType);
            param.setOperatorId(operatorId);
            param.setOperatorName(operatorName);
            param.setTradeType(tradeType);
            param.setOutTradeNo(outTradeNo);
            param.setTraceNo(traceNo);
            ResponseParams<TradeQueryResult> respnse = onepayClient.tradeQuery(param);
            if (respnse.getRespCode().equals("000000")){
                jsonObject = JSON.toJSONString(respnse);
            }else {
                isSuccess = false;
                jsonObject = JSON.toJSONString(respnse);
                error = "请求失败,返回结果:" + respnse.getRespCode() + "," + respnse.getRespMsg();
            }
        } catch (Exception e) {
            isSuccess = false;
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            e.printStackTrace(pw);
            error = sw.toString();
        }
        logService.saveHttpLog(isSuccess, "hop.trade.query", "互联网预交金查询", "POST", null,JSON.toJSONString(param), jsonObject, error,logService.shoppatType);
        return jsonObject;
    }

//============================================预交金充值 end======================================================


    /**
     * 查看线上结算记录
     *
     * @param cardNo 卡号，多个按逗号隔开
     * @param beginDate 开始时间（可空） yyyyMMdd:20200818
     * @param endDate 结束时间（可空） yyyyMMdd:20200818
     * @param queryType online:（不为空）记录明细一起返回，下面两个参数的合体；onlineRecord：查询结算记录 onlineRecordDetail：查询结算明细
     * @param billSn    当 queryType = onlineRecordDetail billsn和traceno必有值
     * @param traceNo
     * @param pageNo
     * @param pageSize
     * @return
     */
    public String querySettledRecord(String cardNo,String beginDate,String endDate,String queryType,String billSn,String traceNo,String pageNo,String pageSize ){
        Boolean isSuccess = true;
        String jsonObject = null;
        String error = null;
        RequestParams requestParams = new RequestParams();
        try {
        OauthYlzConfigDO oauthYlzConfigDO = oauthYlzConfigDao.findById("ylz_balance_config");
        if(oauthYlzConfigDO==null){
            return "未找到支付配置文件";
        }
        String appId = oauthYlzConfigDO.getAppId();
        String appSecret = oauthYlzConfigDO.getAppKey();
        String onepayUrl = oauthYlzConfigDO.getUrl();
        String signType=oauthYlzConfigDO.getSignType();
        String encryptType = oauthYlzConfigDO.getEncType();
        requestParams.setAppId(appId);
        requestParams.setTimestamp(DateUtil.getCurrentDateTime());
        requestParams.setSignType(signType);
        requestParams.setEncryptType(encryptType);
        requestParams.setTimestamp(DateUtil.getCurrentDateTime());
        if (StringUtils.isNoneBlank(pageNo)&&StringUtils.isNoneBlank(pageSize)){
            JSONObject pageParam = new JSONObject();
            pageParam.put("pageNo",pageNo);
            pageParam.put("pageSize",pageSize);
            requestParams.setPageParam(pageParam);
        }
        JSONObject params = new JSONObject();
        params.put("cardNo",cardNo);
        params.put("queryType",queryType);
        if (StringUtils.isNotBlank(beginDate)){
            params.put("beginDate",beginDate);
        }
        if (StringUtils.isNotBlank(endDate)){
            params.put("endDate",endDate);
        }
        if (StringUtils.isNotBlank(billSn)){
            params.put("billSn",billSn);
        }
        if (StringUtils.isNotBlank(traceNo)){
            params.put("traceNo",traceNo);
        }
        requestParams.setParam(params);
        requestParams.setTransType("hop.query.settle");
        OnepayDefaultClient onepayClient = new OnepayDefaultClient(onepayUrl, appId, appSecret, signType, encryptType);
        ResponseParams<JSONObject> respnse = onepayClient.execute(requestParams);
        if (respnse.getRespCode().equals("000000")){
            jsonObject = JSON.toJSONString(respnse);
            System.out.println(jsonObject);
        }else {
            isSuccess = false;
            jsonObject = JSON.toJSONString(respnse);
            error = "请求失败,返回结果:" + respnse.getRespCode() + "," + respnse.getRespMsg();
        }
        } catch (Exception e) {
            e.printStackTrace();
            isSuccess = false;
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            e.printStackTrace(pw);
            error = sw.toString();
        }
        logService.saveHttpLog(isSuccess, "hop.trade.query", "查看线上结算记录", "POST", null,JSON.toJSONString(requestParams), jsonObject, error,logService.shoppatType);
        return jsonObject;
    }

}
