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.

243 lines
8.7 KiB
C#

using CommunityToolkit.Mvvm.ComponentModel;
using System;
using System.Collections.ObjectModel;
using log4net;
using Aucma.Core.CodeBinding.Common;
using LiveCharts;
using Admin.Core.Model;
using Admin.Core.Service;
using LiveCharts.Wpf;
using Admin.Core.IService;
using Microsoft.Extensions.DependencyInjection;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Collections;
using Aucma.Core.CodeBinding.Models;
using System.Reflection.Emit;
using NPOI.SS.Formula.Functions;
using System.Threading;
using System.Windows.Threading;
using static Admin.Core.Service.CodeBindingRecordServices;
using System.Windows.Data;
using System.Windows;
using System.Linq;
using Aucma.Core.Scanner;
using System.Collections.Generic;
using NPOI.HSSF.Record;
/*
* 首页信息
*/
namespace Aucma.Core.CodeBinding.ViewModels
{
public partial class IndexPageViewModel : ObservableObject
{
private static readonly log4net.ILog log = LogManager.GetLogger(typeof(IndexPageViewModel));
private readonly ICodeBindingRecordServices? _codeBindingRecordServices;
public IndexPageViewModel()
{
_codeBindingRecordServices = App.ServiceProvider.GetService<ICodeBindingRecordServices>();
MvCodeHelper.ReceiveCode1Event += receiveCode1;
MvCodeHelper.ReceiveCode2Event += receiveCode2;
// var list = _codeBindingRecordServices.FirstAsync().Result;
LoadData();
// 程序运行 5 秒后添加新数据项
add();
}
#region 加载DataGrid数据
private void LoadData()
{
ListItems.Add(new ReaderInfo() { No = 1, BoxCode ="42423444", ProductCode = "8659452123", BindingResult = "成功", RecordTime = System.DateTime.Now.ToString()});
}
private async void add()
{
await Task.Delay(5000);
ListItems.Add(new ReaderInfo() { No = 2, BoxCode = "42423444", ProductCode = "8659452123", BindingResult = "成功", RecordTime = System.DateTime.Now.ToString() });
ListItems.Add(new ReaderInfo() { No = 3, BoxCode = "42423444", ProductCode = "8659452123", BindingResult = "成功", RecordTime = System.DateTime.Now.ToString() });
ListItems.Add(new ReaderInfo() { No = 4, BoxCode = "42423444", ProductCode = "8659452123", BindingResult = "成功", RecordTime = System.DateTime.Now.ToString() });
ListItems.Add(new ReaderInfo() { No = 5, BoxCode = "42423444", ProductCode = "8659452123", BindingResult = "成功", RecordTime = System.DateTime.Now.ToString() });
ListItems.Add(new ReaderInfo() { No = 6, BoxCode = "42423444", ProductCode = "8659452123", BindingResult = "成功", RecordTime = System.DateTime.Now.ToString() });
ListItems.Add(new ReaderInfo() { No = 7, BoxCode = "42423444", ProductCode = "8659452123", BindingResult = "成功", RecordTime = System.DateTime.Now.ToString() });
ListItems.Add(new ReaderInfo() { No = 8, BoxCode = "42423444", ProductCode = "8659452123", BindingResult = "成功", RecordTime = System.DateTime.Now.ToString() }); ListItems.Add(new ReaderInfo() { No = 1, BoxCode = "42423444", ProductCode = "8659452123", BindingResult = "成功", RecordTime = System.DateTime.Now.ToString() });
ListItems.Add(new ReaderInfo() { No = 9, BoxCode = "42423444", ProductCode = "8659452123", BindingResult = "成功", RecordTime = System.DateTime.Now.ToString() });
ListItems.Add(new ReaderInfo() { No = 10, BoxCode = "42423444", ProductCode = "8659452123", BindingResult = "成功", RecordTime = System.DateTime.Now.ToString() });
}
#endregion
#region 参数定义
/// <summary>
/// 条码1
/// </summary>
private string code1 = string.Empty;
public string Code1
{
get { return code1; }
set
{
code1 = value;
OnPropertyChanged(nameof(Code1));
}
}
/// <summary>
/// 条码1扫描时间
/// </summary>
private string code1Time = string.Empty;
public string Code1Time
{
get { return code1Time; }
set
{
code1Time = value;
OnPropertyChanged(nameof(Code1Time));
}
}
/// <summary>
/// 条码2
/// </summary>
private string code2= string.Empty;
public string Code2
{
get { return code2; }
set
{
code2 = value;
OnPropertyChanged(nameof(Code2));
}
}
/// <summary>
/// 条码2扫描时间
/// </summary>
private string code2Time = string.Empty;
public string Code2Time
{
get { return code2Time; }
set
{
code2Time = value;
OnPropertyChanged(nameof(Code2Time));
}
}
/// <summary>
/// 绑定提示信息
/// </summary>
private string bindingInfo = string.Empty;
public string BindingInfo
{
get { return bindingInfo; }
set
{
bindingInfo = value;
OnPropertyChanged(nameof(BindingInfo));
}
}
#endregion
#region 初始化datagrid
private ObservableCollection<ReaderInfo> listItems = new ObservableCollection<ReaderInfo>() { };
public ObservableCollection<ReaderInfo> ListItems
{
get { return listItems; }
set
{
listItems = value;
OnPropertyChanged();//属性通知
}
}
#endregion
/// <summary>
/// code1扫码信息刷新
/// </summary>
/// <param name="materialType"></param>
private void RefreshCode1(string code1,string time)
{
Code1 = code1;
Code1Time = time;
}
/// <summary>
/// code2扫码信息及表格记录刷新
/// </summary>
/// <param name="materialType"></param>
private void RefreshCode2(CodeBindingRecord record)
{
Code2 = record.ProductCode;
Code2Time = record.RecordTime2.ToString();
if ("成功".Equals(record.BindingResult))
{
BindingInfo = "条码【" + record.BoxCode + "】与SN码【" + record.ProductCode + "】绑定成功";
}
else
{
BindingInfo = "条码【" + record.BoxCode + "】与SN码【" + record.ProductCode + "】绑定失败";
}
Application.Current.Dispatcher.Invoke(() =>
{
ListItems.Add(new ReaderInfo() { No = record.ObjId, BoxCode = record.BoxCode, ProductCode = record.ProductCode, BindingResult = record.BindingResult, RecordTime = record.RecordTime2.ToString() });
});
}
/// <summary>
/// 条码绑定,条码1处理
/// </summary>
/// <param name="materialType"></param>
private async void receiveCode1(string code1)
{
log.Info("进入条码1处理函数receiveCode1,code1:" + code1);
DateTime time = System.DateTime.Now;
string strTime = time.ToString();
// 1.刷新界面条码信息
RefreshCode1(code1, strTime);
// 2.创建任务更新数据库条码1
CodeBindingRecord codeRecord = new CodeBindingRecord();
codeRecord.BoxCode = code1;
codeRecord.BoxName = "去别的数据库查询";
codeRecord.RecordTime1 = time;
Thread.Sleep(1000);
int a = await _codeBindingRecordServices.AddAsync(codeRecord);
//log.Info("条码1记录更新");
}
/// <summary>
/// 条码绑定,条码2处理
/// </summary>
/// <param name="materialType"></param>
private async void receiveCode2(string code2)
{
log.Info("进入条码2处理函数receiveCode1,code2:" + code2);
// 1.数据库匹配条码1并绑定
// 查询条码2为null的最新记录
List<CodeBindingRecord> list = await _codeBindingRecordServices.QueryByTime();
CodeBindingRecord record = list.FirstOrDefault();
record.ProductCode = code2;
record.RecordTime2 = System.DateTime.Now;
record.BindingResult = "成功";
await _codeBindingRecordServices.UpdateAsync(record);
// 2.刷新界面信息
RefreshCode2(record);
// 产品入库
// 3.下发plc放行信号
}
}
}