package com.tax.file.service.impl;


import com.tax.file.mapper.TkTaxUserMapper;
import com.tax.file.model.TkTaxUser;
import com.tax.file.model.TkTaxUserExample;
import com.tax.file.model.invoice.FileStatusModel;
import com.tax.file.service.api.TkFileService;
import com.tax.file.service.api.TkTaxUserService;
import com.yxcheng.token.common.cache.RedisService;
import com.yxcheng.token.common.service.AbstractService;
import com.yxcheng.token.constants.RedisContants;
import com.yxcheng.token.utils.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.Date;
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 {

    private static final Logger logger = LoggerFactory.getLogger(TkTaxUserServiceImpl.class);
    @Autowired
    protected RedisService redisService;
    @Autowired
    protected TkFileService tkFileService;

    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(Date cyDate, String resetDay) {
        return  baseMapper.queryResetMonthApplyTaxnoList(cyDate, resetDay);
    }

    @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() {
        return baseMapper.queryDayReportTaxnoList();
    }

    //optType 0-申请 1-下载
    public void updateDownloadUserInfo(String uuidFlag, TkTaxUser taxUser, String optType) {
        Date curDate = DateUtils.getDate();
        String taxno = taxUser.getTaxno();
        //检测是否全部下载完成
        FileStatusModel fileStatusModel = tkFileService.getFileStatusModel(uuidFlag, taxUser);
        String applyStatus = fileStatusModel.getApplyStatus();
        String downStatus = fileStatusModel.getDownStatus();

        taxUser.setApplyStatus(applyStatus);
        taxUser.setDownStatus(downStatus);
        if(downStatus.equals("2") && taxUser.getInit().equals("1")) {
            taxUser.setOverFlag("1");
            taxUser.setOverDate(curDate);
            //如果是一次性税号，下载完成后直接关闭
            if("1".equals(taxUser.getIsOnce())){
                taxUser.setUserStatus("5");
            }
        }
        if(optType.equals("0")) {
            taxUser.setApplyDownDate(curDate);
            //清除下载限制
            if(redisService.exists(RedisContants.getLimitName(RedisContants.DOWN_FILE_TIMES_LOCK, taxUser.getUserName(), taxUser.getTaxno()))) {
                redisService.del(RedisContants.getLimitName(RedisContants.DOWN_FILE_TIMES_LOCK, taxUser.getUserName(), taxUser.getTaxno()));
            }
        } else {
            taxUser.setDownDate(curDate);
            //清除申请限制
            if(redisService.exists(RedisContants.getLimitName(RedisContants.FILE_APPLY_TIMES_LOCK, taxUser.getUserName(), taxUser.getTaxno()))) {
                redisService.del(RedisContants.getLimitName(RedisContants.FILE_APPLY_TIMES_LOCK, taxUser.getUserName(), taxUser.getTaxno()));
            }
        }
        taxUser.setUpdateDate(curDate);
        updateByPrimaryKeySelective(taxUser);
        logger.info(uuidFlag + " taxno " + taxno + " update user is end.The applyStatus is " + applyStatus + ",downStatus is " + downStatus);
    }
}
