private bool CheckExists(string name, string code, int Id, int CompanyId, IEDEntities db) { try { T_WorkShop objectExists = null; if (!string.IsNullOrEmpty(name)) { objectExists = db.T_WorkShop.FirstOrDefault(c => !c.IsDeleted && c.Id != Id && c.CompanyId == CompanyId && c.Name.Trim().ToUpper().Equals(name.Trim().ToUpper())); } else { objectExists = db.T_WorkShop.FirstOrDefault(c => !c.IsDeleted && c.Id != Id && c.CompanyId == CompanyId && c.Code.Trim().ToUpper().Equals(code.Trim().ToUpper())); } if (objectExists == null) { return(false); } return(true); } catch (Exception ex) { throw ex; } }
bool checkPermis(T_WorkShop obj, int actionUser, bool isOwner) { if (isOwner) { return(true); } return(obj.CreatedUser == actionUser); }
public ResponseBase InsertOrUpdate(WorkShopModel model, bool isOwner) { ResponseBase result = new ResponseBase(); result.IsSuccess = false; var flag = false; try { using (db = new IEDEntities()) { if (CheckExists(model.Name.Trim().ToUpper(), null, model.Id, model.CompanyId, db)) { flag = true; result.IsSuccess = false; result.Errors.Add(new Error() { MemberName = "Create ", Message = "Tên Phân Xưởng này Đã Tồn Tại,Vui Lòng Chọn Tên Khác" }); } else if (!string.IsNullOrEmpty(model.Code)) { if (CheckExists(null, model.Code.Trim().ToUpper(), model.Id, model.CompanyId, db)) { flag = true; result.IsSuccess = false; result.Errors.Add(new Error() { MemberName = "Create ", Message = "Mã Phân Xưởng này Đã Tồn Tại,Vui Lòng Chọn Mã Khác" }); } } if (!flag) { T_WorkShop obj; if (model.Id == 0) { obj = new T_WorkShop(); Parse.CopyObject(model, ref obj); obj.CreatedDate = DateTime.Now; db.T_WorkShop.Add(obj); db.SaveChanges(); result.IsSuccess = true; } else { obj = db.T_WorkShop.FirstOrDefault(x => x.Id == model.Id && !x.IsDeleted); if (obj != null) { if (!checkPermis(obj, model.ActionUser, isOwner)) { result.IsSuccess = false; result.Errors.Add(new Error() { MemberName = "update", Message = "Bạn không phải là người tạo phân xưởng này nên bạn không cập nhật được thông tin cho phân xưởng này." }); } else { obj.Code = model.Code; obj.Name = model.Name; obj.Description = model.Description; obj.UpdatedDate = DateTime.Now; obj.UpdatedUser = model.ActionUser; // cap nhat ben phan tich mat hang var commoAna = db.T_CommodityAnalysis.Where(x => !x.IsDeleted && x.ObjectId == obj.Id && x.ObjectType == (int)eObjectType.isWorkShop); if (commoAna != null && commoAna.Count() > 0) { foreach (var item in commoAna) { item.Name = obj.Name; item.UpdatedUser = model.ActionUser; item.UpdatedDate = DateTime.Now; } } db.SaveChanges(); result.IsSuccess = true; } } else { result.IsSuccess = false; result.Errors.Add(new Error() { MemberName = "UpdateWorkShop", Message = "Thông tin nhập không đúng Vui lòng kiểm tra lại!" }); } } } } } catch (Exception ex) { throw ex; } return(result); }