public Result DeleteSet([FromRoute] int id) { var cnt = ServerConfig.ApiDb.Query <int>("SELECT COUNT(1) FROM `warning_set` WHERE Id = @id AND `MarkedDelete` = 0;", new { id }).FirstOrDefault(); if (cnt == 0) { return(Result.GenError <Result>(Error.WarningSetNotExist)); } ServerConfig.ApiDb.Execute( "UPDATE `warning_set` SET `MarkedDateTime`= @MarkedDateTime, `MarkedDelete`= @MarkedDelete WHERE `Id`= @Id;", new { MarkedDateTime = DateTime.Now, MarkedDelete = true, Id = id }); ServerConfig.ApiDb.Execute( "UPDATE `warning_set_item` SET `MarkedDateTime`= @MarkedDateTime, `MarkedDelete`= @MarkedDelete WHERE `SetId`= @Id;", new { MarkedDateTime = DateTime.Now, MarkedDelete = true, Id = id }); HWarningHelper.UpdateConfig(); return(Result.GenError <Result>(Error.Success)); }
public Result PostSet([FromBody] WarningSetWithItems set) { if (set.Name.IsNullOrEmpty()) { return(Result.GenError <Result>(Error.WarningSetNotEmpty)); } if (set.DataType == WarningDataType.生产数据 && set.StepId == set.BindId) { return(Result.GenError <Result>(Error.WarningSetStepBindDuplicate)); } var names = new List <string> { set.Name }; if (WarningSetHelper.GetHaveSame(1, names)) { return(Result.GenError <Result>(Error.WarningSetIsExist)); } var createUserId = Request.GetIdentityInformation(); var markedDateTime = DateTime.Now; if (set.Items != null && set.Items.Any()) { //if (set.Items.Any(x => !x.ValidDataType())) //{ // return Result.GenError<Result>(Error.WarningSetItemDataTypeError); //} if (set.Items.Any(x => !x.ValidFrequency())) { return(Result.GenError <Result>(Error.WarningSetItemFrequencyError)); } if (set.Items.Any(x => !x.ValidCondition())) { return(Result.GenError <Result>(Error.WarningSetItemConditionError)); } } set.CreateUserId = createUserId; set.MarkedDateTime = markedDateTime; var id = WarningSetHelper.Instance.AddBackId(set); if (set.Items != null && set.Items.Any()) { var dic = new List <DataNameDictionary>(); if (set.DataType == WarningDataType.设备数据) { var dIds = set.Items.Select(x => x.DictionaryId).Where(x => x != 0); dic.AddRange(DataNameDictionaryHelper.Instance.GetAllByIds <DataNameDictionary>(dIds)); } foreach (var item in set.Items) { item.CreateUserId = createUserId; item.MarkedDateTime = markedDateTime; item.SetId = id; item.Item = set.DataType == WarningDataType.设备数据 ? dic.FirstOrDefault(x => x.Id == item.DictionaryId)?.VariableName ?? "" : item.Item; } WarningSetItemHelper.Instance.Add <WarningSetItem>(set.Items); } HWarningHelper.UpdateConfig(); return(Result.GenError <Result>(Error.Success)); }
public Result PutSet([FromBody] WarningSetWithItems set) { var oldSet = WarningSetHelper.Instance.Get <WarningSet>(set.Id); if (oldSet == null) { return(Result.GenError <Result>(Error.WarningSetNotExist)); } if (set.Name.IsNullOrEmpty()) { return(Result.GenError <Result>(Error.WarningSetNotEmpty)); } if (set.DataType == WarningDataType.生产数据 && set.StepId == set.BindId) { return(Result.GenError <Result>(Error.WarningSetStepBindDuplicate)); } var names = new List <string> { set.Name }; var ids = new List <int> { set.Id }; if (WarningSetHelper.GetHaveSame(1, names, ids)) { return(Result.GenError <Result>(Error.WarningSetIsExist)); } var createUserId = Request.GetIdentityInformation(); var markedDateTime = DateTime.Now; if (set.Items != null) { if (set.Items.Any()) { switch (set.DataType) { case WarningDataType.设备数据: set.Items = set.Items.Where(x => x.DictionaryId != 0).ToList(); foreach (var item in set.Items) { item.Item = ""; } break; case WarningDataType.生产数据: set.Items = set.Items.Where(x => x.ItemType != WarningItemType.Default).ToList(); foreach (var item in set.Items) { if (HWarningHelper.生产数据单设备加工信息字段.All(x => x.Item2 != item.ItemType)) { item.DeviceIds = set.DeviceIds; } } break; case WarningDataType.故障数据: set.Items = set.Items; break; case WarningDataType.流程卡数据: set.Items = set.Items.Where(x => x.ItemType != WarningItemType.Default).ToList(); break; default: set.Items.Clear(); break; } //if (set.Items.Any(x => !x.ValidDataType())) //{ // return Result.GenError<Result>(Error.WarningSetItemDataTypeError); //} foreach (var item in set.Items) { item.MarkedDateTime = markedDateTime; item.SetId = set.Id; } if (set.Items.Any(x => x.ItemType == WarningItemType.SpecifyStepTimeOut && x.StepId == 0)) { return(Result.GenError <Result>(Error.WarningSetItemFlowCardStepError)); } if (set.Items.Any(x => !x.ValidFrequency())) { return(Result.GenError <Result>(Error.WarningSetItemFrequencyError)); } if (set.Items.Any(x => !x.ValidCondition())) { return(Result.GenError <Result>(Error.WarningSetItemConditionError)); } var oldWarningSetItems = WarningSetItemHelper.GetWarningSetItemsBySetId(set.Id); var delItems = oldWarningSetItems.Where(x => set.Items.All(y => y.Id != x.Id)); if (delItems.Any()) { WarningSetItemHelper.Instance.Delete(delItems.Select(x => x.Id)); } var updateItems = set.Items.Where(x => x.Id != 0); if (updateItems.Any()) { var uIds = new List <int>(); foreach (var item in updateItems) { var oldItem = oldWarningSetItems.FirstOrDefault(x => x.Id == item.Id); if (oldItem != null && ClassExtension.HaveChange(oldItem, item)) { if (HWarningHelper.生产数据单设备加工信息字段.All(x => x.Item2 != item.ItemType)) { item.DeviceIds = set.DeviceIds; } uIds.Add(item.Id); } } WarningSetItemHelper.Instance.Update(updateItems.Where(x => uIds.Contains(x.Id))); } var newItems = set.Items.Where(x => x.Id == 0); if (newItems.Any()) { foreach (var item in newItems) { item.CreateUserId = createUserId; } WarningSetItemHelper.Instance.Add(newItems); } } else { WarningSetItemHelper.Instance.DeleteFromParent(set.Id); } } if (ClassExtension.HaveChange(oldSet, set)) { set.MarkedDateTime = markedDateTime; WarningSetHelper.Instance.Update(set); } HWarningHelper.UpdateConfig(); return(Result.GenError <Result>(Error.Success)); }