package com.mooctest.event;

import com.google.common.eventbus.Subscribe;
import com.mooctest.model.AggTaskStatus;
import com.mooctest.service.AggTaskStatusService;
import com.mooctest.service.AggregationService;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class TaskStartListener implements InitializingBean {

    @Autowired
    private EventUtil eventUtil;

    @Autowired
    private AggregationService aggregationService;

    @Autowired
    private AggTaskStatusService aggTaskStatusService;

    @Override
    public void afterPropertiesSet() throws Exception {
        eventUtil.register(this);
    }

    @Subscribe
    public void aggretate(TaskStartEvent event) {

        AggTaskStatus aggTaskStatus = aggTaskStatusService.create(event.getExamId(), event.getCaseId());
        aggregationService.aggregate(event.getExamId(), event.getCaseId());
        AggTaskStatus result = aggTaskStatusService.updateStatus(aggTaskStatus.getTaskId(), aggTaskStatus.getStartTime());

    }
}
