@ -10,9 +10,6 @@ using CncRepository.Interface;
namespace CncRepository.Impl
{
/// <summary>
/// cnc_daily_production 产量仓储实现(业务库)
/// </summary>
public class DailyProductionRepository : BusinessRepository , IDailyProductionRepository
{
public DailyProductionRepository ( string connectionString ) : base ( connectionString ) { }
@ -33,54 +30,33 @@ namespace CncRepository.Impl
var parameters = new DynamicParameters ( ) ;
string baseSql = @ "SELECT dp.machine_id AS MachineId, m.name AS MachineName,
dp . program_name AS ProgramName , dp . production_date AS ProductionDate ,
CAST ( dp . total_quantity AS SIGNED ) AS Quantity ,
dp . segment_count AS SegmentCount ,
dp . total_run_time AS TotalRunTime , dp . total_cutting_time AS TotalCuttingTime
( dp . end_total_count - dp . base_total_count ) AS Quantity
FROM cnc_daily_production dp
JOIN cnc_machine m ON dp . machine_id = m . id
WHERE dp . total_quantity > 0 ";
string realtimeSql = @ "SELECT seg.machine_id AS MachineId, m.name AS MachineName,
seg . program_name AS ProgramName , seg . production_date AS ProductionDate ,
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 ,
COUNT ( * ) AS SegmentCount , 0 AS TotalRunTime , 0 AS TotalCuttingTime
FROM cnc_production_segment seg
JOIN cnc_machine m ON seg . machine_id = m . id
WHERE 1 = 1 ";
WHERE 1 = 1 ";
if ( query ? . WorkshopId . HasValue = = true )
{
baseSql + = " AND m.workshop_id = @WorkshopId" ;
realtimeSql + = " AND m.workshop_id = @WorkshopId" ;
parameters . Add ( "WorkshopId" , query . WorkshopId ) ;
}
if ( query ? . MachineId . HasValue = = true )
{
baseSql + = " AND dp.machine_id = @MachineId" ;
realtimeSql + = " AND seg.machine_id = @MachineId" ;
parameters . Add ( "MachineId" , query . MachineId ) ;
}
if ( query ? . WorkerId . HasValue = = true )
{
baseSql + = " AND EXISTS (SELECT 1 FROM cnc_worker_machine wm WHERE wm.machine_id = dp.machine_id AND wm.worker_id = @WorkerId)" ;
realtimeSql + = " AND EXISTS (SELECT 1 FROM cnc_worker_machine wm WHERE wm.machine_id = seg.machine_id AND wm.worker_id = @WorkerId)" ;
parameters . Add ( "WorkerId" , query . WorkerId ) ;
}
realtimeSql + = " GROUP BY seg.machine_id, seg.production_date, seg.program_name, m.name" ;
string unionSql = $"{baseSql} UNION ALL {realtimeSql}" ;
string countSql = $"SELECT COUNT(*) FROM ({unionSql}) AS t" ;
string countSql = $"SELECT COUNT(*) FROM ({baseSql}) AS t" ;
int offset = ( query . Page - 1 ) * query . PageSize ;
string paging = $ " ORDER BY ProductionDate DESC, MachineName LIMIT @Limit OFFSET @Offset";
string paging = " ORDER BY ProductionDate DESC, MachineName LIMIT @Limit OFFSET @Offset" ;
parameters . Add ( "Limit" , query . PageSize ) ;
parameters . Add ( "Offset" , offset ) ;
var items = conn . Query < DailyProductionListItem > ( union Sql + paging , parameters ) . ToList ( ) ;
var items = conn . Query < DailyProductionListItem > ( base Sql + paging , parameters ) . ToList ( ) ;
int total = conn . ExecuteScalar < int > ( countSql , parameters ) ;
return new PagedResult < DailyProductionListItem >
{
Items = items ,
Total = total ,
Page = query . Page ,
PageSize = query . PageSize
} ;
return new PagedResult < DailyProductionListItem > { Items = items , Total = total , Page = query . Page , PageSize = query . PageSize } ;
}
}
@ -97,10 +73,7 @@ namespace CncRepository.Impl
{
using ( var conn = CreateConnection ( ) )
{
string sql = @ "SELECT dp.* FROM cnc_daily_production dp
LEFT JOIN cnc_machine m ON dp . machine_id = m . id
WHERE dp . production_date BETWEEN @Start AND @End
ORDER BY dp . production_date DESC ";
string sql = @"SELECT dp.* FROM cnc_daily_production dp LEFT JOIN cnc_machine m ON dp.machine_id = m.id WHERE dp.production_date BETWEEN @Start AND @End ORDER BY dp.production_date DESC" ;
return conn . Query < DailyProduction > ( sql , new { Start = startDate , End = endDate } ) . ToList ( ) ;
}
}
@ -109,14 +82,8 @@ namespace CncRepository.Impl
{
using ( var conn = CreateConnection ( ) )
{
string sql = @ "SELECT SUM(dp.total_quantity) FROM cnc_daily_production dp
LEFT JOIN cnc_machine m ON dp . machine_id = m . id
WHERE dp . production_date BETWEEN @Start AND @End ";
if ( workshopId . HasValue )
{
sql + = " AND m.workshop_id = @WorkshopId" ;
return conn . ExecuteScalar < decimal > ( sql , new { Start = startDate , End = endDate , WorkshopId = workshopId . Value } ) ;
}
string sql = @"SELECT SUM(dp.end_total_count - dp.base_total_count) FROM cnc_daily_production dp LEFT JOIN cnc_machine m ON dp.machine_id = m.id WHERE dp.production_date BETWEEN @Start AND @End" ;
if ( workshopId . HasValue ) { sql + = " AND m.workshop_id = @WorkshopId" ; return conn . ExecuteScalar < decimal > ( sql , new { Start = startDate , End = endDate , WorkshopId = workshopId . Value } ) ; }
return conn . ExecuteScalar < decimal > ( sql , new { Start = startDate , End = endDate } ) ;
}
}
@ -125,8 +92,7 @@ namespace CncRepository.Impl
{
using ( var conn = CreateConnection ( ) )
{
string sql = @ "SELECT SUM(total_quantity) FROM cnc_daily_production
WHERE production_date BETWEEN @Start AND @End ";
string sql = @"SELECT SUM(end_total_count - base_total_count) FROM cnc_daily_production WHERE production_date BETWEEN @Start AND @End" ;
var res = conn . ExecuteScalar < decimal? > ( sql , new { Start = startDate , End = endDate } ) ;
return res ? ? 0 m ;
}
@ -136,15 +102,7 @@ namespace CncRepository.Impl
{
using ( var conn = CreateConnection ( ) )
{
string sql = @ "SELECT m.id AS Id, m.id AS MachineId, m.name AS MachineName,
SUM ( dp . total_quantity ) AS TotalQuantity , NULL AS ProgramName ,
NULL AS ProductionDate , NULL AS SegmentCount ,
NULL AS TotalRunTime , NULL AS TotalCuttingTime , NULL AS TotalCycleTime
FROM cnc_daily_production dp
JOIN cnc_machine m ON dp . machine_id = m . id
WHERE dp . production_date BETWEEN @Start AND @End
GROUP BY m . id , m . name
ORDER BY SUM ( dp . total_quantity ) DESC LIMIT @Top ";
string sql = @"SELECT m.id AS Id, m.id AS MachineId, m.name AS MachineName, SUM(dp.end_total_count - dp.base_total_count) AS TotalQuantity, NULL AS ProgramName, NULL AS ProductionDate FROM cnc_daily_production dp JOIN cnc_machine m ON dp.machine_id = m.id WHERE dp.production_date BETWEEN @Start AND @End GROUP BY m.id, m.name ORDER BY SUM(dp.end_total_count - dp.base_total_count) DESC LIMIT @Top" ;
return conn . Query < DailyProduction > ( sql , new { Start = startDate , End = endDate , Top = top } ) . ToList ( ) ;
}
}
@ -153,48 +111,25 @@ namespace CncRepository.Impl
{
using ( var conn = CreateConnection ( ) )
{
string sql = @ "SELECT 0 AS Id, NULL AS MachineId, NULL AS MachineName,
SUM ( total_quantity ) AS TotalQuantity , NULL AS ProductionDate ,
NULL AS ProgramName , NULL AS SegmentCount ,
NULL AS TotalRunTime , NULL AS TotalCuttingTime , NULL AS TotalCycleTime
FROM cnc_daily_production
WHERE production_date BETWEEN @Start AND @End
GROUP BY machine_id
ORDER BY SUM ( total_quantity ) DESC LIMIT @Top ";
string sql = @"SELECT 0 AS Id, NULL AS MachineId, NULL AS MachineName, SUM(end_total_count - base_total_count) AS TotalQuantity, NULL AS ProductionDate, NULL AS ProgramName FROM cnc_daily_production WHERE production_date BETWEEN @Start AND @End GROUP BY machine_id ORDER BY SUM(end_total_count - base_total_count) DESC LIMIT @Top" ;
return conn . Query < DailyProduction > ( sql , new { Start = startDate , End = endDate , Top = top } ) . ToList ( ) ;
}
}
#region 三维度产量报表
public MachineProductionSummaryResponse GetMachineSummary ( DateTime startDate , DateTime endDate , int? workshopId )
{
using ( var conn = CreateConnection ( ) )
{
string sql = @ "SELECT COALESCE(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 ) , 0 ) AS TotalQuantity ,
COUNT ( DISTINCT seg . machine_id ) AS RunningMachineCount
FROM cnc_production_segment seg
LEFT JOIN cnc_machine m ON seg . machine_id = m . id
WHERE seg . production_date BETWEEN @Start AND @End ";
string sql = @"SELECT COALESCE(SUM(dp.end_total_count - dp.base_total_count), 0) AS TotalQuantity, COUNT(DISTINCT dp.machine_id) AS RunningMachineCount FROM cnc_daily_production dp LEFT JOIN cnc_machine m ON dp.machine_id = m.id WHERE dp.production_date BETWEEN @Start AND @End" ;
var parameters = new DynamicParameters ( ) ;
parameters . Add ( "Start" , startDate ) ;
parameters . Add ( "End" , endDate ) ;
if ( workshopId . HasValue )
{
sql + = " AND m.workshop_id = @WorkshopId" ;
parameters . Add ( "WorkshopId" , workshopId . Value ) ;
}
parameters . Add ( "Start" , startDate ) ; parameters . Add ( "End" , endDate ) ;
if ( workshopId . HasValue ) { sql + = " AND m.workshop_id = @WorkshopId" ; parameters . Add ( "WorkshopId" , workshopId . Value ) ; }
var result = conn . QuerySingleOrDefault < MachineProductionSummaryResponse > ( sql , parameters ) ;
if ( result = = null ) return new MachineProductionSummaryResponse ( ) ;
if ( result . RunningMachineCount > 0 )
result . AvgPerMachine = Math . Round ( ( decimal ) result . TotalQuantity / result . RunningMachineCount , 1 ) ;
string topSql = @ "SELECT m.name FROM cnc_production_segment seg
LEFT JOIN cnc_machine m ON seg . machine_id = m . id
WHERE seg . production_date BETWEEN @Start AND @End ";
if ( result . RunningMachineCount > 0 ) result . AvgPerMachine = Math . Round ( ( decimal ) result . TotalQuantity / result . RunningMachineCount , 1 ) ;
string topSql = @"SELECT m.name FROM cnc_daily_production dp LEFT JOIN cnc_machine m ON dp.machine_id = m.id WHERE dp.production_date BETWEEN @Start AND @End" ;
if ( workshopId . HasValue ) topSql + = " AND m.workshop_id = @WorkshopId" ;
topSql + = @ " GROUP BY seg.machine_id ORDER BY 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 ) DESC LIMIT 1 ";
topSql + = " GROUP BY dp.machine_id ORDER BY SUM(dp.end_total_count - dp.base_total_count) DESC LIMIT 1" ;
result . TopMachineName = conn . ExecuteScalar < string > ( topSql , parameters ) ? ? "" ;
return result ;
}
@ -204,29 +139,12 @@ namespace CncRepository.Impl
{
using ( var conn = CreateConnection ( ) )
{
string sql = @ "SELECT m.name AS MachineName, seg.program_name AS ProgramName,
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 TotalQuantity ,
cr_runtime . run_hours AS RunTime
FROM cnc_production_segment seg
LEFT JOIN cnc_machine m ON seg . machine_id = m . id
LEFT JOIN (
SELECT cr . machine_id , DATE ( cr . collect_time ) AS d ,
COALESCE ( ROUND ( SUM ( max_min . delta ) / 3600 , 1 ) , 0 ) AS run_hours
FROM cnc_collect_record cr
JOIN ( SELECT machine_id , DATE ( collect_time ) AS dd ,
MAX ( cutting_time ) - MIN ( cutting_time ) AS delta
FROM cnc_collect_record GROUP BY machine_id , DATE ( collect_time ) ) max_min
ON cr . machine_id = max_min . machine_id AND DATE ( cr . collect_time ) = max_min . dd
GROUP BY cr . machine_id , DATE ( cr . collect_time )
) cr_runtime ON cr_runtime . machine_id = seg . machine_id AND cr_runtime . d = seg . production_date
WHERE seg . production_date BETWEEN @Start AND @End ";
string sql = @"SELECT m.name AS MachineName, dp.program_name AS ProgramName, SUM(dp.end_total_count - dp.base_total_count) AS TotalQuantity FROM cnc_daily_production dp LEFT JOIN cnc_machine m ON dp.machine_id = m.id WHERE dp.production_date BETWEEN @Start AND @End" ;
var parameters = new DynamicParameters ( ) ;
parameters . Add ( "Start" , startDate ) ;
parameters . Add ( "End" , endDate ) ;
parameters . Add ( "Start" , startDate ) ; parameters . Add ( "End" , endDate ) ;
if ( workshopId . HasValue ) { sql + = " AND m.workshop_id = @WorkshopId" ; parameters . Add ( "WorkshopId" , workshopId . Value ) ; }
if ( machineId . HasValue ) { sql + = " AND seg .machine_id = @MachineId"; parameters . Add ( "MachineId" , machineId . Value ) ; }
sql + = " GROUP BY seg.machine_id, seg.program_name, m.name, cr_runtime.run_hours ORDER BY TotalQuantity DESC";
if ( machineId . HasValue ) { sql + = " AND dp.machine_id = @MachineId" ; parameters . Add ( "MachineId" , machineId . Value ) ; }
sql + = " GROUP BY dp.machine_id, dp.program_name, m.name ORDER BY TotalQuantity DESC" ;
var items = conn . Query < MachineProductionListItem > ( sql , parameters ) . ToList ( ) ;
for ( int i = 0 ; i < items . Count ; i + + ) { items [ i ] . Rank = i + 1 ; items [ i ] . DayStatus = items [ i ] . DayStatus ? ? "" ; }
return items ;
@ -237,23 +155,11 @@ namespace CncRepository.Impl
{
using ( var conn = CreateConnection ( ) )
{
string sql = @ "SELECT COALESCE(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 ) , 0 ) AS TotalQuantity ,
COUNT ( DISTINCT w . id ) AS ActiveWorkerCount
FROM cnc_production_segment seg
JOIN cnc_worker_machine wm ON seg . machine_id = wm . machine_id
JOIN cnc_worker w ON wm . worker_id = w . id
WHERE seg . production_date BETWEEN @Start AND @End ";
string sql = @"SELECT COALESCE(SUM(dp.end_total_count - dp.base_total_count), 0) AS TotalQuantity, COUNT(DISTINCT w.id) AS ActiveWorkerCount FROM cnc_daily_production dp JOIN cnc_worker_machine wm ON dp.machine_id = wm.machine_id JOIN cnc_worker w ON wm.worker_id = w.id WHERE dp.production_date BETWEEN @Start AND @End" ;
var result = conn . QuerySingleOrDefault < WorkerProductionSummaryResponse > ( sql , new { Start = startDate , End = endDate } ) ;
if ( result = = null ) return new WorkerProductionSummaryResponse ( ) ;
if ( result . ActiveWorkerCount > 0 )
result . AvgPerWorker = Math . Round ( ( decimal ) result . TotalQuantity / result . ActiveWorkerCount , 1 ) ;
string topSql = @ "SELECT w.name FROM cnc_production_segment seg
JOIN cnc_worker_machine wm ON seg . machine_id = wm . machine_id
JOIN cnc_worker w ON wm . worker_id = w . id
WHERE seg . production_date BETWEEN @Start AND @End
GROUP BY w . id , w . name ORDER BY 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 ) DESC LIMIT 1 ";
if ( result . ActiveWorkerCount > 0 ) result . AvgPerWorker = Math . Round ( ( decimal ) result . TotalQuantity / result . ActiveWorkerCount , 1 ) ;
string topSql = @"SELECT w.name FROM cnc_daily_production dp JOIN cnc_worker_machine wm ON dp.machine_id = wm.machine_id JOIN cnc_worker w ON wm.worker_id = w.id WHERE dp.production_date BETWEEN @Start AND @End GROUP BY w.id, w.name ORDER BY SUM(dp.end_total_count - dp.base_total_count) DESC LIMIT 1" ;
result . TopWorkerName = conn . ExecuteScalar < string > ( topSql , new { Start = startDate , End = endDate } ) ? ? "" ;
return result ;
}
@ -263,30 +169,13 @@ namespace CncRepository.Impl
{
using ( var conn = CreateConnection ( ) )
{
string sql = @ "SELECT w.name AS WorkerName,
COUNT ( DISTINCT wm . machine_id ) AS MachineCount ,
COUNT ( DISTINCT seg . program_name ) AS ProgramCount ,
COALESCE ( 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 ) , 0 ) AS TotalQuantity
FROM cnc_worker w
JOIN cnc_worker_machine wm ON wm . worker_id = w . id
LEFT JOIN cnc_production_segment seg ON seg . machine_id = wm . machine_id
AND seg . production_date BETWEEN @Start AND @End ";
string sql = @"SELECT w.name AS WorkerName, COUNT(DISTINCT wm.machine_id) AS MachineCount, COUNT(DISTINCT dp.program_name) AS ProgramCount, COALESCE(SUM(dp.end_total_count - dp.base_total_count), 0) AS TotalQuantity FROM cnc_worker w JOIN cnc_worker_machine wm ON wm.worker_id = w.id LEFT JOIN cnc_daily_production dp ON dp.machine_id = wm.machine_id AND dp.production_date BETWEEN @Start AND @End" ;
var parameters = new DynamicParameters ( ) ;
parameters . Add ( "Start" , startDate ) ;
parameters . Add ( "End" , endDate ) ;
parameters . Add ( "Start" , startDate ) ; parameters . Add ( "End" , endDate ) ;
if ( workerId . HasValue ) { sql + = " AND w.id = @WorkerId" ; parameters . Add ( "WorkerId" , workerId . Value ) ; }
sql + = " GROUP BY w.id, w.name ORDER BY TotalQuantity DESC" ;
var items = conn . Query < WorkerProductionListItem > ( sql , parameters ) . ToList ( ) ;
if ( items . Count > 0 )
{
int grandTotal = items . Sum ( x = > x . TotalQuantity ) ;
for ( int i = 0 ; i < items . Count ; i + + )
{
items [ i ] . Rank = i + 1 ;
if ( grandTotal > 0 ) items [ i ] . Percentage = Math . Round ( ( decimal ) items [ i ] . TotalQuantity / grandTotal * 100 , 1 ) ;
}
}
if ( items . Count > 0 ) { int grandTotal = items . Sum ( x = > x . TotalQuantity ) ; for ( int i = 0 ; i < items . Count ; i + + ) { items [ i ] . Rank = i + 1 ; if ( grandTotal > 0 ) items [ i ] . Percentage = Math . Round ( ( decimal ) items [ i ] . TotalQuantity / grandTotal * 100 , 1 ) ; } }
return items ;
}
}
@ -295,26 +184,16 @@ namespace CncRepository.Impl
{
using ( var conn = CreateConnection ( ) )
{
string sql = @ "SELECT COALESCE(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 ) , 0 ) AS TotalQuantity ,
COUNT ( DISTINCT seg . program_name ) AS RunningProgramCount
FROM cnc_production_segment seg
LEFT JOIN cnc_machine m ON seg . machine_id = m . id
WHERE seg . production_date BETWEEN @Start AND @End ";
string sql = @"SELECT COALESCE(SUM(dp.end_total_count - dp.base_total_count), 0) AS TotalQuantity, COUNT(DISTINCT dp.program_name) AS RunningProgramCount FROM cnc_daily_production dp LEFT JOIN cnc_machine m ON dp.machine_id = m.id WHERE dp.production_date BETWEEN @Start AND @End" ;
var parameters = new DynamicParameters ( ) ;
parameters . Add ( "Start" , startDate ) ;
parameters . Add ( "End" , endDate ) ;
parameters . Add ( "Start" , startDate ) ; parameters . Add ( "End" , endDate ) ;
if ( workshopId . HasValue ) { sql + = " AND m.workshop_id = @WorkshopId" ; parameters . Add ( "WorkshopId" , workshopId . Value ) ; }
var result = conn . QuerySingleOrDefault < ProgramProductionSummaryResponse > ( sql , parameters ) ;
if ( result = = null ) return new ProgramProductionSummaryResponse ( ) ;
if ( result . RunningProgramCount > 0 )
result . AvgPerProgram = Math . Round ( ( decimal ) result . TotalQuantity / result . RunningProgramCount , 1 ) ;
string topSql = @ "SELECT seg.program_name FROM cnc_production_segment seg
LEFT JOIN cnc_machine m ON seg . machine_id = m . id
WHERE seg . production_date BETWEEN @Start AND @End ";
if ( result . RunningProgramCount > 0 ) result . AvgPerProgram = Math . Round ( ( decimal ) result . TotalQuantity / result . RunningProgramCount , 1 ) ;
string topSql = @"SELECT dp.program_name FROM cnc_daily_production dp LEFT JOIN cnc_machine m ON dp.machine_id = m.id WHERE dp.production_date BETWEEN @Start AND @End" ;
if ( workshopId . HasValue ) topSql + = " AND m.workshop_id = @WorkshopId" ;
topSql + = @ " GROUP BY seg.program_name ORDER BY 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 ) DESC LIMIT 1 ";
topSql + = " GROUP BY dp.program_name ORDER BY SUM(dp.end_total_count - dp.base_total_count) DESC LIMIT 1" ;
result . TopProgramName = conn . ExecuteScalar < string > ( topSql , parameters ) ? ? "" ;
return result ;
}
@ -324,33 +203,15 @@ namespace CncRepository.Impl
{
using ( var conn = CreateConnection ( ) )
{
string sql = @ "SELECT seg.program_name AS ProgramName,
COUNT ( DISTINCT seg . machine_id ) AS MachineCount ,
COALESCE ( 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 ) , 0 ) AS TotalQuantity
FROM cnc_production_segment seg
LEFT JOIN cnc_machine m ON seg . machine_id = m . id
WHERE seg . production_date BETWEEN @Start AND @End ";
string sql = @"SELECT dp.program_name AS ProgramName, COUNT(DISTINCT dp.machine_id) AS MachineCount, SUM(dp.end_total_count - dp.base_total_count) AS TotalQuantity FROM cnc_daily_production dp LEFT JOIN cnc_machine m ON dp.machine_id = m.id WHERE dp.production_date BETWEEN @Start AND @End" ;
var parameters = new DynamicParameters ( ) ;
parameters . Add ( "Start" , startDate ) ;
parameters . Add ( "End" , endDate ) ;
if ( ! string . IsNullOrWhiteSpace ( programName ) ) { sql + = " AND seg.program_name = @ProgramName" ; parameters . Add ( "ProgramName" , programName ) ; }
sql + = " GROUP BY seg.program_name ORDER BY TotalQuantity DESC" ;
parameters . Add ( "Start" , startDate ) ; parameters . Add ( "End" , endDate ) ;
if ( ! string . IsNullOrWhiteSpace ( programName ) ) { sql + = " AND dp.program_name = @ProgramName" ; parameters . Add ( "ProgramName" , programName ) ; }
sql + = " GROUP BY dp.program_name ORDER BY TotalQuantity DESC" ;
var items = conn . Query < ProgramProductionListItem > ( sql , parameters ) . ToList ( ) ;
if ( items . Count > 0 )
{
int grandTotal = items . Sum ( x = > x . TotalQuantity ) ;
for ( int i = 0 ; i < items . Count ; i + + )
{
items [ i ] . Rank = i + 1 ;
if ( items [ i ] . MachineCount > 0 ) items [ i ] . AvgPerMachine = Math . Round ( ( decimal ) items [ i ] . TotalQuantity / items [ i ] . MachineCount , 1 ) ;
if ( grandTotal > 0 ) items [ i ] . Percentage = Math . Round ( ( decimal ) items [ i ] . TotalQuantity / grandTotal * 100 , 1 ) ;
}
}
if ( items . Count > 0 ) { int grandTotal = items . Sum ( x = > x . TotalQuantity ) ; for ( int i = 0 ; i < items . Count ; i + + ) { items [ i ] . Rank = i + 1 ; if ( items [ i ] . MachineCount > 0 ) items [ i ] . AvgPerMachine = Math . Round ( ( decimal ) items [ i ] . TotalQuantity / items [ i ] . MachineCount , 1 ) ; if ( grandTotal > 0 ) items [ i ] . Percentage = Math . Round ( ( decimal ) items [ i ] . TotalQuantity / grandTotal * 100 , 1 ) ; } }
return items ;
}
}
# endregion
}
}