public JsonResult Save(WorkShopModel model) { ResponseBase rs; try { if (isAuthenticate) { model.CompanyId = UserContext.CompanyId; model.ActionUser = UserContext.UserID; rs = BLLWorkshop.Instance.InsertOrUpdate(model); if (!rs.IsSuccess) { JsonDataResult.Result = "ERROR"; JsonDataResult.ErrorMessages.AddRange(rs.Errors); } else { JsonDataResult.Result = "OK"; } } } catch (Exception ex) { //add error JsonDataResult.Result = "ERROR"; JsonDataResult.ErrorMessages.Add(new Error() { MemberName = "Update ", Message = "Lỗi: " + ex.Message }); } return(Json(JsonDataResult)); }
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); }