zoukankan      html  css  js  c++  java
  • 分配考场导出考场一整套流程

    #region 分配考场逻辑
    /// <summary>
    /// wjc
    /// 2015-4-3 16:53:57
    /// 分配考场逻辑
    /// 需要注意的有3点:1.考场机位数不够怎么办?  机位数不够不能分配考场,退出程序并提示用户
    ///          2.同一个考场同一时间可以考多个科目,目的是节约场次
    ///          3.同一个学生考试时间不能冲突,即同一个学生,不能在同一时间内既考语文又考数学
    /// </summary>
    /// <param name="examId"></param>
    /// <returns></returns>
    object startGive(string examId)
    {
    string institutionId = jc.User.Info.Prop1;
    int count = (from q in ExamInstution.CreateContext()
    where q.InstitutionId == institutionId && q.ExamId == examId
    select q).Count();
    if (count > 0)
    {
    return new { code = -1, msg = "考场已经提交,不能分配考场,若要重新分配考场,请联系省校教务处!" };
    }

    ReserveExam exam = ReserveExam.Get(examId);
    if (exam == null)
    {
    return new { code = -1, msg = "考试不存在,分配考场失败,请刷新页面重试" };
    }

    //获取考试-学生-科目关联
    List<ReserveStuExam> stuExamList = (from q in ReserveStuExam.CreateContext()
    where q.ExamId == examId && q.InstitutionId == institutionId
    orderby q.CourseId descending
    select q).ToList();
    if (stuExamList.Count == 0)
    {
    return new { code = -2, msg = "还没有学生参加考试无法分配考场" };
    }

    //获取所有的考场轮次
    List<ExamPlaceRound> placeRoundList = (from q in ExamPlaceRound.CreateContext()
    where q.ExamId == examId && q.InstitutionId == institutionId
    select q).ToList();

    List<ExamPlace> placeList = (from q in ExamPlace.CreateContext()
    where q.ExamId == examId && q.InstitutionId == institutionId
    select q).ToList();

    List<ExamRound> roundList = (from q in ExamRound.CreateContext()
    where q.ExamId == examId && q.InstitutionId == institutionId
    select q).ToList();


    if (placeRoundList.Count == 0)
    {
    return new { code = -3, msg = "还没有设置考场轮次,无法分配考场" };
    }


    //判断考场够不够用,若不够用,直接返回
    int studentCourseCount = stuExamList.Count();
    int roundCount = getPlaceRoundCount(examId);//机位数

    if (stuExamList.Count > roundCount)//备用机位和考场分配没有牵连
    {
    return new { code = -4, msg = "考场场次不够,无法分配考场" };
    }

    //从这里开始分配考场

    //需要注意的是,考场的一个轮次同时可以考多个科目,目的是节约场次,所以不用对科目进行限制
    ILinqContext<StuPlace> cx = StuPlace.CreateContext();
    bool flag = false;//分配考场结束标记

    List<ReserveStuExam> stuexam_list = new List<ReserveStuExam>();//该list用于批量更新考场分配状态
    foreach (ExamPlaceRound placeRound in placeRoundList)
    {
    if (flag) { break; }

    string placeId = placeRound.PlaceId;
    string roundId = placeRound.RoundId;
    ExamPlace place = (from q in placeList where q.Id == placeId select q).FirstOrDefault();
    if (place == null)
    {
    return new { code = -6, msg = "考场不存在,分配考场失败" };
    }

    ExamRound examRound = (from q in roundList where q.Id == roundId select q).FirstOrDefault();
    if (examRound == null)
    {
    return new { code = -7, msg = "考场轮次不存在,分配考场失败" };
    }

    int placeCount = place.PlaceCount;
    for (int i = 0; i < placeCount; i++)
    {
    if (stuExamList.Count == 0)
    {
    flag = true;
    break;
    }

    //这里取考场轮次不会重复的学生,如果不存在,直接退出本次循环
    List<ReserveStuExam> roundStuList = (from q in stuExamList
    where string.IsNullOrEmpty(q.RoundIds) || !q.RoundIds.Contains(roundId)
    select q).ToList();

    if (roundStuList.Count == 0)
    {
    break;
    }

    ReserveStuExam stuExam = roundStuList[0];
    stuexam_list.Add(stuExam);
    //插入考场分配表中去
    StuPlace stuPlace = new StuPlace();
    stuPlace.Id = StringUtil.UniqueId();
    stuPlace.CourseId = stuExam.CourseId;
    stuPlace.CreatorId = jc.User.Info.Id;
    stuPlace.DateCreated = DateTime.Now;
    stuPlace.StartTime = examRound.StartTime;
    stuPlace.EndTime = examRound.EndTime;
    stuPlace.UserId = stuExam.UserId;
    stuPlace.RoundId = examRound.Id;
    stuPlace.ExamId = examId;
    stuPlace.PlaceId = place.Id;
    stuPlace.InstitutionId = stuExam.InstitutionId;
    stuPlace.SeatNumber = i + 1;
    cx.Add(stuPlace, true);
    stuExamList.Remove(stuExam);//将分配好的学生从列表中删除
    //更新stuexamlist,这里并没有访问数据库,只是标记某些学生在该轮次中不能再被分配考场
    updateStuExamList(stuExamList, stuExam.UserId, examRound.Id);
    }
    }
    if (stuExamList.Count > 0)
    {
    return new { code = -9, msg = "考场场次不足,考场分配失败,请增加考场场次后再重新分配" };
    }

    //插入数据前,先删除历史数据
    StuPlace.Where("examid={0}", examId).Delete();

    cx.SubmitChanges();
    //更新学生科目关联表,更新考场分配状态

    updateReplaceState(stuexam_list);
    return new { code = 1, msg = "分配考场成功" };
    }

    void updateStuExamList(List<ReserveStuExam> stuExamList, string userId, string roundId)
    {
    List<ReserveStuExam> reserveList = (from q in stuExamList where q.UserId == userId select q).ToList();
    foreach (ReserveStuExam stuExam in reserveList)
    {
    stuExam.RoundIds += roundId + ",";
    }
    }

    /// <summary>
    /// wjc
    /// 2015-4-7 14:36:37
    /// 更新考场分配状态
    /// </summary>
    void updateReplaceState(List<ReserveStuExam> list)
    {
    Array ids = (from q in list select q.Id).ToArray();
    string formatIds = StringUtil.CollectionToCommaDelimitedString(ids, "'");
    ReserveStuExam.Where("id in ({0})", formatIds).Set("HaveRoom", true).Update();
    }

    /// <summary>
    /// wjc
    /// 2015-4-9 17:32:18
    /// 获取考场轮次所有的机位数
    /// </summary>
    /// <param name="examid"></param>
    /// <returns></returns>
    public int getPlaceRoundCount(string examId)
    {
    string institutionId = jc.User.Info.Prop1;
    List<ExamPlace> placeList = (from q in ExamPlace.CreateContext()
    where q.InstitutionId == institutionId && q.ExamId == examId
    select q).ToList();

    List<ExamPlaceRound> roundList = (from q in ExamPlaceRound.CreateContext()
    where q.InstitutionId == institutionId && q.ExamId == examId
    select q).ToList();

    int count = 0;
    foreach (ExamPlace place in placeList)
    {
    int roundCount = (from q in roundList where q.PlaceId == place.Id select q).Count();
    count += roundCount * place.PlaceCount;
    }
    return count;
    }
    #endregion

          



    #region 导出考场编排情况 /// <summary> /// wjc /// 导出考场分配情况,将一次性导出整个教学点本次考试的考场编排情况 /// 2015-4-9 10:05:29 /// 具体逻辑: /// ①系统生成多个excel /// ②将多个excel文件保存到temp文件夹里面去 /// ③压缩temp文件夹,并导出出来 /// </summary> /// <param name="nv"></param> /// <returns></returns> object startExportPlace(NameValueCollection nv) { string examId = nv["examId"]; string institutionId = jc.User.Info.Prop1; List<StuPlace> stuPlaceList = (from q in StuPlace.CreateContext() where q.InstitutionId == institutionId && q.ExamId == examId select q).ToList(); if (stuPlaceList.Count == 0) { return new { code = -1, msg = "考场未分配,不能导出考场编排情况。" }; } try { createXlsToFolder(stuPlaceList, examId); comprssXlsFoler(); } catch (Exception ex) { logger.Error("导出考场编排情况失败,失败原因" + ex.Message); return new { code = -1, msg = "导出考场编排情况失败" }; } string filePath = ServerUtil.MapPath(StringUtil.urldecode(jc.url(string.Format("/downmodel/考场编排结果.zip")))); string fileName = "考场编排结果.zip";//客户端保存的文件名 var file = new FileInfo(filePath); //得到文件 if (file.Exists) //判断文件是否存在 { jc.Context.Response.Clear(); //清空Response对象 /*设置浏览器请求头信息*/ jc.Context.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8)); //指定文件 jc.Context.Response.AddHeader("Content-Length", file.Length.ToString()); //指定文件大小 jc.Context.Response.ContentType = "application/application/octet-stream"; //指定输出方式 jc.Context.Response.WriteFile(file.FullName); //写出文件 jc.Context.Response.Flush(); //输出缓冲区(刷新Response对象) jc.Context.Response.Clear(); //清空Response对象 File.Delete(filePath);//删除压缩包文件 jc.Context.Response.End(); //结束Response对象 } return new { code = 1, msg = "下载成功" }; } /// <summary> /// wjc /// 2015-4-13 9:27:07 /// 生成excel到temp文件夹 /// </summary> /// <param name="stuPlaceList"></param> /// <param name="examId"></param> void createXlsToFolder(List<StuPlace> stuPlaceList, string examId) { var roundList = (from q in stuPlaceList select new { roundId = q.RoundId, placeId = q.PlaceId }).ToList().Distinct().ToList(); string institutionId = jc.User.Info.Prop1; //TODO这里是增量,后期可能会很大,最好的解决办法是再关联表中加学生姓名 List<ReserveStu> stuList = (from q in ReserveStu.CreateContext() where q.InstitutionId == institutionId select q).ToList(); List<ExamPlace> placeList = (from q in ExamPlace.CreateContext() where q.ExamId == examId && q.InstitutionId == institutionId select q).ToList(); List<ReserveCourseExam> courseList = (from q in ReserveCourseExam.CreateContext() //where q.ExamId == examId TODO这个是需要的,因为select2有bug所以暂时注释 select q).ToList(); //为压缩文件准备的temp文件夹,压缩成功后将删除 string tempFolderName = ServerUtil.MapPath(StringUtil.urldecode(jc.url("/downmodel/stuplace_temp"))); if (!Directory.Exists(tempFolderName)) { Directory.CreateDirectory(tempFolderName); } foreach (var roundPlace in roundList) { List<StuPlace> stuplace_list = (from q in stuPlaceList where q.RoundId == roundPlace.roundId && q.PlaceId == roundPlace.placeId orderby q.SeatNumber ascending select q).ToList(); if (stuplace_list.Count == 0) { continue; } string startTime = stuplace_list[0].StartTime.ToString(); string endTime = stuplace_list[0].EndTime.ToString(); string placeName = (from q in placeList where q.Id == roundPlace.placeId select q.Name).FirstOrDefault(); string xlsPath = ServerUtil.MapPath(StringUtil.urldecode(jc.url("/downmodel/导出考场编排模板.xls"))); FileStream file = new FileStream(xlsPath, FileMode.Open, FileAccess.ReadWrite); HSSFWorkbook book = new HSSFWorkbook(file); HSSFSheet sheet = (HSSFSheet)book.GetSheetAt(0); sheet.GetRow(0).GetCell(0).SetCellValue("考试时间:" + startTime + "~" + endTime); //这里需要注意的是第一行为签到单的头部,第二行为签到单的列名称,第三行才是真正有价值的信息 for (int i = 0; i < stuplace_list.Count; i++) { StuPlace sp = stuplace_list[i]; string studentId = sp.UserId; ReserveStu stu = (from q in stuList where q.Id == studentId select q).FirstOrDefault(); string courseId = sp.CourseId; string courseName = (from q in courseList where q.Id == courseId select q.CourseName).FirstOrDefault(); if (stu == null) { continue; }//理论上不会存在这种情况 string stuName = stu.DisplayName; string stuNum = stu.LicenseNumber; string seatNum = sp.SeatNumber.ToString(); sheet.CreateRow(i + 2); sheet.GetRow(i + 2).CreateCell(0); sheet.GetRow(i + 2).CreateCell(1); sheet.GetRow(i + 2).CreateCell(2); sheet.GetRow(i + 2).CreateCell(3); sheet.GetRow(i + 2).GetCell(0).SetCellValue(stuName); sheet.GetRow(i + 2).GetCell(1).SetCellValue(stuNum); sheet.GetRow(i + 2).GetCell(2).SetCellValue(seatNum); sheet.GetRow(i + 2).GetCell(3).SetCellValue(courseName); } string startPlaceTime = stuplace_list[0].StartTime.ToString("yyyyMMddHHmmss");; string endPlaceTime = stuplace_list[0].EndTime.ToString("yyyyMMddHHmmss");; string xlsName = placeName + startPlaceTime + endPlaceTime; string saveXlsPath = ServerUtil.MapPath(StringUtil.urldecode(jc.url(string.Format("/downmodel/stuplace_temp/{0}.xls", xlsName)))); FileStream saveXlsStream = new FileStream(saveXlsPath, FileMode.Create); book.Write(saveXlsStream); file.Close(); saveXlsStream.Close(); } } /// <summary> /// wjc /// 2015-4-10 9:08:43 /// 压缩excel所在的文件夹 /// </summary> void comprssXlsFoler() { string dirPath = ServerUtil.MapPath(StringUtil.urldecode(jc.url("/downmodel/stuplace_temp"))); string zipName = ServerUtil.MapPath(StringUtil.urldecode(jc.url(string.Format("/downmodel/考场编排结果.zip")))); using (ZipOutputStream zipOutPutStream = new ZipOutputStream(File.Create(zipName))) { zipOutPutStream.SetLevel(5); Crc32 crc = new Crc32(); Dictionary<string, DateTime> fileList = GetAllFies(dirPath); foreach (KeyValuePair<string, DateTime> item in fileList) { FileStream fs = File.OpenRead(item.Key.ToString()); byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); ZipEntry entry = new ZipEntry(item.Key.Substring(dirPath.Length)); entry.DateTime = item.Value; entry.Size = fs.Length; fs.Close(); crc.Reset(); crc.Update(buffer); entry.Crc = crc.Value; zipOutPutStream.PutNextEntry(entry); zipOutPutStream.Write(buffer, 0, buffer.Length); } Directory.Delete(dirPath, true);//文件压缩成功后,删除temp文件夹 } } /// <summary> ///wjc ///获取文件下所有文件 ///2015-4-13 9:22:29 /// </summary> /// <param name="dir"></param> /// <returns></returns> Dictionary<string, DateTime> GetAllFies(string dir) { Dictionary<string, DateTime> filesList = new Dictionary<string, DateTime>(); DirectoryInfo fileDire = new DirectoryInfo(dir); if (!fileDire.Exists) { throw new System.IO.FileNotFoundException("目录:" + fileDire.FullName + "没有找到!"); } foreach (FileInfo file in fileDire.GetFiles("*.*")) { filesList.Add(file.FullName, file.LastWriteTime); } return filesList; } #endregion
  • 相关阅读:
    JQuery学习笔记(1)——选择器
    Web前端——表单提交和Js添加选项
    Web前端——JavaScript练习
    Web前端——css
    Web前端——JavaScript笔记
    sirius的学习笔记(2)
    sirius的python学习笔记(1)
    Get和Post的请求
    IIS的配置
    一般处理程序aspx
  • 原文地址:https://www.cnblogs.com/wjcnet/p/4462955.html
Copyright © 2011-2022 走看看