private Merchant_Product_Hierarchy EmptyIfNull(Merchant_Product_Hierarchy p) { p.ma_cay_phan_cap_1 = String.IsNullOrEmpty(p.ma_cay_phan_cap_1) ? "" : p.ma_cay_phan_cap_1; p.ma_cay_phan_cap_2 = String.IsNullOrEmpty(p.ma_cay_phan_cap_2) ? "" : p.ma_cay_phan_cap_2; p.ma_cay_phan_cap_3 = String.IsNullOrEmpty(p.ma_cay_phan_cap_3) ? "" : p.ma_cay_phan_cap_3; p.ma_cay_phan_cap_4 = String.IsNullOrEmpty(p.ma_cay_phan_cap_4) ? "" : p.ma_cay_phan_cap_4; p.ma_cay_phan_cap_5 = String.IsNullOrEmpty(p.ma_cay_phan_cap_5) ? "" : p.ma_cay_phan_cap_5; p.ma_cay_phan_cap_6 = String.IsNullOrEmpty(p.ma_cay_phan_cap_6) ? "" : p.ma_cay_phan_cap_6; p.ma_cay_phan_cap_7 = String.IsNullOrEmpty(p.ma_cay_phan_cap_7) ? "" : p.ma_cay_phan_cap_7; p.ma_cay_phan_cap_8 = String.IsNullOrEmpty(p.ma_cay_phan_cap_8) ? "" : p.ma_cay_phan_cap_8; p.ma_cay_phan_cap_9 = String.IsNullOrEmpty(p.ma_cay_phan_cap_9) ? "" : p.ma_cay_phan_cap_9; p.ma_cay_phan_cap_10 = String.IsNullOrEmpty(p.ma_cay_phan_cap_10) ? "" : p.ma_cay_phan_cap_10; return(p); }
public string UpdateProduct(Merchant_Product product, Merchant_Product_Hierarchy hierarchy, List <Merchant_Product_Image> listimage, List <Merchant_Product_Property> listproperty, string connectstring) { using (var db = new OrmliteConnection().openConn(connectstring)) { try { Merchant_Product checkProduct; if (product.ma_gian_hang == "All") { checkProduct = db.FirstOrDefault <Merchant_Product>("ma_san_pham = {0}", product.ma_san_pham); } else { checkProduct = db.FirstOrDefault <Merchant_Product>("ma_san_pham = {0} and ma_gian_hang = {1}", product.ma_san_pham, product.ma_gian_hang); } if (checkProduct == null) { return("Thông tin không hợp lệ"); } checkProduct.url = (!string.IsNullOrEmpty(product.url) && checkProduct.url != product.url) ? product.url : checkProduct.url; checkProduct.catalog = (!string.IsNullOrEmpty(product.catalog) && checkProduct.catalog != product.catalog) ? product.catalog : checkProduct.catalog; checkProduct.ma_loai_san_pham = !string.IsNullOrEmpty(product.ma_loai_san_pham) ? product.ma_loai_san_pham : checkProduct.ma_loai_san_pham; checkProduct.part_no = !string.IsNullOrEmpty(product.part_no) ? product.part_no : checkProduct.part_no; checkProduct.ten_san_pham = !string.IsNullOrEmpty(product.ten_san_pham) ? product.ten_san_pham : checkProduct.ten_san_pham; checkProduct.mo_ta = !string.IsNullOrEmpty(product.mo_ta) ? product.mo_ta : ""; checkProduct.noi_dung = !string.IsNullOrEmpty(product.noi_dung) ? product.noi_dung : ""; checkProduct.tu_khoa = !string.IsNullOrEmpty(product.tu_khoa) ? product.tu_khoa : ""; checkProduct.tag = !string.IsNullOrEmpty(product.tag) ? product.tag : ""; checkProduct.slug = !string.IsNullOrEmpty(product.slug) ? product.slug : ""; checkProduct.xuat_xu = !string.IsNullOrEmpty(product.xuat_xu) ? product.xuat_xu : ""; checkProduct.thuong_hieu = !string.IsNullOrEmpty(product.thuong_hieu) ? product.thuong_hieu : ""; checkProduct.model = !string.IsNullOrEmpty(product.model) ? product.model : ""; checkProduct.khoi_luong = product.khoi_luong; checkProduct.gia_si = product.gia_si; checkProduct.don_gia = product.don_gia; checkProduct.trang_thai = product.trang_thai; checkProduct.trang_thai_xuat_ban = AllConstant.trang_thai_xuat_ban.CHUA_XUAT_BAN; checkProduct.trang_thai_duyet = AllConstant.trang_thai_duyet.CHUA_DUYET; checkProduct.nguoi_cap_nhat = product.nguoi_cap_nhat; checkProduct.ngay_cap_nhat = DateTime.Now; db.Update(checkProduct); //List<SqlParameter> param = new List<SqlParameter>(); //param.Add(new SqlParameter("@id", checkProduct.id)); //new SqlHelper().ExecuteQuery("p_UpdateSlugForMerchantProduct", param); // Update Hierarchy var checkHierarchy = db.FirstOrDefault <Merchant_Product_Hierarchy>(@"ma_san_pham = {0} and ma_gian_hang = {1} and ma_cay_phan_cap_1 = {2} and ma_cay_phan_cap_2 = {3} and ma_cay_phan_cap_3 = {4} ", checkProduct.ma_san_pham, checkProduct.ma_gian_hang, hierarchy.ma_cay_phan_cap_1, hierarchy.ma_cay_phan_cap_2, hierarchy.ma_cay_phan_cap_3); if (checkHierarchy != null) { db.ExecuteNonQuery(@"update Merchant_Product_Hierarchy set ma_cay_phan_cap_1 = {0}, ma_cay_phan_cap_2 = {1}, ma_cay_phan_cap_3 = {2}, nguoi_cap_nhat = {3}, ngay_cap_nhat = {4} where ma_san_pham = {5} and ma_gian_hang = {6}".Params(hierarchy.ma_cay_phan_cap_1, hierarchy.ma_cay_phan_cap_2, hierarchy.ma_cay_phan_cap_3, product.nguoi_cap_nhat, DateTime.Now, checkProduct.ma_san_pham, checkProduct.ma_gian_hang)); } else { hierarchy.ma_san_pham = checkProduct.ma_san_pham; hierarchy.ma_gian_hang = checkProduct.ma_gian_hang; hierarchy.ngay_tao = DateTime.Now; hierarchy.nguoi_tao = checkProduct.nguoi_tao; hierarchy.ngay_cap_nhat = DateTime.Parse("1900-01-01"); hierarchy.trang_thai = AllConstant.trang_thai.DANG_SU_DUNG; // Giá trị đang hoạt động db.Insert(hierarchy); } // Update Thumbnail if (listimage != null) { db.Delete <Merchant_Product_Image>("ma_san_pham = {0} and ma_gian_hang = {1}", checkProduct.ma_san_pham, checkProduct.ma_gian_hang); foreach (Merchant_Product_Image item in listimage) { if (item.id == 0) { item.ma_san_pham = checkProduct.ma_san_pham; item.ma_gian_hang = checkProduct.ma_gian_hang; item.ngay_tao = DateTime.Now; item.nguoi_tao = product.nguoi_tao; item.ngay_cap_nhat = DateTime.Now; item.nguoi_cap_nhat = product.nguoi_cap_nhat; db.Insert(item); } else { return("Lỗi khi tạo thumbnail hình ảnh"); } } } // Update Property if (listproperty != null) { db.Delete <Merchant_Product_Property>("ma_san_pham = {0} and ma_gian_hang = {1}", checkProduct.ma_san_pham, checkProduct.ma_gian_hang); foreach (Merchant_Product_Property item in listproperty) { if (item.id == 0) { item.ma_gian_hang = checkProduct.ma_gian_hang; item.ma_san_pham = checkProduct.ma_san_pham; item.ngay_tao = DateTime.Now; item.ngay_cap_nhat = DateTime.Parse("1900-01-01"); item.nguoi_cap_nhat = product.nguoi_cap_nhat; item.ngay_cap_nhat = DateTime.Now; db.Insert(item); } else { return("Lỗi khi cập nhật thuộc tính"); } } } //SyncToMySQL(checkProduct.id.ToString(), product.nguoi_cap_nhat); SyncToMySQL(checkProduct.id.ToString(), connectstring); return("true"); } catch (Exception ex) { return("Lỗi thao tác dữ liệu"); } } }
public string CreateProduct(Merchant_Product product, Merchant_Product_Hierarchy hierarchy, List <Merchant_Product_Image> listimage, List <Merchant_Product_Property> listproperty, string connectstring) { using (var db = new OrmliteConnection().openConn(connectstring)) { //using (var dbTrans = db.OpenTransaction(IsolationLevel.ReadCommitted)) { try { if (!string.IsNullOrEmpty(product.ma_san_pham)) { var Checkproduct = db.FirstOrDefault <Merchant_Product>(s => s.ma_san_pham == product.ma_san_pham); if (Checkproduct != null) { return(UpdateProduct(product, hierarchy, listimage, listproperty, connectstring)); } } if (product.id == 0 && !String.IsNullOrEmpty(product.nguoi_tao) && !String.IsNullOrEmpty(product.ma_gian_hang)) { var lastId = db.FirstOrDefault <Merchant_Product>("SELECT TOP 1 * FROM Merchant_Product ORDER BY ma_san_pham DESC"); if (lastId != null && lastId.ma_san_pham.Contains("SP")) { var nextNo = Int32.Parse(lastId.ma_san_pham.Substring(2, 7)) + 1; product.ma_san_pham = "SP" + String.Format("{0:0000000}", nextNo); } else { product.ma_san_pham = "SP" + "0000001"; } product.ma_loai_san_pham = !string.IsNullOrEmpty(product.ma_loai_san_pham) ? product.ma_loai_san_pham : ""; product.part_no = !string.IsNullOrEmpty(product.part_no) ? product.part_no : ""; product.mo_ta = !string.IsNullOrEmpty(product.mo_ta) ? product.mo_ta : ""; product.noi_dung = !string.IsNullOrEmpty(product.noi_dung) ? product.noi_dung : ""; product.tu_khoa = !string.IsNullOrEmpty(product.tu_khoa) ? product.tu_khoa : ""; product.tag = !string.IsNullOrEmpty(product.tag) ? product.tag : ""; product.slug = !string.IsNullOrEmpty(product.slug) ? product.slug : ""; product.xuat_xu = !string.IsNullOrEmpty(product.xuat_xu) ? product.xuat_xu : ""; product.model = !string.IsNullOrEmpty(product.model) ? product.model : ""; product.trang_thai = AllConstant.trang_thai.DANG_SU_DUNG; //giá trị đang hoạt động product.trang_thai_duyet = AllConstant.trang_thai_duyet.CHUA_DUYET; //giá trị chờ duyệt product.trang_thai_xuat_ban = AllConstant.trang_thai_xuat_ban.CHUA_XUAT_BAN; //giá trị chờ xuất bản product.nguoi_xuat_ban = product.nguoi_duyet = ""; product.ngay_xuat_ban = product.ngay_duyet = product.ngay_cap_nhat = DateTime.Parse("1900-01-01"); product.ngay_tao = DateTime.Now; db.Insert(product); Int64 productId = (Int64)db.GetLastInsertId(); //List<SqlParameter> param = new List<SqlParameter>(); //param.Add(new SqlParameter("@id", productId)); //new SqlHelper().ExecuteNoneQuery("p_UpdateSlugForMerchantProduct", param); if (hierarchy.id == 0) { hierarchy.ma_san_pham = product.ma_san_pham; hierarchy.ma_gian_hang = product.ma_gian_hang; hierarchy.ngay_tao = DateTime.Now; hierarchy.nguoi_tao = product.nguoi_tao; hierarchy.ngay_cap_nhat = DateTime.Parse("1900-01-01"); hierarchy.trang_thai = AllConstant.trang_thai.DANG_SU_DUNG; // Giá trị đang hoạt động db.Insert(hierarchy); if (listimage != null) { foreach (Merchant_Product_Image item in listimage) { if (item.id == 0) { item.ma_san_pham = product.ma_san_pham; item.ma_gian_hang = product.ma_gian_hang; item.ngay_tao = DateTime.Now; item.nguoi_tao = product.nguoi_tao; db.Insert(item); } else { //dbTrans.Rollback(); return("Lỗi khi tạo thumbnail hình ảnh"); } } } if (listproperty != null) { foreach (Merchant_Product_Property item in listproperty) { if (item.id == 0) { item.ma_gian_hang = product.ma_gian_hang; item.ma_san_pham = product.ma_san_pham; item.ngay_tao = DateTime.Now; item.nguoi_tao = product.nguoi_tao; item.ngay_cap_nhat = DateTime.Parse("1900-01-01"); db.Insert(item); } else { //dbTrans.Rollback(); return("Lỗi khi tạo thuộc tính"); } } } //dbTrans.Commit(); new Merchant_Product_Warehouse_DAO().CreateUpdate(product.ma_san_pham, product.ma_gian_hang, product.nguoi_tao, connectstring); return("true@@" + product.ma_san_pham); } else { // dbTrans.Rollback(); return("Lỗi khi tạo ngành hàng"); } } else { // dbTrans.Rollback(); return("Lỗi khi tạo sản phẩm"); } } catch (Exception ex) { //dbTrans.Rollback(); return("Lỗi thao tác dữ liệu"); } } } }