package com.koubei.feedback.service.impl;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.koubei.feedback.dao.ICategoryDAO;
import com.koubei.feedback.dao.IQuestionnaireDAO;
import com.koubei.feedback.dao.ISuggestionDAO;
import com.koubei.feedback.dao.ISurveyDAO;
import com.koubei.feedback.dao.IWordDAO;
import com.koubei.feedback.dao.IinformationDAO;
import com.koubei.feedback.dao.IitemDAO;
import com.koubei.feedback.model.Category;
import com.koubei.feedback.model.Information;
import com.koubei.feedback.model.Questionnaire;
import com.koubei.feedback.model.Suggestion;
import com.koubei.feedback.model.Survey;
import com.koubei.feedback.model.Word;
import com.koubei.feedback.service.IQuestionnaireService;
public class QuestionnaireService implements IQuestionnaireService {
private IQuestionnaireDAO quesionnaireDao;
private IWordDAO wordDao;
private ISurveyDAO surveyDao;
private ICategoryDAO categoryDao;
private IinformationDAO informationDao;
private ISuggestionDAO suggestionDao;
private IitemDAO itemDao;
private IWordDAO wordDAO;
/**
* 删除questionnaire及其子项
* @author yuanmao.wsj
* @throws Exception
*/
public void deleteQuestionnaireByQuestionnaireId(int questionnaireId) throws Exception{
//查询questionnaire对应子项的ID列表
List<Category> categoryList = categoryDao.getCategoryByQuestionnaireId(questionnaireId);
List<Information> informationList = informationDao.getInformationByQuestionnaireId(questionnaireId);
List<Suggestion> suggestionList = suggestionDao.getSuggestionByQuestionnaireId(questionnaireId);
//删除information对应的子项
if(informationList.size() != 1) throw new Exception("删除information时异常,List大小不等于1");
int informationId = informationList.get(0).getInformationId();
itemDao.deleteItemByInformationId(informationId);
//删除category对应的子项
for (Category category : categoryList) {
int categoryId = category.getCategoryId();
surveyDao.deleteSurveyByCategoryId(categoryId);
}
//删除suggestion对应的子项
for (Suggestion suggestion : suggestionList) {
int suggestionId = suggestion.getSuggestionId();
wordDAO.deleteWordBySuggestionId(suggestionId);
}
//删除questionnaire及其子项
categoryDao.deleteCategoryByQuestionnaireId(questionnaireId);
informationDao.deleteInformationByQuestionnaireId(questionnaireId);
suggestionDao.deleteSuggestionByQuestionnaireId(questionnaireId);
quesionnaireDao.deleteQuestionnaireByQuestionnaireId(questionnaireId);
}
/**
* 通过UserId得到问卷列表
* @author yuanmao.wsj
* 2011-3-26
* @param userId
* @return Questionnaire
*/
public List<Questionnaire> getQuestionnaireByUserId(int userId)
{
return quesionnaireDao.getQuestionnaireByUserId(userId);
}
@Override
public Questionnaire getQuestionnaireByQuestionnaireId(int questionnaireId) {
return quesionnaireDao.getQuestionnaireByQuestionnaireId(questionnaireId);
}
@Override
public int insertQuestionnaire(Questionnaire questionnaire) {
return quesionnaireDao.insertQuestionnaire(questionnaire);
}
@Override
public void updateQuestionnaire(Questionnaire questionnaire) {
quesionnaireDao.updateQuestionnaire(questionnaire);
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public void acceptQuestionnire(Map<Integer, Integer> scoreMap,
Map<Integer, String> wordMap,int questionnaireId) {
//获得参与总人数
Questionnaire questionnaire = quesionnaireDao.getQuestionnaireByQuestionnaireId(questionnaireId);
int participantNumber = questionnaire.getParticipantNumber();
/**
* 更新survey中的平均分
*/
Iterator surveyId = scoreMap.entrySet().iterator();
Set<Integer> categorySet = new HashSet<Integer>();//保存该请求提交过来的survey中所有的categoryId,保证不重复
while(surveyId.hasNext()){
Map.Entry<Integer, Integer> entry = (Map.Entry<Integer, Integer>)surveyId.next();
//获得数据库中的survey
Survey survey = surveyDao.getSurveyBySurveyId(entry.getKey());
//目前的平均分
float score = survey.getSurveyScore();
//重新计算平均分
score = (score*participantNumber+entry.getValue())/(float)(participantNumber+1);
//储存,更新数据库
survey.setSurveyScore(score);
surveyDao.updateSurvey(survey);
categorySet.add(survey.getCategoryId());
}
/**
* 处理category
*/
Iterator<Integer> categoryIt = categorySet.iterator();
while(categoryIt.hasNext()) {
int categoryId = categoryIt.next();
//拿到该category下面的survey
List<Survey> surveyList = surveyDao.getSurveyByCategoryId(categoryId);
float totalSurveyScore = 0;
for(Survey s : surveyList){
totalSurveyScore+=s.getSurveyScore();
}
//求平均分
float categoryScore = totalSurveyScore/(float)surveyList.size();
Category category = categoryDao.getCategoryBycategoryId(categoryId);
//更改并更新数据库
category.setCategoryScore(categoryScore);
categoryDao.updateCategory(category);
}
/**
* 处理questionnaire
*/
List<Category> categoryList = categoryDao.getCategoryByQuestionnaireId(questionnaire.getQuestionnaireId());
float totalCategoryScore = 0;
for(Category c : categoryList) {
totalCategoryScore += c.getCategoryScore();
}
//求平均并保存数据库
questionnaire.setScore(totalCategoryScore/categoryList.size());
//人数加1,并更新数据库
questionnaire.setParticipantNumber(participantNumber+1);
quesionnaireDao.updateQuestionnaire(questionnaire);
/**
* 插入word中的content
*/
Iterator wordId = wordMap.entrySet().iterator();
while(wordId.hasNext()) {
Map.Entry<Integer, String> entry = (Map.Entry<Integer, String>)wordId.next();
Word word = new Word();
//获得word的外键
word.setSuggestionId(entry.getKey());
//获得评论的内容
word.setWordContent(entry.getValue());
wordDao.insertWord(word);
}
}
public IQuestionnaireDAO getQuesionnaireDao() {
return quesionnaireDao;
}
public void setQuesionnaireDao(IQuestionnaireDAO quesionnaireDao) {
this.quesionnaireDao = quesionnaireDao;
}
public IWordDAO getWordDao() {
return wordDao;
}
public void setWordDao(IWordDAO wordDao) {
this.wordDao = wordDao;
}
public ISurveyDAO getSurveyDao() {
return surveyDao;
}
public void setSurveyDao(ISurveyDAO surveyDao) {
this.surveyDao = surveyDao;
}
public ICategoryDAO getCategoryDao() {
return categoryDao;
}
public void setCategoryDao(ICategoryDAO categoryDao) {
this.categoryDao = categoryDao;
}
public IinformationDAO getInformationDao() {
return informationDao;
}
public void setInformationDao(IinformationDAO informationDao) {
this.informationDao = informationDao;
}
public ISuggestionDAO getSuggestionDao() {
return suggestionDao;
}
public void setSuggestionDao(ISuggestionDAO suggestionDao) {
this.suggestionDao = suggestionDao;
}
public IitemDAO getItemDao() {
return itemDao;
}
public void setItemDao(IitemDAO itemDao) {
this.itemDao = itemDao;
}
public IWordDAO getWordDAO() {
return wordDAO;
}
public void setWordDAO(IWordDAO wordDAO) {
this.wordDAO = wordDAO;
}
}