package com.nokia.sms.action;
import java.util.Arrays;
import java.util.Date;
import java.util.stream.Collectors;

import com.nokia.sms.pojo.SmsReport;
import com.nokia.sms.sgip.message.ReportMessage;
import com.nokia.sms.sgip.message.SgipMessage;
import com.nokia.sms.sgip.mo.server.Action;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ReportAction implements Action {

	public void execute(SgipMessage message) {
		if (message instanceof ReportMessage) {
			// 收到状态报告
			ReportMessage rMessage = (ReportMessage) message;
			log.info("收到Report." + rMessage);
			String flowId = Arrays.stream(rMessage.getSubmitSN())
					.mapToObj(Integer::toString).collect(Collectors.joining("-"));
			SmsReport smsReport = new SmsReport();
			smsReport.setPhone(rMessage.getUserNumber().replaceFirst("86", ""));
			smsReport.setFlowId(flowId);
			smsReport.setReportTime(new Date());
			smsReport.setReportType(rMessage.getReportType());
			smsReport.setState(rMessage.getState());
			smsReport.setErrorCode(rMessage.getErrorCode());
			if (!SmsReport.offer(smsReport)) {
				log.error("短信报告入队失败: {}", smsReport);
			}
		}

	}

}
