|
|
|
|
@ -207,5 +207,87 @@ namespace CncRepository.Impl
|
|
|
|
|
new { Id = machineId, AddressId = collectAddressId });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
public MachineCollectRecordItem GetLatestCollectRecord(int machineId)
|
|
|
|
|
{
|
|
|
|
|
using (var conn = CreateConnection())
|
|
|
|
|
{
|
|
|
|
|
string sql = @"SELECT DATE_FORMAT(cr.collect_time, '%Y-%m-%d %H:%i:%s') AS CollectTime,
|
|
|
|
|
cr.program_name AS ProgramName, cr.part_count AS PartCount, cr.run_status AS RunStatus
|
|
|
|
|
FROM cnc_collect_record cr
|
|
|
|
|
WHERE cr.machine_id = @MachineId
|
|
|
|
|
ORDER BY cr.collect_time DESC LIMIT 1";
|
|
|
|
|
return conn.QuerySingleOrDefault<MachineCollectRecordItem>(sql, new { MachineId = machineId });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
public List<MachineTodayProdItem> GetTodayProduction(int machineId)
|
|
|
|
|
{
|
|
|
|
|
using (var conn = CreateConnection())
|
|
|
|
|
{
|
|
|
|
|
// 优先从已汇总的日产量表取
|
|
|
|
|
string dpSql = @"SELECT program_name AS ProgramName, CAST(total_quantity AS SIGNED) AS Quantity,
|
|
|
|
|
total_run_time AS RunTime, total_cutting_time AS CuttingTime
|
|
|
|
|
FROM cnc_daily_production
|
|
|
|
|
WHERE machine_id = @MachineId AND production_date = CURDATE()";
|
|
|
|
|
var dpItems = conn.Query<MachineTodayProdItem>(dpSql, new { MachineId = machineId }).AsList();
|
|
|
|
|
if (dpItems.Count > 0) return dpItems;
|
|
|
|
|
|
|
|
|
|
// 没有汇总数据则从分段表实时计算
|
|
|
|
|
string segSql = @"SELECT seg.program_name AS ProgramName,
|
|
|
|
|
CAST(SUM(CASE WHEN seg.is_settled=1 THEN seg.quantity
|
|
|
|
|
ELSE COALESCE(seg.end_part_count, seg.start_part_count) - seg.start_part_count END) AS SIGNED) AS Quantity,
|
|
|
|
|
NULL AS RunTime, NULL AS CuttingTime
|
|
|
|
|
FROM cnc_production_segment seg
|
|
|
|
|
WHERE seg.machine_id = @MachineId AND seg.production_date = CURDATE()
|
|
|
|
|
GROUP BY seg.program_name";
|
|
|
|
|
return conn.Query<MachineTodayProdItem>(segSql, new { MachineId = machineId }).AsList();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
public List<MachineTrendItem> GetProductionTrend(int machineId, int days = 7)
|
|
|
|
|
{
|
|
|
|
|
using (var conn = CreateConnection())
|
|
|
|
|
{
|
|
|
|
|
// 优先从日产量表取近N天数据
|
|
|
|
|
string sql = @"SELECT DATE_FORMAT(dp.production_date, '%Y-%m-%d') AS Date,
|
|
|
|
|
CAST(SUM(dp.total_quantity) AS SIGNED) AS Quantity
|
|
|
|
|
FROM cnc_daily_production dp
|
|
|
|
|
WHERE dp.machine_id = @MachineId
|
|
|
|
|
AND dp.production_date >= DATE_SUB(CURDATE(), INTERVAL @Days DAY)
|
|
|
|
|
GROUP BY dp.production_date
|
|
|
|
|
ORDER BY dp.production_date";
|
|
|
|
|
var items = conn.Query<MachineTrendItem>(sql, new { MachineId = machineId, Days = days - 1 }).AsList();
|
|
|
|
|
if (items.Count > 0) return items;
|
|
|
|
|
|
|
|
|
|
// 没有汇总数据则从分段表实时计算
|
|
|
|
|
string segSql = @"SELECT DATE_FORMAT(seg.production_date, '%Y-%m-%d') AS Date,
|
|
|
|
|
CAST(SUM(CASE WHEN seg.is_settled=1 THEN seg.quantity
|
|
|
|
|
ELSE COALESCE(seg.end_part_count, seg.start_part_count) - seg.start_part_count END) AS SIGNED) AS Quantity
|
|
|
|
|
FROM cnc_production_segment seg
|
|
|
|
|
WHERE seg.machine_id = @MachineId
|
|
|
|
|
AND seg.production_date >= DATE_SUB(CURDATE(), INTERVAL @Days DAY)
|
|
|
|
|
GROUP BY seg.production_date
|
|
|
|
|
ORDER BY seg.production_date";
|
|
|
|
|
return conn.Query<MachineTrendItem>(segSql, new { MachineId = machineId, Days = days - 1 }).AsList();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
public List<MachineCollectRecordItem> GetRecentCollectRecords(int machineId, int limit = 20)
|
|
|
|
|
{
|
|
|
|
|
using (var conn = CreateConnection())
|
|
|
|
|
{
|
|
|
|
|
string sql = @"SELECT DATE_FORMAT(cr.collect_time, '%Y-%m-%d %H:%i:%s') AS CollectTime,
|
|
|
|
|
cr.program_name AS ProgramName, cr.part_count AS PartCount, cr.run_status AS RunStatus
|
|
|
|
|
FROM cnc_collect_record cr
|
|
|
|
|
WHERE cr.machine_id = @MachineId
|
|
|
|
|
ORDER BY cr.collect_time DESC LIMIT @Limit";
|
|
|
|
|
return conn.Query<MachineCollectRecordItem>(sql, new { MachineId = machineId, Limit = limit }).AsList();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|