class LogicCancelMeet(object):
    def __init__(self):
        self.meet = Meet()
        self.user = User()
        self.map = DBOHHOMapInformation()
        self.sensor = DBOHHODeviceSensor()

    def cancel_meet(self, user_id, friend_user_id, apply_id, base_url):
        """
        取消见面
        只要自己申请过见面或自己同意过见 以取消见面
        :param user_id: 用户ID
        :param friend_user_id: 另一用户ID
        :param meet_state: 见面状态 0未见,1见面,2 请求相见,3同意相见,4不同意相见,5 取消见面,默认为0
        :return:
        """

        # information = self.user.get_push_user_information(user_id, apply_id, base_url)
        # information["function"] = "cancel meet"
        # self.user.push_user_information(friend_user_id, PUSH_STATE_TYPE_CANCEL_MEET, information)

        self.meet.add_exclude(user_id, friend_user_id)

        user_map = self.map.get_by_user(user_id)
        friend_map = self.map.get_by_user(friend_user_id)
        user_address = user_map.address if user_map else ""
        friend_user_address = friend_map.address if friend_map else ""

        self.meet.add_meet_end(apply_id, user_id, user_address)
        self.meet.add_meet_end(apply_id, friend_user_id, friend_user_address)

        self.meet.delete_meeting(apply_id, user_id)
        self.meet.delete_meeting(apply_id, friend_user_id)

        # user_map_query = self.map.filter_by_user(user_id)
        # self.map.delete_some(user_map_query)

        # friend_map_query = self.map.filter_by_user(friend_user_id)
        # self.map.delete_some(friend_map_query)

        # user_sensor_query = self.sensor.get_query()
        # user_sensor_query = self.sensor.get_by_user(user_sensor_query, user_id)

        # friend_user_query = self.sensor.get_query()
        # friend_sensor_query = self.sensor.get_by_user(friend_user_query, friend_user_id)

        # self.sensor.delete_some(user_sensor_query)
        # self.sensor.delete_some(friend_sensor_query)

        return Result.result_success()
class LogicRefuseMeet(object):
    def __init__(self):
        self.meet = Meet()
        self.user = User()

    def push_information(self, to_user_id, user_id, apply_id, type, base_url):
        information = self.user.get_refuse_meet_user_information(
            user_id, apply_id, base_url)
        information["function"] = "refuse meet"
        return self.user.push_user_information(to_user_id, type, information)

    def refuse_meet(self, user_id, friend_user_id, apply_id, base_url):
        """
        拒绝见面
        只要对方有申请见面,就可以拒绝见面
        :param user_id: 用户ID
        :param friend_user_id: 另一用户ID
        :param meet_state: 见面状态 0未见,1见面,2 请求相见,3同意相见,4不同意相见,5 取消见面,默认为0
        :return:
        """
        # 向对方发送拒绝的推送
        try:
            if user_id and friend_user_id and apply_id:
                type = PUSH_STATE_TYPE_REFUSE_MEET
                if not self.meet.is_meet_end(apply_id, friend_user_id):
                    result = self.push_information(friend_user_id, user_id,
                                                   apply_id, type, base_url)
                    OHHOLog.print_log(result)
                # information = self.user.get_push_user_information(user_id, apply_id, base_url)
                # information["function"] = "refuse meet"
                # self.user.push_user_information(friend_user_id, PUSH_STATE_TYPE_REFUSE_MEET, information)
                self.meet.add_exclude(user_id, friend_user_id)
                self.meet.add_refuse(apply_id, user_id)
                return Result.result_success()
            else:
                OHHOLog.print_log(
                    "user_id:%s, friend_user_id:%s, apply_id:%s" %
                    (str(user_id), str(friend_user_id), str(apply_id)))
                return Result.result_failed("there is some id is 0")
        except Exception as ex:
            OHHOLog.print_log(ex)
            return Result.result_failed(ex)
class LogicRematch(object):
    def __init__(self):
        self.meet = Meet()
        self.user = User()
        self.map = DBOHHOMapInformation()
        self.configuration = DBOHHOUserConfiguration()

    def rematch(self, user_id, friend_user_id, apply_id, is_published):
        # 加到exclude表中, 取消见面
        # type 0:未被惩罚
        # type 1: 惩罚,删除这个人以前的惩罚,加到惩罚表中;关闭本人的配对开关
        self.meet.add_exclude(user_id, friend_user_id)
        user_map = self.map.get_by_user(user_id)
        friend_map = self.map.get_by_user(friend_user_id)
        user_address = user_map.address if user_map else ""
        friend_user_address = friend_map.address if friend_map else ""

        self.meet.add_meet_end(apply_id, user_id, user_address)
        self.meet.add_meet_end(apply_id, friend_user_id, friend_user_address)

        self.meet.delete_meeting(apply_id, user_id)
        self.meet.delete_meeting(apply_id, friend_user_id)
        if not self.meet.is_meet_end(apply_id, friend_user_id):
            data = dict()
            data["user_id"] = user_id
            data["apply_id"] = apply_id
            self.user.push_user_information(friend_user_id,
                                            PUSH_STATE_TYPE_END_MEET, data)

        if is_published:
            self.meet.delete_published_by_user(user_id)
            data = dict()
            data["user_id"] = user_id
            data["apply_id"] = apply_id
            self.meet.add_published(data)
            configuration = self.configuration.get_by_user(user_id)
            if configuration:
                self.configuration.close_match(configuration)
        return Result.result_success()