PART 1
在MySQL数据库中,我创建了“ leave”,“ Sickness”,“ Workers”,“ Orders”,“ Order_status”表。自动生成的MySQL代码在以下链接中:
PART 2
在桌面应用程序方面,我编写了基于算法的代码,该算法可按比例计算疾病时间和休假时间,模式如下图所示:
该代码的详细信息在这里:
private sick_leaves calculate_sickness_leaves(string NrOrder)
{
string query1 = string.Format("SELECT MIN(order_status.BEGIN_DATE), MAX(order_status.END_DATE) " +
"FROM project1.order_status INNER JOIN project1.orders ON orders.ID_ORDER = order_status.ID_ORDER " +
"WHERE orders.ORDER_NUMBER = '" + NrOrder + "';");
SQLdata.connection.Open();
using (var command = new MySqlCommand(query1, SQLdata.connection))
{
using (var reader1 = command.ExecuteReader())
{
while (reader1.Read())
{
date_min = Convert.ToDateTime(reader1[0]);
date_max = Convert.ToDateTime(reader1[1]);
}
}
}
TimeSpan time_of_sickness_to_order = new TimeSpan(0, 0, 0);
TimeSpan time_of_leave_to_order = new TimeSpan(0, 0, 0);
for (DateTime date = date_min; date <= date_max; date = date.AddDays(1))
{
long hours_sickness_total = 0;
long hours_leave_total = 0;
TimeSpan time_of_all_orders;
TimeSpan time_of_order;
string query2 = $"SELECT COUNT(sickness.ID_WORKER) AS 'AMOUNT OF WORKERS ON SICKNESS' " +
$"FROM project1.sickness WHERE DATE(sickness.BEGIN_DATE) <= '{date}' AND DATE(sickness.END_DATE) >= '{date}';";
hours_sickness_total = SQLdata.load_amount_sql(query2) * 8;
string query3 = $"SELECT COUNT(leaves.ID_WORKER) AS 'AMOUNT OF WORKERS ON LEAVE' FROM project1.leaves" +
$" WHERE DATE(leaves.BEGIN_DATE) <= '{date}' AND DATE(leaves.END_DATE) >= '{date}';";
hours_leave_total = SQLdata.load_amount_sql(query3) * 8;
string query4 = $"SELECT IFNULL(TIME_FORMAT(SEC_TO_TIME(SUM(TIME_TO_SEC(order_status.END_DATE) - TIME_TO_SEC(order_status.BEGIN_DATE))), '%H:%i:%s'), '00:00:00') AS 'REALIZATION TIME OF ALL ORDERS IN DAY' " +
$"FROM project1.order_status INNER JOIN project1.orders ON orders.ID_ORDER = order_status.ID_ORDER " +
$"WHERE (DATE(order_status.BEGIN_DATE) = '{date}' AND DATE(order_status.END_DATE) = '{date}');";
time_of_all_orders = SQLdata.load_time_sql(query4);
string query5 = $"SELECT IFNULL(TIME_FORMAT(SEC_TO_TIME(SUM(TIME_TO_SEC(order_status.END_DATE) - TIME_TO_SEC(order_status.BEGIN_DATE))), '%H:%i:%s'), '00:00:00') AS 'REALIZATION TIME OF ORDER IN DAY' " +
$"FROM project1.order_status INNER JOIN project1.orders ON orders.ID_ORDER = order_status.ID_ORDER " +
$"WHERE (DATE(order_status.BEGIN_DATE) = '{date}' AND DATE(order_status.END_DATE) = '{ date}') " +
$"AND orders.ORDER_NUMBER = '{NrOrder}';";
time_of_order = SQLdata.load_time_sql(query5);
hours_sickness_total = ((long)(time_of_order.TotalSeconds / time_of_all_orders.TotalSeconds) * hours_sickness_total);
time_of_sickness_to_order += new TimeSpan(hours_sickness_total * 10000000 * 60 * 60);
hours_leave_total = ((long)(time_of_order.TotalSeconds / time_of_all_orders.TotalSeconds) * hours_leave_total);
time_of_leave_to_order += new TimeSpan(hours_leave_total * 10000000 * 60 * 60);
}
SQLdata.connection.Close();
sick_leaves x = new sick_leaves();
x.sickness_time = time_of_sickness_to_order;
x.leave_time = time_of_leave_to_order;
return x;
}
我不得不编写其他代码来加载以下数据:
1)用户(每天)load_time_sql
可以选择的订单时间以及所选订单中所有订单的时间(每天)。
2)在选定的订单中,每天的生病时间和离开时间的总时间。
public static TimeSpan load_time_sql(string query)
{
TimeSpan x = new TimeSpan(0, 0, 0);
using (var command = new MySqlCommand(query, SQLdata.connection))
{
using (var reader1 = command.ExecuteReader())
{
while (reader1.Read())
{
x = Convert.ToDateTime(reader1[0]).TimeOfDay;
}
return x;
}
}
}
public static int load_amount_sql(string query)
{
int x = 0;
using (var command = new MySqlCommand(query, SQLdata.connection))
{
using (var reader1 = command.ExecuteReader())
{
while (reader1.Read())
{
x = Convert.ToInt32(reader1[0]);
}
return x;
}
}
}
PART 3
现在在此应用程序中,我想选择“测试”顺序。至于按顺序显示工人的生病时间和休假时间,然后在“私人病假_calculate_sickness_leaves”上在datagridview上生成结果,则写在下面的链接中:
生成此订单的报告后,我得到以下结果:
从MySqlDataAdapter adapter4:
+---------+---------+---------------+---------------+------------+------------+
| FNAME | LNAME | BEGIN_DATE | END_DATE | LEAVE_TIME |
+---------+---------+---------------+---+-----------+------------+------------+
| ADAM | GAX | 2019-03-19 7:00:00 | 2019-03-19 15:00:00 | 08:00:00 |
+---------+---------+---------------+---------------+------------+------------+
| ANDREW | WORM | EMPTY | EMPTY | 00:00:00 |
+---------+---------+---------------+---------------+------------+------------+
从MySqlDataAdapter adapter5:
+---------+---------+---------------+---------------+------------+---------------+
| FNAME | LNAME | BEGIN_DATE | END_DATE | SICKNESS_TIME |
+---------+---------+---------------+---+-----------+------------+---------------+
| ADAM | GAX | EMPTY | EMPTY | 00:00:00 |
+---------+---------+---------------+---------------+------------+---------------+
| ANDREW | WORM | 2019-03-19 7:00:00 | 2019-03-20 15:00:00 | 16:00:00 |
+---------+---------+---------------+---------------+------------+---------------+
来自私人的病假叶子calculate_sickness_leaves:
+----------------+--------------+
| sickness_time | leave_time |
+----------------+--------------+
| 00:00:00 | 00:00:00 |
+----------------+--------------+
至于我认为关于病假时间和休假时间的结果是不正确的。这些应该是“ 00:00:00”以外的其他值,因为我看到了ADAM GAX的请假和ANDREW WORM的疾病。那么计算应该是正确的,并且当然应该成比例地计算。
我有一个问题:如何解决按比例正确地汇总休假时间和疾病时间的错误?有任何想法吗?我可以指望任何帮助或建议吗?预先感谢。
我的代码位于下载该代码或其他内容的链接中:
https://www.mediafire.com/folder/pue5ainu4w4a6ii,r9frx0713s2iy51/shared
答案 0 :(得分:0)
如@Arth所指出的,问题是
hours_sickness_total = ((long)(time_of_order.TotalSeconds / time_of_all_orders.TotalSeconds) * hours_sickness_total);
更准确地
(long)(time_of_order.TotalSeconds / time_of_all_orders.TotalSeconds)
代码段Cast double to long演示了此问题。
也许您可以更改为
var percentage = time_of_order.TotalSeconds / time_of_all_orders.TotalSeconds;
hours_sickness_total = (long) (percentage * hours_sickness_total)