﻿ALTER PROCEDURE [dbo].[sp_TrustReport]
(
   @TrustBatchId NVARCHAR(50)
)
AS
SELECT 
('工程名称：'+pro.ProjectName) AS ProjectName,
('单元名称：'+ins.InstallationName+'（'+ins.InstallationCode+'）') AS InstallationName,
(CASE WHEN trust.NDEUuit IS NOT NULL THEN unit.UnitName ELSE (SELECT UnitName FROM dbo.Base_Unit WHERE UnitId=(SELECT TOP 1 UnitId FROM dbo.Project_Unit WHERE ProjectId= trust.ProjectId AND UnitType='4')) END) AS NDEUnit,
trust.TrustBatchCode,nde.DetectionTypeCode,rate.DetectionRateCode,pipe.PipelineCode,
(CASE WHEN nde.SysType='射线检测' THEN 'NB/T47013.2' WHEN nde.SysType='超声波检测' THEN 'NB/T47013.3' 
WHEN nde.SysType='磁粉检测' THEN 'NB/T47013.4' WHEN nde.SysType='渗透检测' THEN 'NB/T47013.5' END) AS ExecStandard,(CASE WHEN nde.SysType='渗透检测' THEN 'Ⅰ' ELSE (CASE WHEN nde.SysType='超声波检测' THEN 'Ⅱ' ELSE (CASE WHEN rate.DetectionRateCode='5%' OR rate.DetectionRateCode='10%' THEN 'Ⅲ' ELSE 'Ⅱ' END) END)END) AS QueClass,dbo.Fun_WeldingMethod(@TrustBatchId) AS WeldingMethodCode,
dbo.Fun_GrooveType(@TrustBatchId) AS GrooveTypeCode,
('申请单位：'+cunit.UnitName) AS CUnitName,
tunit.UnitName AS TrustUnit,
trust.QuaCertFile,--质量证明文件编号
trust.AcceptStandard--制造/验收标准
FROM dbo.Batch_BatchTrust trust 
LEFT JOIN dbo.Base_Project pro ON pro.ProjectId = trust.ProjectId
LEFT JOIN dbo.Project_Installation ins ON ins.InstallationId = trust.InstallationId
LEFT JOIN dbo.Base_Unit unit ON unit.UnitId = trust.NDEUuit
LEFT JOIN dbo.Base_Unit cunit ON cunit.UnitId = trust.UnitId
LEFT JOIN dbo.Base_Unit tunit ON tunit.UnitId = ins.SupervisorUnitId
LEFT JOIN dbo.Base_DetectionType nde ON nde.DetectionTypeId = trust.DetectionTypeId
LEFT JOIN dbo.Pipeline_Pipeline pipe ON pipe.PipelineId = trust.PipelineId
LEFT JOIN dbo.Base_DetectionRate rate ON rate.DetectionRateId = pipe.DetectionRateId
WHERE trust.TrustBatchId=@TrustBatchId
GO

ALTER PROCEDURE [dbo].[TrustItemReport]
(
   @TrustBatchId NVARCHAR(50)
)
AS
/******焊缝检测委托单明细表*******/	  
SELECT	ROW_NUMBER() OVER(ORDER BY jot.WeldJointCode) AS Number,
BatchTrustItem.TrustBatchItemId, 
BatchTrustItem.TrustBatchId, 
pipe.PipelineCode,     --管线号
pipe.SingleNumber,     --单线号
(CASE WHEN PointBatchItem.PointState='2' then (CASE WHEN jot.JointAttribute='固定F' AND RIGHT(jot.WeldJointCode,1)<>'G' THEN jot.WeldJointCode+'G' ELSE jot.WeldJointCode END)+'K'
      ELSE (CASE WHEN jot.JointAttribute='固定F' AND RIGHT(jot.WeldJointCode,1)<>'G' THEN jot.WeldJointCode+'G' ELSE jot.WeldJointCode END) END) AS WeldJointCode,   --焊口号
jot.Specification,  --焊缝规格
mat.MaterialCode,    --材质
(CASE WHEN jot.BackingWelderId!=jot.CoverWelderId THEN backingWelder.WelderCode+'/'+coverWelder.WelderCode ELSE backingWelder.WelderCode END) AS WelderCode,--焊工号
rate.DetectionRateCode AS DetectionRateCode,--检测比例
PointBatchItem.AcceptLevel AS  AcceptLevel, --验收等级
(case when PointBatchItem.IsWelderFirst=1 then '首三件' else '' end) as Remark,
jot.HeartNo1,--炉批1号
jot.HeartNo2,--炉批2号
(case jot.IsHotProess when 1 then '是' else '否' end) as IsHotProessName--是否热处理
FROM dbo.Batch_BatchTrustItem AS BatchTrustItem
LEFT JOIN Batch_PointBatchItem AS PointBatchItem ON PointBatchItem.PointBatchItemId=BatchTrustItem.PointBatchItemId
LEFT JOIN dbo.Batch_PointBatch point ON point.PointBatchId = PointBatchItem.PointBatchId
LEFT JOIN dbo.Base_DetectionRate rate ON rate.DetectionRateId = point.DetectionRateId
LEFT JOIN dbo.Pipeline_WeldJoint jot ON jot.WeldJointId = BatchTrustItem.WeldJointId
LEFT JOIN dbo.Pipeline_Pipeline pipe ON pipe.PipelineId = jot.PipelineId
LEFT JOIN dbo.Welder_Welder backingWelder ON backingWelder.WelderId = jot.BackingWelderId
LEFT JOIN dbo.Welder_Welder coverWelder ON coverWelder.WelderId = jot.CoverWelderId
LEFT JOIN dbo.Base_Material mat ON mat.MaterialId = jot.Material1Id
WHERE BatchTrustItem.TrustBatchId=@TrustBatchId and BatchTrustItem.IsCancelTrust is null
GO

