package com.tax.token.service.impl;


import com.tax.token.mapper.SmsSendLogMapper;
import com.tax.token.model.SmsResFulModel;
import com.tax.token.model.SmsSendLog;
import com.tax.token.model.SmsSendLogExample;
import com.tax.token.service.api.SmsSendLogService;
import com.yxcheng.token.common.service.AbstractService;
import com.yxcheng.token.utils.DateUtils;
import com.yxcheng.token.utils.StringUtil;
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 SmsSendLogServiceImpl extends AbstractService<SmsSendLogMapper,SmsSendLogExample,SmsSendLog> implements SmsSendLogService {

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

    public void saveSmsLog(String timeFlag, String cellphone, SmsResFulModel resModel, Date requestTime,
                           String requestUrl, String smsFrom, String reqParams, String reqContext, String userCode) {
        try {
            SmsSendLog collectLog = new SmsSendLog();
            collectLog.setCellphone(cellphone);
            collectLog.setReqDate(requestTime);
            collectLog.setReqContext(requestUrl);
            collectLog.setResCode(resModel.getCode());
            collectLog.setResMsg(resModel.getMessage());
            collectLog.setSmsFrom(smsFrom);
            collectLog.setUserCode(userCode);

            Date responseTime = DateUtils.getDate();
            collectLog.setResDate(responseTime);
            collectLog.setTimeFlag(timeFlag);
            long timeDiff = responseTime.getTime() - requestTime.getTime();
            collectLog.setTimeDiff(timeDiff);
            collectLog.setRequestUrl(requestUrl);
            collectLog.setReqParams(StringUtil.getShortLog(reqParams, 500));
            collectLog.setResContext(StringUtil.getShortLog(reqContext, 500));
            baseMapper.insert(collectLog);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(timeFlag + " saveSmsLog taxno : " + cellphone + " is error!" + e.getMessage());
        }

    }

}
