package com.tax.user.service.safe.impl;

import com.tax.user.service.safe.RestFulCheckI;
import com.yxcheng.token.common.cache.RedisService;
import com.yxcheng.token.common.enumutil.ResultEnum;
import com.yxcheng.token.common.model.CrRpcResult;
import com.yxcheng.token.common.model.RestfulApi;
import com.yxcheng.token.constants.RedisContants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Order(2)
@Component
public class RestFulCheckSignatureNonceImpl implements RestFulCheckI {
	
	@Autowired
	private RedisService redisService;
	
	@Override
	public boolean checkData(String data, RestfulApi apiModel, CrRpcResult<String> rpcResult) {
		String key = RedisContants.CHECK_SIGNATURENONCE + apiModel.getAccessKeyID() + ":" + apiModel.getSignatureNonce();
		if("twotest".equals(apiModel.getSignature())) {
			return true;
		}
		if(!redisService.exists(key)) {
			redisService.set(key, "0", 600);
			return true;
		} else {
			rpcResult.errorResponse(ResultEnum.sign_nonce_error);
		}
		return false;
	}

}
