module Ly::Zizhuyou

Constants

VERSION

Public Class Methods

bookable_format(result, guarantee_arrival_time) click to toggle source
# File lib/ly/zizhuyou.rb, line 182
def self.bookable_format(result, guarantee_arrival_time)
  ret_val = {status: result['CanBooking'] == 1}
  if ret_val[:status]
    unless result['BookableMessage'].nil?
      bookable_message = JSON.parse(result['BookableMessage'])
      ret_val[:limit_rooms] = bookable_message['ExcessRooms']
      ret_val[:remarks] = bookable_message['ReceiveTextRemark']
      ret_val[:remark_options] = []
      bookable_message['SpecialRequestOptions'].each do |option|
        next if option['Code'].to_i.zero?
        ret_val[:remark_options] << {'code' => option['Code'], 'text' => option['CodeContext'].strip, 'name' => option['Name']}
      end
      guarantee_info = bookable_message['CtripGuaranteeInfo']
      unless guarantee_info.nil? || guarantee_info['CgCode'].nil?
        cg_code = nil
        guarantee_info['CgCode'].each do |code|
          cg = code.to_i
          case cg
            when 2
              cg_code = cg if [4, 1, 3, 5].include?(cg_code) || cg_code.nil?
            when 4
              cg_code = cg if [1, 3, 5].include?(cg_code) || cg_code.nil?
            when 1
              cg_code = cg if [3, 5].include?(cg_code) || cg_code.nil?
            when 3
              cg_code = cg if [5].include?(cg_code) || cg_code.nil?
            when 5
              cg_code = cg
          end
        end
        if [1, 2, 3, 4].include?(cg_code)
          if cg_code == 4
            ret_val[:guarantee] = 1
          elsif cg_code == 2
            ret_val[:guarantee] = 2
          else
            ret_val[:guarantee_arrival_time] = guarantee_arrival_time if cg_code == 3
            ret_val[:guarantee] = 3
            ret_val[:guarantee_amount] = guarantee_info['AmountPercent'].sum
          end
        end
      end
    end
  end
  ret_val
end
prices_format(arrival_date, departure_date, result, room_info = nil) click to toggle source
# File lib/ly/zizhuyou.rb, line 71
def self.prices_format(arrival_date, departure_date, result, room_info = nil)
  return [] if result.nil?
  ret_val = []
  data = {}

  # 聚合房型信息
  pro_infos = {}
  unless room_info.nil? || room_info['ResProBaseInfos'].nil?
    room_info['ResProBaseInfos'].each do |pro_info|
      pro_infos[pro_info['ProductUniqueId']] = pro_info
    end
  end

  # 聚合房型&政策(生成空日期价格
  unless result['ProInfoDetailList'].nil?
    result['ProInfoDetailList'].each do |pro|
      pro_info = pro_infos[pro['ProductUniqueId']]
      unless data.key? pro['ProId']
        data[pro['ProId']] = {
            'ProId' => pro['ProId'],
            'RoomName' => pro['RoomName'],
            'ProductInfo' => pro_info,
            'Products' => {}
        }
      end

      #生成空日期价格
      pro['ProSaleInfoDetails'] = {} if pro['ProSaleInfoDetails'].nil?
      pro['ProSaleInfoDetails'] = (departure_date - arrival_date).to_i.times.map {|day| [(arrival_date + day).strftime('%m/%d/%Y 00:00:00'), nil]}.to_h.merge(pro['ProSaleInfoDetails'])

      data[pro['ProId']]['Products'][pro['ProductUniqueId']] = pro.merge!('ProductInfo' => pro_info) unless pro['ProSaleInfoDetails'].value?(nil)
    end
  end

  data.each do |_, r|
    next if r['Products'].empty? # 跳过无政策房型
    room = {'policies' => []}
    room['room_id'] = r['ProId']
    unless r['ProductInfo'].nil?
      room['room_name'] = r['ProductInfo']['ResProName']
      room['room_info'] = r['ProductInfo']['ResProProps']
    else
      room['room_name'] = r['RoomName']
    end
    room['image'] = ''
    r['Products'].each do |_, p|
      next unless [1, 2].include?(p['PaymentType'])
      policy = {'daily_prices' => []}
      policy['policy_id'] = p['ProductUniqueId']
      unless p['ProductInfo'].nil?
        policy['policy_name'] = p['ProductInfo']['SupPriceName']
        policy['bed_type'] = p['ProductInfo']['BedTypeName']
      else
        policy['policy_name'] = p['PolicyName']
      end

      prices = []
      breakfast = nil
      statuses = []

      guarantee_arrival_time = nil

      remainders = []
      instant_confirmations = []
      reserve_time = Time.strptime("#{arrival_date.strftime('%Y-%m-%d')} #{p['ReserveTime']} +0800", '%Y-%m-%d %H:%M:%S %z')

      p['ProSaleInfoDetails'].each do |d, s|
        prices << s['DistributionSalePrice'] unless s.nil?
        breakfast = s['BreakfastName'] if breakfast.nil?
        status = !(s['InventoryStats'] == 4 || (!s['OpeningSale'] && s['InventoryRemainder'].zero?))
        statuses << status
        policy['daily_prices'] << {
            'date' => Time.strptime(d, '%m/%d/%Y 00:00:00').strftime('%Y-%m-%d'),
            'price' => s['DistributionSalePrice'],
            'breakfast' => s['BreakfastName'],
            'status' => status
        }
        unless s['GuaranteeHoldTime'].nil? || s['GuaranteeHoldTime'].empty?
          guarantee_hold_time = Time.strptime("1970-01-01 #{s['GuaranteeHoldTime']} UTC", '%Y-%m-%d %H:%M:%S %Z')
          guarantee_arrival_time = guarantee_hold_time if guarantee_arrival_time.nil? || (guarantee_hold_time > guarantee_arrival_time)
        end
        remainders << s['InventoryRemainder'] if s['InventoryStats'] == 0 || s['InventoryRemainder'] > 0
        instant_confirmations << (s['InventoryStats'] == 0)
        last_reserve_time = Time.strptime(s['LastReserveTime'], '%m/%d/%Y %H:%M:%S')
        reserve_time = last_reserve_time if reserve_time.nil? || (reserve_time > last_reserve_time)
      end

      next if prices.empty?

      policy['price'] = (prices.sum / prices.size.to_f).round
      policy['breakfast'] = breakfast
      policy['status'] = !statuses.include?(false)

      policy['payment'] = p['PaymentType']
      policy['total_price'] = prices.sum

      policy['guarantee_arrival_time'] = ((guarantee_arrival_time - Time.at(0).utc) / 3600).to_i unless guarantee_arrival_time.nil?


      policy['advance_days'] = p['BeforehandBookingDay']
      policy['reserve_time'] = reserve_time.strftime('%H:%M')
      policy['remainder'] = remainders.min
      policy['instant_confirmation'] = !instant_confirmations.include?(false)

      room['policies'] << policy
    end
    ret_val << room unless room['policies'].empty?
  end
  ret_val
end