Example #1
0
        /// <summary>
        /// Lấy giá trị thuộc tính đưa lên textbox (Nếu có)
        /// </summary>
        /// <param name="lstProduct_Info_Details"></param>
        /// <param name="iPropertyId"></param>
        /// <returns></returns>
        private string GetValue_PropertiesValue(List <ModProduct_Info_DetailsEntity> lstProduct_Info_Details, int iPropertyId)
        {
            string sValue = string.Empty;
            ModProduct_Info_DetailsEntity Product_Info_Detail = lstProduct_Info_Details.Where(p => p.PropertiesListId == iPropertyId).SingleOrDefault();

            if (Product_Info_Detail != null)
            {
                sValue = Product_Info_Detail.Content;
            }

            return("<span name='txtPropertyValue" + iPropertyId + "' id='txtPropertyValue" + iPropertyId + "' style='width: 90%;'>" + sValue + "</span>");
        }
        public void ActionAdd(ModProduct_Info_DetailsModel model)
        {
            if (model.ProductInfoId > 0)
            {
                // khoi tao gia tri mac dinh khi update
                // Lấy thông tin sản phẩm
                ModProduct_InfoEntity objModProduct_InfoEntity = ModProduct_InfoService.Instance.GetByID(model.ProductInfoId);

                // Lấy thông tin chủng loại
                WebMenuEntity objWebMenuEntity = WebMenuService.Instance.GetByID(objModProduct_InfoEntity.MenuID);

                // Lưu lại Nhóm thuộc tính ID
                model.PropertiesGroupsId = (int)objWebMenuEntity.ProductAreaId;

                if (objWebMenuEntity.ProductAreaId <= 0)
                {
                    CPViewPage.Message.ListMessage.Add("Sản phẩm chưa có nhóm thuộc tính nào. Yêu cầu chọn các nhóm cho Sản phẩm để có thể nhập thông tin");
                    ViewBag.Data = item;
                    ViewBag.Model = model;
                    return;
                }

                model.GetPropertiesList_Value = ModProduct_Info_DetailsService.Instance.CreateQuery()
                    .Where(o => o.PropertiesGroupsId == objWebMenuEntity.ProductAreaId && o.ProductInfoId == model.ProductInfoId)
                    .ToList();

                // Where In Lấy danh sách nhóm thuộc tính chứa thuộc tính SP
                model.PropertiesGroup = ModProduct_PropertiesGroupsService.Instance.CreateQuery()
                    .Where(o => o.ID == objWebMenuEntity.ProductAreaId && o.Activity == true)
                    .OrderByAsc(o => o.Order).ToSingle();

                // Lấy danh sách thuộc tính thuộc nhóm bên trên
                model.GetPropertiesList = ModProduct_PropertiesListService.Instance.CreateQuery()
                    .Where(o => o.PropertiesGroupsId == objWebMenuEntity.ProductAreaId && o.Activity == true)
                    .OrderByAsc(o => o.Order)
                    .ToList();

                // Lấy danh sách thứ tự thuộc tính để cập nhật dữ liệu theo thứ tự này
                string sThuTu = string.Empty;

                foreach (var itemProperties in model.GetPropertiesList)
                {
                    sThuTu += itemProperties.ID + ",";
                }
                model.DanhSachThuTuThuocTinh = sThuTu.Trim(',');

                // Lấy danh sách các giá trị của các thuộc tính có sẵn
                List<ModProduct_PropertiesList_ValuesEntity> lstModProduct_PropertiesList_Values= ModProduct_PropertiesList_ValuesService.Instance.CreateQuery().WhereIn(o => o.PropertiesListId, model.DanhSachThuTuThuocTinh).ToList();
                if (lstModProduct_PropertiesList_Values == null)
                    lstModProduct_PropertiesList_Values = new List<ModProduct_PropertiesList_ValuesEntity>();

                model.GetProduct_PropertiesList_Values = lstModProduct_PropertiesList_Values;
            }
            else
            {
                item = new ModProduct_Info_DetailsEntity();

                // khoi tao gia tri mac dinh khi insert
            }

            ViewBag.Data = item;
            ViewBag.Model = model;
        }
        private bool ValidSave(ModProduct_Info_DetailsModel model)
        {
            //TryUpdateModel(item);

            // Lấy thông tin Thuộc tính
            string sThuTu = model.DanhSachThuTuThuocTinh;
            if (string.IsNullOrEmpty(sThuTu))
                return false;

            string[] ArrThuTuId = sThuTu.Split(',');
            ThuocTinh objThuocTinh = null;
            List<ThuocTinh> lstThuocTinh = new List<ThuocTinh>();

            for (int i = 0; i < ArrThuTuId.Length; i++)
            {
                objThuocTinh = new ThuocTinh();
                objThuocTinh.ProductInfoId = model.ProductInfoId;
                objThuocTinh.PropertiesGroupsId = model.PropertiesGroupsId;
                objThuocTinh.PropertiesListId = int.Parse(ArrThuTuId[i]);
                objThuocTinh.Content = CPViewPage.Request.Form["Value" + i];
                if (CPViewPage.Request.Form["chkSaveData" + i] == null || CPViewPage.Request.Form["chkSaveData" + i] != "on")
                    objThuocTinh.SaveData = false;
                else
                    objThuocTinh.SaveData = true;

                // Thêm vào danh sách
                lstThuocTinh.Add(objThuocTinh);
            }

            ViewBag.Data = item;
            ViewBag.Model = model;

            try
            {
                // Xóa dữ liệu cũ
                ModProduct_Info_DetailsService.Instance.Delete("ProductInfoId=" + model.ProductInfoId);

                // Lấy danh sách insert 
                List<ModProduct_Info_DetailsEntity> lstModProduct_Info_DetailsEntity = new List<ModProduct_Info_DetailsEntity>();
                ModProduct_Info_DetailsEntity itemModProduct_Info_DetailsEntity = null;

                foreach (var itemThuocTinh in lstThuocTinh)
                {
                    itemModProduct_Info_DetailsEntity = new ModProduct_Info_DetailsEntity();
                    itemModProduct_Info_DetailsEntity.ProductInfoId = itemThuocTinh.ProductInfoId;
                    itemModProduct_Info_DetailsEntity.PropertiesGroupsId = itemThuocTinh.PropertiesGroupsId;
                    itemModProduct_Info_DetailsEntity.PropertiesListId = itemThuocTinh.PropertiesListId;
                    itemModProduct_Info_DetailsEntity.Content = itemThuocTinh.Content;

                    // Thêm vào list
                    lstModProduct_Info_DetailsEntity.Add(itemModProduct_Info_DetailsEntity);
                }

                // Insert vào DataBase
                ModProduct_Info_DetailsService.Instance.Save(lstModProduct_Info_DetailsEntity);

                // Thêm giá trị các thuộc tính vào bảng thuộc tính - giá trị
                List<ModProduct_PropertiesList_ValuesEntity> lstModProduct_PropertiesList_ValuesEntity = new List<ModProduct_PropertiesList_ValuesEntity>();
                ModProduct_PropertiesList_ValuesEntity itemPropertiesList_ValuesEntity = null;
                List<ModProduct_PropertiesList_ValuesEntity> lstModProduct_PropertiesList_ValuesEntity_Check = ModProduct_PropertiesList_ValuesService.Instance.CreateQuery().WhereIn(o => o.PropertiesListId, sThuTu).ToList();
                if (lstModProduct_PropertiesList_ValuesEntity_Check == null)
                    lstModProduct_PropertiesList_ValuesEntity_Check = new List<ModProduct_PropertiesList_ValuesEntity>();

                foreach (var itemThuocTinh in lstThuocTinh)
                {
                    if (itemThuocTinh.SaveData == false)
                        continue;

                    // Kiểm tra xem giá trị tồn tại chưa
                    itemPropertiesList_ValuesEntity = lstModProduct_PropertiesList_ValuesEntity_Check
                        .Where(p => p.PropertiesListId == itemThuocTinh.PropertiesListId && p.Content.ToUpper() == itemThuocTinh.Content.ToUpper()).SingleOrDefault();

                    // Nếu tồn tại rồi thì không thêm nữa
                    if (itemPropertiesList_ValuesEntity != null)
                        continue;

                    itemPropertiesList_ValuesEntity = new ModProduct_PropertiesList_ValuesEntity();

                    itemPropertiesList_ValuesEntity.PropertiesListId = itemThuocTinh.PropertiesListId;
                    itemPropertiesList_ValuesEntity.Content = itemThuocTinh.Content;

                    // Thêm vào danh sách
                    lstModProduct_PropertiesList_ValuesEntity.Add(itemPropertiesList_ValuesEntity);
                }

                // Insert vào DataBase
                ModProduct_PropertiesList_ValuesService.Instance.Save(lstModProduct_PropertiesList_ValuesEntity);
            }
            catch (Exception ex)
            {
                Global.Error.Write(ex);
                CPViewPage.Message.ListMessage.Add(ex.Message);
                return false;
            }

            return true;
        }