package com.ruoyi.api.labour.domain;

import com.ruoyi.common.enums.HttpStatusEnum;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.AuthRsaUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.sign.Md5Utils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

/**
 * 劳务人员签名推送信息 请求参数
 *
 * @author: JiangYuQi
 * @date: 2024/01/13 12:21
 */
@Data
@Slf4j
public class LabourSignetVo {

    /**
     * 签名
     * RSA(dataMd5 + 时间戳)加密
     */
    @NotBlank(message = "签名不能为空")
    private String sign;

    /**
     * 推送数据 明文参数
     */
    @NotBlank(message = "推送数据不能为空")
    private String data;

    /**
     * 时间戳
     */
    @NotNull(message = "时间戳不能为空")
    private Long timestamp;

    /**
     * 获取数据签名
     *
     * @author: JiangYuQi
     * @date: 2024/01/13 10:17
     */
    public Boolean getDataSign(String privateKey) {
        boolean signFlag = false;
        try {
            String decryptByPrivateKey = AuthRsaUtils.decryptByPrivateKey(privateKey,sign);
            String privateDataStr = Md5Utils.hash(data) + timestamp;
            log.info("签名值...{}",privateDataStr);
            log.info("解密值...{}",decryptByPrivateKey);
            if (StringUtils.equals(privateDataStr, decryptByPrivateKey)) {
                signFlag = true;
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return signFlag;
    }

    /**
     * 效验时间签名[30分钟有效期]
     */
    public Boolean checkTimestamp() {
        boolean timestampFlag = false;
        long timePoor = Math.abs(timestamp - System.currentTimeMillis());
        if (timePoor < 1000 * 60 * 30) {
            timestampFlag = true;
        }
        return timestampFlag;
    }
}
