public CResult <bool> InsertDeviceType(WebDeviceType model) { LogHelper.Info(MethodBase.GetCurrentMethod().ToString()); LogHelper.Info("model", model); if (string.IsNullOrEmpty(model.ProjectID)) { return(new CResult <bool>(false, ErrorCode.ParameterError)); } using (var context = new DeviceMgmtEntities()) { if (context.Project.Any(t => t.IsValid && t.ID == model.ProjectID) == false) { return(new CResult <bool>(false, ErrorCode.ProjectNotExist)); } if (context.DeviceType.Any(t => t.Name.ToUpper() == model.Name.ToUpper() && t.ProjectID == model.ProjectID && t.IsValid)) { return(new CResult <bool>(false, ErrorCode.DeviceTypeNameIsExist)); } var maintainItemIDs = model.MaintainItems.Select(t => t.ID).ToList(); if (maintainItemIDs.Count > 0) { if (context.MaintainItem.Count(t => maintainItemIDs.Contains(t.ID) && t.IsValid && t.ProjectID == model.ProjectID) < maintainItemIDs.Count) { return(new CResult <bool>(false, ErrorCode.MaintainItemNotExist)); } } var entity = new DeviceType(); entity.CreateDate = DateTime.Now; entity.CreateUserID = model.CreateUserID; entity.ID = Guid.NewGuid().ToString(); entity.Name = model.Name; entity.IsValid = true; entity.Note = model.Note; entity.ProjectID = model.ProjectID; foreach (var item in model.MaintainItems) { var relation = new DeviceTypeMaintainItemRel() { DeviceTypeID = entity.ID, MaintainItemID = item.ID }; context.DeviceTypeMaintainItemRel.Add(relation); } context.DeviceType.Add(entity); return(context.Save()); } }
public ActionResult Edit(WebDeviceType webDeviceType) { try { webDeviceType.ProjectID = this.GetCurrentProjectID(); var result = new DeviceTypeBLL().UpdateDeviceType(webDeviceType); return(JsonContentHelper.GetJsonContent(result)); } catch { return(View()); } }
public ActionResult Create(WebDeviceType webDeviceType) { try { webDeviceType.ProjectID = this.GetCurrentProjectID(); webDeviceType.CreateUserID = this.GetCurrentUserID(); webDeviceType.ProjectID = this.GetCurrentProjectID(); var result = new DeviceTypeBLL().InsertDeviceType(webDeviceType); return(JsonContentHelper.GetJsonContent(result)); } catch { return(View()); } }
public CResult <WebDeviceType> GetDeviceTypeByID(string deviceTypeID) { LogHelper.Info(MethodBase.GetCurrentMethod().ToString()); LogHelper.Info("deviceTypeID", deviceTypeID); if (string.IsNullOrEmpty(deviceTypeID)) { return(new CResult <WebDeviceType>(null, ErrorCode.ParameterError)); } using (var context = new DeviceMgmtEntities()) { var entity = context.DeviceType.FirstOrDefault(t => t.ID == deviceTypeID && t.IsValid); if (entity == null) { return(new CResult <WebDeviceType>(null, ErrorCode.DeviceTypeNotExist)); } var items = (from rel in context.DeviceTypeMaintainItemRel join item in context.MaintainItem on rel.MaintainItemID equals item.ID where rel.DeviceTypeID == entity.ID select new WebMaintainItem() { ID = item.ID, Name = item.Name }).ToList(); var model = new WebDeviceType() { CreateDate = entity.CreateDate, ID = entity.ID, Name = entity.Name, ProjectID = entity.ProjectID, Note = entity.Note, MaintainItems = items }; LogHelper.Info("result", model); return(new CResult <WebDeviceType>(model)); } }
public CResult <bool> UpdateDeviceType(WebDeviceType model) { LogHelper.Info(MethodBase.GetCurrentMethod().ToString()); LogHelper.Info("model", model); if (string.IsNullOrEmpty(model.ID)) { return(new CResult <bool>(false, ErrorCode.ParameterError)); } using (var context = new DeviceMgmtEntities()) { var entity = context.DeviceType.FirstOrDefault(t => t.ID == model.ID && t.IsValid); if (entity == null) { return(new CResult <bool>(false, ErrorCode.DeviceTypeNotExist)); } if (context.DeviceType.Any(t => t.Name.ToUpper() == model.Name.ToUpper() && t.ProjectID == entity.ProjectID && t.IsValid && t.ID != model.ID)) { return(new CResult <bool>(false, ErrorCode.DeviceTypeNameIsExist)); } entity.Name = model.Name; entity.Note = model.Note; var maintainItemIDs = model.MaintainItems.Select(t => t.ID).ToList(); if (maintainItemIDs.Count > 0) { if (context.MaintainItem.Count(t => maintainItemIDs.Contains(t.ID) && t.IsValid && t.ProjectID == model.ProjectID) < maintainItemIDs.Count) { return(new CResult <bool>(false, ErrorCode.MaintainItemNotExist)); } } var oldRelations = context.DeviceTypeMaintainItemRel.Where(t => t.DeviceTypeID == entity.ID); var oldRelationItemIDs = oldRelations.Select(t => t.MaintainItemID); var newRelationItemIDs = model.MaintainItems.Select(t => t.ID).ToList(); var needDeletes = oldRelations.Where(t => newRelationItemIDs.Contains(t.MaintainItemID) == false).ToList(); var needAdd = model.MaintainItems.Where(t => oldRelationItemIDs.Contains(t.ID) == false).ToList(); foreach (var item in needDeletes) { context.DeviceTypeMaintainItemRel.Remove(item); } foreach (var item in needAdd) { var relation = new DeviceTypeMaintainItemRel() { DeviceTypeID = entity.ID, MaintainItemID = item.ID }; context.DeviceTypeMaintainItemRel.Add(relation); } context.Entry(entity).State = EntityState.Modified; return(context.Save()); } }