const BaseController = require('./BaseController'); const heytripService = require('../services/heytripService'); const { QuotedHotelsPrice } = require('../vendor/heytrip'); class Api extends BaseController { hotelSearch = async(ctx) => { const { keyword, checkin, checkout } = ctx.query; const page = ctx.query.page || 1; const size = ctx.query.pagesize || 10; try { // if (!keyword || !checkin || !checkout) { if (!keyword) { ctx.throw(400, 'Missing required parameters`keyword`.'); } const { limit, offset } = this.getPagination(page, size); const { rows, count } = await heytripService.hotelSearch(keyword, { limit, offset }); let quoteRes = []; if (checkin && checkout) { const allIds = rows.map((item) => Number(item.hotel_id)); const basePriceParam = { hotelIds: allIds, nationality: 'CN', // 默认取中国报价 CheckInDate: checkin, CheckOutDate: checkout, adultNum: 1, roomCount: 1, }; quoteRes = await QuotedHotelsPrice(basePriceParam); heytripService.writeHeytripRequestLog({ action: 'heytripBasePrice', body: basePriceParam, }); } const quoteMapped = quoteRes.reduce((r, c) => ({ ...r, [c.Id]: c }), {}); const res = rows.map((item) => ({ ...item, base_price: quoteMapped[item.hotel_id] || {} })); ctx.body = this.response(0, 'hotel_search', { rows: res, count }, page, size); } catch (error) { // console.error(error); ctx.statusCode = error.statusCode; ctx.body = this.response(1, error.message || 'An error occurred.'); } } } module.exports = new Api();