110 lines
3.8 KiB
C#
110 lines
3.8 KiB
C#
using DOAN.Model.MES.product;
|
|
using DOAN.Model.MES.product.Dto;
|
|
using Mapster;
|
|
using Microsoft.Extensions.Configuration;
|
|
using printsoftware.infrastructure.DAON_Attribute;
|
|
using SqlSugar;
|
|
using System.Configuration;
|
|
using System.Data;
|
|
using System.Reflection;
|
|
|
|
namespace printsoftware
|
|
{
|
|
public partial class Form1 : Form
|
|
{
|
|
private SqlSugarClient Context;
|
|
public Form1()
|
|
{
|
|
InitializeComponent();
|
|
// 构建配置
|
|
var builder = new ConfigurationBuilder()
|
|
.SetBasePath(Directory.GetCurrentDirectory())
|
|
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
|
|
|
|
var _configuration = builder.Build();
|
|
|
|
|
|
// 初始化数据库连接
|
|
Context = new SqlSugarClient(new ConnectionConfig()
|
|
{
|
|
ConnectionString = _configuration.GetSection("ConnectionStrings:Conn").Value, // 替换为你的数据库连接字符串
|
|
DbType = (SqlSugar.DbType)int.Parse(_configuration.GetSection("ConnectionStrings:DbType").Value), // 根据你的数据库类型进行更改
|
|
IsAutoCloseConnection = bool.Parse(_configuration.GetSection("ConnectionStrings:IsAutoCloseConnection").Value),
|
|
InitKeyType = InitKeyType.Attribute // 从实体属性中读取主键和自增列信息
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查询按钮 查询指定日期工单
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void button2_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 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;
|
|
}
|
|
}
|
|
|
|
}
|