/// <summary> /// Lấy ra danh sách danh mục theo CategoryCode /// CreatedBy: Cuongpv1 /// CreatedDate: 23/05/2019 /// </summary> /// <param name="CategoryCode"></param> /// <returns></returns> public object GetCategoryByCateCode(string CategoryCode) { try { TblCategory category = db.TblCategory.Where(v => v.IsDelete == false && v.CategoryCode == CategoryCode).FirstOrDefault(); TblCategoryGroup categroup = db.TblCategoryGroup.Where(v => v.IsDelete == false && v.CategoryCode == CategoryCode).FirstOrDefault(); ListCategory objCategory = new ListCategory(); if (category != null) { objCategory.CategoryCode = category.CategoryCode; objCategory.CategoryTypeCode = category.CategoryTypeCode; objCategory.CategoryName = category.CategoryName; objCategory.CategoryDescription = category.CategoryDescription; objCategory.children = GetAllChildCategory(category.CategoryCode); } else { objCategory.CategoryCode = categroup.CategoryCode; objCategory.CategoryTypeCode = categroup.CategoryTypeCode; objCategory.CategoryName = categroup.CategoryGroupName; objCategory.CategoryDescription = categroup.CategoryDescription; objCategory.children = GetAllChildCategory(categroup.CategoryCode); } return(objCategory); } catch (Exception ex) { return(new { code = 500, messsage = ex.Message }); } }
/// <summary> /// xóa danh mục /// CreatedBy: Cuongpv1 /// CreatedDate: 23/05/2019 /// </summary> /// <param name="categoryCode"></param> /// <returns></returns> public object DeleteCategory(string categoryCode) { try { int code = 0; object deleteCateCode = new object(); if (categoryCode != "") { List <TblAttributeConstraint> checkUsing = db.TblAttributeConstraint.Where(v => v.IsDelete == false && (v.ContraintsType == categoryCode || v.LinkContraints == categoryCode)).ToList(); List <TblAttributes> checkDataUsing = db.TblAttributes.Where(v => v.IsDelete == false && (v.DataType == categoryCode || v.AttributeType == categoryCode || v.CategoryParentCode == categoryCode || v.DefaultValue == categoryCode)).ToList(); if (checkDataUsing.Count > 0) { deleteCateCode = new { code = 0, CategoryCode = "" }; } else if (checkUsing.Count > 0) { deleteCateCode = new { code = 0, CategoryCode = "" }; } else { TblCategory deleteCategory = db.TblCategory.Where(v => v.IsDelete == false && v.CategoryCode == categoryCode).FirstOrDefault(); TblCategoryGroup deleteCategroup = db.TblCategoryGroup.Where(v => v.IsDelete == false && v.CategoryCode == categoryCode).FirstOrDefault(); if (deleteCategory != null) { //delete child List <TblCategory> deleteChild = db.TblCategory.Where(v => v.CategoryTypeCode == categoryCode && v.IsDelete == false).ToList(); List <TblCategoryGroup> deleteChild1 = db.TblCategoryGroup.Where(v => v.CategoryTypeCode == categoryCode && v.IsDelete == false).ToList(); if (deleteChild1.Count > 0) { deleteCateCode = new { code = 0, CategoryCode = "" }; } else { if (deleteChild.Count > 0) { db.TblCategory.RemoveRange(deleteChild); db.SaveChanges(); } db.TblCategory.Remove(deleteCategory); code = db.SaveChanges(); deleteCateCode = new { code = code, CategoryCode = deleteCategory.CategoryCode }; } } else if (deleteCategroup != null) { List <TblCategoryGroup> deleteCateGroupchild = db.TblCategoryGroup.Where(v => v.CategoryTypeCode == categoryCode && v.IsDelete == false).ToList(); List <TblCategory> deleteCategoryChild = db.TblCategory.Where(v => v.CategoryTypeCode == categoryCode && v.IsDelete == false).ToList(); if (deleteCateGroupchild.Count > 0) { deleteCateCode = new { code = 0, CategoryCode = "" }; } else { if (deleteCategoryChild.Count > 0) { db.TblCategory.RemoveRange(deleteCategoryChild); db.SaveChanges(); } db.TblCategoryGroup.Remove(deleteCategroup); code = db.SaveChanges(); deleteCateCode = new { code = code, CategoryCode = deleteCategroup.CategoryCode }; } } else { deleteCateCode = new { code = 2, CategoryCode = "" }; } } } else { deleteCateCode = new { code = 2, CategoryCode = "" }; } return(deleteCateCode); } catch (Exception ex) { return(new { code = 500, messsage = ex.Message }); } }
/// <summary> /// Tạo mới danh mục /// CreatedBy: Cuongpv1 /// CreatedDate: 23/05/2019 /// </summary> /// <param name="tblCategory">object</param> /// <returns></returns> public object AddCategory(CategoryChildren tblCategory) { int code = 0; object objCategory = new object(); try { string cateCode = LocDau(tblCategory.CategoryName).ToUpper(); string cateCode1 = (cateCode != "") ? Regex1(cateCode, CategoryConstant.regex, CategoryConstant.Space) : ""; TblCategoryGroup addCateGroup = new TblCategoryGroup(); TblCategory addCategory = new TblCategory(); TblCategory checkCateCode = db.TblCategory.Where(v => v.CategoryCode == cateCode1 && v.IsDelete == false).FirstOrDefault(); TblCategoryGroup checkCateGroup = db.TblCategoryGroup.Where(v => v.CategoryCode == cateCode1 && v.IsDelete == false).FirstOrDefault(); if (tblCategory.CategoryTypeCode != "") { if (checkCateCode == null) { if (checkCateGroup == null) { using (var ts = new TransactionScope()) { addCateGroup.CategoryCode = cateCode1; addCateGroup.CategoryTypeCode = tblCategory.CategoryTypeCode; addCateGroup.CategoryGroupName = tblCategory.CategoryName; addCateGroup.CategoryDescription = tblCategory.CategoryDescription; addCateGroup.CreateBy = tblCategory.CreateBy; addCateGroup.CreateDate = DateTime.Now; addCateGroup.IsDelete = false; db.TblCategoryGroup.Add(addCateGroup); code = db.SaveChanges(); if (tblCategory.children.Count > 0) { AddCategoryChild(addCateGroup.CategoryCode, tblCategory.children); } objCategory = new { code = code, CategoryCode = addCateGroup.CategoryCode }; ts.Complete(); } } else { objCategory = new { code = code, CategoryCode = "" }; } } else { objCategory = new { code = code, CategoryCode = "" }; } } else { if (checkCateGroup == null) { if (checkCateCode == null) { using (var ts = new TransactionScope()) { addCategory.CategoryCode = cateCode1; addCategory.CategoryTypeCode = tblCategory.CategoryTypeCode; addCategory.CategoryName = tblCategory.CategoryName; addCategory.CategoryDescription = tblCategory.CategoryDescription; addCategory.IsDelete = false; addCategory.CreateBy = tblCategory.CreateBy; addCategory.CreateDate = DateTime.Now; db.TblCategory.Add(addCategory); code = db.SaveChanges(); if (tblCategory.children.Count > 0) { AddCategoryChild(addCategory.CategoryCode, tblCategory.children); } objCategory = new { code = code, CategoryCode = addCategory.CategoryCode }; ts.Complete(); } } else { objCategory = new { code = code, CategoryCode = "" }; } } else { objCategory = new { code = code, CategoryCode = "" }; } } return(objCategory); } catch (Exception ex) { return(new { code = 500, messsage = ex.Message }); } }
/// <summary> /// chỉnh sửa danh mục /// CreatedBy: Cuongpv1 /// CreatedDate: 23/05/2019 /// </summary> /// <param name="tblCategory">object</param> /// <returns></returns> public object UpdateCategory(CategoryChildren tblCategory) { try { int code = 0; object objCategory = new object(); string cateCode = LocDau(tblCategory.CategoryName).ToUpper(); string cateCode1 = (cateCode != "") ? Regex1(cateCode, CategoryConstant.regex, CategoryConstant.Space) : ""; TblCategoryGroup checkCateGroup = db.TblCategoryGroup.Where(v => v.CategoryCode == tblCategory.CategoryCode && v.IsDelete == false).FirstOrDefault(); TblCategory checkCateCode = db.TblCategory.Where(v => v.CategoryCode == tblCategory.CategoryCode && v.IsDelete == false).FirstOrDefault(); if (tblCategory.CategoryTypeCode != "") { if (checkCateGroup != null) { TblCategoryGroup checkCategoryCode_New = db.TblCategoryGroup.Where(v => v.CategoryCode == cateCode1 && v.CategoryGroupName.ToLower().TrimStart().TrimEnd() == tblCategory.CategoryName.ToLower().TrimStart().TrimEnd() && v.IsDelete == false).FirstOrDefault(); if (checkCategoryCode_New != null) { using (var ts = new TransactionScope()) { checkCateGroup.CategoryCode = cateCode1; checkCateGroup.CategoryGroupName = tblCategory.CategoryName; checkCateGroup.CategoryTypeCode = tblCategory.CategoryTypeCode; checkCateGroup.CategoryDescription = tblCategory.CategoryDescription; checkCateGroup.UpdateBy = tblCategory.UpdateBy; checkCateGroup.UpdateDate = DateTime.Now; checkCateGroup.IsDelete = false; db.Entry(checkCateGroup).State = EntityState.Modified; code = db.SaveChanges(); if (tblCategory.children.Count > 0) { AddCategoryChild(checkCateGroup.CategoryCode, tblCategory.children); } if (tblCategory.deleteCategory.Count > 0) { DeleteCategoryChild(tblCategory.deleteCategory); } objCategory = new { code = code, CategoryCode = checkCateGroup.CategoryCode }; ts.Complete(); } } else { TblCategoryGroup checkTblCategoryGroup = db.TblCategoryGroup.Where(v => v.CategoryCode == cateCode1 && v.IsDelete == false).FirstOrDefault(); if (checkTblCategoryGroup != null) { objCategory = new { code = 0, CategoryCode = "" }; } else { using (var ts = new TransactionScope()) { checkCateGroup.CategoryCode = cateCode1; checkCateGroup.CategoryGroupName = tblCategory.CategoryName; checkCateGroup.CategoryTypeCode = tblCategory.CategoryTypeCode; checkCateGroup.CategoryDescription = tblCategory.CategoryDescription; checkCateGroup.UpdateBy = tblCategory.UpdateBy; checkCateGroup.UpdateDate = DateTime.Now; checkCateGroup.IsDelete = false; db.Entry(checkCateGroup).State = EntityState.Modified; code = db.SaveChanges(); if (tblCategory.children.Count > 0) { AddCategoryChild(checkCateGroup.CategoryCode, tblCategory.children); } if (tblCategory.deleteCategory.Count > 0) { DeleteCategoryChild(tblCategory.deleteCategory); } objCategory = new { code = code, CategoryCode = checkCateGroup.CategoryCode }; ts.Complete(); } } } } else { TblCategory checkCateCode1 = db.TblCategory.Where(v => v.CategoryCode == tblCategory.CategoryCode && v.IsDelete == false).FirstOrDefault(); using (var ts = new TransactionScope()) { TblCategoryGroup addCategory = new TblCategoryGroup(); addCategory.CategoryCode = cateCode1; addCategory.CategoryGroupName = tblCategory.CategoryName; addCategory.CategoryTypeCode = tblCategory.CategoryTypeCode; addCategory.CategoryDescription = tblCategory.CategoryDescription; addCategory.CreateBy = tblCategory.CreateBy; addCategory.CreateDate = DateTime.Now; addCategory.IsDelete = false; db.TblCategoryGroup.Add(addCategory); code = db.SaveChanges(); if (checkCateCode1 != null) { db.TblCategory.Remove(checkCateCode1); db.SaveChanges(); } if (tblCategory.children.Count > 0) { AddCategoryChild(addCategory.CategoryCode, tblCategory.children); } if (tblCategory.deleteCategory.Count > 0) { DeleteCategoryChild(tblCategory.deleteCategory); } objCategory = new { code = code, CategoryCode = addCategory.CategoryCode }; ts.Complete(); } } } else { if (checkCateCode != null) { TblCategory checkCateCode_New = db.TblCategory.Where(v => v.CategoryCode == tblCategory.CategoryCode && v.CategoryName.ToLower().TrimStart().TrimEnd() == tblCategory.CategoryName.ToLower().TrimStart().TrimEnd() && v.IsDelete == false).FirstOrDefault(); if (checkCateCode_New != null) { using (var ts = new TransactionScope()) { checkCateCode.CategoryCode = cateCode1; checkCateCode.CategoryName = tblCategory.CategoryName; checkCateCode.CategoryTypeCode = tblCategory.CategoryTypeCode; checkCateCode.CategoryDescription = tblCategory.CategoryDescription; checkCateCode.CreateBy = tblCategory.CreateBy; checkCateCode.UpdateDate = DateTime.Now; checkCateCode.UpdateBy = tblCategory.UpdateBy; checkCateCode.IsDelete = false; db.Entry(checkCateCode).State = EntityState.Modified; code = db.SaveChanges(); if (tblCategory.children.Count > 0) { AddCategoryChild(checkCateCode.CategoryCode, tblCategory.children); } if (tblCategory.deleteCategory.Count > 0) { DeleteCategoryChild(tblCategory.deleteCategory); } objCategory = new { code = code, CategoryCode = checkCateCode.CategoryCode }; ts.Complete(); } } else { TblCategory checkCategory = db.TblCategory.Where(v => v.CategoryCode == cateCode1 && v.IsDelete == false).FirstOrDefault(); if (checkCategory != null) { objCategory = new { code = 0, CategoryCode = "" }; } else { TblCategoryGroup removeCategoryGroup = db.TblCategoryGroup.Where(v => v.CategoryCode == cateCode1 && v.IsDelete == false).FirstOrDefault(); using (var ts = new TransactionScope()) { TblCategory addCate = new TblCategory(); addCate.CategoryCode = cateCode1; addCate.CategoryTypeCode = tblCategory.CategoryTypeCode; addCate.CategoryName = tblCategory.CategoryName; addCate.CategoryDescription = tblCategory.CategoryDescription; addCate.IsDelete = false; addCate.CreateBy = tblCategory.UpdateBy; addCate.CreateDate = DateTime.Now; db.TblCategory.Add(addCate); code = db.SaveChanges(); if (removeCategoryGroup != null) { db.TblCategoryGroup.Remove(removeCategoryGroup); db.SaveChanges(); } if (tblCategory.children.Count > 0) { AddCategoryChild(addCate.CategoryCode, tblCategory.children); } if (tblCategory.deleteCategory.Count > 0) { DeleteCategoryChild(tblCategory.deleteCategory); } objCategory = new { code = code, CategoryCode = addCate.CategoryCode }; ts.Complete(); } } } } } return(objCategory); } catch (Exception ex) { return(new { code = 500, messsage = ex.Message }); } }