def push_rateplan(self, rateplan_id, with_cancel_rule=True, with_roomrate=True): self.log.info("<< push rateplan {}>>".format(rateplan_id)) from models.rate_plan import RatePlanModel from models.room_rate import RoomRateModel rateplan = RatePlanModel.get_by_id(self.session, rateplan_id, with_delete=True) roomrate = RoomRateModel.get_by_rateplan(self.session, rateplan_id, with_delete=True) if not rateplan or not roomrate: self.log.info('not found') return self.post_rateplan(rateplan) if with_cancel_rule: self.post_cancel_rule(rateplan) if with_roomrate: self.post_roomrate(rateplan.merchant_id, roomrate)
def new_rate_plan(self, merchant_id, hotel_id, roomtype_id, name, meal_num, punish_type, ahead_days, stay_days, pay_type=None, guarantee_type=None, guarantee_start_time=None): room = CooperateRoomTypeModel.get_by_id(self.db, roomtype_id) if not room: raise JsonException(errcode=404, errmsg='room not exist') rate_plan = RatePlanModel.get_by_merchant_hotel_room_name( self.db, merchant_id, hotel_id, roomtype_id, name) if rate_plan: raise JsonException(errcode=405, errmsg="name exist") new_rateplan = RatePlanModel.new_rate_plan(self.db, merchant_id, hotel_id, roomtype_id, room.base_hotel_id, room.base_roomtype_id, name, meal_num, punish_type, ahead_days, stay_days, pay_type, guarantee_type, guarantee_start_time, commit=False) self.db.flush() new_roomrate = RoomRateModel.new_roomrate( self.db, hotel_id, roomtype_id, room.base_hotel_id, room.base_roomtype_id, new_rateplan.id, meal_num, commit=False) self.db.flush() rateplan_pusher = RatePlanPusher(self.db) roomrate_pusher = RoomRatePusher(self.db) try: if not (yield rateplan_pusher.post_rateplan(new_rateplan)): raise JsonException(2000, 'rateplan push fail') if not (yield rateplan_pusher.post_cancel_rule(new_rateplan)): raise JsonException(2001, 'cancel rule push fail') if not (yield roomrate_pusher.push_roomrate(new_rateplan.merchant_id, new_roomrate)): raise JsonException(2002, 'roomrate push fail') self.db.commit() except JsonException, e: self.db.rollback() raise e
def push_roomrate_by_rateplans(self, rateplans): from models.room_rate import RoomRateModel rateplan_ids = [rateplan.id for rateplan in rateplans] roomrates = RoomRateModel.get_by_rateplans(self.session, rateplan_ids) merchant_roomrates = [] for roomrate in roomrates: for rateplan in rateplans: if rateplan.id == roomrate.rate_plan_id: merchant_roomrates.append((rateplan.merchant_id, roomrate)) break roomrate_datas = [] for merchant_roomrate in merchant_roomrates: roomrate_datas.extend( self.generate_roomrate_datas(merchant_roomrate[0], merchant_roomrate[1])) roomrate_data_list = [ roomrate_datas[i:i + self.MAX_PUSH_NUM] for i in range(0, len(roomrate_datas), self.MAX_PUSH_NUM) ] for roomrate_data in roomrate_data_list: self.post_roomrates(roomrate_data)
def push_by_id(self, rateplan_id, with_roomrate=True, with_cancel_rule=True): rateplan = RatePlanModel.get_by_id(self.db, rateplan_id, with_delete=True) roomrate = RoomRateModel.get_by_rateplan(self.db, rateplan_id, with_delete=True) if not rateplan or not roomrate: raise gen.Return() if not (yield self.post_rateplan(rateplan)): raise JsonException(1000, 'push rateplan fail') if with_cancel_rule: self.post_cancel_rule(rateplan)
def _delete_rateplans(self, rateplans): if not rateplans: raise gen.Return(True) rateplan_ids = [rateplan.id for rateplan in rateplans] roomrates = RoomRateModel.get_by_rateplans(self.db, rateplan_ids) for rateplan in rateplans: rateplan.is_delete = 1 for roomrate in roomrates: roomrate.is_delete = 1 r = yield RatePlanPusher(self.db).update_rateplans_valid_status(rateplan_ids) raise gen.Return(r)
def _delete_rateplans(self, rateplans): if not rateplans: raise gen.Return(True) rateplan_ids = [rateplan.id for rateplan in rateplans] roomrates = RoomRateModel.get_by_rateplans(self.db, rateplan_ids) for rateplan in rateplans: rateplan.is_delete = 1 for roomrate in roomrates: roomrate.is_delete = 1 r = yield RatePlanPusher( self.db).update_rateplans_valid_status(rateplan_ids) raise gen.Return(r)
def modify_rateplan(self, rateplan_id, name, meal_num, punish_type, guarantee_type, guarantee_start_time, ahead_days, stay_days): rateplan = RatePlanModel.get_by_id(self.db, rateplan_id) if not rateplan: raise JsonException(errcode=404, errmsg="rateplan not found") if name is not None: _rateplan = RatePlanModel.get_by_merchant_hotel_room_name(self.db, rateplan.merchant_id, rateplan.hotel_id, rateplan.roomtype_id, name) if _rateplan and _rateplan.id != rateplan.id: raise JsonException(errcode=405, errmsg="name exist") else: rateplan.name = name if meal_num is not None: roomrate = RoomRateModel.set_meal( self.db, rateplan.id, meal_num, commit=False) if punish_type is not None: rateplan.punish_type = punish_type if rateplan.pay_type == rateplan.PAY_TYPE_ARRIVE: if guarantee_type is not None: rateplan.guarantee_type = guarantee_type if guarantee_start_time is not None: rateplan.guarantee_start_time = guarantee_start_time if ahead_days is not None: rateplan.ahead_days = ahead_days if stay_days is not None: rateplan.stay_days = stay_days self.db.flush() rateplan_pusher = RatePlanPusher(self.db) roomrate_pusher = RoomRatePusher(self.db) try: if not (yield rateplan_pusher.post_rateplan(rateplan)): raise JsonException(2000, 'rateplan push fail') if not (yield rateplan_pusher.post_cancel_rule(rateplan)): raise JsonException(2001, 'cancel rule push fail') if not (yield roomrate_pusher.push_roomrate(rateplan.merchant_id, roomrate)): raise JsonException(2002, 'roomrate push fail') self.db.commit() except JsonException, e: self.db.rollback() raise e
def push_roomrate_by_rateplans(self, rateplans): from models.room_rate import RoomRateModel rateplan_ids = [rateplan.id for rateplan in rateplans] roomrates= RoomRateModel.get_by_rateplans(self.session, rateplan_ids) merchant_roomrates = [] for roomrate in roomrates: for rateplan in rateplans: if rateplan.id == roomrate.rate_plan_id: merchant_roomrates.append((rateplan.merchant_id, roomrate)) break roomrate_datas = [] for merchant_roomrate in merchant_roomrates: roomrate_datas.extend(self.generate_roomrate_datas(merchant_roomrate[0], merchant_roomrate[1])) roomrate_data_list = [roomrate_datas[i: i+self.MAX_PUSH_NUM] for i in range(0, len(roomrate_datas), self.MAX_PUSH_NUM)] for roomrate_data in roomrate_data_list: self.post_roomrates(roomrate_data)
def set_price(self, merchant_id, roomrate_id, price, start_date, end_date, weekdays=None): roomrate = RoomRateModel.set_price(self.db, roomrate_id, price, start_date, end_date, weekdays, commit=False) if not roomrate: raise JsonException(1001, 'roomrate not found') self.db.flush() push_r = yield RoomRatePusher(self.db).push_roomrate( merchant_id, roomrate) if push_r: self.db.commit() else: raise JsonException(1002, 'push stock fail') raise gen.Return(roomrate)
def get_by_room(self, merchant_id, hotel_id, roomtype_id): rateplans = RatePlanModel.get_by_room(self.db, merchant_id, hotel_id, roomtype_id) rateplan_ids = [rateplan.id for rateplan in rateplans] roomrates= RoomRateModel.get_by_rateplans(self.db, rateplan_ids) Log.info(roomrates) return rateplans, roomrates
def push_roomrate_by_rateplan(self, rateplan): roomrate = RoomRateModel.get_by_rateplan(self.db, rateplan.id, with_delete=True) r = yield self.push_roomrate(rateplan.merchant_id, roomrate) raise gen.Return(r)