package com.tax.file.utils;

import com.yxcheng.token.common.cache.RedisService;
import com.yxcheng.token.utils.SpringUtil;
import com.yxcheng.token.utils.StringUtil;

/**
 * @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 ExpCacheUtils {

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

    public static final String CACHE_TAXSYS_DOWNLOADLIST_TASKNO_PREFIX = "taxsys:downloadList:taskno:";
    public static final String CACHE_TAXSYS_DOWNLOADLIST_TASKCOUNT_PREFIX = "taxsys:downloadList:taskcount:";

    public static final String CACHE_TAXSYS_DOWNLOAD_TASKNO_PREFIX = "taxsys:download:taskno:";
    public static final String CACHE_TAXSYS_DOWNLOAD_TASKCOUNT_PREFIX = "taxsys:download:taskcount:";

    //10分钟内有效
    public static void setDownloadListTaskNoByTaxno(String userName, String taxno, String taskno) {
        redisService.set(CACHE_TAXSYS_DOWNLOADLIST_TASKNO_PREFIX + userName + taxno, taskno, 60 * 10);
        //检测未握手次数
        String taskCount = redisService.get(CACHE_TAXSYS_DOWNLOADLIST_TASKCOUNT_PREFIX + userName + taxno);
        if(StringUtil.isNotEmpty(taskCount)) {
            redisService.set(CACHE_TAXSYS_DOWNLOADLIST_TASKCOUNT_PREFIX + userName + taxno, String.valueOf(Integer.parseInt(taskCount) + 1), 60 * 10);
        } else {
            redisService.set(CACHE_TAXSYS_DOWNLOADLIST_TASKCOUNT_PREFIX + userName + taxno, "1", 60 * 10);
        }
    }

    //发票下载同步
    public static String getDownloadListTaskNoByTaxno(String userName, String taxno) {
        return redisService.get(CACHE_TAXSYS_DOWNLOADLIST_TASKNO_PREFIX + userName + taxno);
    }

    public static int getDownloadListTaskCountByTaxno(String userName, String taxno) {
        String taskCount = redisService.get(CACHE_TAXSYS_DOWNLOADLIST_TASKCOUNT_PREFIX + userName + taxno);
        if(StringUtil.isNotEmpty(taskCount)) {
            return Integer.parseInt(taskCount);
        }
        return 0;
    }

    public static void delDownloadListTaskNoByTaxno(String userName, String taxno) {
        redisService.del(CACHE_TAXSYS_DOWNLOADLIST_TASKNO_PREFIX + userName + taxno);
        redisService.del(CACHE_TAXSYS_DOWNLOADLIST_TASKCOUNT_PREFIX + userName + taxno);
    }

    //10分钟内有效
    public static void setDownloadTaskNoByTaxno(String userName, String taskno) {
        redisService.set(CACHE_TAXSYS_DOWNLOAD_TASKNO_PREFIX + userName, taskno, 60 * 10);
        //检测未握手次数
        String taskCount = redisService.get(CACHE_TAXSYS_DOWNLOAD_TASKCOUNT_PREFIX + userName);
        if(StringUtil.isNotEmpty(taskCount)) {
            redisService.set(CACHE_TAXSYS_DOWNLOAD_TASKCOUNT_PREFIX + userName, String.valueOf(Integer.parseInt(taskCount) + 1), 60 * 10);
        } else {
            redisService.set(CACHE_TAXSYS_DOWNLOAD_TASKCOUNT_PREFIX + userName, "1", 60 * 10);
        }
    }

    //发票下载同步
    public static String getDownloadTaskNoByTaxno(String userName) {
        return redisService.get(CACHE_TAXSYS_DOWNLOAD_TASKNO_PREFIX + userName);
    }

    public static int getDownloadTaskCountByTaxno(String userName) {
        String taskCount = redisService.get(CACHE_TAXSYS_DOWNLOAD_TASKCOUNT_PREFIX + userName);
        if(StringUtil.isNotEmpty(taskCount)) {
            return Integer.parseInt(taskCount);
        }
        return 0;
    }

    public static void delDownloadTaskNoByTaxno(String userName) {
        redisService.del(CACHE_TAXSYS_DOWNLOAD_TASKNO_PREFIX + userName);
        redisService.del(CACHE_TAXSYS_DOWNLOAD_TASKCOUNT_PREFIX + userName);
    }
}
