package cn.pluss.platform.yinsheng;

import cn.pluss.platform.util.RSAUtils;
import cn.pluss.platform.util.SignUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.core.io.ByteArrayResource;

import java.util.UUID;

/**
 * @author djh
 * 上传图片的请求参数实体类
 */
@Data
@Slf4j
public class ReqUploadImg {

    private ReqUploadImg() {}

    public static ReqUploadImg newInstance(String picType) {
        val entity = new ReqUploadImg();
        entity.reqFlowId = (System.currentTimeMillis() + "").substring(0, 12);
        entity.sysFlowId = "12201908021735339318742";
        entity.picType = picType;

        return entity;
    }

    public static void sign(ReqUploadImg entity, String priKey) {
        entity.picFile = null;
        val json = (JSONObject) JSON.toJSON(entity);
        json.remove("reqFlowId");
        json.remove("picFile");
        val signContent = SignUtils.getYSSignContent(json);
        try {
            entity.signValue = RSAUtils.sign(priKey, signContent);
        } catch (Exception e) {
            log.warn("签名异常");
        }
    }

    /**
     * 流水号
     */
    private String reqFlowId;

    /**
     * 图片文件
     */
    private ByteArrayResource picFile;

    /**
     * 图片类型
     * {@link cn.pluss.platform.yinsheng.ImageType}
     */
    private String picType;

    /**
     * 流水号
     */
    private String sysFlowId;

    /**
     * 校验值
     */
    private String signValue;
}
