You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

190 lines
6.6 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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
{
/// <summary>
/// UserInfo.xaml 的交互逻辑
/// </summary>
public partial class UserInfo : UserControl
{
private AppConfigDto appConfig = AppConfigDto.Instance;
private static IBaseServices<SysUserInfo> _sysUserbaseServices = new BaseServices<SysUserInfo>();
/// <summary>
/// level
/// 3:ME
/// 2:PRD技师
/// 4:PE
/// 5:管理员
/// </summary>
public UserInfo()
{
InitializeComponent();
}
private async void Refresh()
{
Expression<Func<SysUserInfo, bool>> exp = s1 => true;
if (userText.Text.Trim().ToString() != "")
{
exp = exp.And(x => x.Name.Contains(userText.Text.Trim().ToString()));
}
Expression<Func<SysUserInfo, object>> order = (x) => x.objId;
List<SysUserInfo> 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<SysUserInfo> MesUserInfo)
{
try
{
Expression<Func<SysUserInfo, bool>> exp = s1 => true;
Expression<Func<SysUserInfo, object>> order = (x) => x.objId;
List<SysUserInfo> 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<SysUserInfo> CompareAndUpdate(List<SysUserInfo> sourceList, List<SysUserInfo> targetList)
{
// 1. 将targetList转为字典CardID为键高效查找
var targetDict = targetList.ToDictionary(d => d.JobID);
// 2. 存储需要从sourceList删除的对象target中没有的
var toRemove = new List<SysUserInfo>();
// 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;
}
}
}