
---   同步巫工数据库 sql 语句
---   BLV_RCU_DB 同服务器下存在  BLW  名字的巫工数据库服务器对象 
---   即可BLV_RCU_DB数据库中可一键执行，执行过程大概 5s  会自动添加 同步表 NEW_TABLE_UPDATE_TIME_MARK
---   文中部分注释是为了 事务 请勿随意取消注释
---   最后更新时间:2022-04-12 14:13
---   author:陈伟

BEGIN TRAN 

use BLV_RCU_DB;

-- 创建 同步基础表
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'NEW_TABLE_UPDATE_TIME_MARK') AND type in (N'U'))
begin
-- 如果需要创建 删除原表格请取消注释 NEW_TABLE_UPDATE_TIME_MARK
--  drop table NEW_TABLE_UPDATE_TIME_MARK
print '已经存在NEW_TABLE_UPDATE_TIME_MARK表格'
end
ELSE
BEGIN
create table NEW_TABLE_UPDATE_TIME_MARK(
ID int identity(1,1) primary key  not null ,
NEW_DB_TABLE_NAME NVARCHAR(50) NOT NULL ,
LAST_UPDATE_TIME datetime  null default null

)
END
--go

-- 插入同步表数据
-- 插入酒店表同步
IF EXISTS(SELECT * FROM NEW_TABLE_UPDATE_TIME_MARK WHERE  NEW_DB_TABLE_NAME ='TBL_HOTEL_GROUP_INFO')
BEGIN
insert NEW_TABLE_UPDATE_TIME_MARK values ('TBL_HOTEL_GROUP_INFO',null)
END
-- 插入酒店表同步
IF EXISTS(SELECT * FROM NEW_TABLE_UPDATE_TIME_MARK WHERE  NEW_DB_TABLE_NAME ='TBL_HOTEL_BASIC_INFO')
BEGIN
insert NEW_TABLE_UPDATE_TIME_MARK values ('TBL_HOTEL_BASIC_INFO',null)
END

-- 插入房型表同步
IF EXISTS(SELECT * FROM NEW_TABLE_UPDATE_TIME_MARK WHERE  NEW_DB_TABLE_NAME ='TBL_ROOM_TYPE_LIST')
BEGIN
insert NEW_TABLE_UPDATE_TIME_MARK values ('TBL_ROOM_TYPE_LIST',null)
END

-- 插入房间表同步
IF EXISTS(SELECT * FROM NEW_TABLE_UPDATE_TIME_MARK WHERE  NEW_DB_TABLE_NAME ='TBL_ROOM_BASIC_INFO')
BEGIN
insert NEW_TABLE_UPDATE_TIME_MARK values ('TBL_ROOM_BASIC_INFO',null)
END

--go
-- select * from BLW.CRICS.[dbo].tb_Sys_HotelGroups
-- select * from NEW_TABLE_UPDATE_TIME_MARK
-- select * from sys.servers 
-- select * from TBL_HOTEL_GROUP_INFO
-- select * from NEW_TABLE_UPDATE_TIME_MARK
-- go


----------------------------------------------------  同步酒店组表开始
--TRUNCATE TABLE TBL_HOTEL_GROUP_INFO;

 DECLARE @LAST_UPDATE_TIME datetime

select @LAST_UPDATE_TIME = LAST_UPDATE_TIME from NEW_TABLE_UPDATE_TIME_MARK where NEW_DB_TABLE_NAME ='TBL_HOTEL_GROUP_INFO'

select @LAST_UPDATE_TIME

--select * from BLW.CRICS.[dbo].tb_Sys_HotelGroups  where @LAST_UPDATE_TIME IS NULL OR  LAST_MODIFIED_TIME > @LAST_UPDATE_TIME

---  更新数据

update dbo.TBL_HOTEL_GROUP_INFO set  HOTEL_GROUP_NAME = b.[Name] ,SORT = b.SORT,PARENT_ID = b.ParentID,UPDATE_TIMEMARK = GETDATE() from dbo.TBL_HOTEL_GROUP_INFO a,
 BLW.CRICS.[dbo].tb_Sys_HotelGroups  b where a.HOTEL_GROUP__ID = b.id and ( @LAST_UPDATE_TIME IS NULL OR  LAST_MODIFIED_TIME > @LAST_UPDATE_TIME)

 -- 标记已经删除数据

 update dbo.TBL_HOTEL_GROUP_INFO set  IsDel = 1 ,UPDATE_TIMEMARK = GETDATE()  where  HOTEL_GROUP__ID not in (select id from    BLW.CRICS.[dbo].tb_Sys_HotelGroups)



--- 插入新数据

SET IDENTITY_INSERT dbo.TBL_HOTEL_GROUP_INFO ON

INSERT TBL_HOTEL_GROUP_INFO (HOTEL_GROUP__ID,HOTEL_GROUP_NAME,PARENT_ID,SORt,KEY_IN,KEY_IN_DATETIME,APPROVE,APPROVE_DATE,UPDATE_TIMEMARK,REMARK)
 
select ID,[Name],ParentID,SORT,0,GETDATE(),null,null,GETDATE(),null from BLW.CRICS.[dbo].tb_Sys_HotelGroups 

where id not in (select HOTEL_GROUP__ID from TBL_HOTEL_GROUP_INFO) and ( @LAST_UPDATE_TIME IS NULL OR  LAST_MODIFIED_TIME > @LAST_UPDATE_TIME)

SET IDENTITY_INSERT dbo.TBL_HOTEL_GROUP_INFO off



-- 更新最后时间
update    NEW_TABLE_UPDATE_TIME_MARK  set LAST_UPDATE_TIME = GETDATE() where NEW_DB_TABLE_NAME ='TBL_HOTEL_GROUP_INFO'
 
 
 --SELECT * FROM TBL_HOTEL_GROUP_INFO
 --go
---------------------------------------------- 同步酒店组表同步结束

--go

-------------------------------------------------房型表同步开始


--TRUNCATE TABLE TBL_ROOM_TYPE_LIST;

--DECLARE @LAST_UPDATE_TIME datetime


-- 获取房间表最后同步时间
select @LAST_UPDATE_TIME = LAST_UPDATE_TIME from NEW_TABLE_UPDATE_TIME_MARK where NEW_DB_TABLE_NAME ='TBL_ROOM_TYPE_LIST'

 -- 标记已经删除数据
update  dbo.TBL_ROOM_TYPE_LIST set   IsDel = 1 ,UPDATE_TIMEMARK = GETDATE() where ROOM_TYPE_ID not in (select id from BLW.CRICS.[dbo].TB_ROOMTYPE  )



 
---  更新数据

update  dbo.TBL_ROOM_TYPE_LIST set  HOTEL_ID = b.[HOTELID]  ,ROOM_TYPE_NAME = b.[NAME],UPDATE_TIMEMARK = GETDATE() from dbo.TBL_ROOM_TYPE_LIST a,
 BLW.CRICS.[dbo].TB_ROOMTYPE  b where a.ROOM_TYPE_ID = b.ID and ( @LAST_UPDATE_TIME IS NULL OR  LAST_MODIFIED_TIME > @LAST_UPDATE_TIME)


--- 插入新数据

SET IDENTITY_INSERT dbo.TBL_ROOM_TYPE_LIST ON

INSERT TBL_ROOM_TYPE_LIST (ROOM_TYPE_ID,HOTEL_ID,ROOM_TYPE_NAME ,UPDATE_TIMEMARK,PROJECT_NUMBER)
 
select ID,[HOTELID],[NAME],GETDATE(), CODE from BLW.CRICS.[dbo].TB_ROOMTYPE 

where id not in (select ROOM_TYPE_ID from TBL_ROOM_TYPE_LIST) and ( @LAST_UPDATE_TIME IS NULL OR  LAST_MODIFIED_TIME > @LAST_UPDATE_TIME)

SET IDENTITY_INSERT dbo.TBL_ROOM_TYPE_LIST off



-- 更新最后时间
update    NEW_TABLE_UPDATE_TIME_MARK  set LAST_UPDATE_TIME = GETDATE() where NEW_DB_TABLE_NAME ='TBL_ROOM_TYPE_LIST'

--update    NEW_TABLE_UPDATE_TIME_MARK  set LAST_UPDATE_TIME = null where NEW_DB_TABLE_NAME ='TBL_ROOM_TYPE_LIST'
 
 --select * from NEW_TABLE_UPDATE_TIME_MARK

-- select * from TBL_ROOM_TYPE_LIST

 --select * from BLW.CRICS.[dbo].TB_ROOMTYPE

 -------------------------------------------------房型表同步结束
--go


 -------------------------------------------------酒店表同步开始

--TRUNCATE TABLE TBL_HOTEL_BASIC_INFO;

--DECLARE @LAST_UPDATE_TIME datetime

  
-- 获取酒店表最后同步时间
select @LAST_UPDATE_TIME = LAST_UPDATE_TIME from NEW_TABLE_UPDATE_TIME_MARK where NEW_DB_TABLE_NAME ='TBL_HOTEL_BASIC_INFO'

---  更新数据

update  dbo.TBL_HOTEL_BASIC_INFO 
set 
LOG_IMAGE =  b.LogoPath ,
HOTEL_NAME_CN = b.[name],
sort = b.sort,
HOTEL_NAME_EN = b.ename, 
KEY_IN_DATETIME = b.createddate,
[IsApprove] = b.IsApprove,
VALID_DATETIME = b.ValidateDate,
PROJECT_NUMBER = b.[code],
[STATUS] = b.[STATUS],
[KEY_nAME] =  CreatedBy,
DETAIL_ADD = b.[Address],
HOTEL_CONTACT_PHONE = b.[Phone],
HOTEL_GROUP = b.SysHotelGroupID,
UPDATE_TIMEMARK = GETDATE() from dbo.TBL_HOTEL_BASIC_INFO a,
 BLW.CRICS.[dbo].tb_Sys_Hotels  b where a.Idold =  b.ID and ( @LAST_UPDATE_TIME IS NULL OR  LAST_MODIFIED_TIME > @LAST_UPDATE_TIME)


 -- 标记已经删除数据

 update  dbo.TBL_HOTEL_BASIC_INFO set IsDel = 1 ,UPDATE_TIMEMARK = GETDATE() where Idold not in (select id from BLW.CRICS.[dbo].tb_Sys_Hotels  )


--- 插入新数据

--- SET IDENTITY_INSERT dbo.TBL_HOTEL_BASIC_INFO ON

INSERT TBL_HOTEL_BASIC_INFO (
LOG_IMAGE,
Idold,
HOTEL_NAME_CN,
sort,
HOTEL_NAME_EN, 
KEY_IN_DATETIME ,
[IsApprove] ,
VALID_DATETIME,
PROJECT_NUMBER,
[STATUS],
DETAIL_ADD,
HOTEL_CONTACT_PHONE ,
HOTEL_GROUP ,
UPDATE_TIMEMARK,
VALID_STATUS,
[KEY_Name]
)
select LogoPath, id, b.[name],b.sort,b.ename,createddate,IsApprove,ValidateDate,b.[code],b.[STATUS], b.[Address],b.[Phone],b.SysHotelGroupID,GETDATE(),0, CreatedBy  from BLW.CRICS.[dbo].tb_Sys_Hotels  b

where id not in (select Idold from TBL_HOTEL_BASIC_INFO) and ( @LAST_UPDATE_TIME IS NULL OR  LAST_MODIFIED_TIME > @LAST_UPDATE_TIME)

-- SET IDENTITY_INSERT dbo.TBL_HOTEL_BASIC_INFO off



-- 更新最后时间
update    NEW_TABLE_UPDATE_TIME_MARK  set LAST_UPDATE_TIME = GETDATE() where NEW_DB_TABLE_NAME ='TBL_HOTEL_BASIC_INFO'

--update    NEW_TABLE_UPDATE_TIME_MARK  set LAST_UPDATE_TIME = null where NEW_DB_TABLE_NAME ='TBL_HOTEL_BASIC_INFO'
 
 --select * from NEW_TABLE_UPDATE_TIME_MARK

-- select top 5 * from TBL_HOTEL_BASIC_INFO order by KEY_IN_DATETIME desc

-- select top 5 * from BLW.CRICS.[dbo].tb_Sys_Hotels order by createddate desc


 -------------------------------------------------酒店表同步结束
 

 --go


 -------------------------------------------------酒店表同步结束



--------------------------------------房间表 （主机表) 尚未同步


--TRUNCATE TABLE TBL_ROOM_BASIC_INFO;



--DECLARE @LAST_UPDATE_TIME datetime
  
-- 获取房间表最后同步时间
select @LAST_UPDATE_TIME = LAST_UPDATE_TIME from NEW_TABLE_UPDATE_TIME_MARK where NEW_DB_TABLE_NAME ='TBL_ROOM_BASIC_INFO'

---  更新数据
update  dbo.TBL_ROOM_BASIC_INFO 
set  
MAC = b.MAC,
HOTEL_ID = b.HOTELID ,
ROOM_NUMBER = b.ROOMNUMBER,
REMARK = b.REMARK,
ROOM_TYPE_ID = b.RoomTypeID,
UPDATE_TIMEMARK =  GETDATE()
from  TBL_ROOM_BASIC_INFO  a,
BLW.CRICS.[dbo].tb_Hosts b where a.ROOM_ID =  b.id and ( @LAST_UPDATE_TIME IS NULL OR  b.LAST_MODIFIED_TIME > @LAST_UPDATE_TIME)

---  插入数据
SET IDENTITY_INSERT dbo.TBL_ROOM_BASIC_INFO ON

INSERT TBL_ROOM_BASIC_INFO(
ROOM_ID,
MAC,
HOTEL_ID,
ROOM_NUMBER,
REMARK,
ROOM_TYPE_ID,
UPDATE_TIMEMARK
)
select id,  MAC,HOTELID,ROOMNUMBER,REMARK,RoomTypeID,GETDATE() from BLW.CRICS.[dbo].tb_Hosts
where ( @LAST_UPDATE_TIME IS NULL OR  LAST_MODIFIED_TIME > @LAST_UPDATE_TIME) 
and id not in (select ROOM_ID from TBL_ROOM_BASIC_INFO) 

SET IDENTITY_INSERT dbo.TBL_ROOM_BASIC_INFO off

-- 更新最后时间
update    NEW_TABLE_UPDATE_TIME_MARK  set LAST_UPDATE_TIME = GETDATE() where NEW_DB_TABLE_NAME ='TBL_ROOM_BASIC_INFO'
-- update    NEW_TABLE_UPDATE_TIME_MARK  set LAST_UPDATE_TIME = null where NEW_DB_TABLE_NAME ='TBL_ROOM_BASIC_INFO'

 -------------------------------------------------房间表同步结束
 

 --go


-- select top 10  LAST_MODIFIED_TIME,*  from  BLW.CRICS.[dbo].tb_Hosts where LAST_MODIFIED_TIME = null

-- select top 10  *  from NEW_TABLE_UPDATE_TIME_MARK


--select top 10  * from TBL_ROOM_BASIC_INFO



-- select top 10 *  from  BLW.CRICS.[dbo].tb_Hosts where
 
-- LAST_MODIFIED_TIME > ( select LAST_UPDATE_TIME  from NEW_TABLE_UPDATE_TIME_MARK where NEW_DB_TABLE_NAME ='TBL_ROOM_BASIC_INFO')

--   select count(*)   from  BLW.CRICS.[dbo].tb_Hosts
PRINT @@ERROR
IF @@ERROR > 0
BEGIN
ROLLBACK TRAN
END
ELSE
BEGIN
COMMIT TRAN;  
END

select top 10  *  from NEW_TABLE_UPDATE_TIME_MARK