public async Task <ActionResult> Create(NhaCreatingViewModel model) { if (ModelState.IsValid) { Nha nha = new Nha(); decimal giaThueBQ = 0; if (!string.IsNullOrEmpty(model.TongGiaThue) && !string.IsNullOrEmpty(model.TongDienTichSuDung)) { giaThueBQ = Convert.ToDecimal(model.TongGiaThue) / Convert.ToDecimal(model.TongDienTichSuDung); } nha.MatBangId = model.MatBangId; nha.QuanId = Convert.ToInt64(model.QuanId); nha.DuongId = Convert.ToInt64(model.DuongId); nha.SoNha = StringHelper.KillChars(model.SoNha); nha.TenToaNha = StringHelper.KillChars(model.TenToaNha); nha.MatTienTreoBien = string.IsNullOrEmpty(model.MatTienTreoBien) ? 0 : float.Parse(model.MatTienTreoBien, CultureInfo.InvariantCulture.NumberFormat); nha.BeNgangLotLong = string.IsNullOrEmpty(model.BeNgangLotLong) ? 0 : float.Parse(model.BeNgangLotLong, CultureInfo.InvariantCulture.NumberFormat); nha.DienTichDat = string.IsNullOrEmpty(model.DienTichDat) ? 0 : float.Parse(model.DienTichDat, CultureInfo.InvariantCulture.NumberFormat); nha.DienTichDatSuDungTang1 = string.IsNullOrEmpty(model.DienTichDatSuDungTang1) ? 0 : float.Parse(model.DienTichDatSuDungTang1, CultureInfo.InvariantCulture.NumberFormat); nha.SoTang = string.IsNullOrEmpty(model.SoTang) ? 0 : Convert.ToInt32(model.SoTang); nha.TongDienTichSuDung = string.IsNullOrEmpty(model.TongDienTichSuDung) ? 0 : float.Parse(model.TongDienTichSuDung, CultureInfo.InvariantCulture.NumberFormat); nha.DiChungChu = model.DiChungChu == "1" ? true : false; nha.Ham = model.Ham == "1" ? true : false; nha.ThangMay = model.ThangMay == "1" ? true : false; nha.NoiThatKhachThueCuId = Convert.ToInt32(model.NoiThatKhachThueCuId); nha.DanhGiaPhuHopVoiId = model.DanhGiaPhuHopVoiId; nha.TongGiaThue = string.IsNullOrEmpty(model.TongGiaThue) ? 0 : Convert.ToDecimal(model.TongGiaThue); nha.GiaThueBQ = giaThueBQ; //string.IsNullOrEmpty(model.GiaThueBQ) ? 0 : Convert.ToDecimal(model.GiaThueBQ); nha.TenNguoiLienHeVaiTro = StringHelper.KillChars(model.TenNguoiLienHeVaiTro); nha.SoDienThoai = StringHelper.KillChars(model.SoDienThoai); nha.NgayCNHenLienHeLai = string.IsNullOrEmpty(model.NgayCNHenLienHeLai) ? (DateTime?)null : DateTime.ParseExact(model.NgayCNHenLienHeLai, "dd/MM/yyyy", CultureInfo.InvariantCulture); nha.CapDoTheoDoiId = Convert.ToInt32(model.CapDoTheoDoiId); nha.ImageDescription1 = StringHelper.KillChars(model.ImageDescription1); nha.ImageDescription2 = StringHelper.KillChars(model.ImageDescription2); nha.ImageDescription3 = StringHelper.KillChars(model.ImageDescription3); nha.ImageDescription4 = StringHelper.KillChars(model.ImageDescription4); nha.GhiChu = StringHelper.KillChars(model.GhiChu); nha.NgayTao = DateTime.Now; nha.NguoiTaoId = AccountId; nha.NguoiPhuTrachId = AccountId; nha.TrangThai = 0; //Chờ duyệt int result = 0; try { result = await _repository.GetRepository <Nha>().CreateAsync(nha, AccountId); } catch { } if (result > 0) { TempData["Success"] = "Nhập dữ liệu nhà mới thành công!"; } return(RedirectToAction("Index")); } else { ViewBag.DangMatBang = await _repository.GetRepository <MatBang>().GetAllAsync(); ModelState.AddModelError(string.Empty, "Nhập dữ liệu nhà mới không thành công! Vui lòng kiểm tra và thử lại!"); return(View(model)); } }
public async Task <ActionResult> Update(long id) { Nha nha = await _repository.GetRepository <Nha>().ReadAsync(id); SetViewBag(false); //SetViewBag(); var matBang = _repository.GetRepository <MatBang>().GetAll(); var listMatBangArr = new List <MatBangItem>(); if (matBang.Any()) { foreach (var item in matBang) { listMatBangArr.Add(new MatBangItem { FieldKey = item.Id, FieldName = item.Name, IsSelected = false }); } } if (!string.IsNullOrEmpty(nha.MatBangId)) { string[] arrmatbangid = nha.MatBangId.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); if (arrmatbangid.Count() > 0) { for (var i = 0; i < arrmatbangid.Count(); i++) { foreach (var item in listMatBangArr.Where(w => w.FieldKey == Convert.ToInt64(arrmatbangid[i]))) { item.IsSelected = true; } } } } var danhGia = _repository.GetRepository <DanhGiaPhuHopVoi>().GetAll(); var listDanhGiaArr = new List <DanhGiaPhuHopVoiItem>(); if (danhGia.Any()) { foreach (var item in danhGia) { listDanhGiaArr.Add(new DanhGiaPhuHopVoiItem { FieldKey = item.Id, FieldName = item.Name, IsSelected = false }); } } if (!string.IsNullOrEmpty(nha.DanhGiaPhuHopVoiId)) { string[] arrdanhgiaid = nha.DanhGiaPhuHopVoiId.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries); if (arrdanhgiaid.Count() > 0) { for (var i = 0; i < arrdanhgiaid.Count(); i++) { foreach (var item in listDanhGiaArr.Where(w => w.FieldKey == Convert.ToInt32(arrdanhgiaid[i]))) { item.IsSelected = true; } } } } NhaUpdatingViewModel model = new NhaUpdatingViewModel() { Id = nha.Id, SoNha = nha.SoNha, //MatBangId = nha.MatBangId, ListMatBangArr = listMatBangArr, ListDanhGiaPhuHopVoiArr = listDanhGiaArr, QuanId = nha.QuanId.ToString(), DuongId = nha.DuongId.ToString(), TenToaNha = nha.TenToaNha, NoiThatKhachThueCuId = nha.NoiThatKhachThueCuId.ToString(), //DanhGiaPhuHopVoiId = nha.DanhGiaPhuHopVoiId.ToString(), CapDoTheoDoiId = nha.CapDoTheoDoiId.ToString(), MatTienTreoBien = nha.MatTienTreoBien.ToString(), BeNgangLotLong = nha.BeNgangLotLong.ToString(), DienTichDat = nha.DienTichDat.ToString(), DienTichDatSuDungTang1 = nha.DienTichDatSuDungTang1.ToString(), SoTang = nha.SoTang.ToString(), TongDienTichSuDung = nha.TongDienTichSuDung.ToString(), DiChungChu = nha.DiChungChu ? "1" : "0", Ham = nha.Ham ? "1" : "0", ThangMay = nha.ThangMay ? "1" : "0", TongGiaThue = nha.TongGiaThue.ToString(), GiaThueBQ = nha.GiaThueBQ.ToString(), TenNguoiLienHeVaiTro = nha.TenNguoiLienHeVaiTro, SoDienThoai = nha.SoDienThoai, NgayCNHenLienHeLai = nha.NgayCNHenLienHeLai.HasValue ? nha.NgayCNHenLienHeLai.Value.ToString("dd/MM/yyyy") : "", ImageDescription1 = nha.ImageDescription1, ImageDescription2 = nha.ImageDescription2, ImageDescription3 = nha.ImageDescription3, ImageDescription4 = nha.ImageDescription4, GhiChu = nha.GhiChu, }; //Backup model để so sánh xem có update gì không, sau đó lưu vào lịch sử _modelBK = model; return(View(model)); }
public async Task <ActionResult> Update(long id, NhaUpdatingViewModel model, NhaUpdatingViewModel _modelBK) { try { if (ModelState.IsValid) { Nha nha = await _repository.GetRepository <Nha>().ReadAsync(id); decimal giaThueBQ = 0; if (!string.IsNullOrEmpty(model.TongGiaThue) && !string.IsNullOrEmpty(model.TongDienTichSuDung)) { giaThueBQ = Convert.ToDecimal(model.TongGiaThue.Replace(",", "")) / Convert.ToDecimal(model.TongDienTichSuDung.Replace(",", "")); } nha.MatBangId = model.MatBangId; nha.QuanId = Convert.ToInt64(model.QuanId); nha.DuongId = Convert.ToInt64(model.DuongId); nha.SoNha = StringHelper.KillChars(model.SoNha); nha.TenToaNha = StringHelper.KillChars(model.TenToaNha); nha.MatTienTreoBien = string.IsNullOrEmpty(model.MatTienTreoBien) ? 0 : float.Parse(model.MatTienTreoBien.Replace(",", ""), CultureInfo.InvariantCulture.NumberFormat); nha.BeNgangLotLong = string.IsNullOrEmpty(model.BeNgangLotLong) ? 0 : float.Parse(model.BeNgangLotLong.Replace(",", ""), CultureInfo.InvariantCulture.NumberFormat); nha.DienTichDat = string.IsNullOrEmpty(model.DienTichDat) ? 0 : float.Parse(model.DienTichDat.Replace(",", ""), CultureInfo.InvariantCulture.NumberFormat); nha.DienTichDatSuDungTang1 = string.IsNullOrEmpty(model.DienTichDatSuDungTang1) ? 0 : float.Parse(model.DienTichDatSuDungTang1.Replace(",", ""), CultureInfo.InvariantCulture.NumberFormat); nha.SoTang = string.IsNullOrEmpty(model.SoTang) ? 0 : Convert.ToInt32(model.SoTang); nha.TongDienTichSuDung = string.IsNullOrEmpty(model.TongDienTichSuDung) ? 0 : float.Parse(model.TongDienTichSuDung.Replace(",", ""), CultureInfo.InvariantCulture.NumberFormat); nha.DiChungChu = model.DiChungChu == "1" ? true : false; nha.Ham = model.Ham == "1" ? true : false; nha.ThangMay = model.ThangMay == "1" ? true : false; nha.NoiThatKhachThueCuId = Convert.ToInt32(model.NoiThatKhachThueCuId); nha.DanhGiaPhuHopVoiId = model.DanhGiaPhuHopVoiId; nha.TongGiaThue = string.IsNullOrEmpty(model.TongGiaThue) ? 0 : Convert.ToDecimal(model.TongGiaThue.Replace(",", "")); nha.GiaThueBQ = giaThueBQ; //string.IsNullOrEmpty(model.GiaThueBQ) ? 0 : Convert.ToDecimal(model.GiaThueBQ); nha.TenNguoiLienHeVaiTro = model.TenNguoiLienHeVaiTro; nha.SoDienThoai = model.SoDienThoai; nha.NgayCNHenLienHeLai = string.IsNullOrEmpty(model.NgayCNHenLienHeLai) ? (DateTime?)null : DateTime.ParseExact(model.NgayCNHenLienHeLai.Replace("-", "/"), "dd/MM/yyyy", CultureInfo.InvariantCulture); nha.CapDoTheoDoiId = Convert.ToInt32(model.CapDoTheoDoiId); nha.ImageDescription1 = model.ImageDescription1; nha.ImageDescription2 = model.ImageDescription2; nha.ImageDescription3 = model.ImageDescription3; nha.ImageDescription4 = model.ImageDescription4; nha.GhiChu = model.GhiChu; int result = await _repository.GetRepository <Nha>().UpdateAsync(nha, AccountId); //So sánh để tìm ra nội dung thay đổi if (result > 0) { //string noiDungThayDoi = ""; //string strOld = ""; //string strNew = ""; ////Mặt bằng //foreach (var item in _modelBK.ListMatBangArr) //{ // strOld = strOld == "" ? item.FieldName : strOld + ", " + item.FieldName; //} //foreach (var item in nha.MatBangId.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)) //{ // string name = _repository.GetRepository<MatBang>().GetAll().Where(t => t.Id == Convert.ToInt32(item)).Select(o => o.Name).ToString(); // strNew = strNew == "" ? name : strNew + ", " + name; //} //if (strNew != strOld) //{ // noiDungThayDoi = "Sửa mặt bằng từ: " + strOld + " thành: " + strNew; //} ////Quận //strOld = strNew = ""; //if (_modelBK.QuanId != nha.QuanId.ToString()) //{ // strOld = _repository.GetRepository<Quan>().GetAll().Where(t => t.Id == Convert.ToInt32(_modelBK.QuanId)).Select(o => o.Name).ToString(); // strNew = _repository.GetRepository<Quan>().GetAll().Where(t => t.Id == Convert.ToInt32(nha.QuanId)).Select(o => o.Name).ToString(); // noiDungThayDoi = noiDungThayDoi == "" ? "Sửa quận từ: " + strOld + " thành: " + strNew : noiDungThayDoi + "<br/>" + "Sửa quận từ: " + strOld + " thành: " + strNew; //} ////Đường //strOld = strNew = ""; //if (_modelBK.DuongId != nha.DuongId.ToString()) //{ // strOld = _repository.GetRepository<Duong>().GetAll().Where(t => t.Id == Convert.ToInt32(_modelBK.DuongId)).Select(o => o.Name).ToString(); // strNew = _repository.GetRepository<Duong>().GetAll().Where(t => t.Id == Convert.ToInt32(nha.DuongId)).Select(o => o.Name).ToString(); // noiDungThayDoi = noiDungThayDoi == "" ? "Sửa đường từ: " + strOld + " thành: " + strNew : noiDungThayDoi + "<br/>" + "Sửa đường từ: " + strOld + " thành: " + strNew; //} ////Số nhà //strOld = strNew = ""; //if (_modelBK.SoNha != nha.SoNha) //{ // noiDungThayDoi = noiDungThayDoi == "" ? "Sửa số nhà từ: " + _modelBK.SoNha + " thành: " + nha.SoNha : noiDungThayDoi + "<br/>" + "Sửa số nhà từ: " + _modelBK.SoNha + " thành: " + nha.SoNha; //} TempData["Success"] = "Cập nhật bài viết thành công!"; return(RedirectToAction("Index")); } else { ModelState.AddModelError(string.Empty, "Cập nhật bài viết không thành công! Vui lòng kiểm tra và thử lại!"); return(View(model)); } } else { //var quan = _repository.GetRepository<Quan>().GetAll().OrderBy(o => o.Name).ToList(); //ViewBag.QuanDropdownlist = new SelectList(quan, "Id", "Name", model.QuanId); //ViewBag.DuongDropdownlist = new SelectList(_repository.GetRepository<Duong>().GetAll(o => o.QuanId == model.QuanId).OrderBy(o => o.Name).ToList(), "Id", "Name", model.DuongId); ModelState.AddModelError(string.Empty, "Vui lòng nhập chính xác các thông tin!"); return(View(model)); } } catch (Exception ex) { throw; } }
public ActionResult ImportNha(HttpPostedFileBase uploadFile) { StringBuilder strValidations = new StringBuilder(string.Empty); try { if (uploadFile.ContentLength > 0) { string filePath = Path.Combine(HttpContext.Server.MapPath("/Uploads/files"), Path.GetFileName(uploadFile.FileName)); uploadFile.SaveAs(filePath); DataSet ds = new DataSet(); string ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties=Excel 12.0;"; using (OleDbConnection conn = new System.Data.OleDb.OleDbConnection(ConnectionString)) { conn.Open(); using (DataTable dtExcelSchema = conn.GetSchema("Tables")) { string sheetName = "Data$"; string query = "SELECT * FROM [" + sheetName + "]"; OleDbDataAdapter adapter = new OleDbDataAdapter(query, conn); adapter.Fill(ds, "Items"); if (ds.Tables.Count > 0) { if (ds.Tables[0].Rows.Count > 0) { Nha nha = new Nha(); //fdsfdsf int resultCount = 0; int recordExcelCount = ds.Tables[0].Rows.Count; using (TransactionScope tscope = new TransactionScope()) { foreach (DataRow r in ds.Tables[0].Rows) { string matBang = Convert.ToString(r["Mặt bằng"]); long matBangId = (_repository.GetRepository <MatBang>().Read(o => o.Name == matBang)) != null ? (_repository.GetRepository <MatBang>().Read(o => o.Name == matBang)).Id : 0; string quan = Convert.ToString(r["Quận"]); long quanId = (_repository.GetRepository <Quan>().Read(o => o.Name == quan)).Id; string duong = Convert.ToString(r["Đường"]); long duongId = (_repository.GetRepository <Duong>().Read(o => o.Name == duong)).Id; string noiThatKhachThueCu = Convert.ToString(r["Nội thất khách thuê cũ"]); long noiThatKhachThueCuId = (_repository.GetRepository <NoiThatKhachThueCu>().Read(o => o.Name == noiThatKhachThueCu)) != null ? (_repository.GetRepository <NoiThatKhachThueCu>().Read(o => o.Name == noiThatKhachThueCu)).Id : 1; string danhGiaPhuHopVoi = Convert.ToString(r["Đánh giá phù hợp với"]); long danhGiaPhuHopVoiId = (_repository.GetRepository <DanhGiaPhuHopVoi>().Read(o => o.Name == danhGiaPhuHopVoi)) != null ?(_repository.GetRepository <DanhGiaPhuHopVoi>().Read(o => o.Name == danhGiaPhuHopVoi)).Id : 0; string capDoTheoDoi = Convert.ToString(r["Cấp độ theo dõi"]); int capDoTheoDoiId = (_repository.GetRepository <CapDoTheoDoi>().Read(o => o.Name == capDoTheoDoi)) != null? (_repository.GetRepository <CapDoTheoDoi>().Read(o => o.Name == capDoTheoDoi)).Id : 1; nha.MatBangId = matBangId == 0 ? null : Convert.ToString(matBangId); nha.QuanId = quanId; nha.DuongId = duongId; nha.SoNha = r["Số nhà"] == DBNull.Value ? "" : r["Số nhà"].ToString(); nha.TenToaNha = r["Tên toàn nhà"] == DBNull.Value ? "" : (string)r["Tên toàn nhà"]; nha.MatTienTreoBien = r["Mặt tiền treo biển"] == DBNull.Value ? 0 : float.Parse(r["Mặt tiền treo biển"].ToString()); nha.BeNgangLotLong = r["Bề ngang lọt lòng"] == DBNull.Value ? 0 : float.Parse(r["Bề ngang lọt lòng"].ToString()); nha.DienTichDat = r["Diện tích đất"] == DBNull.Value ? 0 : float.Parse(r["Diện tích đất"].ToString()); nha.DienTichDatSuDungTang1 = r["Diện tích đất sử dụng tầng 1"] == DBNull.Value ? 0 : float.Parse(r["Diện tích đất sử dụng tầng 1"].ToString()); nha.SoTang = r["Số tầng"] == DBNull.Value ? 0 : int.Parse(r["Số tầng"].ToString()); nha.TongDienTichSuDung = r["Tổng diện tích sử dụng"] == DBNull.Value ? 0 : int.Parse(r["Tổng diện tích sử dụng"].ToString()); nha.DiChungChu = (string)r["Đi chung chủ"] == "Có" ? true : false; nha.Ham = (string)r["Hầm"] == "Có" ? true : false; nha.ThangMay = (string)r["Thang máy"] == "Có" ? true : false; nha.NoiThatKhachThueCuId = noiThatKhachThueCuId; nha.DanhGiaPhuHopVoiId = danhGiaPhuHopVoiId == 0? null : Convert.ToString(danhGiaPhuHopVoiId); nha.TongGiaThue = r["Tổng giá thuê"] == DBNull.Value ? 0 : decimal.Parse(r["Tổng giá thuê"].ToString()); nha.GiaThueBQ = r["Giá thuê BQ"] == DBNull.Value ? 0 : decimal.Parse(r["Giá thuê BQ"].ToString()); nha.TenNguoiLienHeVaiTro = r["Tên người liên hệ - vai trò"] == DBNull.Value ? "" : (string)r["Tên người liên hệ - vai trò"]; nha.SoDienThoai = r["Số điện thoại"] == DBNull.Value ? "" : r["Số điện thoại"].ToString(); nha.NgayCNHenLienHeLai = r["Ngày CN hẹn liên hệ lại"] == DBNull.Value ? (DateTime?)null : Convert.ToDateTime(r["Ngày CN hẹn liên hệ lại"]); nha.CapDoTheoDoiId = capDoTheoDoiId; nha.GhiChu = r["Ghi chú"] == DBNull.Value ? "" : (string)r["Ghi chú"]; nha.NgayTao = DateTime.Now; nha.NguoiTaoId = AccountId; nha.TrangThai = 0; //Chờ duyệt int result = 0; try { result = _repository.GetRepository <Nha>().Create(nha, AccountId); } catch { } if (result > 0) { resultCount++; } } if (resultCount == recordExcelCount) { tscope.Complete(); TempData["Success"] = "Import dữ liệu thành công!"; return(RedirectToAction("Index")); } else { Transaction.Current.Rollback(); tscope.Dispose(); TempData["Error"] = "Import dữ liệu không thành công, vui lòng thử lại!"; return(RedirectToAction("Index")); } } } } } } } } catch (Exception ex) { TempData["Error"] = "Import dữ liệu không thành công, vui lòng thử lại!"; return(RedirectToAction("Index")); } return(RedirectToAction("Index")); }