package com.tax.file.service.impl;


import com.tax.file.mapper.FileTaxLogMapper;
import com.tax.file.model.DownFileReqModel;
import com.tax.file.model.FileTaxLog;
import com.tax.file.model.FileTaxLogExample;
import com.tax.file.model.TkFile;
import com.tax.file.service.api.FileTaxLogService;
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.Date;


@Transactional
@Component
@ComponentScan(basePackages = {"com.tax.file.dao", "com.tax.file.integration"})
public class FileTaxLogServiceImpl extends AbstractService<FileTaxLogMapper,FileTaxLogExample,FileTaxLog> implements FileTaxLogService {

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

    @Override
    public void saveLog(String uuidFlag, String taxno, Date reqDate, String reqContext, String resContext, Date resDate, String applyStatus, TkFile entity, String resultIndex) {
        try{
            FileTaxLog fileLog = new FileTaxLog();
            fileLog.setTaxno(taxno);
            fileLog.setReqDate(reqDate);
            fileLog.setReqContext(reqContext);
            fileLog.setResContext(resContext);
            fileLog.setResDate(resDate);
            fileLog.setApplyStatus(applyStatus);
            fileLog.setUuidFlag(uuidFlag);
            fileLog.setLostTime(DateUtils.getSecondsDiff(reqDate,resDate));
            fileLog.setInvDateStart(entity.getInvDateStart());
            fileLog.setInvDateEnd(entity.getInvDateEnd());
            fileLog.setZoneCode(entity.getZoneCode());
            fileLog.setFileMonth(entity.getFileMonth());
            fileLog.setFileDataType(entity.getFileDataType());
            fileLog.setResResult(resultIndex);
            fileLog.setFileMd5(entity.getFileMd5());
            fileLog.setInvSpec(entity.getInvSpec());
            fileLog.setInvType(entity.getInvType());
            fileLog.setUserName(entity.getUserName());
            this.baseMapper.insert(fileLog);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(uuidFlag + " taxno is " + taxno + ", save tax log is error!" + e.getMessage());
        }

    }

    @Override
    public void saveLog(String uuidFlag, String userName, String taxno, Date reqDate, String reqContext, String resContext, Date resDate, String applyStatus, DownFileReqModel downFileReqModel, String resultIndex) {
        try{
            FileTaxLog fileLog = new FileTaxLog();
            fileLog.setTaxno(taxno);
            fileLog.setReqDate(reqDate);
            fileLog.setReqContext(reqContext);
            fileLog.setResContext(resContext);
            fileLog.setResDate(resDate);
            fileLog.setApplyStatus(applyStatus);
            fileLog.setUuidFlag(uuidFlag);
            fileLog.setLostTime(DateUtils.getSecondsDiff(reqDate,resDate));
            fileLog.setInvDateStart(DateUtils.str2Date(downFileReqModel.getApplyDateStart(), DateUtils.date_sdf));
            fileLog.setInvDateEnd(DateUtils.str2Date(downFileReqModel.getApplyDateEnd(), DateUtils.date_sdf));
            fileLog.setZoneCode(downFileReqModel.getDqdm());
            fileLog.setResResult(resultIndex);
            fileLog.setInvSpec(downFileReqModel.getFileInvSpec());
            fileLog.setInvType(downFileReqModel.getFileInvType());
            fileLog.setUserName(userName);
            this.baseMapper.insert(fileLog);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(uuidFlag + " taxno is " + taxno + ", save tax log is error!" + e.getMessage());
        }

    }
}
