using MaterialTraceability.Entity.DAO;
using MaterialTraceability.Entity.DTO;
using MaterialTraceability.SqlSugar;
using MaterialTraceability.SqlSugar.ServiceImpl;
using MaterialTraceabilityUI.Common;
using MaterialTraceabilityUI.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace MaterialTraceabilityUI
{
///
/// UserInfo.xaml 的交互逻辑
///
public partial class UserInfo : UserControl
{
private AppConfigDto appConfig = AppConfigDto.Instance;
private static IBaseServices _sysUserbaseServices = new BaseServices();
///
/// level
/// 3:ME
/// 2:PRD技师
/// 4:PE
/// 5:管理员
///
public UserInfo()
{
InitializeComponent();
}
private async void Refresh()
{
Expression> exp = s1 => true;
if (userText.Text.Trim().ToString() != "")
{
exp = exp.And(x => x.Name.Contains(userText.Text.Trim().ToString()));
}
Expression> order = (x) => x.objId;
List UserInfoRecord = await _sysUserbaseServices.Query(exp, order, false);
if (UserInfoRecord != null)
{
UserInfoRecord.ForEach(x =>
{
if (x.Level == "1")
{
x.Level = "未知";
}
else if (x.Level == "2")
{
x.Level = "PRD技师";
}else if (x.Level == "3")
{
x.Level = "ME";
}
else if (x.Level == "4")
{
x.Level = "PE";
}
else if (x.Level == "5")
{
x.Level = "管理员";
}
});
UserInfoDataGrid.ItemsSource = UserInfoRecord;
}
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
Refresh();
}
private void Seach_Click(object sender, RoutedEventArgs e)
{
Refresh();
}
private void Loaddata_Click(object sender, RoutedEventArgs e)
{
try
{
MesHttpClient mesHttpClient = new MesHttpClient();
mesHttpClient.init();
var data = mesHttpClient.LoadMESdata(appConfig.resource);
//更新本地库
CompareData(data.Data);
Refresh();
}
catch (Exception ex)
{
}
}
private async void CompareData(List MesUserInfo)
{
try
{
Expression> exp = s1 => true;
Expression> order = (x) => x.objId;
List LocalUserInfoRecord = await _sysUserbaseServices.Query(exp, order, false);
if (LocalUserInfoRecord != null)
{
var newdata = CompareAndUpdate(LocalUserInfoRecord, MesUserInfo);
//先清空表
await _sysUserbaseServices.TruncateTable();
int ifalg = await _sysUserbaseServices.Add(newdata);
}
}
catch (Exception ex)
{
}
}
// 比较两个Data对象(同一CardID)的其他属性是否相同
private static bool ArePropertiesEqual(SysUserInfo source, SysUserInfo target)
{
if (source == null || target == null) return false;
return source.CardID == target.CardID
&& source.Name == target.Name
&& source.Level == target.Level
&& source.PWD == target.PWD;
}
// 比较两个列表并更新sourceList
public List CompareAndUpdate(List sourceList, List targetList)
{
// 1. 将targetList转为字典(CardID为键),高效查找
var targetDict = targetList.ToDictionary(d => d.JobID);
// 2. 存储需要从sourceList删除的对象(target中没有的)
var toRemove = new List();
// 3. 遍历sourceList,匹配target并更新差异
foreach (var source in sourceList)
{
if (targetDict.TryGetValue(source.JobID, out var target))
{
// 找到匹配的CardID,检查属性是否不同
if (!ArePropertiesEqual(source, target))
{
// 属性不同,用target的数据更新source
source.CardID = target.CardID;
source.Name = target.Name;
source.Level = target.Level;
source.PWD = target.PWD;
Console.WriteLine($"已更新JobID={source.JobID}的对象");
}
// 从字典移除已处理的,剩余的就是target独有的(待新增)
targetDict.Remove(source.JobID);
}
else
{
// source有而target没有,标记为待删除
toRemove.Add(source);
}
}
// 4. 执行删除(从sourceList移除toRemove中的对象)
foreach (var item in toRemove)
{
sourceList.Remove(item);
Console.WriteLine($"已删除JobID={item.JobID}的对象(target中不存在)");
}
// 5. 处理target独有的对象(新增到sourceList)
foreach (var newItem in targetDict.Values)
{
sourceList.Add(newItem);
Console.WriteLine($"已新增JobID={newItem.JobID}的对象(source中不存在)");
}
return sourceList;
}
}
}