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

import cn.com.qmth.stmms.biz.exam.model.MarkGroup;
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 MarkGroupDeleteThread implements Runnable {

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

    private MarkGroup group;

    private MarkService markService;

    private LockService lockService;

    public MarkGroupDeleteThread(MarkGroup group, MarkService markService, LockService lockService) {
        this.group = group;
        this.markService = markService;
        this.lockService = lockService;
    }

    @Override
    public void run() {
        if (group == null) {
            return;
        }
        try {
            lockService.waitlock(LockType.EXAM_SUBJECT, group.getExamId(), group.getSubjectCode());
            lockService.waitlock(LockType.GROUP, group.getExamId(), group.getSubjectCode(), group.getNumber());
            markService.deleteGroup(group);
        } catch (Exception e) {
            log.error("delete group error", e);
        } finally {
            lockService.unlock(LockType.GROUP, group.getExamId(), group.getSubjectCode(), group.getNumber());
            lockService.unlock(LockType.EXAM_SUBJECT, group.getExamId(), group.getSubjectCode());
            lockService.unlock(LockType.GROUP_DELETE, group.getExamId(), group.getSubjectCode(), group.getNumber());
        }
    }

}
