package cn.com.qmth.stmms.admin.thread;

import cn.com.qmth.stmms.biz.exam.model.Marker;
import cn.com.qmth.stmms.biz.lock.LockService;
import cn.com.qmth.stmms.biz.mark.service.MarkService;
import cn.com.qmth.stmms.common.enums.LockType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MarkerResetThread implements Runnable {

    protected static Logger log = LoggerFactory.getLogger(MarkerResetThread.class);

    private Marker marker;

    private MarkService markService;

    private LockService lockService;

    public MarkerResetThread(Marker marker, MarkService markService, LockService lockService) {
        this.marker = marker;
        this.markService = markService;
        this.lockService = lockService;
    }

    @Override
    public void run() {
        if (marker == null) {
            return;
        }
        try {
            lockService.watch(LockType.EXAM_SUBJECT, marker.getExamId(), marker.getSubjectCode());
            lockService.watch(LockType.GROUP, marker.getExamId(), marker.getSubjectCode(), marker.getGroupNumber());
            lockService.waitlock(LockType.MARKER, marker.getId());
            markService.resetMarker(marker);
        } catch (Exception e) {
            log.error("reset marker error", e);
        } finally {
            lockService.unlock(LockType.MARKER, marker.getId());
            lockService.unwatch(LockType.GROUP, marker.getExamId(), marker.getSubjectCode(), marker.getGroupNumber());
            lockService.unwatch(LockType.EXAM_SUBJECT, marker.getExamId(), marker.getSubjectCode());
            lockService.unlock(LockType.MARKER_RESET, marker.getId());
        }
    }

}
