package org.tio.sitexxx.service.service.pay.vo.ncount.base;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import org.tio.sitexxx.service.vo.Const;

import java.lang.reflect.Field;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <pre>
 *    @author : vagrancy
 *    e-mail : 18050829067@163.com
 *    time   : 2025-11-25 10:14
 *    desc   :
 * </pre>
 */
@Data
public abstract class NCountBaseVo {
    public static final String	VERSION			= "version";
    public static final String	TRAN_CODE		= "tranCode";
    public static final String	MER_ID			= "merId";
    public static final String	MER_ORDER_ID	= "merOrderId";
    public static final String	CHARSET			= "charset";
    public static final String	SIGN_TYPE		= "signType";
    public static final String	SIGN_VALUE		= "signValue";
    public static final String	MER_ATTACH		= "merAttach";
    public static final String	SUBMIT_TIME		= "submitTime";
    public static final String	MSG_CIPHER_TEXT	= "msgCiphertext";
    public static final String	RESULT_CODE		= "resultCode";
    public static final String	ERROR_CODE		= "errorCode";
    public static final String	ERROR_MSG		= "errorMsg";

    // 暂定公共的报文字段
    protected String	tranCode;
    protected String	version;
    protected String	merId;
    protected String	merOrderId;
    protected String	signType;
    protected String	signValue;
    protected String	msgCiphertext;
    protected String	charset;
    protected String	merAttach;
    protected String	submitTime;

    //提交地址
    protected String submitUrl;

    /**
     * 获取公共返回参数<br>
     * Map中的key必须与返回XML文件中的field的name值一致<br>
     * 子类可选择性重写
     * @return
     */
    @SuppressWarnings("static-access")
    public Map<String, Object> getCommonRespParams() {
        Map<String, Object> commonParams = new HashMap<String, Object>();
        commonParams.put(this.VERSION, this.version);
        commonParams.put(this.TRAN_CODE, this.tranCode);
        commonParams.put(this.MER_ORDER_ID, this.merOrderId);
        commonParams.put(this.MER_ID, this.merId);
        commonParams.put(this.MER_ATTACH, this.merAttach);
        commonParams.put(this.CHARSET, this.charset);
        commonParams.put(this.SIGN_TYPE, this.signType);
        return commonParams;
    }

    /**
     * 获取加密json串明文
     * @author wgy
     * @date 2018年8月27日 下午6:08:46
     * @description 一句话描述功能
     * @return String
     */
    public abstract String getEncryptJsonStr();

    /**
     * 获取验签字段
     * @return
     */
    public abstract String getVerifyJsonStr();

    /**
     * 获取提交字段
     * @return
     */
    public abstract String getSubmitJsonStr();

    public void initCommonParams(String tranCode, Integer uid) {
        this.version = "1.0";
        this.tranCode = tranCode;
        this.merId = Const.WALLET_MERCHANTID;
        this.merOrderId = tranCode + "_" + uid + "_" + DateUtil.format(new Date(), "yyyyMMddHHmmssSSS");
        this.submitTime = DateUtil.format(new Date(), "yyyyMMddHHmmss");
        this.signType = "1";
        this.merAttach = "";
        this.charset = "UTF-8";
    }

    public String getValueByReflet(Object model, String paraName) {
        // 返回值
        String value = "";
        try {
            // 获取属性值
            Field[] fields = model.getClass().getDeclaredFields();

            for (Field field : fields) {
                field.setAccessible(true);

                if (field.getName().equals(paraName)) {
                    value = (String) field.get(model);

                    break;
                }
            }
        } catch (Exception e) {

        }
        if (paraName.equals("userName")) {
            System.out.println(value);
        }
        return value;
    }

    public String getJsonStr(Object model, String[] strArr) {
        JSONObject json = new JSONObject(true);
        for (String file : strArr) {
            json.put(file, getValueByReflet(model, file));
        }
        return json.toJSONString();
    }
}

