package cn.com.qmth.stmms.biz.exam.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.qmth.stmms.biz.common.BaseQueryService;
import cn.com.qmth.stmms.biz.exam.dao.MarkerClassDao;
import cn.com.qmth.stmms.biz.exam.model.Marker;
import cn.com.qmth.stmms.biz.exam.model.MarkerClass;
import cn.com.qmth.stmms.biz.exam.service.MarkerClassService;
import cn.com.qmth.stmms.biz.exam.service.MarkerService;

@Service("markerClassService")
public class MarkerClassServiceImpl extends BaseQueryService<MarkerClass> implements MarkerClassService {

    @Autowired
    private MarkerClassDao markerClassDao;

    @Autowired
    private MarkerService markerService;

    @Override
    public MarkerClass save(MarkerClass m) {
        return markerClassDao.save(m);
    }

    @Override
    public List<MarkerClass> findByMarkerId(int markerId) {
        return markerClassDao.findByMarkerId(markerId);
    }

    @Override
    public long countByMarkerId(int markerId) {
        return markerClassDao.countByMarkerId(markerId);
    }

    @Transactional
    @Override
    public void save(Marker marker, String[] classes) {
        this.deleteByMarkerId(marker.getId());
        for (String className : classes) {
            MarkerClass m = new MarkerClass();
            m.setClassName(className);
            m.setMarkerId(marker.getId());
            markerClassDao.save(m);
        }
        marker.setClassCount((int)countByMarkerId(marker.getId()));
        markerService.save(marker);
    }

    @Override
    public void deleteByMarkerId(int markerId) {
        markerClassDao.deleteByMarkerId(markerId);
    }

    @Override
    public List<String> findClassNameByMarkerId(int markerId) {
        List<String> list = new ArrayList<String>();
        List<MarkerClass> classes =this.findByMarkerId(markerId);
        for (MarkerClass markerClass : classes) {
            list.add(markerClass.getClassName());
        }
        return list;
    }

}
