コード例 #1
0
ファイル: HocTapController.cs プロジェクト: trunglam12/SMS
        public JsonResult UpdateBangDiem(string models, int idNam, string idHocKy)
        {
            try
            {
                var viewModels = JsonConvert.DeserializeObject <IEnumerable <BangDiemViewModel> >(models);
                IEnumerable <LoaiDiem> listLoaiDiem = _HocTapService.GetAllLoaiDiem();
                //loai diem phai ton tai truoc
                LoaiDiem loaiDiemMieng = listLoaiDiem.Where(m => m.TenLoaiDiem == "Điểm Miệng").FirstOrDefault();
                LoaiDiem loaiDiem15    = listLoaiDiem.Where(m => m.TenLoaiDiem == "Điểm 15'").FirstOrDefault();
                LoaiDiem loaiDiem1T    = listLoaiDiem.Where(m => m.TenLoaiDiem == "Điểm 1 Tiết").FirstOrDefault();
                LoaiDiem loaiDiemHK    = listLoaiDiem.Where(m => m.TenLoaiDiem == "Điểm Học Kỳ").FirstOrDefault();
                foreach (BangDiemViewModel model in viewModels)
                {
                    List <CotDiem> listCotDiemTemp = new List <CotDiem>();    //store for calculate dtb
                    //normal case, BangDiem already exist
                    if (model.MaBangDiem > 0)
                    {
                        BangDiemHKMH bangDiem = _HocTapService.GetBangDiemByID(model.MaBangDiem);
                        //Mieng 1
                        if (model.MaDiemMieng_1 != -1)
                        {
                            if (model.DiemMieng_1 != null)
                            {
                                CotDiem cotDiem = _HocTapService.GetCotDiemByID(model.MaDiemMieng_1);
                                cotDiem.GiaTri = (float)model.DiemMieng_1;
                                _HocTapService.UpdateCotDiem(cotDiem);
                            }
                            else
                            {
                                CotDiem cotDiem = _HocTapService.GetCotDiemByID(model.MaDiemMieng_1);
                                _HocTapService.DeleteCotDiem(cotDiem);
                            }
                        }
                        else
                        {
                            if (model.MaDiemMieng_1 == -1 && model.DiemMieng_1 != null)
                            {
                                CotDiem cotDiem = new CotDiem();
                                cotDiem.GiaTri     = (float)model.DiemMieng_1;
                                cotDiem.LoaiDiem   = loaiDiemMieng;
                                cotDiem.TenCotDiem = "cot diem";
                                cotDiem.MaBangDiem = bangDiem.MaBangDiem;
                                _HocTapService.InsertCotDiem(cotDiem);
                                listCotDiemTemp.Add(cotDiem);
                            }
                        }

                        //Mieng 2
                        if (model.MaDiemMieng_2 != -1)
                        {
                            if (model.DiemMieng_2 != null)
                            {
                                CotDiem cotDiem = _HocTapService.GetCotDiemByID(model.MaDiemMieng_2);
                                cotDiem.GiaTri = (float)model.DiemMieng_2;
                                _HocTapService.UpdateCotDiem(cotDiem);
                            }
                            else
                            {
                                CotDiem cotDiem = _HocTapService.GetCotDiemByID(model.MaDiemMieng_2);
                                _HocTapService.DeleteCotDiem(cotDiem);
                            }
                        }
                        else
                        {
                            if (model.MaDiemMieng_2 == -1 && model.DiemMieng_2 != null)
                            {
                                CotDiem cotDiem = new CotDiem();
                                cotDiem.GiaTri     = (float)model.DiemMieng_2;
                                cotDiem.LoaiDiem   = loaiDiemMieng;
                                cotDiem.TenCotDiem = "cot diem";
                                cotDiem.MaBangDiem = bangDiem.MaBangDiem;
                                _HocTapService.InsertCotDiem(cotDiem);
                                listCotDiemTemp.Add(cotDiem);
                            }
                        }

                        //15' 1
                        if (model.MaDiem15_1 != -1)
                        {
                            if (model.Diem15_1 != null)
                            {
                                CotDiem cotDiem = _HocTapService.GetCotDiemByID(model.MaDiem15_1);
                                cotDiem.GiaTri = (float)model.Diem15_1;
                                _HocTapService.UpdateCotDiem(cotDiem);
                            }
                            else
                            {
                                CotDiem cotDiem = _HocTapService.GetCotDiemByID(model.MaDiem15_1);
                                _HocTapService.DeleteCotDiem(cotDiem);
                            }
                        }
                        else
                        {
                            if (model.MaDiem15_1 == -1 && model.Diem15_1 != null)
                            {
                                CotDiem cotDiem = new CotDiem();
                                cotDiem.GiaTri     = (float)model.Diem15_1;
                                cotDiem.LoaiDiem   = loaiDiem15;
                                cotDiem.TenCotDiem = "cot diem";
                                cotDiem.MaBangDiem = bangDiem.MaBangDiem;
                                _HocTapService.InsertCotDiem(cotDiem);
                                listCotDiemTemp.Add(cotDiem);
                            }
                        }

                        //15' 2
                        if (model.MaDiem15_2 != -1)
                        {
                            if (model.Diem15_2 != null)
                            {
                                CotDiem cotDiem = _HocTapService.GetCotDiemByID(model.MaDiem15_2);
                                cotDiem.GiaTri = (float)model.Diem15_2;
                                _HocTapService.UpdateCotDiem(cotDiem);
                            }
                            else
                            {
                                CotDiem cotDiem = _HocTapService.GetCotDiemByID(model.MaDiem15_2);
                                _HocTapService.DeleteCotDiem(cotDiem);
                            }
                        }
                        else
                        {
                            if (model.MaDiem15_2 == -1 && model.Diem15_2 != null)
                            {
                                CotDiem cotDiem = new CotDiem();
                                cotDiem.GiaTri     = (float)model.Diem15_2;
                                cotDiem.LoaiDiem   = loaiDiem15;
                                cotDiem.TenCotDiem = "cot diem";
                                cotDiem.MaBangDiem = bangDiem.MaBangDiem;
                                _HocTapService.InsertCotDiem(cotDiem);
                                listCotDiemTemp.Add(cotDiem);
                            }
                        }

                        //1 Tiet 1
                        if (model.MaDiem1T_1 != -1)
                        {
                            if (model.Diem1T_1 != null)
                            {
                                CotDiem cotDiem = _HocTapService.GetCotDiemByID(model.MaDiem1T_1);
                                cotDiem.GiaTri = (float)model.Diem1T_1;
                                _HocTapService.UpdateCotDiem(cotDiem);
                            }
                            else
                            {
                                CotDiem cotDiem = _HocTapService.GetCotDiemByID(model.MaDiem1T_1);
                                _HocTapService.DeleteCotDiem(cotDiem);
                            }
                        }
                        else
                        {
                            if (model.MaDiem1T_1 == -1 && model.Diem1T_1 != null)
                            {
                                CotDiem cotDiem = new CotDiem();
                                cotDiem.GiaTri     = (float)model.Diem1T_1;
                                cotDiem.LoaiDiem   = loaiDiem1T;
                                cotDiem.TenCotDiem = "cot diem";
                                cotDiem.MaBangDiem = bangDiem.MaBangDiem;
                                _HocTapService.InsertCotDiem(cotDiem);
                                listCotDiemTemp.Add(cotDiem);
                            }
                        }

                        //1 Tiet 2
                        if (model.MaDiem1T_2 != -1)
                        {
                            if (model.Diem1T_2 != null)
                            {
                                CotDiem cotDiem = _HocTapService.GetCotDiemByID(model.MaDiem1T_2);
                                cotDiem.GiaTri = (float)model.Diem1T_2;
                                _HocTapService.UpdateCotDiem(cotDiem);
                            }
                            else
                            {
                                CotDiem cotDiem = _HocTapService.GetCotDiemByID(model.MaDiem1T_2);
                                _HocTapService.DeleteCotDiem(cotDiem);
                            }
                        }
                        else
                        {
                            if (model.MaDiem1T_2 == -1 && model.Diem1T_2 != null)
                            {
                                CotDiem cotDiem = new CotDiem();
                                cotDiem.GiaTri     = (float)model.Diem1T_2;
                                cotDiem.LoaiDiem   = loaiDiem1T;
                                cotDiem.TenCotDiem = "cot diem";
                                cotDiem.MaBangDiem = model.MaBangDiem;
                                cotDiem.MaBangDiem = bangDiem.MaBangDiem;
                                _HocTapService.InsertCotDiem(cotDiem);
                                listCotDiemTemp.Add(cotDiem);
                            }
                        }

                        //Hoc ky
                        if (model.MaDiemHK != -1)
                        {
                            if (model.DiemHK != null)
                            {
                                CotDiem cotDiem = _HocTapService.GetCotDiemByID(model.MaDiemHK);
                                cotDiem.GiaTri = (float)model.DiemHK;
                                _HocTapService.UpdateCotDiem(cotDiem);
                            }
                            else
                            {
                                CotDiem cotDiem = _HocTapService.GetCotDiemByID(model.MaDiemHK);
                                _HocTapService.DeleteCotDiem(cotDiem);
                            }
                        }
                        else
                        {
                            if (model.MaDiemHK == -1 && model.DiemHK != null)
                            {
                                CotDiem cotDiem = new CotDiem();
                                cotDiem.GiaTri     = (float)model.DiemHK;
                                cotDiem.LoaiDiem   = loaiDiemHK;
                                cotDiem.TenCotDiem = "cot diem";
                                cotDiem.MaBangDiem = bangDiem.MaBangDiem;
                                _HocTapService.InsertCotDiem(cotDiem);
                                listCotDiemTemp.Add(cotDiem);
                            }
                        }

                        bangDiem = _HocTapService.GetBangDiemByID(model.MaBangDiem);
                        float sumDiem = 0;
                        float sumHeSo = 0;
                        foreach (CotDiem cotDiem in bangDiem.dsCotDiem)
                        {
                            sumDiem += cotDiem.GiaTri * cotDiem.LoaiDiem.HeSo;
                            sumHeSo += cotDiem.LoaiDiem.HeSo;
                        }
                        bangDiem.DiemTB = sumDiem / sumHeSo;
                        _HocTapService.UpdateBangDiem(bangDiem);
                        model.DiemTrungBinh = bangDiem.DiemTB;
                    }
                    else    //bang diem moi
                    {
                        BangDiemHKMH bangDiem = new BangDiemHKMH();
                        bangDiem.MaHocKy   = idHocKy;
                        bangDiem.MaHocSinh = model.MaHocSinh;
                        bangDiem.MaMonHoc  = model.MaMonHoc;
                        bangDiem.MaNamHoc  = idNam;
                        bangDiem.DiemTB    = 0;
                        _HocTapService.InsertBangDiem(bangDiem);
                        //mieng 1
                        if (model.DiemMieng_1 != null)
                        {
                            listCotDiemTemp.Add(new CotDiem()
                            {
                                GiaTri     = (float)model.DiemMieng_1,
                                LoaiDiem   = loaiDiemMieng,
                                TenCotDiem = "cot diem",
                                MaBangDiem = bangDiem.MaBangDiem
                            });
                        }

                        //1 tiet 1
                        if (model.DiemMieng_2 != null)
                        {
                            listCotDiemTemp.Add(new CotDiem()
                            {
                                GiaTri     = (float)model.DiemMieng_2,
                                LoaiDiem   = loaiDiemMieng,
                                TenCotDiem = "cot diem",
                                MaBangDiem = bangDiem.MaBangDiem
                            });
                        }

                        //15' 1
                        if (model.Diem15_1 != null)
                        {
                            listCotDiemTemp.Add(new CotDiem()
                            {
                                GiaTri     = (float)model.Diem15_1,
                                LoaiDiem   = loaiDiem15,
                                TenCotDiem = "cot diem",
                                MaBangDiem = bangDiem.MaBangDiem
                            });
                        }

                        //15' 2
                        if (model.Diem15_2 != null)
                        {
                            listCotDiemTemp.Add(new CotDiem()
                            {
                                GiaTri     = (float)model.Diem15_2,
                                LoaiDiem   = loaiDiem15,
                                TenCotDiem = "cot diem",
                                MaBangDiem = bangDiem.MaBangDiem
                            });
                        }

                        //1 tiet 1
                        if (model.Diem1T_1 != null)
                        {
                            listCotDiemTemp.Add(new CotDiem()
                            {
                                GiaTri     = (float)model.Diem1T_1,
                                LoaiDiem   = loaiDiem1T,
                                TenCotDiem = "cot diem",
                                MaBangDiem = bangDiem.MaBangDiem
                            });
                        }

                        //1 tiet 2
                        if (model.Diem1T_2 != null)
                        {
                            listCotDiemTemp.Add(new CotDiem()
                            {
                                GiaTri     = (float)model.Diem1T_2,
                                LoaiDiem   = loaiDiem1T,
                                TenCotDiem = "cot diem",
                                MaBangDiem = bangDiem.MaBangDiem
                            });
                        }

                        //hk
                        if (model.DiemHK != null)
                        {
                            listCotDiemTemp.Add(new CotDiem()
                            {
                                GiaTri     = (float)model.DiemHK,
                                LoaiDiem   = loaiDiemHK,
                                TenCotDiem = "cot diem",
                                MaBangDiem = bangDiem.MaBangDiem
                            });
                        }

                        float sumDiem = 0;
                        float sumHeSo = 0;
                        foreach (CotDiem cotDiem in listCotDiemTemp)
                        {
                            sumDiem += cotDiem.GiaTri * cotDiem.LoaiDiem.HeSo;
                            sumHeSo += cotDiem.LoaiDiem.HeSo;
                        }
                        bangDiem.DiemTB = sumDiem / sumHeSo;
                        _HocTapService.InsertCotDiem(listCotDiemTemp);
                        _HocTapService.UpdateBangDiem(bangDiem);
                        model.DiemTrungBinh = bangDiem.DiemTB;
                        model.MaBangDiem    = bangDiem.MaBangDiem;
                    }
                }
                return(Json(viewModels));
            }
            catch (Exception e)
            {
                return(Json(new { error = e.Message }));
            }
        }
コード例 #2
0
ファイル: HocTapService.cs プロジェクト: trunglam12/SMS
 /// <summary>
 /// delete CotDiem
 /// </summary>
 /// <param name="entity">CotDiem</param>
 public void DeleteCotDiem(CotDiem entity)
 {
     _CotDiemRepository.Delete(entity);
 }
コード例 #3
0
ファイル: HocTapService.cs プロジェクト: trunglam12/SMS
 /// <summary>
 /// add CotDiem
 /// </summary>
 /// <param name="entity">CotDiem</param>
 public void InsertCotDiem(CotDiem entity)
 {
     _CotDiemRepository.Insert(entity);
 }
コード例 #4
0
ファイル: HocTapService.cs プロジェクト: trunglam12/SMS
 /// <summary>
 /// update BangDiem
 /// </summary>
 /// <param name="entity">BangDiem</param>
 public void UpdateCotDiem(CotDiem entity)
 {
     _CotDiemRepository.Update(entity);
 }
コード例 #5
0
ファイル: HocTapService.cs プロジェクト: trunglam12/SMS
        /// <summary>
        /// get CotDiem by id
        /// </summary>
        /// <returns>list CotDiem</returns>
        public CotDiem GetCotDiemByID(int id)
        {
            CotDiem cotDiem = _CotDiemRepository.Entities.Find(id);

            return(cotDiem);
        }