package com.ym.mec.biz.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

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

import com.ym.mec.biz.dal.dao.StudentPreRegistrationDao;
import com.ym.mec.biz.dal.dto.PreRegisterSubjectDto;
import com.ym.mec.biz.dal.dto.StudentPreRegistrationDto;
import com.ym.mec.biz.dal.entity.StudentPreRegistration;
import com.ym.mec.biz.service.StudentPreRegistrationService;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.entity.Mapper;
import com.ym.mec.common.page.PageInfo;
import com.ym.mec.common.page.QueryInfo;
import com.ym.mec.common.service.impl.BaseServiceImpl;
import com.ym.mec.util.collection.MapUtil;

@Service
public class StudentPreRegistrationServiceImpl extends BaseServiceImpl<Long, StudentPreRegistration>  implements StudentPreRegistrationService {
	
	@Autowired
	private StudentPreRegistrationDao studentPreRegistrationDao;

	@Override
	public BaseDAO<Long, StudentPreRegistration> getDAO() {
		return studentPreRegistrationDao;
	}

	@Override
	public PageInfo<StudentPreRegistrationDto> queryListForPage(QueryInfo queryInfo) {
		PageInfo<StudentPreRegistrationDto> pageInfo = new PageInfo<StudentPreRegistrationDto>(queryInfo.getPage(), queryInfo.getRows());
		Map<String, Object> params = new HashMap<String, Object>();
		MapUtil.populateMap(params, queryInfo);
		
		List<StudentPreRegistrationDto> dataList = null;
		int count = this.findCount(params);
		if (count > 0) {
			pageInfo.setTotal(count);
			params.put("offset", pageInfo.getOffset());
			dataList = studentPreRegistrationDao.queryListForPage(params);
		}
		if (count == 0) {
			dataList = new ArrayList<StudentPreRegistrationDto>();
		}
		pageInfo.setRows(dataList);
		return pageInfo;
	}

	@Override
	public List<PreRegisterSubjectDto> querySubjectNum(String musicGroupId) {
		Set<String> subjectNames = new HashSet<String>();
		Map<String, Integer> firstMap = new HashMap<String, Integer>();
		Map<String, Integer> secondMap = new HashMap<String, Integer>();

		List<Mapper> firstMapperList = studentPreRegistrationDao.queryFirstSubjectNum(musicGroupId);
		for (Mapper mapper : firstMapperList) {
			firstMap.put(String.valueOf(mapper.getKey()), Integer.parseInt(mapper.getValue().toString()));
			subjectNames.add(String.valueOf(mapper.getKey()));
		}

		List<Mapper> secondMapperList = studentPreRegistrationDao.querySecondSubjectNum(musicGroupId);
		for (Mapper mapper : secondMapperList) {
			secondMap.put(String.valueOf(mapper.getKey()), Integer.parseInt(mapper.getValue().toString()));
			subjectNames.add(String.valueOf(mapper.getKey()));
		}

		List<PreRegisterSubjectDto> list = new ArrayList<PreRegisterSubjectDto>();
		for (String subjectName : subjectNames) {
			PreRegisterSubjectDto dto = new PreRegisterSubjectDto();
			dto.setSubjectName(subjectName);
			dto.setFirstSubjectNum(firstMap.get(subjectName) == null ? 0 : firstMap.get(subjectName));
			dto.setSecondSubjectNum(secondMap.get(subjectName) == null ? 0 : secondMap.get(subjectName));

			list.add(dto);
		}

		return list;
	}
	
}