You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
193 lines
10 KiB
193 lines
10 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Data.SqlClient;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows;
|
|
using System.Windows.Controls;
|
|
using System.Windows.Controls.Primitives;
|
|
using System.Windows.Data;
|
|
using System.Windows.Documents;
|
|
using System.Windows.Input;
|
|
using System.Windows.Media;
|
|
using System.Windows.Media.Imaging;
|
|
using System.Windows.Navigation;
|
|
using System.Windows.Shapes;
|
|
using System.Windows.Threading;
|
|
|
|
namespace StatisticsUI
|
|
{
|
|
/// <summary>
|
|
/// MainWindow.xaml 的交互逻辑
|
|
/// </summary>
|
|
public partial class MainWindow : Window
|
|
{
|
|
public MainWindow()
|
|
{
|
|
InitializeComponent();
|
|
}
|
|
|
|
|
|
private static IniFile.IniFiles Configini = new IniFile.IniFiles(Convert.ToString(System.AppDomain.CurrentDomain.BaseDirectory) + "StatisticsUI.ini");
|
|
private static string SQLIP = Configini.IniReadvalue("SQL_SERVER", "SQL1"); //读配置文件
|
|
private static string SQLNAME = Configini.IniReadvalue("SQL_SERVER", "SQL2");
|
|
private static string SQMOD = Configini.IniReadvalue("SQL_SERVER", "SQL3");
|
|
private static string SQLUSER = Configini.IniReadvalue("SQL_SERVER", "SQL4");
|
|
private static string SQLPASWORD = Configini.IniReadvalue("SQL_SERVER", "SQL5");
|
|
private static DataTable DyelotsBulkedRecipe = new DataTable();
|
|
private static DataTable Machine = new DataTable();
|
|
private static DataTable Gram = new DataTable();
|
|
private static DataTable Machine_Gram = new DataTable();
|
|
SqlConnection conn_SC;
|
|
string Connstr_SC;
|
|
public static string DataGridStatistics_ProductCode = null;
|
|
public static string date_start_time;
|
|
public static string date_end_time;
|
|
public static string query_start = null;//全局变量开始时间
|
|
public static string query_end = null;//全局变量结束时间
|
|
public static int type = 0;//全局变量结束时间
|
|
|
|
private void DataGridStatistics_MouseDoubleClick(object sender, MouseButtonEventArgs e)//数据表双击事件
|
|
{
|
|
int rownum = DataGridStatistics.SelectedIndex;//获取鼠标选中行并定义变量
|
|
if (rownum != -1)//判断鼠标定位是否有效
|
|
{
|
|
/*定位选中行及指定列单元格文本信息*/
|
|
DataGridStatistics_ProductCode = (DataGridStatistics.Columns[0].GetCellContent(DataGridStatistics.Items[rownum]) as TextBlock).Text;//定位第0列,原料代码
|
|
try
|
|
{
|
|
string T = "SELECT d.Machine AS MachineName, ISNULL(SUM(dbr.DispenseGrams)/1000, 0) " +
|
|
"AS TotalDispenseGrams FROM DyelotsBulkedRecipe dbr " +
|
|
"INNER JOIN Dyelots d ON dbr.Dyelot = d.Dyelot WHERE " +
|
|
"dbr.ProductCode = '" + DataGridStatistics_ProductCode + "'" +
|
|
"AND Created >= '" + query_start + " " + date_start_time + "' AND Created< '" +
|
|
query_end + " " + date_end_time + "'" +
|
|
"GROUP BY d.Machine ORDER BY d.Machine";
|
|
conn_SC.OpenAsync(); //连接数据库
|
|
Gram.Clear();
|
|
SqlDataAdapter Grame_ = new SqlDataAdapter(T, Connstr_SC);
|
|
|
|
Grame_.Fill(Gram);
|
|
DataGridStatistics_mac.ItemsSource = Gram.DefaultView;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "错误");//返回无效
|
|
}
|
|
finally { conn_SC.Close(); }
|
|
}
|
|
}
|
|
|
|
private void Button_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
query_start = this.query_date_start.Text;//传递开始日期
|
|
date_start_time = this.query_date_start_time.Text;//传递开始时间
|
|
query_end = this.query_date_end.Text;//传递结束日期
|
|
date_end_time = this.query_date_end_time.Text;//传递结束时间
|
|
|
|
int query_TimeCompare = DateTime.Compare(Convert.ToDateTime(query_start), Convert.ToDateTime(query_end)); //比较开始结束时间,小于-1 等于0 大于1
|
|
if (query_TimeCompare == 1)//判断查询时间是否有效,等于1无效
|
|
{
|
|
MessageBox.Show("SC:无效查询时间", "错误");//返回无效时间弹窗
|
|
}
|
|
else
|
|
{
|
|
string DyelotsBulkedRecipe_sql;
|
|
string Machine_sql;
|
|
if (this.stuff_ProductType.Text == "染料") type = 1;//原料类型
|
|
if (this.stuff_ProductType.Text == "助剂") type = 2;
|
|
if (this.stuff_ProductType.Text == "粉体助剂") type = 3;
|
|
if (this.stuff_ProductType.Text == "全部原料")
|
|
{
|
|
DyelotsBulkedRecipe_sql =
|
|
"SELECT [ProductCode],[ProductName],SUM(DispenseGrams)/1000 as TotalDispenseGrams,COUNT(*) as PrescriptionCount " +
|
|
"FROM [BatchDyeingCentral].[dbo].[DyelotsBulkedRecipe] where Created > '" + query_start + " " +
|
|
date_start_time + "' AND Created < '" + query_end + " " + date_end_time + "'AND DispenseGrams IS NOT NULL" +
|
|
" GROUP BY [ProductCode],[ProductName] ORDER BY TotalDispenseGrams DESC ";
|
|
Machine_sql =
|
|
"SELECT m.Name AS MachineName," +
|
|
"COUNT(DISTINCT d.Dyelot) AS DyelotCount," +
|
|
"COUNT(DISTINCT CASE WHEN (dbr.DispenseResult = 301 OR dbr.DyeResult = 301) " +
|
|
"THEN CONCAT(dbr.Dyelot, '|', dbr.ReDye, '', '|', dbr.StepNumber, '')" +
|
|
"END ) AS UniqueRecipeCount FROM Machines m LEFT JOIN Dyelots d ON m.Name = d.Machine " +
|
|
"AND d.CreationTime >= '" + query_start + " " + date_start_time + "' AND d.CreationTime < '" + query_end +
|
|
" " + date_end_time + "'"+
|
|
"LEFT JOIN DyelotsBulkedRecipe dbr ON d.Dyelot = dbr.Dyelot GROUP BY m.Name ORDER BY m.Name";
|
|
}
|
|
else
|
|
{
|
|
DyelotsBulkedRecipe_sql =
|
|
"SELECT [ProductCode],[ProductName],SUM(DispenseGrams)/1000 as TotalDispenseGrams,COUNT(*) as PrescriptionCount " +
|
|
"FROM [BatchDyeingCentral].[dbo].[DyelotsBulkedRecipe] where Created >= '" + query_start + " " + date_start_time + "' AND Created < '" +
|
|
query_end + " " + date_end_time + "'AND ProductType='"+ type + "'AND DispenseGrams IS NOT NULL" +
|
|
" GROUP BY [ProductCode],[ProductName] ORDER BY TotalDispenseGrams DESC ";
|
|
Machine_sql =
|
|
"SELECT m.Name AS MachineName," +
|
|
"COUNT(DISTINCT d.Dyelot) AS DyelotCount," +
|
|
"COUNT(DISTINCT CASE WHEN (dbr.DispenseResult = 301 OR dbr.DyeResult = 301) AND dbr.ProductType = '" + type+
|
|
"' THEN CONCAT(dbr.Dyelot, '|', dbr.ReDye, '', '|', dbr.StepNumber, '')END"+
|
|
" ) AS UniqueRecipeCount FROM Machines m LEFT JOIN Dyelots d ON m.Name = d.Machine " +
|
|
"AND d.CreationTime >= '" + query_start + " " + date_start_time + "' AND d.CreationTime < '" + query_end +
|
|
" " + date_end_time + "' LEFT JOIN DyelotsBulkedRecipe dbr ON d.Dyelot = dbr.Dyelot AND dbr.Created>='" + query_start +
|
|
" " + date_start_time + "' AND dbr.Created < '" + query_end + " " + date_end_time + "' GROUP BY m.Name ORDER BY m.Name";
|
|
}
|
|
conn_SC.OpenAsync(); //连接数据库
|
|
Machine.Clear();
|
|
DyelotsBulkedRecipe.Clear();
|
|
SqlDataAdapter DyelotsBulkedRecipe_ = new SqlDataAdapter(DyelotsBulkedRecipe_sql, Connstr_SC);
|
|
SqlDataAdapter Machine_ = new SqlDataAdapter(Machine_sql, Connstr_SC);
|
|
try
|
|
{
|
|
DyelotsBulkedRecipe_.Fill(DyelotsBulkedRecipe);
|
|
DataGridStatistics.ItemsSource = DyelotsBulkedRecipe.DefaultView;
|
|
|
|
Machine_.Fill(Machine);
|
|
for (int i = Machine.Rows.Count - 1; i >= 0; i--)
|
|
{
|
|
DataRow row = Machine.Rows[i];
|
|
|
|
// 检查目标列是否为0(处理DBNull情况)
|
|
if (row["DyelotCount"] != DBNull.Value &&
|
|
Convert.ToDouble(row["DyelotCount"]) == 0)
|
|
{
|
|
Machine.Rows.RemoveAt(i);
|
|
}
|
|
}
|
|
Machine.AcceptChanges();
|
|
DataGridStatistics_mac.ItemsSource = Machine.DefaultView;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "错误");//返回无效时间弹窗
|
|
}
|
|
finally
|
|
{
|
|
conn_SC.Close();
|
|
}
|
|
}
|
|
}
|
|
|
|
private void Window_Loaded(object sender, RoutedEventArgs e)
|
|
{
|
|
Connstr_SC = "server=" + SQLIP + ";database=" + SQLNAME + ";User ID=" + SQLUSER + ";Password=" + SQLPASWORD;
|
|
conn_SC = new SqlConnection(Connstr_SC);
|
|
|
|
Gram.Columns.Add("ProductCode", typeof(string));
|
|
Gram.Columns.Add("ProductName", typeof(string));
|
|
Gram.Columns.Add("TotalDispenseGrams", typeof(string));
|
|
Gram.Columns.Add("PrescriptionCount", typeof(string));
|
|
|
|
Machine_Gram.Columns.Add("MachineName", typeof(string));
|
|
Machine_Gram.Columns.Add("TotalDispenseGrams", typeof(string));
|
|
Machine_Gram.Columns.Add("DyelotCount", typeof(string));
|
|
Machine_Gram.Columns.Add("UniqueRecipeCount", typeof(string));
|
|
|
|
this.query_date_start.Text = DateTime.Now.ToString("yyyy-MM-dd"); ;//传递开始日期
|
|
this.query_date_end.Text = DateTime.Now.AddDays(1).ToString("yyyy-MM-dd"); ;//传递结束日期
|
|
}
|
|
}
|
|
}
|
|
|