EventSubmitTask.java 6.62 KB
package com.xiniunet.service.railway.util.thread;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.fastjson.JSON;
import com.xiniunet.basic.enumeration.SmsTemplateProviderEnum;
import com.xiniunet.framework.security.Passport;
import com.xiniunet.railway.domain.Event;
import com.xiniunet.railway.request.EventExtendCreateRequest;
import com.xiniunet.railway.request.EventGetRequest;
import com.xiniunet.service.railway.biz.EventExtendManager;
import com.xiniunet.service.railway.biz.EventManager;
import com.xiniunet.service.railway.dal.NoticePersonMapper;
import com.xiniunet.service.railway.po.NoticePersonPO;
import com.xiniunet.service.railway.util.SendSmsUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.concurrent.TimeUnit;

import static com.xiniunet.service.railway.Constant.RAILWAY_TENANT_ID;

/**
 * @author xiang
 */
public class EventSubmitTask implements Runnable {

    private final Logger log = LoggerFactory.getLogger(EventSubmitTask.class);

    private final NoticePersonMapper noticePersonMapper;

    private final EventExtendManager eventExtendManager;

    private final EventManager eventManager;

    /**事件id*/
    private final Long eventId;

    private final Passport passport;

    public EventSubmitTask(Long eventId,
                           EventManager eventManager,
                           EventExtendManager eventExtendManager,
                           NoticePersonMapper noticePersonMapper,
                           Passport passport) {
        this.eventId = eventId;
        this.eventManager = eventManager;
        this.eventExtendManager = eventExtendManager;
        this.noticePersonMapper = noticePersonMapper;
        this.passport = passport;
    }

    @Override
    public void run() {
        EventExtendCreateRequest extendCreateRequest = new EventExtendCreateRequest();
        boolean isNoticed = true;
        log.warn("=========event_submit---{}", eventId);
        try {
            if (eventId == null) {
                isNoticed = false;
                throw new RuntimeException("事件为空,事件id也为空");
            }
            Event event;
            int waitCount = 0;
            do {
                EventGetRequest getEventRequest = new EventGetRequest();
                getEventRequest.setId(eventId);
                event = eventManager.get(getEventRequest, passport).getEvent();
                if (event == null) {
                    waitCount++;
                    try {
                        // 等待10ms,mysql事务提交
                        TimeUnit.MILLISECONDS.sleep(20);
                        log.warn("=========event_submit_eventId={}_waitCount---{}", eventId, waitCount);
                    } catch (InterruptedException e) {
                        log.warn(e.getMessage(), e);
                    }
                }
            } while (event == null && waitCount < 16);
            if (event == null) {
                log.error("事件为空:event=null ---- eventId={}", eventId);
                isNoticed = false;
                return;
            }

            String description = event.getDescription();
            Map<String, String> params = new HashMap<>(6);
            params.put("station_name", event.getStation());
            params.put("event_time", event.getHappenTime());
            params.put("event_number", event.getAppEventId() != null ? event.getAppEventId() : "");
            params.put("line_carriage", (event.getLineName() + event.getCarriageNumber()).replaceAll("null", ""));
            params.put("event_titile", event.getTitle());
            params.put("event_description", description == null ? "" : description.length() > 20 ? description.substring(0, 20) + "... " : description);

            List<SmsSendRequest> smsList = new ArrayList<>();
            if (!StringUtils.isBlank(event.getStationId()) && !StringUtils.isBlank(event.getLineId())) {
                log.warn("=========event_submit_by_station---{}", eventId);
                // 按车站发消息
                List<NoticePersonPO> persons = noticePersonMapper.queryPersonByStation("line", event.getLineId(), "station", event.getStationId());
                if (persons.isEmpty()) {
                    isNoticed = false;
                    return;
                }
                for (NoticePersonPO person : persons) {
                    smsList.add(getSmsSendRequest(person, JSON.toJSONString(params), "shmetro.station.event.deal"));
                }

            } else if (StringUtils.isBlank(event.getStationId()) && !StringUtils.isBlank(event.getLineId())) {
                log.warn("=========event_submit_by_line---{}", eventId);
                // 按线路发消息
                List<NoticePersonPO> persons = noticePersonMapper.queryPersonByLine("line", event.getLineId());
                if (persons.isEmpty()) {
                    isNoticed = false;
                    return;
                }
                for (NoticePersonPO person : persons) {
                    smsList.add(getSmsSendRequest(person, JSON.toJSONString(params), "shmetro.carriage.event.deal"));
                }
            }
            SmsSendResponse smsSendResponse = SendSmsUtils.send(smsList);
            isNoticed = !smsSendResponse.hasError();
            log.warn("eventId-----------------------{}==smsSendResponse===={}", eventId, JSON.toJSONString(smsSendResponse));
        } catch (Exception e) {
            log.error("=========event_submit_exception---{}", eventId);
            log.error("=========event_submit_exception---" + e.getMessage(), e);
            isNoticed = false;
        } finally {
            log.error("=========event_submit_finally---{}--isNoticed={}", eventId, isNoticed);
            extendCreateRequest.setId(eventId);
            extendCreateRequest.setIsNoticed(isNoticed);
            extendCreateRequest.setNoticeTime(isNoticed ? new Date() : null);
            extendCreateRequest.setIsHurried(false);
            eventExtendManager.create(extendCreateRequest, passport);
        }
    }

    private SmsSendRequest getSmsSendRequest(NoticePersonPO person, String params, String function) {
        SmsSendRequest smsSendRequest = new SmsSendRequest();
        smsSendRequest.setTenantId(RAILWAY_TENANT_ID);
        smsSendRequest.setFunction(function);
        smsSendRequest.setParam(params);
        smsSendRequest.setMobile(person.getUserMobile());
        smsSendRequest.setProvider(SmsTemplateProviderEnum.ALIYUN.name());
        smsSendRequest.setBusinessId(eventId);
        smsSendRequest.setBusinessType(person.getType());
        smsSendRequest.setSign("平安地铁");
        return smsSendRequest;
    }
}