package com.tax.file.service.impl;


import com.alibaba.fastjson.JSON;
import com.tax.file.mapper.TkTaxUserMapper;
import com.tax.file.model.TkTaxUser;
import com.tax.file.model.TkTaxUserExample;
import com.tax.file.service.api.TkTaxUserService;
import com.yxcheng.token.common.cache.RedisService;
import com.yxcheng.token.common.service.AbstractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;


@Transactional
@Component
@ComponentScan(basePackages = {"com.tax.token.dao", "com.tax.token.integration"})
public class TkTaxUserServiceImpl extends AbstractService<TkTaxUserMapper,TkTaxUserExample,TkTaxUser> implements TkTaxUserService {

    @Autowired
    private RedisService redisService;

    public List<TkTaxUser> queryApplyTaxnoList() {
        return  baseMapper.queryApplyTaxnoList();
    }

    public List<TkTaxUser> queryDownloadTaxnoList() {
        return  baseMapper.queryDownloadTaxnoList();
    }
    public List<TkTaxUser> queryResetApplyTaxnoList() {
        return  baseMapper.queryResetApplyTaxnoList();
    }
    public List<TkTaxUser> queryResetMonthApplyTaxnoList() {
        return  baseMapper.queryResetMonthApplyTaxnoList();
    }

    @Override
    public List<TkTaxUser> queryClearDataTaxnoList(Integer pageNum) {
        return baseMapper.queryClearDataTaxnoList(pageNum);
    }
    @Override
    public List<Map<String, Object>> queryApplyFileByMonitor() {
        return  baseMapper.queryApplyFileByMonitor();
    }

    public void updateTaxUser(TkTaxUser tkTaxUser) {
        baseMapper.updateTaxUser(tkTaxUser);
    }

    @Override
    public List<TkTaxUser> queryDayReportTaxnoList(String startDate, String endDate) {
        return baseMapper.queryDayReportTaxnoList(startDate, endDate);
    }

    public TkTaxUser queryTaxUser(String userName, String taxno) {
        return baseMapper.queryTaxnoInfo(userName, taxno);
    }


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

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

    public void updateClearTaxUser(TkTaxUser tkTaxUser) {
        baseMapper.updateClearTaxUser(tkTaxUser);
    }
}
