

---修改安全检查视图(添加数据来源字段Registration.DataSource)
ALTER VIEW [dbo].[View_Hazard_HazardRegister] AS 
SELECT Registration.HazardRegisterId, 
	Registration.HazardCode, 
	Registration.RegisterDate, 
	Registration.RegisterDef, 
	Registration.Rectification, 
	Registration.Place, 
	Registration.ResponsibleUnit, 
	Registration.Observer, 
	Registration.HazardValue,
	case when Registration.HazardValue='3' then '重大' else '一般' end as Risk_Level,
	Registration.RectifiedDate, 
	Registration.ProjectId, 
	Project.UnitId AS PUnitId,
	Project.ProjectState as ProjectState,
	null as isDelete,
	ProjectUnit.UnitName AS PUnitName,
	case when ProjectUnit.UnitId='d72a27c9-4ba9-41c5-ab0b-c010409f20f2' then 1 else 0 end as IsThisUnit,
	ProjectUnit.IsBranch as IsBranch,
	Project.ProjectAddress,
	Registration.states,
	Registration.IsEffective,
	Registration.ResponsibleMan,
	Registration.CheckManId,
	Registration.CheckTime,
	Registration.RectificationPeriod,
	Registration.ImageUrl,
	Registration.RectificationImageUrl,
	Registration.RectificationTime,
	Registration.ConfirmMan,
	Registration.ConfirmDate,
	Registration.HandleIdea, 
	Registration.CutPayment, 
	Registration.ProblemTypes, 
	Registration.CheckSpecialId,
	Registration.CheckItemDetailId,
	Registration.SupCheckItemSetId,
	Registration.CheckItemSetId,
	Registration.SafeSupervisionId,
	Registration.SafeSupervisionIsOK,
	Registration.WorkPackageId,
	Registration.WorkPackageName,
	Registration.IsUpdate,
	Project.ProjectName,
	Registration.CheckCycle,
	ISNULL(WorkArea.UnitWorkName,'') AS WorkAreaName,
	Unit.UnitName  as ResponsibilityUnitName,
	Unit.CollCropCode as ResponsibilityUnitCollCropCode,
	User1.PersonName AS ResponsibilityManName,
	ISNULL(User1.Telephone,'') AS ResponsibilityManTel,
	User2.PersonName AS CheckManName,
	ISNULL(User2.Telephone,'') AS  CheckManTel,
	User2.UnitId AS SendUnitId,
	User3.PersonName AS ConfirmManName,
	ISNULL(User3.Telephone,'') AS  ConfirmManTel,
	User4.PersonName AS ResponsibilityManName2,
	CCManNames = STUFF(( SELECT ',' + PersonName FROM Person_Persons
				    where PATINDEX('%,' + RTRIM(Person_Persons.PersonId) + ',%',',' + Registration.CCManIds + ',')>0
					ORDER BY PATINDEX('%,' + RTRIM(Registration.CCManIds) + ',%',',' + Registration.CCManIds + ',')
					FOR XML PATH('')), 1, 1,''),
	ISNULL(User4.Telephone,'') AS  ResponsibilityMan2Tel,
	(CASE WHEN Registration.states='1' and (Registration.SafeSupervisionIsOK is null OR Registration.SafeSupervisionIsOK=0) THEN '待整改' 
	WHEN Registration.states='1' and Registration.SafeSupervisionIsOK=1 THEN '合格' 
	WHEN Registration.states='2' THEN '已整改-待复查验收' 
	WHEN Registration.states='3' THEN '已闭环' 
	ELSE '已作废' END ) AS StatesStr,
	RegisterTypes.RegisterTypesId,
	RegisterTypes.RegisterTypesName,
	Registration.DIC_ID,
	Registration.CCManIds,
	Registration.DataSource,
	Registration.RegisterTypes2Id ,
	RegisterTypes2.RegisterTypesName as RegisterTypes2Name,
	Registration.RegisterTypes3Id ,
	RegisterTypes3.RegisterTypesName as RegisterTypes3Name,
	Registration.RegisterTypes4Id ,
	RegisterTypes4.RegisterTypesName as RegisterTypes4Name,
	Registration.Requirements
FROM dbo.HSSE_Hazard_HazardRegister AS Registration
LEFT JOIN dbo.Base_Project AS Project ON Project.ProjectId = Registration.ProjectId
LEFT JOIN dbo.Base_Unit AS ProjectUnit ON Project.UnitId = ProjectUnit.UnitId
LEFT JOIN dbo.WBS_UnitWork AS WorkArea ON WorkArea.UnitWorkId = Registration.Place 
LEFT JOIN dbo.HSSE_Hazard_HazardRegisterTypes AS RegisterTypes ON RegisterTypes.RegisterTypesId = Registration.RegisterTypesId 
LEFT JOIN dbo.HSSE_Hazard_HazardRegisterTypes AS RegisterTypes2 ON RegisterTypes2.RegisterTypesId = Registration.RegisterTypes2Id 
LEFT JOIN dbo.HSSE_Hazard_HazardRegisterTypes AS RegisterTypes3 ON RegisterTypes3.RegisterTypesId = Registration.RegisterTypes3Id 
LEFT JOIN dbo.HSSE_Hazard_HazardRegisterTypes AS RegisterTypes4 ON RegisterTypes4.RegisterTypesId = Registration.RegisterTypes4Id 
LEFT JOIN dbo.Base_Unit AS Unit ON Unit.UnitId = Registration.ResponsibleUnit
LEFT JOIN dbo.Person_Persons AS User1 ON User1.PersonId = Registration.ResponsibleMan   
LEFT JOIN dbo.Person_Persons AS User2 ON User2.PersonId = Registration.CheckManId
LEFT JOIN dbo.Person_Persons AS User3 ON User3.PersonId = Registration.ConfirmMan
LEFT JOIN dbo.Person_Persons AS User4 ON User4.PersonId = Registration.ResponsibleMan2
go
