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.

339 lines
12 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 Admin.Core.Common;
using Admin.Core.IService;
using Admin.Core.Model;
using Admin.Core.Service;
using Aucma.Core.PrintTo.Common;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using FastReport;
using log4net;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
/**
* 补码打印
* */
namespace Aucma.Core.PrintTo.ViewModels
{
public partial class SupplementViewModel : ObservableObject
{
private readonly IPrintSuppleMentBarCodeServices _printSuppleMentBarCodeServices;
protected readonly IPrintBarCodeServices _printBarCodeServices;
private static readonly log4net.ILog log = LogManager.GetLogger(typeof(SupplementViewModel));
private PrintState printState { get; set; } = PrintState.Idle;//打印状态
PrintSuppleMentBarCode printSuppleMentBarCode = null;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="Id">补打表主键</param>
/// <param name="barType">补打类型</param>
public SupplementViewModel(int Id,string barType)
{
_printSuppleMentBarCodeServices = App.ServiceProvider.GetService<IPrintSuppleMentBarCodeServices>();
_printBarCodeServices = App.ServiceProvider.GetService<IPrintBarCodeServices>();
printSuppleMentBarCode = _printSuppleMentBarCodeServices.FirstAsync(d => d.ObjId == Id).Result;
if (printSuppleMentBarCode!=null)
{
_orderCode = printSuppleMentBarCode.OrderCode;
ProductCode = printSuppleMentBarCode.MaterialCode;
ProductName = printSuppleMentBarCode.MaterialName;
_barCode = printSuppleMentBarCode.MaterialBarCode;
PrintAmount = 1;
PrintName = printSuppleMentBarCode.PrintName;
PrintBarType = printSuppleMentBarCode.PrintBarType;
}
BarType = barType;
Progress = 0;
MaxProgress = 1;//最大数
State = "Green";
ErrorNum = 0;//异常
SuspendEnabled = "False";//暂停打印
OperateEnabled = "True";//打印
}
#region 打印
[RelayCommand]
public async Task StartOperate()
{
try
{
string printer = Appsettings.app("Printer", "PrinterName");
Progress = 1;
if (string.IsNullOrEmpty(PrintBarType)) return;
string flag = _barCode.Substring(0, 1);
if (BarType == "newBarCode")//替换原有的MES码
{
string newBarCode = await GetMaxNumAsync(ProductCode, flag);
PintBarCode(printer, newBarCode);
printSuppleMentBarCode.MaterialBarCode = _barCode;
printSuppleMentBarCode.PrintNewBarCode = newBarCode;
printSuppleMentBarCode.PrintTime = DateTime.Now;
await _printSuppleMentBarCodeServices.UpdateAsync(printSuppleMentBarCode);
//var obj=await _printBarCodeServices.FirstAsync(d=>d.MaterialBarcode== _barCode);
//obj.MaterialBarcode = newBarCode;
//await _printBarCodeServices.UpdateAsync(obj);
}
else
{
PintBarCode(printer, _barCode);
printSuppleMentBarCode.PrintTime = DateTime.Now;
//打印完成 不可以再次打印
printSuppleMentBarCode.IsPrint = "1";
await _printSuppleMentBarCodeServices.UpdateAsync(printSuppleMentBarCode);
}
//打印完成
SetCompletetd();
}
catch (Exception ex)
{
log.Error($"打印出错:{ex.Message}");
Console.WriteLine($"打印出错:{ex.Message}");
}
}
private void PintBarCode(string printer,string barCode)
{
#region 打印
//对接打印机
Report barReport = new Report();
barReport.Load(System.Environment.CurrentDirectory + @"\Report\MaterialBar.frx");//打印报表位置
// 设置打印机和打印选项
barReport.PrintSettings.ShowDialog = false; // 是否打开打印机选择框
barReport.PrintSettings.Printer = printer; // 设置打印机名称
barReport.PrintSettings.Copies = 1; // 设置打印份数
//barReport.PrintSettings.PageRange = "1-3"; // 设置打印页范围
barReport.SetParameterValue("BoxBarData.Id", barCode);
barReport.SetParameterValue("BoxBarData.Order_No", _orderCode);//订单号
barReport.SetParameterValue("BoxBarData.Order_Material_Name", PrintName);//物料简码
barReport.SetParameterValue("BoxBarData.Bar_Code", barCode);//二维码
PrintProgress = $"[1/1] {barCode}";
barReport.Print();
#endregion
}
#endregion
#region 选中订单参数
#region 订单编码
private string _orderCode;
public string OrderCode { get => _orderCode; set => SetProperty(ref _orderCode, value); }
#endregion
#region 订单编码
private string _orderNo;
public string OrderNo { get => _orderNo; set => SetProperty(ref _orderNo, value); }
#endregion
#region 打印名称
private string _printName;
public string PrintName
{
get => _printName;
set => SetProperty(ref _printName, value);
}
#endregion
#region 产品编码
private string _productCode;
public string ProductCode { get => _productCode; set => SetProperty(ref _productCode, value); }
#endregion
#region 产品名称
private string _productName;
public string ProductName { get => _productName; set => SetProperty(ref _productName, value); }
#endregion
#region 物料编码
private string _barCode;
public string BarCode { get => _barCode; set => SetProperty(ref _barCode, value); }
#endregion
#region 型号简码
private string _materialName;
public string MaterialName { get => _materialName; set => SetProperty(ref _materialName, value); }
#endregion
#region 物料编码
private string _materialCode;
public string MaterialCode { get => _materialCode; set => SetProperty(ref _materialCode, value); }
#endregion
#region 打印数量
private int _printAmount;
public int PrintAmount { get => _printAmount; set => SetProperty(ref _printAmount, value); }
#endregion
#region 异常数量
private int _errorNum;
public int ErrorNum { get => _errorNum; set => SetProperty(ref _errorNum, value); }
#endregion
#region 打印进度
private string _printProgress;
public string PrintProgress { get => _printProgress; set => SetProperty(ref _printProgress, value); }
#endregion
#region 进度条最大值
private int _maxProgress;
public int MaxProgress { get => _maxProgress; set => SetProperty(ref _maxProgress, value); }
#endregion
#region 进度条进度
private int _progress;
public int Progress { get => _progress; set => SetProperty(ref _progress, value); }
#endregion
#region 补打类型
private string _barType;
public string BarType { get => _barType; set => SetProperty(ref _barType, value); }
#endregion
#region 打印状态
private string _state;
public string State { get => _state; set => SetProperty(ref _state, value); }
#endregion
#region 开启、暂停状态按钮 名称
private string _stopOrStart;
public string StopOrStart { get => _stopOrStart; set => SetProperty(ref _stopOrStart, value); }
#endregion
#region 操作打印按钮状态
private string _operateEnabled;
public string OperateEnabled
{
get => _operateEnabled;
set => SetProperty(ref _operateEnabled, value);
}
#endregion
#region 操作暂停按钮状态
private string _suspendEnabled;
public string SuspendEnabled
{
get => _suspendEnabled;
set => SetProperty(ref _suspendEnabled, value);
}
#endregion
#region 条码类型
private string _printBarType;
public string PrintBarType { get => _printBarType; set => SetProperty(ref _printBarType, value); }
#endregion
#endregion
#region 完成
/// <summary>
/// 暂停
/// </summary>
private void SetCompletetd()
{
printState = PrintState.Completetd;
State = "Green";
StopOrStart = "已完成";
OperateEnabled = "True";
}
#endregion
#region 关闭当前界面
/// <summary>
/// 关闭当前界面
/// </summary>
/// <param name="parameter"></param>
[RelayCommand]
public void CloseWindow(object parameter)
{
var window = parameter as Window;
if (window == null) return;
if (MessageBox.Show("确定要退出打印吗?", "系统提醒", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.Yes)
{
printState = PrintState.Stopped;
WeakReferenceMessenger.Default.Send<string>("RefreshSupplement");//刷新窗口
window.Close();
}
}
#endregion
#region 获取最大值
/// <summary>
/// 获取最大值
/// </summary>
/// <param name="MaterialCode"></param>
/// <param name="falg">条码类型:内胆码或者 箱壳码</param>
/// <returns></returns>
public async Task<string> GetMaxNumAsync(string materialCode, string falg)
{
string barCode = $"{falg}{DateTime.Now.ToString("yyMMdd")}{materialCode}";
try
{
List<int> tempList = new List<int>();
var list = await _printBarCodeServices.QueryAsync(d => d.MaterialBarcode.Contains(barCode));
if (list.Count() == 0)
{
return barCode + "0001";
}
foreach (var item in list)
{
string code = item.MaterialBarcode.Substring(item.MaterialBarcode.Length - 4);
int num = Convert.ToInt32(code);
tempList.Add(num);
}
int row = tempList.Max() + 1;
if (row == 10000)
{
MessageBox.Show("订单打印数据已经超过10000无法查询打印条码数据。", "系统提醒");
return null;
}
string maxStr = (tempList.Max() + 1).ToString();
string rowNum = maxStr.PadLeft(4, '0');
return barCode + rowNum;
}
catch
{
return barCode + "0001";
}
}
#endregion
}
}