package cn.pluss.platform.entity;

import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @author DJH
 * 瑞银信商户入网相关接口数据参数
 */
@Data
public class RyxAccessModel {



    private String accessId;

    /**
     * 订单号，每次传不同值
     */
    private String accessMerchId;

    /**
     * 请求时间，时间格式为yyyyMMddHHmmss
     */
    private String reqTime;

    /**
     * 加密数据，此数据由AES(AES/CBC/PKCS5Padding)进行加密
     */
    private String info;

    /**
     * 签名信息，对info原始数据进行签名，签名使用我们的私钥
     */
    private String sign;

    /**
     * AES密钥进行加密
     */
    private String encryptKey;

    /**
     * 其他参数，可为空
     */
    private Object otherParam;

    /**
     * 商户资料图片
     */
    private List<ByteArrayResource> files;

//    private List<File> files;

    public static MultiValueMap<String, Object> toMap(RyxAccessModel model) {
        MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        map.put("accessId", Collections.singletonList(model.getAccessId()));
        map.put("accessMerchId", Collections.singletonList(model.getAccessMerchId()));
        map.put("reqTime", Collections.singletonList(model.getReqTime()));
        map.put("info", Collections.singletonList(model.getInfo()));
        map.put("sign", Collections.singletonList(model.getSign()));
        map.put("encryptKey", Collections.singletonList(model.getEncryptKey()));

        if (model.getOtherParam() != null) {
            map.put("otherParam", Collections.singletonList(model.getOtherParam()));
        }

        if (model.getFiles() != null) {
            List<Object> files = new ArrayList<>(model.getFiles());
            map.put("files", files);
        }

        return map;
    }

    public void addFile(ByteArrayResource file) {
        if (files == null) {
            files = new ArrayList<>();
        }
        files.add(file);
    }
}
