package cn.com.qmth.stmms.ms.marking.service;

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

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

import cn.com.qmth.stmms.ms.core.domain.user.MarkUser;
import cn.com.qmth.stmms.ms.core.domain.user.MarkerGroup;
import cn.com.qmth.stmms.ms.core.repository.MarkerGroupRepo;

/**
 * 分组服务
 * Created by zhengmin on 2016/9/22.
 */
@Service
public class GroupingService {

    @Autowired
    private MarkerGroupRepo markerGroupRepo;

    /**
     * 自动分组
     * @param count 组数
     * @param markers 评卷员列表
     * @return
     */
    public List<MarkerGroup> autoGrouping(int count, Iterable<MarkUser> markers){
        List<MarkerGroup> groups = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            groups.add(new MarkerGroup());
        }
        Iterator<MarkUser> iterator = markers.iterator();
        int idx = 0;
        while (iterator.hasNext()){
            if(idx >= count){
                idx = 0;
            }
            MarkUser marker = iterator.next();
            groups.get(idx).setWorkId(marker.getWorkId());
            groups.get(idx).setSubject(marker.getSubject());
            groups.get(idx).addMarker(marker);
            idx++;
        }
        markerGroupRepo.save(groups);
        return groups;
    }
}
