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



import com.yxcheng.token.common.enumutil.ResultEnum;
import com.yxcheng.token.common.model.CrRpcResult;
import com.yxcheng.token.common.model.RestfulApi;
import com.tax.user.service.safe.RestFulCheckI;
import com.yxcheng.token.utils.DateUtils;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by 曹小红 on 2017-04-11.
 */
@Order(3)
@Component
public class RestFulCheckTimeDiffImpl implements RestFulCheckI {


    @Override
    public boolean checkData(String data, RestfulApi apiModel, CrRpcResult<String> rpcResult) {

        try {
            Date clientDate = DateUtils.str2Date(apiModel.getTimeStamp(), new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"));
            if(clientDate == null){
                rpcResult.errorResponse(ResultEnum.time_fmt_error);
                return false;
            }
            Date serverDate = DateUtils.getDate();
            if(Math.abs(clientDate.getTime() - serverDate.getTime()) < 5 * 60 * 1000) {
                return true;
            } else {
                rpcResult.errorResponse(ResultEnum.time_diff_error);
            }
        } catch (Exception e) {
            e.printStackTrace();
            rpcResult.errorResponse(ResultEnum.time_diff_error);
        }
        return false;
    }

    public static void main(String[] args) {
        try {
            RestfulApi apiModel = new RestfulApi();
            apiModel.setTimeStamp("2020-03-26T08:50:36.624Z");
            Date clientDate = DateUtils.str2Date(apiModel.getTimeStamp(), new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"));
            if(clientDate == null){
                System.out.println("1255555555555553");
            }
            Date serverDate = DateUtils.getDate();
            if(Math.abs(clientDate.getTime() - serverDate.getTime()) < 5 * 60 * 1000) {
                System.out.println("123");
            } else {
                System.out.println("1234");
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("12345555");
        }
    }
}
