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


import com.tax.user.model.SysClientUser;
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 com.yxcheng.token.utils.DateUtils;
import com.yxcheng.token.utils.SpringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

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

    protected static Logger logger = LoggerFactory.getLogger(RestFulCheckUserValidImpl.class);


    @Override
    public boolean checkData(String data, RestfulApi apiModel, CrRpcResult<String> rpcResult) {
        RedisService redisService = SpringUtil.getBean(RedisService.class);
        String accesskeyId = apiModel.getAccessKeyID();
        SysClientUser clientUser = redisService.get(RedisContants.CHECK_FULLINFO + accesskeyId, SysClientUser.class);
        if(null != clientUser) {
            // 如果有效标志为无效(0)，则返回false
            if (clientUser.getUserStatus() == null || "0".equalsIgnoreCase(clientUser.getUserStatus())) {
                rpcResult.errorResponse(ResultEnum.user_disvaild);
                return false;
            }
            // 如果超过有效期，则返回false
            // 当前日期
            String strCurrentDate = DateUtils.getDate("yyyyMMdd");
            if (clientUser.getEndDate() == null
                    || (Integer.parseInt(strCurrentDate) > Integer.parseInt(DateUtils.date2Str(clientUser.getEndDate(), DateUtils.yyyyMMdd)))) {
                rpcResult.errorResponse(ResultEnum.user_disvaild);
                return false;
            }
        } else {
            rpcResult.errorResponse(ResultEnum.user_disvaild);
            return false;
        }
        return true;
    }


}
