D_Insert.sql 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. --漏报抽取任务表
  2. if not exists(select * from sysobjects where id=object_id('D_ReportLeak_TaskSettings'))
  3. BEGIN
  4. EXEC dbo.sp_executesql @statement = N'
  5. SET ANSI_NULLS ON
  6. SET QUOTED_IDENTIFIER ON
  7. CREATE TABLE [dbo].[D_ReportLeak_TaskSettings](
  8. [ID] [int] IDENTITY(1,1) NOT NULL,
  9. [TaskDay] [int] NOT NULL,
  10. [TaskHour] [int] NOT NULL,
  11. [TaskMinute] [int] NOT NULL,
  12. [TaskEnabled] [bit] NOT NULL,
  13. [StartDate] [datetime] NOT NULL,
  14. [CurrentDate] [datetime],
  15. [ExtractDays] [int] NOT NULL
  16. CONSTRAINT [PK_D_ReportLeak_TaskSettings] PRIMARY KEY CLUSTERED
  17. (
  18. [ID] ASC
  19. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  20. ) ON [PRIMARY]
  21. '
  22. --新漏报任务设置数据迁移
  23. insert into D_ReportLeak_TaskSettings(TaskDay,TaskHour,TaskMinute,TaskEnabled,StartDate,CurrentDate,ExtractDays )
  24. select AutoDbBakEveryDay,AutoDbBakAtHour,AutoDbBakAtMinute,AutoDbBakEnabled,
  25. (select top 1 value from Q_Options where Name='LeakStartDate'),
  26. (select top 1 value from Q_Options where Name='LeakCurrentDate'),
  27. (select top 1 value from Q_Options where Name='LeakLeakDay')
  28. from DataBaseAutoBak where id=5
  29. --删除原有旧漏报任务设置
  30. delete from DataBaseAutoBak where id=5
  31. delete from Q_Options where Name='LeakStartDate'
  32. delete from Q_Options where Name='LeakCurrentDate'
  33. delete from Q_Options where Name='LeakLeakDay'
  34. End
  35. --ReportLeakJoinLogs 添加 LeakJobId
  36. if not exists(select * from syscolumns where id=object_id('ReportLeakJoinLogs') and name='LeakJobId')
  37. BEGIN
  38. EXEC dbo.sp_executesql @statement = N'alter table ReportLeakJoinLogs add LeakJobId int NULL'
  39. END
  40. --增加房颤和脑梗死,重复病例设置
  41. if not exists(select * from D_RepeatReportSetting where ReportClass =32 and RepeatReportClass=6)
  42. BEGIN
  43. INSERT [dbo].[D_RepeatReportSetting] ([ReportClass], [RepeatReportClass], [Icd9Code], [Icd10Code], [Enabled]) VALUES (32, 6, NULL, NULL, 0)
  44. END
  45. --DVT和CS重复,优先CS
  46. --DVT和EP重复,优先EP
  47. --DVT和SEP重复,优先SEP
  48. --STK和SEP出重复,优先SEP
  49. --ICH和SEP重复,优先SEP
  50. if not exists(select * from D_RepeatReportSetting where ReportClass =12 and RepeatReportClass=10)
  51. BEGIN
  52. INSERT [dbo].[D_RepeatReportSetting] ([ReportClass], [RepeatReportClass], [Icd9Code], [Icd10Code], [Enabled]) VALUES (12, 10, NULL, NULL, 0)
  53. INSERT [dbo].[D_RepeatReportSetting] ([ReportClass], [RepeatReportClass], [Icd9Code], [Icd10Code], [Enabled]) VALUES (12, 20, NULL, NULL, 0)
  54. INSERT [dbo].[D_RepeatReportSetting] ([ReportClass], [RepeatReportClass], [Icd9Code], [Icd10Code], [Enabled]) VALUES (12, 35, NULL, NULL, 0)
  55. INSERT [dbo].[D_RepeatReportSetting] ([ReportClass], [RepeatReportClass], [Icd9Code], [Icd10Code], [Enabled]) VALUES (6, 35, NULL, NULL, 0)
  56. INSERT [dbo].[D_RepeatReportSetting] ([ReportClass], [RepeatReportClass], [Icd9Code], [Icd10Code], [Enabled]) VALUES (26, 35, NULL, NULL, 0)
  57. END
  58. --增加慢阻肺和房颤重复病例除外
  59. if not exists(select * from D_RepeatReportSetting where ReportClass =32 and RepeatReportClass=11)
  60. BEGIN
  61. INSERT [dbo].[D_RepeatReportSetting] ([ReportClass], [RepeatReportClass], [Icd9Code], [Icd10Code], [Enabled]) VALUES (32, 11, NULL, NULL, 0)
  62. END
  63. --是否允许填报人员填报数据时标记无需上报
  64. if not exists(select * from Q_Options where Name='CanReportChangeStatus')
  65. BEGIN
  66. INSERT [dbo].[Q_Options] ([Name], [Value]) VALUES ('CanReportChangeStatus','True')
  67. END
  68. if not exists(select * from Report_NoReportNote where [ReportClass]=36 and [Note] = '同一疾病30日内重复入院')
  69. BEGIN
  70. INSERT [dbo].[Report_NoReportNote] ([ReportClass], [Note], [Selected])
  71. VALUES (36, N'同一疾病30日内重复入院', 1)
  72. END
  73. --添加科室关系对应表
  74. if not exists(select * from sysobjects where id=object_id('D_DeptDictMatch'))
  75. BEGIN
  76. EXEC dbo.sp_executesql @statement = N'
  77. SET ANSI_NULLS ON
  78. SET QUOTED_IDENTIFIER ON
  79. CREATE TABLE [dbo].[D_DeptDictMatch](
  80. [ID] [int] IDENTITY(1,1) NOT NULL,
  81. [UserDeptId] [int] NOT NULL,
  82. [CYDept] [nvarchar](max) NULL,
  83. CONSTRAINT [PK_D_DeptDict] PRIMARY KEY CLUSTERED
  84. (
  85. [ID] ASC
  86. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  87. ) ON [PRIMARY]'
  88. EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'用户科室id' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'D_DeptDictMatch', @level2type=N'COLUMN',@level2name=N'UserDeptId'
  89. EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'出院科室' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'D_DeptDictMatch', @level2type=N'COLUMN',@level2name=N'CYDept'
  90. END
  91. --血透腹透如果和房颤重复,优先上报血透腹透
  92. if not exists(select * from D_RepeatReportSetting where ReportClass =32 and RepeatReportClass=44)
  93. BEGIN
  94. INSERT [dbo].[D_RepeatReportSetting] ([ReportClass], [RepeatReportClass], [Icd9Code], [Icd10Code], [Enabled]) VALUES (32, 44, NULL, NULL, 0)
  95. INSERT [dbo].[D_RepeatReportSetting] ([ReportClass], [RepeatReportClass], [Icd9Code], [Icd10Code], [Enabled]) VALUES (32, 45, NULL, NULL, 0)
  96. END
  97. --不根据出院日期判断重复上报 终末期肾病血液透析·HD、终末期肾病腹膜透析·DPD
  98. if not exists(select * from Q_Options where Name='IsRepeatNotByCYDate')
  99. BEGIN
  100. INSERT [dbo].[Q_Options] ([Name], [Value]) VALUES ('IsRepeatNotByCYDate','False')
  101. END
  102. --同一疾病30天内重复入院规则设置
  103. if not exists(select * from Q_Options where Name='IsRepeatRY30Setting')
  104. BEGIN
  105. INSERT [dbo].[Q_Options] ([Name], [Value]) VALUES ('IsRepeatRY30Setting','1')
  106. END
  107. --VTE抽取数据时,在原有的抽取规则下,排除ICD9字段不为空的数据
  108. if not exists(select * from Q_Options where Name='VTEExtractByIcd9Null')
  109. BEGIN
  110. INSERT [dbo].[Q_Options] ([Name], [Value]) VALUES ('VTEExtractByIcd9Null','False')
  111. END
  112. --增加下一次运行时间字段NextRun,缺省值null
  113. if not exists(select * from syscolumns where id=object_id('DataBaseAutoBak') and name='NextRun')
  114. EXEC dbo.sp_executesql @statement = N'alter table DataBaseAutoBak add [NextRun] DateTime NULL'
  115. if not exists(select * from syscolumns where id=object_id('D_ReportLeak_TaskSettings') and name='NextRun')
  116. EXEC dbo.sp_executesql @statement = N'alter table D_ReportLeak_TaskSettings add [NextRun] DateTime NULL'
  117. IF NOT EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[D_View_ReportStatsData]'))
  118. EXEC dbo.sp_executesql @statement = N'CREATE VIEW [dbo].[D_View_ReportStatsData]
  119. AS
  120. SELECT
  121. convert(int,ReportClass) as ReportClass
  122. ,[HNumber] as [Base_HNumber] --住院号
  123. ,[VisitId] as [Base_VisitId] --住院次数
  124. ,( case when [LYFS] in (''a'',''1'',''A'') then ''a''
  125. when [LYFS] in (''b'',''2'',''B'') then ''b''
  126. when [LYFS] in (''c'',''3'') then ''c''
  127. when [LYFS] in (''d'',''4'') then ''d''
  128. when [LYFS] in (''e'',''5'') then ''e''
  129. when [LYFS] in (''f'',''9'',''oth'') then ''oth''
  130. else [LYFS] end) [Base_LYFS] --离院方式
  131. ,[RYDate] as [Base_RYDate]--入院时间
  132. ,[CYDate] as [Base_CYDate]--出院时间
  133. ,[BirthDate] as [Base_CSRQ]--出生日期
  134. ,Report_Leak.CYDept as [CYDept]--出院科室
  135. ,[ZGDoctor] as [ZGDoctor]--质控医生
  136. ,[BGName] --上报医师
  137. ,[DealReport] -- 未报
  138. ,[TotalFei] as [ZFY]--总费用
  139. ,null [CM_2_1] --手术后并发症
  140. ,null as Add_TJTime --提交时间
  141. ,[D_DeptDictMatch].UserDeptId as Add_OperDeptId --填报科室
  142. ,null as Add_UserName --填报员
  143. ,null as SBWorkDayCha --上报时间差
  144. ,null as ReportLeak_ID --数据管理与漏报关联关系
  145. ,null as status --状态
  146. FROM [Report_Leak]
  147. left outer join D_DeptDictMatch on D_DeptDictMatch.CYDept = Report_Leak.CYDept
  148. where DealReport in(0,2)and (IsDelete is null or IsDelete=0)
  149. union all
  150. SELECT
  151. ReportClass
  152. ,[Base_HNumber] as [Base_HNumber]--住院号
  153. ,[Base_VisitId] as [Base_VisitId]--住院次数
  154. ,( case when [Base_LYFS] in (''a'',''1'') then ''a''
  155. when [Base_LYFS] in (''b'',''2'') then ''b''
  156. when [Base_LYFS] in (''c'',''3'') then ''c''
  157. when [Base_LYFS] in (''d'',''4'') then ''d''
  158. when [Base_LYFS] in (''e'',''5'') then ''e''
  159. when [Base_LYFS] in (''f'',''9'',''oth'') then ''oth''
  160. else [Base_LYFS] end) [Base_LYFS] --离院方式--离院方式
  161. ,[Base_RYDate]as [Base_RYDate]--入院时间
  162. ,(case
  163. when REPORTCLASS in(44,45) and [Base_CYDate] is null
  164. then Add_TJTime -- HD、DPD提交时间
  165. when REPORTCLASS=51 and [Base_CYDate] is not null then
  166. Base_CYDate --OIT 出院时间
  167. when REPORTCLASS=51 and [Base_CYDate] is null and JSON_VALUE(DataInfo,''$."CM-0-2-4-2"'') is not null
  168. then JSON_VALUE(DataInfo,''$."CM-0-2-4-2"'') --OIT 填报页面出院时间
  169. when REPORTCLASS=51 and [Base_CYDate] is null and (JSON_VALUE(DataInfo,''$."CM-0-2-4-2"'') is null or JSON_VALUE(DataInfo,''$."CM-0-2-4-2"'') ='''' )
  170. then Add_TJTime --OIT提交时间
  171. else [Base_CYDate] end
  172. )
  173. as [Base_CYDate]--出院时间
  174. ,[Base_CSRQ] as [Base_CSRQ]--出生日期
  175. ,null as [CYDept] --出院科室
  176. ,[Base_ZGYSName] as [ZGDoctor]--质控医生
  177. ,[Base_BGName] as [BGName] --上报医生
  178. ,1 as [DealReport] --已报
  179. ,JSON_VALUE(DataInfo,''$."CM-6-1"'') as [ZFY]--总费用
  180. ,case when ReportClass=10 then JSON_VALUE(DataInfo,''$."CS-6-1-1"'')
  181. when ReportClass=39 then JSON_VALUE(DataInfo,''$."PACG-7-1-1"'')
  182. else JSON_VALUE(DataInfo,''$."CM-2-1"'')
  183. end as [CM_2_1] --手术后并发症
  184. ,Add_TJTime --提交时间
  185. ,Add_OperDeptId --填报科室
  186. ,Add_UserName --填报员
  187. ,SBWorkDayCha as SBWorkDayCha --上报时间差
  188. ,ReportLeak_ID --数据管理与漏报关联关系
  189. ,Status --状态
  190. FROM [D_MedicalRecord]
  191. where Status<>0 and (BackUpFlag is null or BackUpFlag=0)
  192. '
  193. if not exists(select * from syscolumns where id=object_id('D_Disease_Category') and name='DiseaseDifficulty')
  194. EXEC dbo.sp_executesql @statement = N'alter table D_Disease_Category add DiseaseDifficulty int NULL'
  195. --添加 标记锁定 权限
  196. if not exists (select * from [Q_Quanxian] where ID = 31)
  197. begin
  198. SET IDENTITY_INSERT [dbo].[Q_Quanxian] ON
  199. INSERT [dbo].[Q_Quanxian] ([Id], [QuanxianName], [Sort]) VALUES (31, N'标记锁定', 31)
  200. SET IDENTITY_INSERT [dbo].[Q_Quanxian] OFF
  201. end
  202. --D_MedicalRecord 添加锁定标记
  203. if not exists(select * from syscolumns where id=object_id('D_MedicalRecord') and name='IsLock')
  204. EXEC dbo.sp_executesql @statement = N'alter table D_MedicalRecord add IsLock bit NULL'
  205. --增加 sep和af重复优先上报af
  206. if not exists(select * from D_RepeatReportSetting where ReportClass =35 and RepeatReportClass=32)
  207. BEGIN
  208. INSERT [dbo].[D_RepeatReportSetting] ([ReportClass], [RepeatReportClass], [Icd9Code], [Icd10Code], [Enabled]) VALUES (35, 32, NULL, NULL, 0)
  209. END
  210. --是否开启预抽取
  211. if not exists(select * from Q_Options where Name='ReportLeakProExtract')
  212. BEGIN
  213. INSERT [dbo].[Q_Options] ([Name], [Value]) VALUES ('ReportLeakProExtract','True')
  214. END
  215. --添加疑似病种表
  216. if not exists(select * from sysobjects where id=object_id('D_SuspectRecord'))
  217. BEGIN
  218. EXEC dbo.sp_executesql @statement = N'
  219. SET ANSI_NULLS ON
  220. SET QUOTED_IDENTIFIER ON
  221. CREATE TABLE [dbo].[D_SuspectRecord](
  222. [ID] [int] IDENTITY(1,1) NOT NULL,
  223. [ReportClass] [int] NOT NULL,
  224. [HNumber] [nvarchar](50) NOT NULL,
  225. [VisitId] [int] NULL,
  226. [PatientName] [nvarchar](50) NULL,
  227. [Sex] [nvarchar](50) NULL,
  228. [BirthDate] [datetime] NULL,
  229. [Age] [nvarchar](50) NULL,
  230. [RYDate] [datetime] NULL,
  231. [CYDate] [datetime] NULL,
  232. [DoctorName] [nvarchar](50) NULL,
  233. [RYDept] [nvarchar](50) NULL,
  234. [JudgCondition] [nvarchar](max) NULL,
  235. [QualityPoint] [nvarchar](max) NULL,
  236. [GainDate] [datetime] NOT NULL,
  237. CONSTRAINT [PK_D_SuspectReport] PRIMARY KEY CLUSTERED
  238. (
  239. [ID] ASC
  240. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  241. ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]'
  242. END