优化排产计划脚本

This commit is contained in:
李鹏飞 2025-09-28 10:58:15 +08:00
parent 5b19599969
commit 9760d553ab
1 changed files with 114 additions and 0 deletions

View File

@ -0,0 +1,114 @@
--drop PROCEDURE [dbo].[Sp_HJGL_ProductionPlanStatistics]
CREATE PROCEDURE [dbo].[Sp_HJGL_ProductionPlanStatistics]
@projectId nvarchar(50)=null
AS
BEGIN
SET NOCOUNT ON; -- 减少网络流量,提高性能
-- 使用CTE预先过滤和聚合数据
WITH PipelineFiltered AS (
SELECT UnitWorkId, FlowingSection, PipelineId
FROM HJGL_Pipeline
WHERE PipeArea='1'
AND FlowingSection IS NOT NULL
AND FlowingSection != ''
AND (@projectId IS NULL OR UnitWorkId IN (
SELECT UnitWorkId FROM WBS_UnitWork WHERE ProjectId = @projectId
))
),
-- 工厂预制总达因
TotalDiaCTE AS (
SELECT p.UnitWorkId, p.FlowingSection, SUM(wj.Size) as TotalDia
FROM HJGL_WeldJoint wj
INNER JOIN PipelineFiltered p ON p.PipelineId = wj.PipelineId
WHERE wj.JointAttribute='预制口'
GROUP BY p.UnitWorkId, p.FlowingSection
),
-- 当日完成工作量
CurrentDayCompletedCTE AS (
SELECT p.UnitWorkId, p.FlowingSection, SUM(wj.Size) as CurrentDayCompletedDia
FROM HJGL_WeldJoint wj
INNER JOIN PipelineFiltered p ON p.PipelineId = wj.PipelineId
INNER JOIN HJGL_WeldingDaily wd ON wd.WeldingDailyId = wj.WeldingDailyId
WHERE wj.JointAttribute='预制口'
AND wj.WeldingDailyId IS NOT NULL
AND wd.WeldingDate >= CAST(GETDATE() AS DATE) -- 优化日期比较,使用日期范围
AND wd.WeldingDate < DATEADD(DAY, 1, CAST(GETDATE() AS DATE))
GROUP BY p.UnitWorkId, p.FlowingSection
),
-- 累计已完成量
TotalCompletedCTE AS (
SELECT p.UnitWorkId, p.FlowingSection, SUM(wj.Size) as totalCompletedDia
FROM HJGL_WeldJoint wj
INNER JOIN PipelineFiltered p ON p.PipelineId = wj.PipelineId
WHERE wj.JointAttribute='预制口'
AND wj.WeldingDailyId IS NOT NULL
GROUP BY p.UnitWorkId, p.FlowingSection
),
-- 当前焊工数量
WelderCountCTE AS (
SELECT p.UnitWorkId, p.FlowingSection,
COUNT(DISTINCT pp.PersonId) as WelderCount -- 使用DISTINCT避免重复计数
FROM Person_Persons pp
INNER JOIN HJGL_WeldJoint wj ON wj.BackingWelderId = pp.PersonId
INNER JOIN PipelineFiltered p ON p.PipelineId = wj.PipelineId
WHERE wj.JointAttribute='预制口'
AND wj.WeldingDailyId IS NOT NULL
GROUP BY p.UnitWorkId, p.FlowingSection
),
-- 预警焊工数量
WarningWelderCountCTE AS (
SELECT p.UnitWorkId, p.FlowingSection,
COUNT(DISTINCT pp.PersonId) as WarningWelderCount
FROM Person_Persons pp
INNER JOIN HJGL_WeldJoint wj ON wj.BackingWelderId = pp.PersonId
INNER JOIN PipelineFiltered p ON p.PipelineId = wj.PipelineId
INNER JOIN Welder_WelderQualify wq ON wq.WelderId = pp.PersonId
WHERE wj.JointAttribute='预制口'
AND wj.WeldingDailyId IS NOT NULL
AND wq.LimitDate < GETDATE() -- 焊工资格过期预警
GROUP BY p.UnitWorkId, p.FlowingSection
)
SELECT DISTINCT
unitWork.UnitWorkId,
unitWork.UnitWorkCode,
unitWork.UnitWorkName,
unitWork.ProjectId,
pipeline.FlowingSection,
isnull(total.TotalDia,0) as TotalDia,--
p.PlanStartDate,--
p.PlanEndDate,--
p.Days as TotalDays,--
(case when p.PlanEndDate>= getdate() then (p.Days-(DATEDIFF(day,p.PlanStartDate,GETDATE()))) else '' end) as RemainingDays,--
(case when p.Days>0 then cast(isnull(total.TotalDia,0)/p.Days as decimal(18,2)) else 0 end) as AvgDayCompleteDia, --
ISNULL(currentDay.CurrentDayCompletedDia,0) as CurrentDayCompletedDia, --
cast(case when (case when p.PlanEndDate>= getdate() then (p.Days-(DATEDIFF(day,p.PlanStartDate,GETDATE()))) else 0 end)>0 then
(isnull(total.TotalDia,0)-isnull(totalCompleted.totalCompletedDia,0))/ (case when p.PlanEndDate>= getdate() then (p.Days-(DATEDIFF(day,p.PlanStartDate,GETDATE()))) else 0 end) else 0 end as decimal(18,2)) as NextDayComplete, --
isnull(totalCompleted.totalCompletedDia,0) as totalCompletedDia, --
cast(cast((case when isnull(total.TotalDia,0)>0 then
(isnull(totalCompleted.totalCompletedDia,0) / isnull(total.TotalDia,0)*100) else 0 end) as decimal(18,2)) as varchar(10))+'%' as CompletedRate, --
welder.WelderCount, --
warningWelder.WarningWelderCount --
FROM WBS_UnitWork AS unitWork
INNER JOIN PipelineFiltered pipeline ON pipeline.UnitWorkId = unitWork.UnitWorkId -- 使用INNER JOIN因为pipeline数据必须存在
LEFT JOIN HJGL_ProductionSchedulingPlan p ON p.PipelineId = unitWork.UnitWorkId AND p.FlowNum = pipeline.FlowingSection
--
LEFT JOIN TotalDiaCTE total ON total.UnitWorkId = unitWork.UnitWorkId AND total.FlowingSection = pipeline.FlowingSection
--
LEFT JOIN CurrentDayCompletedCTE currentDay ON currentDay.UnitWorkId = unitWork.UnitWorkId AND currentDay.FlowingSection = pipeline.FlowingSection
--
LEFT JOIN TotalCompletedCTE totalCompleted ON totalCompleted.UnitWorkId = unitWork.UnitWorkId AND totalCompleted.FlowingSection = pipeline.FlowingSection
--
LEFT JOIN WelderCountCTE welder ON welder.UnitWorkId = unitWork.UnitWorkId AND welder.FlowingSection = pipeline.FlowingSection
--
LEFT JOIN WarningWelderCountCTE warningWelder ON warningWelder.UnitWorkId = unitWork.UnitWorkId AND warningWelder.FlowingSection = pipeline.FlowingSection
WHERE (@projectId IS NULL OR unitWork.ProjectId = @projectId)
AND pipeline.FlowingSection IS NOT NULL;
END