2024-12-18 16:12:20 +08:00

138 lines
5.2 KiB
C#

using Mapster;
using Printer.Infrastructure.DOAN_Attribute;
using Printer.Model;
using Printer.Model.Dto;
using Seagull.BarTender.Print;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Printer
{
public partial class Form1 : Form
{
private SqlSugarClient Context;
private Engine BartenderEngine = null;
private LabelFormatDocument LabelFormatProduct = null;
public Form1()
{
InitializeComponent();
// 初始化数据库连接
Context = new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = "Data Source=127.0.0.1;User ID=root;Password=123456;Initial Catalog=bzfm_mes;Port=3306", // 替换为你的数据库连接字符串
DbType = SqlSugar.DbType.MySql, // 根据你的数据库类型进行更改
IsAutoCloseConnection = true,
InitKeyType = InitKeyType.Attribute // 从实体属性中读取主键和自增列信息
});
}
private void Form1_Load(object sender, EventArgs e)
{
//初始化打印机引擎
try
{
BartenderEngine = new Engine(true);
}
catch (PrintEngineException exception)
{
MessageBox.Show(this, exception.Message);
this.Close(); // 关闭程序
return;
}
}
/// <summary>
/// 查询按钮 查询指定日期工单
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Select_Click(object sender, EventArgs e)
{
DateTime startTime = this.dateTimePicker1.Value;
DateTime endTime = this.dateTimePicker2.Value;
string workorder = this.textBox1.Text;
string workshop = this.textBox2.Text;
string group = this.textBox3.Text;
string route = this.textBox4.Text;
List<ProWorkorderDto> data = Context.Queryable<ProWorkorder>()
.Where(it => it.WorkorderDate >= startTime && it.WorkorderDate <= endTime)
.WhereIF(!string.IsNullOrEmpty(workorder), it => it.Workorder == workorder)
.WhereIF(!string.IsNullOrEmpty(workshop), it => it.WorkshopCode == workshop)
.WhereIF(!string.IsNullOrEmpty(group), it => it.GroupCode == group)
.WhereIF(!string.IsNullOrEmpty(route), it => it.RouteCode == route)
.ToList().Adapt<List<ProWorkorderDto>>();
var dataTable = ToDataTable(data);
// 绑定到 DataGridView
dataGridView1.DataSource = dataTable;
}
private void btn_print_Click(object sender, EventArgs e)
{
LabelFormatProduct?.Close(SaveOptions.DoNotSaveChanges);
LabelFormatProduct = BartenderEngine.Documents.Open("D:\\mes\\Label\\Trace.btw");
LabelFormatProduct.PrintSetup.PrinterName = "";
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
try
{
LabelFormatProduct.SubStrings.SetSubString("workorder", dataGridView1.Rows[i].Cells[0].Value.ToString());
LabelFormatProduct.SubStrings.SetSubString("stoveCode", dataGridView1.Rows[i].Cells[2].Value.ToString());
LabelFormatProduct.SubStrings.SetSubString("qty", dataGridView1.Rows[i].Cells[16].Value.ToString());
LabelFormatProduct?.Print();
}
catch (Exception)
{
MessageBox.Show("打印异常");
}
}
}
private DataTable ToDataTable<T>(List<T> items)
{
DataTable dataTable = new DataTable(typeof(T).Name);
// 获取所有公共实例属性
PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo prop in Props)
{
// 查找 SugarColumn 特性
var DOANColumnAttr = prop.GetCustomAttribute<DOAN_Column>();
string columnName = DOANColumnAttr != null && !string.IsNullOrEmpty(DOANColumnAttr.ColumnName)
? DOANColumnAttr.ColumnName
: prop.Name;
// 设置 DataColumns 使用从特性中获取的列名
dataTable.Columns.Add(columnName, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
}
foreach (T item in items)
{
var values = new object[Props.Length];
for (int i = 0; i < Props.Length; i++)
{
// 获取属性值
values[i] = Props[i].GetValue(item, null);
}
// 添加一行数据
dataTable.Rows.Add(values);
}
return dataTable;
}
}
}