package com.tax.file.service.impl;


import com.tax.file.mapper.FileReportMapper;
import com.tax.file.model.FileReport;
import com.tax.file.model.FileReportExample;
import com.tax.file.model.TkTaxUser;
import com.tax.file.service.api.FileReportService;
import com.yxcheng.token.common.service.AbstractService;
import com.yxcheng.token.utils.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Transactional
@Component
@ComponentScan(basePackages = {"com.tax.file.dao", "com.tax.file.integration"})
public class FileReportServiceImpl extends AbstractService<FileReportMapper,FileReportExample,FileReport> implements FileReportService {

    private static final Logger logger = LoggerFactory.getLogger(FileReportServiceImpl.class);

    @Override
    public List<FileReport> queryInitFileDayReport(TkTaxUser taxUser) {
        return  baseMapper.queryInitFileDayReport(taxUser);
    }

    @Override
    public  FileReport queryFileDayReport(FileReport fileReport) {
        return baseMapper.queryFileDayReport(fileReport);
    }


    public void saveDayReport(String uuidFlag, FileReport dayReport, TkTaxUser taxUser) {
        String taxno = taxUser.getTaxno();
        if(dayReport.getCountNum() > 0) {
            FileReport report = baseMapper.queryFileDayReport(dayReport);
            if(null == report) {
                dayReport.setUserName(taxUser.getUserName());
                dayReport.setUserStatus(taxUser.getUserStatus());
                dayReport.setZoneCode(taxUser.getZoneCode());
                dayReport.setCountMonth(DateUtils.getMonth(dayReport.getCountDate()));
                dayReport.setCreateTime(DateUtils.getDate());
                baseMapper.insertSelective(dayReport);
            } else {
                report.setCountNum(dayReport.getCountNum());
                baseMapper.updateByPrimaryKeySelective(report);
            }
        }
        logger.info(uuidFlag + " run dayReport " + taxno + " curDate=" + DateUtils.date2Str(dayReport.getCountDate(), DateUtils.date_sdf) + " is success! countnum is :" + dayReport.getCountNum());
    }
}
