package com.tax.token.service.impl;


import com.tax.token.mapper.TkTokenRemarkLogMapper;
import com.tax.token.model.TkToken;
import com.tax.token.model.TkTokenRemarkLog;
import com.tax.token.model.TkTokenRemarkLogExample;
import com.tax.token.service.api.TkTokenRemarkLogService;
import com.yxcheng.token.common.service.AbstractService;
import com.yxcheng.token.utils.DateUtils;
import com.yxcheng.token.utils.StringUtil;
import org.apache.commons.lang.StringUtils;
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.token.dao", "com.tax.token.integration"})
public class TkTokenRemarkLogServiceImpl extends AbstractService<TkTokenRemarkLogMapper,TkTokenRemarkLogExample,TkTokenRemarkLog> implements TkTokenRemarkLogService {

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

    public void saveRemarkLog(String uuId, TkToken tkToken) {
        try {
            if(StringUtils.isNotBlank(tkToken.getTokenCode())){

                Date curDate = DateUtils.getDate();

                TkTokenRemarkLogExample example = new TkTokenRemarkLogExample();
                example.createCriteria().andTaxnoEqualTo(tkToken.getTaxno()).andUserCodeEqualTo(tkToken.getUserCode());
                TkTokenRemarkLog tkTokenRemarkLog = getOne(example);

                //如果正常取到token，清理异常记录
                if(tkToken.getTokenCode().equals("0") && null!=tkTokenRemarkLog) {
                    deleteByPrimaryKey(tkTokenRemarkLog.getId());
                    logger.info(uuId + " saveRemarkLog delete! " + tkTokenRemarkLog);
                } else {
                    if(!tkToken.getTokenCode().equals("0") && !tkToken.getTokenCode().equals("2")) {
                        //不等于才刷新
                        if(null != tkTokenRemarkLog){
                            if(!tkTokenRemarkLog.getTokenCode().equals(tkToken.getTokenCode())) {
                                tkTokenRemarkLog.setUpdateDate(curDate);
                                tkTokenRemarkLog.setTokenCode(tkToken.getTokenCode());
                                tkTokenRemarkLog.setRemarks(StringUtil.getShortLog(tkToken.getTokenMessage(), 5000));
                                tkTokenRemarkLog.setTimeFlag(uuId);
                                updateByPrimaryKey(tkTokenRemarkLog);
                                logger.info(uuId + " saveRemarkLog update! " + tkTokenRemarkLog);
                            }
                        }else{
                            tkTokenRemarkLog = new TkTokenRemarkLog();
                            tkTokenRemarkLog.setTaxno(tkToken.getTaxno());
                            tkTokenRemarkLog.setUserCode(tkToken.getUserCode());
                            tkTokenRemarkLog.setCreateDate(curDate);
                            tkTokenRemarkLog.setUpdateDate(curDate);
                            tkTokenRemarkLog.setTokenCode(tkToken.getTokenCode());
                            tkTokenRemarkLog.setRemarks(StringUtil.getShortLog(tkToken.getTokenMessage(), 5000));
                            tkTokenRemarkLog.setTimeFlag(uuId);
                            insert(tkTokenRemarkLog);
                            logger.info(uuId + " saveRemarkLog insert! " + tkTokenRemarkLog);
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(uuId + " saveRemarkLog is error! " + e.getMessage());
        }
    }


}
