-- 插入PACS报告
INSERT INTO peisdb.dbo.Report([EID], [ReportNo] , [Class2], [Title], [Reporter], [ReportTime], [Description], [Summary]   ) 
SELECT 
 [EID]
 ,B.ID AS  [ReportNo] 
 , AccessionNumber AS [Class2]
 , NAME AS  [Title] 
 , PacsVerifyOperName AS [Reporter]
 , PacsVerifyOperDate AS [ReportTime]
 , PacsText1  AS [Description]
 , PacsText2 AS [Summary] 
  FROM PEIS.DBO.EnrollmentApplicationsheet A(NOLOCK)
  LEFT JOIN peisdb.dbo.Enrollment_FeeItem B ON A.EnrollmentId = B.EID AND A.NAME=B.FeeItemName AND A.DepartmentName=B.DeptName
 WHERE A.ApplicationType=1 AND A.SendOperDate IS NOT NULL AND A.RemoveOperDate IS NULL AND EID IS NOT NULL AND PacsVerifyOperDate IS NOT NULL
 AND  NOT EXISTS(SELECT 1 FROM PEISDB.DBO.Report WHERE EID=B.EID AND ReportNo=B.ID)
        
-- 插入图片
INSERT INTO [dbo].[Exam_PacsImage]( [EID], [ReportNo], [Image] )   
SELECT B.EnrollmentId AS EID,A.AccessionNumber AS [ReportNo] ,A.Picture FROM  PEIS.DBO.EnrollmentApplicationSheetPicture A
LEFT JOIN  PEIS.DBO.EnrollmentApplicationSheet B ON B.Id=A.EnrollmentApplicationSheetId WHERE A.AccessionNumber IS NOT NULL
-- 更新ReportNo
UPDATE [dbo].[Exam_PacsImage] SET ReportNo=T.ReportNo
FROM (
SELECT  A.ID,B.ReportNo  FROM  [dbo].[Exam_PacsImage] A
LEFT JOIN peisdb.dbo.Report B ON A.EID=B.EID AND A.ReportNo=B.Class2
WHERE B.ReportNo IS NOT NULL) T WHERE [Exam_PacsImage].ID=T.ID

-- pacs 报告图片 
-- SELECT TOP 10 * FROM REPORT A
-- WHERE A.ReportImage IS NULL
	 
--SELECT * FROM  	PACS.DICOMSERVER.DBO.PEIS_PacsResult  WHERE PatientCode='69090'
-- -- OR Report.Class2=CAST(T.AccessionNumber AS BIGINT))