private bool CheckExists(string name, string code, int Id, int WorkShopId, IEDEntities db) { try { using (db = new IEDEntities()) { T_Line objectExists = null; if (!string.IsNullOrEmpty(name)) { objectExists = db.T_Line.FirstOrDefault(c => !c.IsDeleted && c.Id != Id && c.WorkShopId == WorkShopId && c.Name.Trim().ToUpper().Equals(name.Trim().ToUpper())); } else { objectExists = db.T_Line.FirstOrDefault(c => !c.IsDeleted && c.Id != Id && c.WorkShopId == WorkShopId && c.Code.Trim().ToUpper().Equals(code.Trim().ToUpper())); } if (objectExists == null) { return(false); } return(true); } } catch (Exception ex) { throw ex; } }
bool checkPermis(T_Line obj, int actionUser, bool isOwner) { if (isOwner) { return(true); } return(obj.CreatedUser == actionUser); }
/// <summary> /// 提交(新增/编辑) /// </summary> /// <returns></returns> public ActionResult Submit(T_Line line) { var successResult = new LineService().Submit(line); return(Json(successResult, JsonRequestBehavior.AllowGet)); }
/// <summary> /// 删除 /// </summary> /// <param name="station"></param> /// <returns></returns> public ActionResult DeleteSave(T_Line line) { var successResult = new LineService().DeleteSave(line); return(Json(successResult, JsonRequestBehavior.AllowGet)); }
public ResponseBase InsertOrUpdate(LineModel model, bool isOwner) { ResponseBase result = new ResponseBase(); var flag = false; try { using (db = new IEDEntities()) { if (CheckExists(model.Name, null, model.Id, model.WorkShopId, db)) { flag = true; result.IsSuccess = false; result.Errors.Add(new Error() { MemberName = "Create ", Message = "Tên Chuyền 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, model.Id, model.WorkShopId, db)) { flag = true; result.IsSuccess = false; result.Errors.Add(new Error() { MemberName = "Create ", Message = "Mã Chuyền này Đã Tồn Tại,Vui Lòng Chọn Mã Khác" }); } } if (!flag) { T_Line obj; if (model.Id == 0) { obj = new T_Line(); Parse.CopyObject(model, ref obj); obj.CreatedDate = DateTime.Now; obj.CreatedUser = model.ActionUser; db.T_Line.Add(obj); db.SaveChanges(); result.IsSuccess = true; } else { obj = db.T_Line.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 chuyền này nên bạn không cập nhật được thông tin cho chuyền này." }); } else { obj.Code = model.Code; obj.Name = model.Name; obj.CountOfLabours = model.CountOfLabours; obj.WorkShopId = model.WorkShopId; obj.Description = model.Description; obj.UpdatedDate = DateTime.Now; obj.UpdatedUser = model.ActionUser; db.SaveChanges(); result.IsSuccess = true; } } else { result.IsSuccess = false; result.Errors.Add(new Error() { MemberName = "UpdateLine", Message = "Chuyền này Không tồn tại hoặc đã bị xóa. Vui lòng kiểm tra lại!" }); } } } } } catch (Exception ex) { throw ex; } return(result); }