package com.neusoft.smart.pos.payment.utils;

import com.neusoft.smart.pos.framework.utils.SpringUtils;
import com.neusoft.smart.pos.payment.persistent.domain.MchPaymentTradeLogWithBLOBs;
import com.neusoft.smart.pos.payment.persistent.domain.PaymentTradeLogWithBLOBs;
import com.neusoft.smart.pos.payment.persistent.mapper.MchPaymentTradeLogMapper;
import com.neusoft.smart.pos.payment.persistent.mapper.PaymentTradeLogMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 聚合支付日志存储工具类
 */
public class LogUtils {

    private final static Logger logger = LoggerFactory.getLogger(LogUtils.class);

    /**
     * 日志常量
     * 订单生成、订单查询、申请退款、支付成功回调、退款成功回调
     */
    public static final String LOG_DESCRIPTION_DDSC = "订单生成";
    public static final String LOG_DESCRIPTION_DDCS = "订单查询";
    public static final String LOG_DESCRIPTION_DDQX = "订单取消";
    public static final String LOG_DESCRIPTION_DDCX = "订单撤销";
    public static final String LOG_DESCRIPTION_SQTK = "申请退款";
    public static final String LOG_DESCRIPTION_SQZZF = "授权转支付";
    public static final String LOG_DESCRIPTION_ZFCGHD = "支付成功回调";
    public static final String LOG_DESCRIPTION_TKCGHD = "退款成功回调";
    public static final String LOG_DESCRIPTION_RLCSH = "人脸支付初始化";

    /**
     * 第三方支付平台请求操作日志记录
     * @param mcode：商户号
     * @param mOrderNumber：业务系统订单号
     * @param payType：支付方式
     * @param sendData：发送数据
     * @param receiveData：接收数据
     * @param requestType：请求类型
     */
    public static void savePaymentTradeLog(String mcode,String mOrderNumber,String payType,String description,String sendData,String receiveData,String requestType){
        try{
            PaymentTradeLogMapper paymentTradeLogMapper = SpringUtils.getBean(PaymentTradeLogMapper.class);
            PaymentTradeLogWithBLOBs record = new PaymentTradeLogWithBLOBs();
            record.setMcode(mcode);
            record.setmOrderNumber(mOrderNumber);
            record.setPayType(payType);
            record.setDescription(description);
            record.setSendData(sendData);
            record.setReceiveData(receiveData);
            record.setRequestType(requestType);
            paymentTradeLogMapper.insertSelective(record);
        }catch (Exception e){
            logger.error("订单号"+mOrderNumber+"订单第三方订单生成日志记录失败。");
        }
    }

    /**
     * 业务服务请求操作日志记录
     * @param mcode：商户号
     * @param mOrderNumber：业务系统订单号
     * @param payType：支付方式
     * @param description：描述(订单生成、订单查询、申请退款、支付成功回调、退款成功回调)
     * @param sendData：发送数据
     * @param receiveData：接收数据
     * @param requestType：请求类型
     */
    public static void saveMchPaymentTradeLog(String mcode,String mOrderNumber,String payType,String description,String sendData,String receiveData,String requestType){
        try{
            MchPaymentTradeLogMapper mchPaymentTradeLogMapper = SpringUtils.getBean(MchPaymentTradeLogMapper.class);
            MchPaymentTradeLogWithBLOBs record = new MchPaymentTradeLogWithBLOBs();
            record.setMcode(mcode);
            record.setmOrderNumber(mOrderNumber);
            record.setPayType(payType);
            record.setDescription(description);
            record.setSendData(sendData);
            record.setReceiveData(receiveData);
            record.setRequestType(requestType);
            mchPaymentTradeLogMapper.insertSelective(record);
        }catch (Exception e){
            logger.error("订单号"+mOrderNumber+"订单聚合平台订单生成日志记录失败。");
        }
    }
}
