Commit c1d74936 by lcx

bug修改 - 二次绩效数据保存,删除重复数据

parent be771d58
...@@ -49,8 +49,8 @@ public ApiResponse List() ...@@ -49,8 +49,8 @@ public ApiResponse List()
[HttpPost] [HttpPost]
public ApiResponse SaveValue(int secondid, [FromBody] List<ag_fixatitem> request) public ApiResponse SaveValue(int secondid, [FromBody] List<ag_fixatitem> request)
{ {
if (secondid != 0 && (request == null || request.Count == 0)) //if (secondid != 0 && (request == null || request.Count == 0))
secondAllotService.DelValue(secondid, request); // secondAllotService.DelValue(secondid, request);
var allotCount = request.Where(t => t.AllotId > 0).Select(t => t.AllotId).Distinct().Count(); var allotCount = request.Where(t => t.AllotId > 0).Select(t => t.AllotId).Distinct().Count();
if (allotCount != 1 || request.Any(t => t.AllotId == 0)) if (allotCount != 1 || request.Any(t => t.AllotId == 0))
......
...@@ -538,51 +538,81 @@ public bool SaveValue(List<ag_fixatitem> request) ...@@ -538,51 +538,81 @@ public bool SaveValue(List<ag_fixatitem> request)
var second = perforAgsecondallotRepository.GetEntity(t => t.Id == secondId); var second = perforAgsecondallotRepository.GetEntity(t => t.Id == secondId);
if (second == null) if (second == null)
throw new PerformanceException("二次绩效ID不存在"); throw new PerformanceException("二次绩效ID不存在");
var fixatitems = perforAgfixatitemRepository.GetEntities(t => t.SecondId == secondId); var fixatitems = perforAgfixatitemRepository.GetEntities(t => t.SecondId == secondId);
var result = DelValue(fixatitems, request);
List<ag_fixatitem> update = new List<ag_fixatitem>(), insert = new List<ag_fixatitem>();
if (fixatitems != null && fixatitems.Any())
{
foreach (var item in request) foreach (var item in request)
{ {
if (fixatitems != null && fixatitems.Any(t => t.SecondId == secondId && t.RowNumber == item.RowNumber && t.ItemName == item.ItemName)) if (fixatitems.Any(t => t.RowNumber == item.RowNumber && t.ItemName == item.ItemName))
{ {
var cellItem = fixatitems.First(t => t.SecondId == secondId && t.RowNumber == item.RowNumber && t.ItemName == item.ItemName); var cellItem = fixatitems.FirstOrDefault(t => t.RowNumber == item.RowNumber && t.ItemName == item.ItemName);
if (cellItem == null)
continue;
cellItem.ItemValue = item.ItemValue; cellItem.ItemValue = item.ItemValue;
cellItem.Sort = item.Sort; cellItem.Sort = item.Sort;
cellItem.SourceType = item.SourceType; cellItem.SourceType = item.SourceType;
cellItem.Type = item.Type; cellItem.Type = item.Type;
cellItem.UnitType = item.UnitType; cellItem.UnitType = item.UnitType;
cellItem.SpecialAttr = item.SpecialAttr; cellItem.SpecialAttr = item.SpecialAttr;
perforAgfixatitemRepository.Update(cellItem); update.Add(cellItem);
} }
else else
{ {
perforAgfixatitemRepository.Add(item); insert.Add(item);
} }
} }
return DelValue(second.Id, request); }
else
insert = request;
result = perforAgfixatitemRepository.UpdateRange(update.ToArray());
result = perforAgfixatitemRepository.AddRange(insert.ToArray());
return result;
} }
/// <summary> /// <summary>
/// 删除未保存的历史数据 /// 删除未保存的历史数据
/// </summary> /// </summary>
/// <param name="secondId"></param> /// <param name="fixatlist">数据库中查询到的值</param>
/// <param name="fixatitems"></param> /// <param name="fixatitems">需要保存的数据</param>
/// <returns></returns> /// <returns></returns>
public bool DelValue(int secondId, List<ag_fixatitem> fixatitems) public bool DelValue(List<ag_fixatitem> fixatlist, List<ag_fixatitem> fixatitems)
{ {
if (secondId == 0) var result = true;
throw new PerformanceException("二次绩效Id无效"); // 提交数据为空时,删除所有数据
var fixatlist = perforAgfixatitemRepository.GetEntities(t => t.SecondId == secondId);
if (fixatitems == null || fixatitems.Count == 0) if (fixatitems == null || fixatitems.Count == 0)
{ {
return perforAgfixatitemRepository.RemoveRange(fixatlist.ToArray()); if (fixatlist != null && fixatlist.Any())
result = perforAgfixatitemRepository.RemoveRange(fixatlist.ToArray());
return result;
} }
if (fixatlist != null && fixatlist.Any())
{
var groupData = fixatlist.GroupBy(t => new { t.RowNumber, t.ItemName }).Select(t => new
{
t.Key.RowNumber,
t.Key.ItemName,
Count = t.Count(),
Id = t.Max(group => group.ID)
});
// 删除重复数据
if (groupData.Any(t => t.Count > 1))
{
var delData = fixatlist.Where(t => !groupData.Select(w => w.Id).Contains(t.ID));
int flag = fixatlist.RemoveAll(t => !groupData.Select(w => w.Id).Contains(t.ID));
result = perforAgfixatitemRepository.RemoveRange(delData.ToArray());
}
}
// 删除行号不存在的数据
var saveRows = fixatitems.Select(t => t.RowNumber).Distinct(); var saveRows = fixatitems.Select(t => t.RowNumber).Distinct();
var delRows = fixatlist.Select(t => t.RowNumber).Distinct().Except(saveRows); var delRows = fixatlist.Select(t => t.RowNumber).Distinct().Except(saveRows);
if (delRows != null && delRows.Count() > 0) if (delRows != null && delRows.Count() > 0)
return perforAgfixatitemRepository.RemoveRange(fixatlist.Where(t => delRows.Contains(t.RowNumber)).ToArray()); result = perforAgfixatitemRepository.RemoveRange(fixatlist.Where(t => delRows.Contains(t.RowNumber)).ToArray());
return true; return result;
} }
/// <summary> /// <summary>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment