package com.tax.token.service.impl;

import com.tax.token.mapper.TkTokenLogMapper;
import com.tax.token.model.TkToken;
import com.tax.token.model.TkTokenLog;
import com.tax.token.service.api.TkTokenLogService;
import com.yxcheng.token.utils.DateUtils;
import com.yxcheng.token.utils.StringUtil;
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;

@Transactional
@Component
@ComponentScan(basePackages = {"com.tax.token.dao", "com.tax.token.integration"})
public class TkTokenLogServiceImpl implements TkTokenLogService{

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

    @Autowired
    private TkTokenLogMapper tokenLogMapper;

    @Override
    public void saveTokenLog(String uuId, TkToken token, String tokenFrom) {
        try {
            TkTokenLog tokenLog = new TkTokenLog();
            tokenLog.setTaxno(token.getTaxno());
            tokenLog.setReqDate(token.getReqDate());
            tokenLog.setToken(token.getToken());
            tokenLog.setTokenStatus(token.getTokenStatus());
            tokenLog.setUserCode(token.getUserCode());
            tokenLog.setResDate(DateUtils.getDate());
            tokenLog.setTokenCode(token.getTokenCode());
            tokenLog.setRemarks(StringUtil.getShortLog(token.getTokenMessage(), 500));
            tokenLog.setZoneCode(token.getZoneCode());
            tokenLog.setTokenFrom(tokenFrom);
            tokenLog.setTimeFlag(uuId);
            tokenLog.setTaxRequestId(token.getRequestId());
            tokenLogMapper.insert(tokenLog);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(uuId + " saveTokenLog is error! " + e.getMessage());
        }
    }
}
