//获取投票人数 public int getVoteCount(Question q) { VoteDao votedao = new VoteDao(); List <Vote> votelist = votedao.getByQuestion(q); QuestionDao questiondao = new Dao.QuestionDao(); q.voteCount = votelist.Count; questiondao.update(q); return(q.voteCount); }
//获取统计结果 public String getOptionData(Question q) { String optionData = null; VoteDao votedao = new VoteDao(); List <Vote> votelist = votedao.getByQuestion(q); if (q.optionNum == 1) { if (votelist.Count != 0) { int sum = 0; foreach (Vote v in votelist) { sum += int.Parse(v.voteRecord); } optionData = (sum / votelist.Count).ToString();//取投票结果的平均值 } else { optionData = 0.ToString();//没有投票记录时结果为0 } } else { //多选 [{'value':50,'name':'直接访问'},{'value':50,'name':'邮件营销'},{'value':50,'name':'联盟广告'},{'value':50,'name':'视频广告'},{'value':50,'name':'搜索引擎'}] JArray jarry = new JArray(); String[] names = q.queOption.Split('#'); int[] values = null; foreach (Vote v in votelist) { String[] records = v.voteRecord.Split('#'); int[] sum = new int[records.Length - 1]; //统计每个选项的总分 for (int i = 0; i < records.Length - 1; i++) { sum[i] += int.Parse(records[i]); } //计算平均分 for (int i = 0; i < sum.Length; i++) { sum[i] /= votelist.Count; } values = sum; } if (values == null) { values = new int[q.optionNum]; } //组装 for (int i = 0; i < names.Length - 1; i++) { JObject json = new JObject(); json["name"] = names[i]; json["value"] = values[i]; jarry.Add(json); } optionData = jarry.ToString(); } QuestionDao questiondao = new Dao.QuestionDao(); q.optionData = optionData; questiondao.update(q); return(q.optionData); }