public bool Delete(CanBoEntity _CanBo)
 {
     try
     {
         this.m_UnitOfWork.CanBoRepository.Delete(_CanBo);
         return true;
     }
     catch (Exception e)
     {
         System.Console.WriteLine(e.ToString());
         return false;
     }
 }
        public CanBoEntity GetWithId(int Id)
        {
            CanBos item = m_UnitOfWork.CanBoRepository.GetByID(Id);

            if (item != null)
            {
                CanBoEntity CanBo = new CanBoEntity();

                CanBo.Id = item.Id;
                CanBo.HinhAnh = item.HinhAnh;
                CanBo.HoTen = item.HoTen;
                CanBo.CMND = item.CMND;
                CanBo.Email = item.Email;
                CanBo.GioiTinh = null;
                if (item.GioiTinh != null)
                    CanBo.GioiTinh = (bool)item.GioiTinh;
                CanBo.NgaySinh = item.NgaySinh;
                CanBo.XaPhuong = item.XaPhuong;
                CanBo.MSSV = item.MSSV;
                CanBo.NoiOHienNay = item.NoiOHienNay;
                CanBo.XaPhuong1 = item.XaPhuong1;
                CanBo.SoDienThoai = item.SoDienThoai;
                CanBo.NamHocTap = null;
                CanBo.DiemRenLuyenGanNhat = null;
                CanBo.DiemTBTichLuy = null;
                CanBo.DiemTBHocKiGanNhat = null;
                CanBo.NgayVaoDangDuBi = null;
                CanBo.NgayVaoDangChinhThuc = null;
                CanBo.NgayVaoDoanTN = null;
                CanBo.NgayVaoHoiSV = null;
                if (CanBo.NamHocTap != null)
                    CanBo.NamHocTap = (int)item.NamHocTap;
                if (CanBo.DiemTBHocKiGanNhat != null)
                    CanBo.DiemTBHocKiGanNhat = (decimal)item.DiemTBHocKiGanNhat;
                if (CanBo.DiemTBTichLuy != null)
                    CanBo.DiemTBTichLuy = (decimal)item.DiemTBTichLuy;
                if (CanBo.DiemRenLuyenGanNhat != null)
                    CanBo.DiemRenLuyenGanNhat = (decimal)item.DiemRenLuyenGanNhat;
                if (CanBo.NgayVaoDoanTN != null)
                    CanBo.NgayVaoDoanTN = (DateTime)item.NgayVaoDoanTN;
                if (CanBo.NgayVaoHoiSV != null)
                    CanBo.NgayVaoHoiSV = (DateTime)item.NgayVaoHoiSV;
                if (CanBo.NgayVaoDangDuBi != null)
                    CanBo.NgayVaoDangDuBi = (DateTime)item.NgayVaoDangDuBi;
                if (CanBo.NgayVaoDangChinhThuc != null)
                    CanBo.NgayVaoDangChinhThuc = (DateTime)item.NgayVaoDangChinhThuc;
                CanBo.ThanhTich = item.ThanhTich;
                CanBo.SoThich = item.SoThich;
                CanBo.SoTruong = item.SoTruong;
                CanBo.SoDoan = item.SoDoan;
                CanBo.KyLuat = item.KyLuat;
                CanBo.ChuyenMon_CanBo = null;
                CanBo.QuanHuyen_CanBo = null;
                CanBo.CanBo_QuanHuyen = null;
                CanBo.TinhThanh_CanBo = null;
                CanBo.TinhThanh_CanBo1 = null;
                CanBo.TonGiao_CanBo = null;
                CanBo.TrinhDoHocVan_CanBo = null;
                if (item.ChuyenMon_CanBo != null)
                    CanBo.ChuyenMon_CanBo = (int)item.ChuyenMon_CanBo;
                if (item.QuanHuyen_CanBo != null)
                    CanBo.QuanHuyen_CanBo = (int)item.QuanHuyen_CanBo;
                if (item.CanBo_QuanHuyen != null)
                    CanBo.CanBo_QuanHuyen = (int)item.CanBo_QuanHuyen;
                if (item.TinhThanh_CanBo != null)
                    CanBo.TinhThanh_CanBo = (int)item.TinhThanh_CanBo;
                if (item.TinhThanh_CanBo1 != null)
                    CanBo.TinhThanh_CanBo1 = (int)item.TinhThanh_CanBo1;
                if (item.TonGiao_CanBo != null)
                    CanBo.TonGiao_CanBo = (int)item.TonGiao_CanBo;
                if (item.TrinhDoHocVan_CanBo != null)
                    CanBo.TrinhDoHocVan_CanBo = (int)item.TrinhDoHocVan_CanBo;

                return CanBo;
            }
            else
            {
                return null;
            }
        }
        public bool Update(CanBoEntity _CanBo)
        {
            var CanBo = this.m_UnitOfWork.CanBoRepository.GetByID(_CanBo.Id);

            if (CanBo == null)
                return false;

            if (CanBo != null)
            {
                CanBo.Id = _CanBo.Id;
                CanBo.HinhAnh = _CanBo.HinhAnh;
                CanBo.HoTen = _CanBo.HoTen;
                CanBo.CMND = _CanBo.CMND;
                CanBo.Email = _CanBo.Email;
                CanBo.GioiTinh = _CanBo.GioiTinh;
                CanBo.NgaySinh = _CanBo.NgaySinh;
                CanBo.XaPhuong = _CanBo.XaPhuong;
                CanBo.MSSV = _CanBo.MSSV;
                CanBo.NoiOHienNay = _CanBo.NoiOHienNay;
                CanBo.XaPhuong1 = _CanBo.XaPhuong1;
                CanBo.SoDienThoai = _CanBo.SoDienThoai;
                CanBo.NamHocTap = _CanBo.NamHocTap;
                CanBo.DiemTBHocKiGanNhat = _CanBo.DiemTBHocKiGanNhat;
                CanBo.DiemTBTichLuy = _CanBo.DiemTBTichLuy;
                CanBo.DiemRenLuyenGanNhat = _CanBo.DiemRenLuyenGanNhat;
                CanBo.NgayVaoDoanTN = _CanBo.NgayVaoDoanTN;
                CanBo.NgayVaoHoiSV = _CanBo.NgayVaoHoiSV;
                CanBo.NgayVaoDangDuBi = _CanBo.NgayVaoDangDuBi;
                CanBo.NgayVaoDangChinhThuc = _CanBo.NgayVaoDangChinhThuc;
                CanBo.ThanhTich = _CanBo.ThanhTich;
                CanBo.SoThich = _CanBo.SoThich;
                CanBo.SoTruong = _CanBo.SoTruong;
                CanBo.SoDoan = _CanBo.SoDoan;
                CanBo.KyLuat = _CanBo.KyLuat;
                CanBo.ChuyenMon_CanBo = _CanBo.ChuyenMon_CanBo;
                CanBo.QuanHuyen_CanBo = _CanBo.QuanHuyen_CanBo;
                CanBo.CanBo_QuanHuyen = _CanBo.CanBo_QuanHuyen;
                CanBo.TinhThanh_CanBo = _CanBo.TinhThanh_CanBo;
                CanBo.TinhThanh_CanBo1 = _CanBo.TinhThanh_CanBo1;
                CanBo.TonGiao_CanBo = _CanBo.TonGiao_CanBo;
                CanBo.TrinhDoHocVan_CanBo = _CanBo.TrinhDoHocVan_CanBo;

                this.m_UnitOfWork.CanBoRepository.Update(CanBo);
                this.m_UnitOfWork.Save();
            }

            return true;
        }
        public bool Create(CanBoEntity _CanBo)
        {
            try
            {
                var CanBo = new CanBos
                {
                    Id = _CanBo.Id,
                    HinhAnh = _CanBo.HinhAnh,
                    HoTen = _CanBo.HoTen,
                    CMND = _CanBo.CMND,
                    Email = _CanBo.Email,
                    GioiTinh = _CanBo.GioiTinh,
                    NgaySinh = _CanBo.NgaySinh,
                    XaPhuong = _CanBo.XaPhuong,
                    MSSV = _CanBo.MSSV,
                    NoiOHienNay = _CanBo.NoiOHienNay,
                    XaPhuong1 = _CanBo.XaPhuong1,
                    SoDienThoai = _CanBo.SoDienThoai,
                    NamHocTap = _CanBo.NamHocTap,
                    DiemTBHocKiGanNhat = _CanBo.DiemTBHocKiGanNhat,
                    DiemTBTichLuy = _CanBo.DiemTBTichLuy,
                    DiemRenLuyenGanNhat = _CanBo.DiemRenLuyenGanNhat,
                    NgayVaoDoanTN = _CanBo.NgayVaoDoanTN,
                    NgayVaoHoiSV = _CanBo.NgayVaoHoiSV,
                    NgayVaoDangDuBi = _CanBo.NgayVaoDangDuBi,
                    NgayVaoDangChinhThuc = _CanBo.NgayVaoDangChinhThuc,
                    ThanhTich = _CanBo.ThanhTich,
                    SoThich = _CanBo.SoThich,
                    SoTruong = _CanBo.SoTruong,
                    SoDoan = _CanBo.SoDoan,
                    KyLuat = _CanBo.KyLuat,
                    ChuyenMon_CanBo = _CanBo.ChuyenMon_CanBo,
                    QuanHuyen_CanBo = _CanBo.QuanHuyen_CanBo,
                    CanBo_QuanHuyen = _CanBo.CanBo_QuanHuyen,
                    TinhThanh_CanBo = _CanBo.TinhThanh_CanBo,
                    TinhThanh_CanBo1 = _CanBo.TinhThanh_CanBo1,
                    TonGiao_CanBo = _CanBo.TonGiao_CanBo,
                    TrinhDoHocVan_CanBo = _CanBo.TrinhDoHocVan_CanBo,
                };

                if (CanBo.NgayVaoDangDuBi == DateTime.MinValue)
                    CanBo.NgayVaoDangDuBi = null;
                if (CanBo.NgayVaoDangChinhThuc == DateTime.MinValue)
                    CanBo.NgayVaoDangChinhThuc = null;
                if (CanBo.NgayVaoHoiSV == DateTime.MinValue)
                    CanBo.NgayVaoHoiSV = null;
                if (CanBo.NgayVaoDoanTN == DateTime.MinValue)
                    CanBo.NgayVaoDoanTN = null;
                if (CanBo.ChuyenMon_CanBo == 0)
                    CanBo.ChuyenMon_CanBo = null;
                if (CanBo.QuanHuyen_CanBo == 0)
                    CanBo.QuanHuyen_CanBo = null;
                if(CanBo.CanBo_QuanHuyen == 0)
                    CanBo.CanBo_QuanHuyen = null;
                if (CanBo.TinhThanh_CanBo == 0)
                    CanBo.TinhThanh_CanBo = null;
                if (CanBo.TinhThanh_CanBo1 == 0)
                    CanBo.TinhThanh_CanBo1 = null;
                if (CanBo.TonGiao_CanBo == 0)
                    CanBo.TonGiao_CanBo = null;
                if (CanBo.TrinhDoHocVan_CanBo == 0)
                    CanBo.TrinhDoHocVan_CanBo = null;
                this.m_UnitOfWork.CanBoRepository.Insert(CanBo);
                this.m_UnitOfWork.Save();

                return true;
            }
            catch(Exception e)
            {
                System.Console.WriteLine(e.ToString());
                return false;
            }
        }
        public List<CanBoEntity> GetAll()
        {
            IEnumerable<CanBos> model = this.m_UnitOfWork.CanBoRepository.GetAll();
            List<CanBoEntity> result = new List<CanBoEntity>();

            foreach (var item in model)
            {
                CanBoEntity CanBo = new CanBoEntity();
                CanBo.Id = item.Id;
                CanBo.HinhAnh = item.HinhAnh;
                CanBo.HoTen = item.HoTen;
                CanBo.CMND = item.CMND;
                CanBo.Email = item.Email;
                CanBo.GioiTinh = null;
                if(item.GioiTinh != null)
                    CanBo.GioiTinh = (bool)item.GioiTinh;
                CanBo.NgaySinh = item.NgaySinh;
                CanBo.XaPhuong = item.XaPhuong;
                CanBo.MSSV = item.MSSV;
                CanBo.NoiOHienNay = item.NoiOHienNay;
                CanBo.XaPhuong1 = item.XaPhuong1;
                CanBo.SoDienThoai = item.SoDienThoai;
                CanBo.NamHocTap = null;
                CanBo.DiemRenLuyenGanNhat = null;
                CanBo.DiemTBTichLuy = null;
                CanBo.DiemTBHocKiGanNhat = null;
                CanBo.NgayVaoDangDuBi = null;
                CanBo.NgayVaoDangChinhThuc = null;
                CanBo.NgayVaoDoanTN = null;
                CanBo.NgayVaoHoiSV = null;
                if(CanBo.NamHocTap != null)
                    CanBo.NamHocTap = (int)item.NamHocTap;
                if(CanBo.DiemTBHocKiGanNhat != null)
                    CanBo.DiemTBHocKiGanNhat = (decimal)item.DiemTBHocKiGanNhat;
                if(CanBo.DiemTBTichLuy != null)
                     CanBo.DiemTBTichLuy = (decimal)item.DiemTBTichLuy;
                if(CanBo.DiemRenLuyenGanNhat != null)
                    CanBo.DiemRenLuyenGanNhat = (decimal)item.DiemRenLuyenGanNhat;
                if(CanBo.NgayVaoDoanTN != null)
                    CanBo.NgayVaoDoanTN = (DateTime)item.NgayVaoDoanTN;
                if(CanBo.NgayVaoHoiSV != null)
                    CanBo.NgayVaoHoiSV = (DateTime)item.NgayVaoHoiSV;
                if(CanBo.NgayVaoDangDuBi != null)
                    CanBo.NgayVaoDangDuBi = (DateTime)item.NgayVaoDangDuBi;
                if(CanBo.NgayVaoDangChinhThuc != null)
                    CanBo.NgayVaoDangChinhThuc = (DateTime)item.NgayVaoDangChinhThuc;
                CanBo.ThanhTich = item.ThanhTich;
                CanBo.SoThich = item.SoThich;
                CanBo.SoTruong = item.SoTruong;
                CanBo.SoDoan = item.SoDoan;
                CanBo.KyLuat = item.KyLuat;
                CanBo.ChuyenMon_CanBo = null;
                CanBo.QuanHuyen_CanBo = null;
                CanBo.CanBo_QuanHuyen = null;
                CanBo.TinhThanh_CanBo = null;
                CanBo.TinhThanh_CanBo1 = null;
                CanBo.TonGiao_CanBo = null;
                CanBo.TrinhDoHocVan_CanBo = null;
                if (item.ChuyenMon_CanBo != null)
                    CanBo.ChuyenMon_CanBo = (int)item.ChuyenMon_CanBo;
                if(item.QuanHuyen_CanBo != null)
                    CanBo.QuanHuyen_CanBo = (int)item.QuanHuyen_CanBo;
                if(item.CanBo_QuanHuyen != null)
                    CanBo.CanBo_QuanHuyen = (int)item.CanBo_QuanHuyen;
                if(item.TinhThanh_CanBo != null)
                    CanBo.TinhThanh_CanBo = (int)item.TinhThanh_CanBo;
                if(item.TinhThanh_CanBo1 != null)
                    CanBo.TinhThanh_CanBo1 = (int)item.TinhThanh_CanBo1;
                if(item.TonGiao_CanBo != null)
                    CanBo.TonGiao_CanBo = (int)item.TonGiao_CanBo;
                if(item.TrinhDoHocVan_CanBo != null)
                    CanBo.TrinhDoHocVan_CanBo = (int)item.TrinhDoHocVan_CanBo;

                result.Add(CanBo);
            }

            return result;
        }
        /// only return json to client
        public ActionResult Create(CanBoEntity canbo)
        {
            CanBoService service = new CanBoService();

            if (canbo == null)
            {
                RenderResult.RequestError(ViewData, "Lỗi đối số không hợp lệ");
                return Json(JsonConvert.SerializeObject(ViewData));
            }

            if (service.Create(canbo))
            {
                return Json(RenderResult.RequestCompleted(ViewData, "Thêm đơn vị thành công"));
            }
            else
            {

                return Json(RenderResult.RequestCompleted(ViewData, "Lỗi khi thêm đơn vị"));
            }
        }
        public ActionResult Delete(CanBoEntity canbo)
        {
            CanBoService service = new CanBoService();

            try
            {
                if (service.Delete(canbo))
                    return Json(RenderResult.RequestCompleted(ViewData, "Xóa thành công"));

                return Json(RenderResult.RequestCompleted(ViewData, "Xóa không thành công"));
            }
            catch (Exception e)
            {
                System.Console.WriteLine(e.ToString());
                return Json(RenderResult.RequestError(ViewData, "Lỗi xảy ra"));
            }

        }
        public ActionResult Edit(CanBoEntity canbo)
        {
            CanBoService service = new CanBoService();

            if (canbo == null)
            {
                return Json(RenderResult.RequestError(ViewData, "Lỗi đối số không hợp lệ"), JsonRequestBehavior.AllowGet);
            }

            try
            {
                if (service.Update(canbo))
                    return Json(RenderResult.RequestCompleted(ViewData, "Chỉnh sửa thành công"));

                return Json(RenderResult.RequestCompleted(ViewData, "Chỉnh sửa không thành công"));
            }
            catch (Exception e)
            {
                System.Console.WriteLine(e.ToString());

                return Json(RenderResult.RequestError(ViewData, "Lỗi xảy ra"));
            }
        }