package com.tax.file.service.util;

import com.alibaba.fastjson.JSON;
import com.tax.file.model.TkTaxUser;
import com.tax.file.service.api.TkTaxUserService;
import com.yxcheng.token.common.cache.RedisService;
import com.yxcheng.token.utils.SpringUtil;

/**
 * @ProjectName: tax-sys-test
 * @Package: com.tax.file.utils
 * @ClassName: ExpCacheUtils
 * @Author: ddx
 * @Description: ${description}
 * @Date: 2021-11-11 9:48
 * @Version: 1.0
 */
public class UserUtils {

    private static RedisService redisService = SpringUtil.getApplicationContext().getBean(RedisService.class);
    private static TkTaxUserService tkTaxUserService = SpringUtil.getApplicationContext().getBean(TkTaxUserService.class);

    public static final String CACHE_USER_INFO_PREFIX = "taxsys:taxuser:";

    public static TkTaxUser getTaxUserBytaxno(String userName, String taxno) {
        TkTaxUser taxUser = null;
        if(redisService.exists(CACHE_USER_INFO_PREFIX + userName + ":" + taxno)) {
            taxUser = JSON.parseObject(redisService.get(CACHE_USER_INFO_PREFIX + userName + ":" + taxno),  TkTaxUser.class);
        } else {
            taxUser = tkTaxUserService.queryTaxUser(userName, taxno);
            if(null != taxUser) {
                redisService.set(CACHE_USER_INFO_PREFIX + userName + ":" + taxUser.getTaxno(), JSON.toJSONString(taxUser), "utf-8", 3600);
            }
        }
        return taxUser;
    }

    public static TkTaxUser flushTaxUserBytaxno(String userName, String taxno) {
        TkTaxUser taxUser = tkTaxUserService.queryTaxUser(userName, taxno);
        if(null != taxUser) {
            redisService.set(CACHE_USER_INFO_PREFIX+ userName + ":" + taxUser.getTaxno(), JSON.toJSONString(taxUser), "utf-8", 3600);
        }
        return taxUser;
    }

}
