package com.qmth.qrzk.repository.user.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

import com.qmth.qrzk.repository.common.base.BaseDao;
import com.qmth.qrzk.repository.user.model.TeacherAssessment;
import com.qmth.qrzk.repository.user.model.TeacherAssessmentDetail;

@Service
public class TeacherAssessmentServiceImpl {
	@Autowired private BaseDao dao;
	
	@Autowired 
	private PaperTaskServiceImpl paperTaskService;
	
	public TeacherAssessment queryAssessmentByTeacherIdAndTaskId(int teacherId, int taskId){
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("teacherId", teacherId);
		params.put("taskId", taskId);
		return dao.get("queryAssessmentByTeacherIdAndTaskId", params);
	}
	
	public List<TeacherAssessment> queryAssessmentsByBatchId(int batchId){
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("batchId", batchId);
		List<TeacherAssessment> assessments = dao.getList("queryAssessmentsByBatchId", params);
		for(TeacherAssessment assessment : assessments){
			assessment.setTask(paperTaskService.getPaperTaskById(assessment.getTaskId()));
		}
		return assessments;
	}
	
	public void addTeacherAssessment(TeacherAssessment assessment){
		dao.insert("addTeacherAssessment", assessment);
		List<TeacherAssessmentDetail> details = assessment.getDetails();
		for (TeacherAssessmentDetail detail : details) {
			detail.setTeacherAssessmentId(assessment.getId());
		}
		
		for(TeacherAssessmentDetail detail : details){
			dao.insert("addTeacherAssessmentDetail", detail);
		}
	}
	
	public void update(TeacherAssessment assessment){
		dao.update("updateTeacherAssessment", assessment);
		List<TeacherAssessmentDetail> details = assessment.getDetails();
		for (TeacherAssessmentDetail detail : details) {
			detail.setTeacherAssessmentId(assessment.getId());
			dao.update("updateTeacherAssessmentDetail", detail);
		}
		
	}
}
