private bool CheckExists(string keyword, int Id, bool isCheckName)
        {
            try
            {
                T_ManipulationTypeLibrary maniType = null;
                keyword = keyword.Trim().ToUpper();
                if (isCheckName)
                {
                    maniType = db.T_ManipulationTypeLibrary.FirstOrDefault(x => !x.IsDeleted && x.Id != Id && x.Name.Trim().ToUpper().Equals(keyword));
                }
                else
                {
                    maniType = db.T_ManipulationTypeLibrary.FirstOrDefault(x => !x.IsDeleted && x.Id != Id && x.Code.Trim().ToUpper().Equals(keyword));
                }

                if (maniType == null)
                {
                    return(false);
                }
                return(true);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Ejemplo n.º 2
0
        public JsonResult Save(T_ManipulationTypeLibrary maniType)
        {
            ResponseBase responseResult;

            try
            {
                if (maniType.Id == 0)
                {
                    maniType.CreatedUser = UserContext.UserID;
                    maniType.CreatedDate = DateTime.Now;
                    responseResult       = BLLManipulationTypeLibrary.Instance.InsertOrUpdate(maniType);
                }
                else
                {
                    maniType.UpdatedUser = UserContext.UserID;
                    maniType.UpdatedDate = DateTime.Now;
                    responseResult       = BLLManipulationTypeLibrary.Instance.InsertOrUpdate(maniType);
                }
                if (!responseResult.IsSuccess)
                {
                    JsonDataResult.Result = "ERROR";
                    JsonDataResult.ErrorMessages.AddRange(responseResult.Errors);
                }
                else
                {
                    JsonDataResult.Result = "OK";
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(Json(JsonDataResult));
        }
 public ResponseBase InsertOrUpdate(T_ManipulationTypeLibrary model)
 {
     try
     {
         using (db = new IEDEntities())
         {
             var result = new ResponseBase();
             T_ManipulationTypeLibrary obj = null;
             bool flag = false;
             if (CheckExists(model.Name.Trim().ToUpper(), model.Id, true))
             {
                 result.IsSuccess = false;
                 result.Errors.Add(new Error()
                 {
                     MemberName = "Insert  ", Message = "Tên này đã tồn tại trong Loại Thao Tác này. Vui lòng chọn lại Tên khác !."
                 });
                 flag = true;
             }
             if (!string.IsNullOrEmpty(model.Code))
             {
                 if (CheckExists(model.Code.Trim().ToUpper(), model.Id, false))
                 {
                     result.IsSuccess = false;
                     result.Errors.Add(new Error()
                     {
                         MemberName = "Insert  ", Message = "Mã này đã tồn tại trong Loại Thao Tác này. Vui lòng chọn lại Mã khác !."
                     });
                     flag = true;
                 }
             }
             if (!flag)
             {
                 if (model.Id == 0)
                 {
                     obj = new T_ManipulationTypeLibrary();
                     Parse.CopyObject(model, ref obj);
                     obj.Node = obj.Node == "0" ? "0," : obj.Node + obj.ParentId + ",";
                     db.T_ManipulationTypeLibrary.Add(obj);
                 }
                 else
                 {
                     //Update
                     obj = db.T_ManipulationTypeLibrary.FirstOrDefault(x => !x.IsDeleted && x.Id == model.Id);
                     if (obj == null)
                     {
                         result.IsSuccess = false;
                         result.Errors.Add(new Error()
                         {
                             MemberName = "Update  ", Message = "Dữ liệu bạn đang thao tác đã bị xóa hoặc không tồn tại. Vui lòng kiểm tra lại !."
                         });
                         flag = true;
                     }
                     else
                     {
                         if (model.IsUseMachine && !obj.IsUseMachine && obj.T_ManipulationLibrary.Count > 0)
                         {
                             result.IsSuccess = false;
                             result.Errors.Add(new Error()
                             {
                                 MemberName = "Update  ", Message = "Loại Thao Tác này đã có Thao Tác con.\nBạn không thể chuyển sang Loại sử dụng Thiết Bị được.\nVui lòng kiểm tra lại !."
                             });
                             flag = true;
                         }
                         else
                         {
                             obj.Name         = model.Name;
                             obj.Code         = model.Code;
                             obj.IsUseMachine = model.IsUseMachine;
                             obj.Description  = model.Description;
                             obj.UpdatedUser  = model.UpdatedUser;
                             obj.UpdatedDate  = model.UpdatedDate;
                         }
                     }
                 }
                 if (!flag)
                 {
                     db.SaveChanges();
                     result.IsSuccess = true;
                 }
             }
             return(result);
         }
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }