SELECT Person.PersonId,
	Person.CardNo,
	Person.PersonName,
	Person.Sex,
	(Case Person.Sex WHEN '1' THEN '男' WHEN '2' THEN '女' ELSE '' END) AS SexName,
	Person.IdentityCard,
	Person.Address,
	Person.ProjectId,
	Person.UnitId,
	Person.Birthday,
	Person.TeamGroupId,
	Person.WorkAreaId,
	Person.WorkPostId,
	Person.InTime,
	Person.OutTime,
	Person.OutResult,
	Person.Telephone,
	Person.PositionId,
	Person.PostTitleId,
	Person.PhotoUrl,
	Person.IsUsed,
	(CASE Person.IsUsed WHEN 1 THEN '是' ELSE '否' END) AS IsUsedName, 
	Person.IsUsedType,
	(CASE Person.IsUsedType WHEN '1' THEN '在岗' WHEN '0' THEN '离岗' ELSE '待审核' END) AS IsUsedTypeName, 
	Person.IsCardUsed,
	(CASE Person.IsCardUsed WHEN 'TRUE' THEN '是' ELSE '否' END) AS IsCardUsedName,
	Person.PersonIndex,
	Project.ProjectCode,
	Project.ProjectName,
	Unit.UnitCode,
	Unit.UnitName,
	TeamGroup.TeamGroupName,
	--WorkArea.UnitWorkCode AS WorkAreaCode,
	WorkAreaCode= STUFF((SELECT ',' + UnitWorkCode FROM dbo.WBS_UnitWork where PATINDEX('%,' + RTRIM(UnitWorkId) + ',%',',' +Person.WorkAreaId + ',')>0 FOR XML PATH('')), 1, 1,''),
	--WorkArea.UnitWorkName AS WorkAreaName
	WorkAreaName= STUFF((SELECT ',' + UnitWorkName FROM dbo.WBS_UnitWork where PATINDEX('%,' + RTRIM(UnitWorkId) + ',%',',' +Person.WorkAreaId + ',')>0 FOR XML PATH('')), 1, 1,''),
	Post.WorkPostName,
	Position.PositionName,
	Title.PostTitleName,
	Depart.DepartName,
	Depart.DepartId,
	Post.PostType,
	Post.IsHsse,
	(SELECT COUNT(*) FROM EduTrain_TrainRecordDetail T WHERE T.PersonId=Person.PersonId) AS TrainCount,
	Person.AuditorId,
	sysUser.UserName AS AuditorName,
	Person.AuditorDate,
	Person.IDCardUrl,
	Person.IsForeign,
	case when Person.IsForeign=1 then '是' else '否' end as IsForeignStr,
	Person.IdcardType,
	BasicData1.dictName as IdcardTypeName,
	Person.IdcardStartDate,
	Person.IdcardEndDate,
	Person.IdcardForever,
	case when Person.IdcardForever='Y' then '是' else '否' end as IdcardForeverStr,
	Person.PoliticsStatus,
	BasicData2.dictName as PoliticsStatusName,
	Person.IdcardAddress,
	Person.Nation,
	BasicData3.dictName as NationName,
	Person.EduLevel,
	BasicData4.dictName as EduLevelName,
	Person.MaritalStatus,
	BasicData5.dictName as MaritalStatusName,
	Person.CountryCode,
	Country.cname as CountryName,
	Person.ProvinceCode,
	City.cname as ProvinceName,
	Person.MainCNProfessionalId,
	CNProfessional.ProfessionalName as MainCNProfessionalName,
	Person.ViceCNProfessionalId,
	'' as ViceCNProfessionalName,
	Person.IsOutside,
	case when Person.IsOutside=1 then '是' else '否' end as IsOutsideStr
FROM SitePerson_Person AS Person
LEFT JOIN Base_Project AS Project ON Project.ProjectId=Person.ProjectId
LEFT JOIN Base_Unit AS Unit ON Unit.UnitId = Person.UnitId
LEFT JOIN ProjectData_TeamGroup AS TeamGroup ON TeamGroup.TeamGroupId=Person.TeamGroupId
--LEFT JOIN WBS_UnitWork AS WorkArea ON WorkArea.UnitWorkId = Person.WorkAreaId
LEFT JOIN Base_WorkPost AS Post ON Post.WorkPostId = Person.WorkPostId
LEFT JOIN Base_Position AS Position ON Position.PositionId = Person.PositionId
Left JOIN Base_PostTitle AS Title ON Title.PostTitleId = Person.PostTitleId
Left JOIN Base_Depart AS Depart ON Depart.DepartId = Person.DepartId
Left JOIN Sys_User AS sysUser ON sysUser.UserId = Person.AuditorId
Left JOIN RealName_BasicData AS BasicData1 ON BasicData1.dictCode = Person.IdcardType
Left JOIN RealName_BasicData AS BasicData2 ON BasicData2.dictCode = Person.PoliticsStatus
Left JOIN RealName_BasicData AS BasicData3 ON BasicData3.dictCode = Person.Nation
Left JOIN RealName_BasicData AS BasicData4 ON BasicData4.dictCode = Person.EduLevel
Left JOIN RealName_BasicData AS BasicData5 ON BasicData5.dictCode = Person.MaritalStatus
Left JOIN RealName_Country AS Country ON Country.CountryId = Person.CountryCode
Left JOIN RealName_City AS City ON City.provinceCode = Person.ProvinceCode
Left JOIN Base_CNProfessional AS CNProfessional ON CNProfessional.CNProfessionalId = Person.MainCNProfessionalId





ALTER TABLE SitePerson_Person
ADD IsUsedType char(1) COLLATE Chinese_PRC_CI_AS  NULL
go

EXEC sp_addextendedproperty
'MS_Description', N'人员状态',
'SCHEMA', N'dbo',
'TABLE', N'SitePerson_Person',
'COLUMN', N'IsUsedType'
GO

