use qingdao_fayuanxiuse


SELECT * FROM TWeChatUser WHERE FOpenId = 'o9qXw0Pwm2q0Uly_g5fpu1DJ_EYY'
SELECT * FROM TWeChatUser WHERE FAppId = 'o9qXw0Pwm2q0Uly_g5fpu1DJ_EYY'

SELECT * FROM TWeChatUser WHERE FOpenId = 'o9qXw0ACYrERyefB2ejWwJA2xwUo'
SELECT * FROM TWeChatUser WHERE FAppId = 'o9qXw0ACYrERyefB2ejWwJA2xwUo'

SELECT * FROM TWeChatUser WHERE FOpenId = 'o9qXw0CPFNsztTEtBn_a8joLcWNc'

SELECT * FROM TWeChatUser WHERE FOpenId IN( 'o9qXw0CPFNsztTEtBn_a8joLcWNc','o9qXw0ACYrERyefB2ejWwJA2xwUo','o9qXw0Pwm2q0Uly_g5fpu1DJ_EYY')

o9qXw0Pwm2q0Uly_g5fpu1DJ_EYY


 



ALTER TABLE TWeChatUser DROP   CONSTRAINT PK_WECHAT_USER 
GO

ALTER TABLE TWeChatUser WITH NOCHECK ADD   
CONSTRAINT PK_WECHAT_USER PRIMARY KEY NONCLUSTERED   
(  
FUserType, FAppId, FOpenId, FMobile
)  
GO 

 drop table #User1
 
SELECT 'Employee' AS FUserType,FOpenId AS FMobile,FMobile AS FAppId,FAppId AS FOpenId,FCreateTime ,FActive
INTO #User1
FROM TWeChatUser AS A
WHERE FUserType = 'Employee'
AND FOpenId LIKE '1%' 

UNION ALL

SELECT 'Employee' AS FUserType,FMobile AS FMobile,FOpenId AS FAppId,FAppId AS FOpenId,FCreateTime ,FActive
 FROM TWeChatUser AS A
WHERE FUserType = 'Employee'
AND FOpenId LIKE 'wx86d2ff4d5b7e61bc' 

 
UPDATE A 
   SET A.FCreateTime = B.FCreateTime 
 FROM TWeChatUser AS A,#User1 AS B
WHERE A.FMobile = B.FMobile
AND A.FAppId = B.FAppId
AND A.FOpenId = B.FOpenId
AND A.FCreateTime < B.FCreateTime

INSERT TWeChatUser(FUserType,FMobile,FAppId,FOpenId,FCreateTime,FActive)
SELECT FUserType,FMobile,FAppId,FOpenId,FCreateTime,FActive FROM #User1 AS A
WHERE NOT EXISTS(SELECT 1 FROM TWeChatUser AS B
WHERE A.FMobile = B.FMobile AND A.FAppId = B.FAppId AND A.FOpenId = B.FOpenId)

DELETE TWeChatUser 
WHERE FUserType = 'Employee'
AND FOpenId LIKE '1%' 

DELETE TWeChatUser 
WHERE FUserType = 'Employee'
AND FOpenId LIKE 'wx86d2ff4d5b7e61bc' 


UPDATE A
   SET A.FCreateTime = B.FLoginTime
  FROM TWeChatUser AS A,(
select FWeChatOpenId,FMobile,FLoginTime from
(
select *, ROW_NUMBER() over(partition by FWeChatOpenId order by FLoginTime ASC) as rowNum
from TLoginSession
) ranked
where ranked.rowNum < 2
AND FUserType = 'MOBEmployee'
) AS B
WHERE B.FWeChatOpenId = A.FOpenId
AND A.FMobile = B.FMobile
AND A.FCreateTime > B.FLoginTime




UPDATE TEmployeeAttendLog SET FWeChatOpenId = FDeviceId  ,FDeviceId = ''
WHERE FDeviceId != '' AND FDeviceId NOT IN(SELECT FDeviceId FROM TAttendDevice)




SELECT * FROM TWeChatUser
WHERE FUserType = 'Employee'
ORDER BY FOpenId

WITH T AS (
SELECT A.FMobile,A.FOpenId,A.FCreateTime 
FROM TWeChatUser AS A,(
SELECT FOpenId, MIN(CONVERT(VARCHAR,FCreateTime,20) + FMobile) AS FKey
FROM TWeChatUser
WHERE FUserType = 'Employee'
GROUP BY FOpenId
) AS B
WHERE A.FOpenId = B.FOpenId
AND A.FUserType = 'Employee'
AND CONVERT(VARCHAR,A.FCreateTime,20) + A.FMobile  = B.FKey
)
SELECT * FROM T


SELECT * FROM TEmployeeAttendLog

order by ftime desc


AND A.FOpenId = 'o9qXw0Pwm2q0Uly_g5fpu1DJ_EYY'

SELECT CONVERT(VARCHAR,GETDATE(),20)