@ -97,7 +97,7 @@ class Heytrip {
// nest: true,
// nest: true,
} ) ;
} ) ;
return { count , rows : rows . map ( ( item ) => item . dataValues ) } ;
return { count , rows : rows . map ( ( item ) => item . toJSON( ) ) } ;
// return { count, rows };
// return { count, rows };
} ;
} ;
@ -110,6 +110,17 @@ class Heytrip {
return ret ;
return ret ;
} ;
} ;
/ * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* 同步heytrip的酒店
* 1. 获取可用的酒店id , 缺少的设为 [ 99 = 下架 ]
* 2. 根据ID获取酒店详情 , 无信息设为下架
* /
/ * *
* @ deprecated
* 第一次同步录库 , 完成
* /
syncAids = async ( ) => {
syncAids = async ( ) => {
const lastPageIndex = await this . getLastPageIndex ( ) ;
const lastPageIndex = await this . getLastPageIndex ( ) ;
const pageIndex = lastPageIndex + 1 ;
const pageIndex = lastPageIndex + 1 ;
@ -131,6 +142,9 @@ class Heytrip {
} ;
} ;
} ;
} ;
/ * *
* 获取酒店ID
* /
syncAidState = async ( ) => {
syncAidState = async ( ) => {
const today = new Date ( ) ;
const today = new Date ( ) ;
today . setHours ( 0 , 0 , 0 , 0 ) ; // set the time to 00:00:00.000
today . setHours ( 0 , 0 , 0 , 0 ) ; // set the time to 00:00:00.000
@ -140,7 +154,7 @@ class Heytrip {
pageIndex = lastPageIndex ;
pageIndex = lastPageIndex ;
if ( isEmpty ( lastPageIndex ) ) {
if ( isEmpty ( lastPageIndex ) ) {
lastPageIndex = await this . getLastPageIndex ( { last _modify _time : { [ Op . gt ] : today } , page _index : { [ Op . lt ] : 9999 } } ) ;
lastPageIndex = await this . getLastPageIndex ( { last _modify _time : { [ Op . gt ] : today } , page _index : { [ Op . lt ] : 9999 } } ) ;
console . log ( 'syncAidState' , 'lastPageIndex' , lastPageIndex ) ;
// console.log('syncAidState', 'lastPageIndex', lastPageIndex);
pageIndex = lastPageIndex + 1 ;
pageIndex = lastPageIndex + 1 ;
}
}
@ -187,11 +201,13 @@ class Heytrip {
if ( ! isEmpty ( stateNormal ) ) {
if ( ! isEmpty ( stateNormal ) ) {
await HeytripIds . update ( { update _flag : 0 , page _index : pageIndex , last _modify _time : Sequelize . fn ( 'NOW' ) } , { where : { hotel _id : stateNormal } } ) ;
await HeytripIds . update ( { update _flag : 0 , page _index : pageIndex , last _modify _time : Sequelize . fn ( 'NOW' ) } , { where : { hotel _id : stateNormal } } ) ;
}
}
// 新增ID
const newIds = validIds . filter ( ( id ) => ! savedIds . map ( ( item ) => item . hotel _id ) . includes ( id ) ) ;
const newIds = validIds . filter ( ( id ) => ! savedIds . map ( ( item ) => item . hotel _id ) . includes ( id ) ) ;
if ( ! isEmpty ( newIds ) ) {
if ( ! isEmpty ( newIds ) ) {
const insertRows = newIds . map ( ( id ) => ( { hotel _id : id , page _index : pageIndex , update _flag : 1 } ) ) ;
const insertRows = newIds . map ( ( id ) => ( { hotel _id : id , page _index : pageIndex , update _flag : 1 , priority : - 10 } ) ) ;
await HeytripIds . bulkCreate ( insertRows ) ;
await HeytripIds . bulkCreate ( insertRows ) ;
}
}
// 页码滚动
const oldToNext = savedPageIds . filter ( ( item ) => ! validIds . includes ( ( item . hotel _id ) ) ) . map ( ( item ) => item . hotel _id ) ;
const oldToNext = savedPageIds . filter ( ( item ) => ! validIds . includes ( ( item . hotel _id ) ) ) . map ( ( item ) => item . hotel _id ) ;
if ( ! isEmpty ( oldToNext ) ) {
if ( ! isEmpty ( oldToNext ) ) {
await HeytripIds . update ( { page _index : Number ( pageIndex ) + 9999 , update _flag : 99 } , { where : { hotel _id : oldToNext } } ) ;
await HeytripIds . update ( { page _index : Number ( pageIndex ) + 9999 , update _flag : 99 } , { where : { hotel _id : oldToNext } } ) ;
@ -203,13 +219,29 @@ class Heytrip {
} ;
} ;
}
}
newHotels = async ( ) => {
/ * *
* 获取新添加的酒店
* 在 ` syncAidState ` 中设置了 ` update_flag=1 ` , ` priority=-10 `
* /
newHotels = async ( lgc ) => {
const [ rows ] = await Sequelize . query (
const [ rows ] = await Sequelize . query (
'SELECT i.hotel_id, IFNULL(h.hi_sn ,0) info_exists FROM heytrip_ids as i LEFT JOIN hotelinfo AS h ON h.hotel_id = i.hotel_id WHERE h.hi_sn IS NULL AND update_flag != 0 ORDER BY info_exists LIMIT 10'
` SELECT i.hotel_id ,IFNULL(h.hi2_sn, 0) info_exists
FROM heytrip _ids AS i
LEFT JOIN hotelinfo2 AS h ON h . hotel _id = i . hotel _id
AND h . lgc = $ { lgc }
WHERE h . hi2 _sn IS NULL
AND update _flag = 1
ORDER BY info _exists LIMIT 10 `
, { logging : false }
) ;
) ;
return rows ;
const res = await this . syncInitHotelLgcDetailsAction ( rows , LGC _MAPPED [ lgc ] ) ;
return res ;
} ;
} ;
/ * *
* 获取缺少的语种详情
* /
newHotelsLgc = async ( lgc ) => {
newHotelsLgc = async ( lgc ) => {
const [ rows ] = await Sequelize . query (
const [ rows ] = await Sequelize . query (
` SELECT i.hotel_id ,IFNULL(h.hi2_sn, 0) info_exists
` SELECT i.hotel_id ,IFNULL(h.hi2_sn, 0) info_exists
@ -233,6 +265,11 @@ class Heytrip {
const res = await this . syncInitHotelDetailsAction ( rows , LGC _MAPPED [ '2' ] ) ;
const res = await this . syncInitHotelDetailsAction ( rows , LGC _MAPPED [ '2' ] ) ;
return res ;
return res ;
} ;
} ;
/ * *
* 中国酒店 : 获取缺少的语种详情
* * 中国 : id > 20000000
* /
chinaHotelsLgc2 = async ( lgc ) => {
chinaHotelsLgc2 = async ( lgc ) => {
const [ rows ] = await Sequelize . query (
const [ rows ] = await Sequelize . query (
` SELECT i.hotel_id
` SELECT i.hotel_id
@ -242,6 +279,7 @@ class Heytrip {
AND h2 . lgc = $ { lgc }
AND h2 . lgc = $ { lgc }
WHERE h2 . hi2 _sn IS NULL
WHERE h2 . hi2 _sn IS NULL
AND i . hotel _id > 20000000
AND i . hotel _id > 20000000
AND i . update _flag != 99
LIMIT 10 `
LIMIT 10 `
, { logging : false }
, { logging : false }
) ;
) ;
@ -263,6 +301,7 @@ class Heytrip {
/ * *
/ * *
* @ deprecated
* @ deprecated
* 第一次录库 , 已执行
* /
* /
syncInitHotelDetailsAction = async ( rows , lgcObj = LGC _MAPPED [ DEFAULT _LGC ] ) => {
syncInitHotelDetailsAction = async ( rows , lgcObj = LGC _MAPPED [ DEFAULT _LGC ] ) => {
let allIds = [ ] ;
let allIds = [ ] ;
@ -309,11 +348,13 @@ class Heytrip {
}
}
} ;
} ;
/ * *
* 录入酒店的信息
* todo : 更新详情
* /
syncInitHotelLgcDetailsAction = async ( rows , lgcObj = LGC _MAPPED [ DEFAULT _LGC ] ) => {
syncInitHotelLgcDetailsAction = async ( rows , lgcObj = LGC _MAPPED [ DEFAULT _LGC ] ) => {
let allIds = [ ] ;
try {
try {
allIds = rows . map ( ( item ) => item . hotel _id ) ;
const allIds = rows . map ( ( item ) => item . hotel _id ) ;
console . log ( 'allIds' , allIds ) ;
if ( isEmpty ( rows ) ) {
if ( isEmpty ( rows ) ) {
return { next : ! isEmpty ( allIds ) , data : allIds } ;
return { next : ! isEmpty ( allIds ) , data : allIds } ;
@ -321,7 +362,6 @@ class Heytrip {
const _BaseInfoExists = await Hotelinfo . findAll ( { where : { hotel _id : allIds } , } ) ;
const _BaseInfoExists = await Hotelinfo . findAll ( { where : { hotel _id : allIds } , } ) ;
const _BaseInfoExistsMapped = _BaseInfoExists . reduce ( ( ru , c ) => ( { ... ru , [ ` ${ c . hotel _id } ` ] : c } ) , { } ) ;
const _BaseInfoExistsMapped = _BaseInfoExists . reduce ( ( ru , c ) => ( { ... ru , [ ` ${ c . hotel _id } ` ] : c } ) , { } ) ;
const existsIds = _BaseInfoExists . map ( ( item ) => ` ${ item . hotel _id } ` ) ;
const existsIds = _BaseInfoExists . map ( ( item ) => ` ${ item . hotel _id } ` ) ;
// console.log('existsIds', existsIds);
const res = await AccommodationsDetails ( {
const res = await AccommodationsDetails ( {
Language : lgcObj . locale ,
Language : lgcObj . locale ,
@ -331,22 +371,30 @@ class Heytrip {
// hotel info
// hotel info
const insertData = resolveDetails ( res , lgcObj ) ;
const insertData = resolveDetails ( res , lgcObj ) ;
// return insertData; // debug: 0
// return insertData; // debug: 0
const resIds = insertData . info . map ( ( item ) => item . hotel _id ) ;
const resIds = insertData . info . map ( ( item ) => ` ${ item . hotel _id } ` ) ;
/** 开始Database */
/** 开始Database */
const result = await Sequelize . transaction ( async ( transaction ) => {
const result = await Sequelize . transaction ( async ( transaction ) => {
const sequelizeOptions = { logging : false , transaction } ;
const sequelizeOptions = { logging : false , transaction } ;
let Info ;
let Info ;
/ * *
* 无返回数据 , 设为失效 ` 99 `
* * 但部分中国酒店 , 只有中文数据 , 请求英文无返回
* /
const offInfo = allIds . filter ( ( iitem ) => ! resIds . includes ( ` ${ iitem } ` ) ) ;
const offInfo = allIds . filter ( ( iitem ) => ! resIds . includes ( ` ${ iitem } ` ) ) ;
if ( ! isEmpty ( offInfo ) ) {
if ( ! isEmpty ( offInfo ) ) {
await HeytripIds . update ( { update _flag : 99 } , { where : { hotel _id : offInfo } , ... sequelizeOptions } ) ;
const flag = Number ( lgcObj . lgc ) === 1 ? 2 : 99 ; // 等待获取中文数据
const priority = Number ( lgcObj . lgc ) === 1 ? 0 : 99 ;
await HeytripIds . update ( { update _flag : flag , priority } , { where : { hotel _id : offInfo } , ... sequelizeOptions } ) ;
}
}
const updateInfo = insertData . info . filter ( ( iitem ) => existsIds . includes ( ` ${ iitem . hotel _id } ` ) ) ;
const updateInfo = insertData . info . filter ( ( iitem ) => existsIds . includes ( ` ${ iitem . hotel _id } ` ) ) ;
if ( ! isEmpty ( updateInfo ) ) {
if ( ! isEmpty ( updateInfo ) ) {
await HeytripIds . update ( { update _flag : 0 } , { where : { hotel _id : updateInfo . map ( ( x ) => x . hotel _id ) } , ... sequelizeOptions } ) ;
await HeytripIds . update ( { update _flag : 0 , priority : 0 } , { where : { hotel _id : updateInfo . map ( ( x ) => x . hotel _id ) } , ... sequelizeOptions } ) ;
for await ( const updateRow of updateInfo ) {
for await ( const updateRow of updateInfo ) {
// 有中英文的, 把名称合并, eg.上海意家人酒店Yijiaren Hotel
const _BaseName = _BaseInfoExistsMapped [ ` ${ updateRow . hotel _id } ` ] . hotel _name ;
const _BaseName = _BaseInfoExistsMapped [ ` ${ updateRow . hotel _id } ` ] . hotel _name ;
if ( ( _BaseName || '' ) . includes ( ( updateRow . hotel _name || '' ) . substring ( 0 , 4 ) ) ) {
if ( ( _BaseName || '' ) . includes ( ( updateRow . hotel _name || '' ) . substring ( 0 , 4 ) ) ) {
continue ;
continue ;
@ -354,9 +402,9 @@ class Heytrip {
const _BaseAddress = _BaseInfoExistsMapped [ ` ${ updateRow . hotel _id } ` ] . address ;
const _BaseAddress = _BaseInfoExistsMapped [ ` ${ updateRow . hotel _id } ` ] . address ;
await Hotelinfo . update (
await Hotelinfo . update (
{
{
update _flag : 0 ,
update _flag : 0 , priority : 0 ,
hotel _name : lgcObj . lgc === 1 ? ` ${ _BaseName } ${ updateRow . hotel _name } ` : ` ${ updateRow . hotel _name } ${ _BaseName } ` ,
hotel _name : Number ( lgcObj . lgc ) === 1 ? ` ${ _BaseName } ${ updateRow . hotel _name } ` : ` ${ updateRow . hotel _name } ${ _BaseName } ` ,
address : lgcObj . lgc === 1 ? ` ${ _BaseAddress } ${ updateRow . address } ` : ` ${ updateRow . address } ${ _BaseAddress } ` ,
address : Number ( lgcObj . lgc ) === 1 ? ` ${ _BaseAddress } ${ updateRow . address } ` : ` ${ updateRow . address } ${ _BaseAddress } ` ,
} ,
} ,
{ where : { hotel _id : updateRow . hotel _id } , ... sequelizeOptions }
{ where : { hotel _id : updateRow . hotel _id } , ... sequelizeOptions }
) ;
) ;
@ -378,7 +426,8 @@ class Heytrip {
if ( ! isEmpty ( insertData . reviews _summaries ) ) await ReviewsSummaries . bulkCreate ( insertData . reviews _summaries , sequelizeOptions ) ;
if ( ! isEmpty ( insertData . reviews _summaries ) ) await ReviewsSummaries . bulkCreate ( insertData . reviews _summaries , sequelizeOptions ) ;
if ( ! isEmpty ( insertData . locations ) ) await Locations . bulkCreate ( insertData . locations , sequelizeOptions ) ;
if ( ! isEmpty ( insertData . locations ) ) await Locations . bulkCreate ( insertData . locations , sequelizeOptions ) ;
if ( ! isEmpty ( newInfo ) ) await HeytripIds . update ( { update _flag : 0 } , { where : { hotel _id : newInfo . map ( ( x ) => x . hotel _id ) } , ... sequelizeOptions } ) ;
if ( ! isEmpty ( newInfo ) ) await HeytripIds . update ( { update _flag : 0 , priority : 0 } , { where : { hotel _id : newInfo . map ( ( x ) => x . hotel _id ) } , ... sequelizeOptions } ) ;
if ( ! isEmpty ( updateInfo ) ) await HeytripIds . update ( { update _flag : 0 , priority : 0 } , { where : { hotel _id : updateInfo . map ( ( x ) => x . hotel _id ) } , ... sequelizeOptions } ) ;
return Info ;
return Info ;
} ) ;
} ) ;
@ -387,10 +436,13 @@ class Heytrip {
} catch ( error ) {
} catch ( error ) {
console . log ( error ) ;
console . log ( error ) ;
return { next : false , restart : true , data : allIds } ;
return { next : false , restart : true , } ;
}
}
} ;
} ;
/ * *
* 获取实时的报价
* /
getHotelAvailability = async ( param ) => {
getHotelAvailability = async ( param ) => {
const { hotel _id , checkin , checkout , adults , children _ages , rooms , nationality } = param ;
const { hotel _id , checkin , checkout , adults , children _ages , rooms , nationality } = param ;
const paramBody = {
const paramBody = {
@ -427,4 +479,5 @@ class Heytrip {
return await Logs . create ( data , { logging : false } ) ;
return await Logs . create ( data , { logging : false } ) ;
} ;
} ;
}
}
module . exports = new Heytrip ( ) ;
module . exports = new Heytrip ( ) ;