Revisione metodo cancellazione in cascata x update Proj
This commit is contained in:
@@ -383,49 +383,65 @@ namespace EgtBEAMWALL.DataLayer.Controllers
|
||||
/// <returns></returns>
|
||||
public ProjModel UpdateBtlParts(int ProjId, List<Core.BTLPartM> BtlPartList)
|
||||
{
|
||||
// record del proj corrente
|
||||
var currData = FindByProjId(ProjId);
|
||||
|
||||
// 2021.05.03 modifica update: esistenti le MODIFICO
|
||||
// 2021.05.03 modifica update: esistenti le MODIFICO, nuove aggiungo, inesistenti elimino in cascata con Part
|
||||
|
||||
// sel delle BTLParts da proj
|
||||
// sel delle BTLParts del proj
|
||||
List<BTLPartModel> oldBtlParts = dbCtx
|
||||
.BTLPartList
|
||||
.Where(x => x.ProjDbId == currData.ProjDbId)
|
||||
.ToList();
|
||||
|
||||
// converto le BtlParts
|
||||
// converto le BtlParts da core --> DB
|
||||
List<BTLPartModel> newBtlParts = BtlPartList.Select(x => BTLPartController.ConvertFromCore(x, currData.ProjDbId)).ToList();
|
||||
|
||||
try
|
||||
{
|
||||
// elementi NON + presenti da eliminare
|
||||
var old2rem = oldBtlParts.Except(newBtlParts).ToList();
|
||||
var new2Add = newBtlParts.Except(oldBtlParts).ToList();
|
||||
var existing = oldBtlParts.Except(old2rem).ToList();
|
||||
var newOnes = new2Add.Except(new2Add).ToList();
|
||||
// elementi BtlPartId NON + presenti da eliminare
|
||||
List<int> bpi2rem = oldBtlParts.Select(x => x.PartId).Except(newBtlParts.Select(y => y.PartId)).ToList();
|
||||
List<int> bpi2add = newBtlParts.Select(x => x.PartId).Except(oldBtlParts.Select(y => y.PartId)).ToList();
|
||||
List<int> bpiExis = newBtlParts.Select(x => x.PartId).Intersect(oldBtlParts.Select(y => y.PartId)).ToList();
|
||||
|
||||
// aggiorno existing...
|
||||
foreach (var item in existing)
|
||||
foreach (var currPartId in bpiExis)
|
||||
{
|
||||
// recupero nuovo item...
|
||||
var newItem = newOnes.Where(x => x.PartId == item.PartId).FirstOrDefault();
|
||||
if (newItem != null)
|
||||
// recupero item da aggiornare...
|
||||
var oldItem = oldBtlParts.Where(x => x.PartId == currPartId).FirstOrDefault();
|
||||
// dati nuovo item
|
||||
var newItem = newBtlParts.Where(x => x.PartId == currPartId).FirstOrDefault();
|
||||
if (newItem != null && oldItem != null)
|
||||
{
|
||||
item.CALC_State = newItem.CALC_State;
|
||||
item.CNT = newItem.CNT;
|
||||
item.DO = newItem.DO;
|
||||
item.H = newItem.H;
|
||||
item.L = newItem.L;
|
||||
item.W = newItem.W;
|
||||
item.Material = newItem.Material;
|
||||
item.NAM = newItem.NAM;
|
||||
item.PDN = newItem.PDN;
|
||||
oldItem.CALC_State = newItem.CALC_State;
|
||||
oldItem.CNT = newItem.CNT;
|
||||
oldItem.DO = newItem.DO;
|
||||
oldItem.H = newItem.H;
|
||||
oldItem.L = newItem.L;
|
||||
oldItem.W = newItem.W;
|
||||
oldItem.Material = newItem.Material;
|
||||
oldItem.NAM = newItem.NAM;
|
||||
oldItem.PDN = newItem.PDN;
|
||||
}
|
||||
}
|
||||
|
||||
// aggiungo le nuove part
|
||||
foreach (var newPartId in bpi2add)
|
||||
{
|
||||
var newItem = newBtlParts.Where(x => x.PartId == newPartId).FirstOrDefault();
|
||||
dbCtx.BTLPartList.Add(newItem);
|
||||
}
|
||||
|
||||
// elimino dal DB i non + esistenti
|
||||
dbCtx.BTLPartList.RemoveRange(old2rem);
|
||||
// aggiungo le nuove
|
||||
dbCtx.BTLPartList.AddRange(new2Add);
|
||||
foreach (var oldPartId in bpi2rem)
|
||||
{
|
||||
// elimino parts nei MachGroup
|
||||
var oldIstPartList = dbCtx.PartList.Where(x => x.BTLPart.PartId == oldPartId).ToList();
|
||||
dbCtx.PartList.RemoveRange(oldIstPartList);
|
||||
// elimino BtlParts
|
||||
var oldItem = oldBtlParts.Where(x => x.PartId == oldPartId).FirstOrDefault();
|
||||
dbCtx.BTLPartList.Remove(oldItem);
|
||||
}
|
||||
}
|
||||
catch
|
||||
{ }
|
||||
|
||||
@@ -53,6 +53,9 @@
|
||||
<Reference Include="MySql.Data.Entity.EF6, Version=6.10.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\MySql.Data.Entity.6.10.9\lib\net452\MySql.Data.Entity.EF6.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Newtonsoft.Json.13.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.ComponentModel" />
|
||||
<Reference Include="System.ComponentModel.DataAnnotations" />
|
||||
@@ -108,6 +111,7 @@
|
||||
</Compile>
|
||||
<Compile Include="Migrations\Configuration.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Utils.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="App.config" />
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace EgtBEAMWALL.DataLayer
|
||||
{
|
||||
public static partial class LinqExtensions
|
||||
{
|
||||
#region Public Methods
|
||||
|
||||
public static IEnumerable<T> ExceptUsingJSonCompare<T>
|
||||
(this IEnumerable<T> first, IEnumerable<T> second)
|
||||
{
|
||||
return first.Except(second, new JSonEqualityComparer<T>());
|
||||
}
|
||||
|
||||
#endregion Public Methods
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Comparer generico basato su Json
|
||||
/// https://stackoverflow.com/questions/7042090/linq-except-with-custom-iequalitycomparer/7098076#7098076
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public class JSonEqualityComparer<T> : IEqualityComparer<T>
|
||||
{
|
||||
#region Public Methods
|
||||
|
||||
public bool Equals(T x, T y)
|
||||
{
|
||||
return String.Equals
|
||||
(
|
||||
Newtonsoft.Json.JsonConvert.SerializeObject(x),
|
||||
Newtonsoft.Json.JsonConvert.SerializeObject(y)
|
||||
);
|
||||
}
|
||||
|
||||
public int GetHashCode(T obj)
|
||||
{
|
||||
return Newtonsoft.Json.JsonConvert.SerializeObject(obj).GetHashCode();
|
||||
}
|
||||
|
||||
#endregion Public Methods
|
||||
}
|
||||
}
|
||||
@@ -3,4 +3,5 @@
|
||||
<package id="EntityFramework" version="6.4.4" targetFramework="net452" />
|
||||
<package id="MySql.Data" version="6.10.9" targetFramework="net452" />
|
||||
<package id="MySql.Data.Entity" version="6.10.9" targetFramework="net452" />
|
||||
<package id="Newtonsoft.Json" version="13.0.1" targetFramework="net452" />
|
||||
</packages>
|
||||
Reference in New Issue
Block a user