package com.tax.token.service.impl;

import com.alibaba.fastjson.JSON;
import com.tax.token.mapper.TkRealTokenLogMapper;
import com.tax.token.model.TkRealTokenLog;
import com.tax.token.model.TkToken;
import com.tax.token.service.api.TkRealTokenLogService;
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;

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

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

    @Autowired
    private TkRealTokenLogMapper realTokenLogMapper;

    @Override
    public void saveRealTokenLog(String uuId, TkToken token, String tokenFrom) {
        try {
            TkRealTokenLog tokenLog = new TkRealTokenLog();
            tokenLog.setTaxno(token.getTaxno());
            tokenLog.setReqDate(token.getReqDate());
            tokenLog.setToken(token.getToken());
            tokenLog.setTokenStatus(token.getTokenStatus());
            if(null!=token.getTaxUserInfo()) {
                tokenLog.setSecert(token.getTaxUserInfo());
            }
            tokenLog.setUserCode(token.getUserCode());
            tokenLog.setRemarks(token.getTokenMessage());
            tokenLog.setZoneCode(token.getZoneCode());
            tokenLog.setServerNo(token.getServerNo());
            tokenLog.setResContext(StringUtil.getShortLog(token.getResContext(), 3000));
            tokenLog.setIsValid(token.getCheckType());
            tokenLog.setIpServerNo(token.getTarget());
            tokenLog.setKeyCode(token.getSite());
            if(null==token.getResDate()) {
                tokenLog.setResDate(DateUtils.getDate());
            } else {
                tokenLog.setResDate(token.getResDate());
            }
            tokenLog.setLostTime(DateUtils.getMillSecondsDiff(tokenLog.getReqDate(),tokenLog.getResDate()));
            tokenLog.setTimeFlag(uuId);
            realTokenLogMapper.insertSelective(tokenLog);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(uuId + " saveRealTokenLog is error! " + e.getMessage());
        }
    }
}
