示例#1
0
        private void Import_To_Grid(string FilePath, string Extension, string isHDR)
        {
            logger.Debug(String.Format("####### --- Begin Start Load File at {0} --- ####", DateTime.Now));
            int rejected = 0;
            List <TB_SCORE_ROUND_1> stdList         = new List <TB_SCORE_ROUND_1>();
            List <TB_EXAM_ABSENT>   stdAppsentList  = new List <TB_EXAM_ABSENT>();
            List <TB_CONCERN>       stdConcerntList = new List <TB_CONCERN>();

            using (ChinaPhet10Entities context = new ChinaPhet10Entities())
            {
                context.Configuration.AutoDetectChangesEnabled = false;
                using (CsvFileReader reader = new CsvFileReader(FilePath))
                {
                    CsvRow row = new CsvRow();
                    while (reader.ReadRow(row))
                    {
                        try
                        {
                            int stdCode = ((row[0] == null) ? 0 : (row[0].Equals("")) ? 0 : Convert.ToInt32(row[0]));
                            if (!stdList.Any(x => x.STD_CODE == stdCode))
                            {
                                TB_SCORE_ROUND_1 round1 = new TB_SCORE_ROUND_1();
                                round1.STD_CODE    = stdCode;
                                round1.ROUND_SCORE = ((row[1] == null) ? 0 : (row[1].Equals("")) ? 0 : Convert.ToInt32(row[1]));
                                round1.PRIZE_ID    = 0;
                                stdList.Add(round1);
                                //บันทึกขาดสอบ
                                if (row[2].ToString().Equals("F") && round1.ROUND_SCORE == 0)
                                {
                                    if (!stdAppsentList.Any(x => x.STD_CODE == stdCode))
                                    {
                                        TB_EXAM_ABSENT tb_exam_absent = new TB_EXAM_ABSENT();
                                        tb_exam_absent.STD_CODE = stdCode;
                                        stdAppsentList.Add(tb_exam_absent);
                                    }
                                }
                                //สงสัยว่าผิดระดับชั้น
                                if (row[3].ToString().Equals("T"))
                                {
                                    if (!stdConcerntList.Any(x => x.STD_CODE == stdCode))
                                    {
                                        TB_CONCERN concern = new TB_CONCERN();
                                        concern.STD_CODE = stdCode;
                                        stdConcerntList.Add(concern);
                                    }
                                }
                            }
                            else
                            {
                                rejected++;
                                logger.Debug("Reject>>duplicate student code:" + stdCode);
                            }
                        }
                        catch (Exception ex)
                        {
                            logger.Error(ex.Message);
                        }
                    }
                }
                //Begin Insert
                foreach (TB_EXAM_ABSENT _val in stdAppsentList)
                {
                    context.TB_EXAM_ABSENT.Add(_val);
                }
                context.SaveChanges();
                foreach (TB_CONCERN _val in stdConcerntList)
                {
                    context.TB_CONCERN.Add(_val);
                }
                context.SaveChanges();
                int index = 1;
                foreach (TB_SCORE_ROUND_1 _val in stdList)
                {
                    context.TB_SCORE_ROUND_1.Add(_val);
                    index++;
                    if (index % 200 == 0)
                    {
                        context.SaveChanges();
                    }
                }
                context.SaveChanges();
            }
            logger.Debug("######## SUMMARY ########");
            logger.Debug(String.Format("Total {0} (s),Reject {1} (s)>>>> Remain {2} (s)", stdList.Count, rejected, stdList.Count - rejected));
            logger.Debug(String.Format("####### --- Finish Load File at {0} --- ####", DateTime.Now));
        }
        public ActionResult Index1(Round1ReportModelCriteria _model)
        {
            if (Session["USER"] == null)
            {
                return(RedirectToAction("../Account/ManagementLogin"));
            }

            List <TB_APPLICATION_STUDENT> studentLists = db.TB_APPLICATION_STUDENT.Where(s => s.STD_LEVEL_ID == _model.studentLevel).OrderBy(s => s.STD_ID).ToList();
            List <TB_M_LEVEL>             tbMLevel     = db.TB_M_LEVEL.Where(l => l.LEVEL_ID > 0).ToList();

            if (studentLists != null)
            {
                if (studentLists.Count > 0)
                {
                    if (_model.action.Equals("บันทึก"))
                    {
                        //ล้างข้อมูลเก่าออก
                        foreach (TB_APPLICATION_STUDENT std in studentLists)
                        {
                            TB_EXAM_ABSENT studentSeat = db.TB_EXAM_ABSENT.Where(s => s.STD_CODE == std.TB_STUDENT_SEAT.STUDENT_CODE).FirstOrDefault();
                            if (studentSeat != null)
                            {
                                db.TB_EXAM_ABSENT.Remove(studentSeat);
                            }
                        }
                        //บันทึกเด็กที่ขาดใหม่
                        foreach (String id in _model.SelectedStudentIDs)
                        {
                            int            absentId       = Convert.ToInt32(id);
                            TB_EXAM_ABSENT tb_exam_absent = new TB_EXAM_ABSENT();
                            tb_exam_absent.STD_CODE = absentId;
                            db.TB_EXAM_ABSENT.Add(tb_exam_absent);
                        }
                        db.SaveChanges();
                    }

                    List <Round1Report01Model> lists = new List <Round1Report01Model>();
                    int seq = 1;
                    foreach (TB_APPLICATION_STUDENT std in studentLists)
                    {
                        if (std.TB_STUDENT_SEAT == null)
                        {
                            ViewBag.ResultMsg = "ยังไม่ได้จัดผังสอบ ไม่สามารถทำรายการได้!";

                            ViewBag.StudentLevel = new SelectList(tbMLevel, "LEVEL_ID", "LEVEL_NAME_TH", _model.studentLevel);
                            return(View(_model));
                        }


                        Round1Report01Model rpt = new Round1Report01Model();
                        rpt.seq             = seq;
                        rpt.studentCode     = std.TB_STUDENT_SEAT.STUDENT_CODE + "";
                        rpt.studentFullName = std.TB_M_TITLE.TITLE_NAME_TH + std.STD_NAME + "  " + std.STD_SURNAME;
                        rpt.schoolName      = std.TB_APPLICATION_SCHOOL.SCHOOL_NAME;
                        rpt.remark          = "";
                        seq++;
                        lists.Add(rpt);
                    }

                    List <String> absentStudents = new List <string>();
                    foreach (TB_EXAM_ABSENT exam in db.TB_EXAM_ABSENT.ToList())
                    {
                        absentStudents.Add(exam.STD_CODE + "");
                    }
                    if (lists != null)
                    {
                        var pageIndex = _model.Page ?? 1;
                        _model.reports            = lists.ToPagedList(pageIndex, 50);
                        _model.SelectedStudentIDs = absentStudents;
                    }
                }
            }
            ViewBag.StudentLevel = new SelectList(tbMLevel, "LEVEL_ID", "LEVEL_NAME_TH", _model.studentLevel);
            return(View(_model));
        }