# -*- coding: utf-8 -*-

import itertools

from group.models import GroupPhotoInfo


def get_current_photos(group_id, user_id, current_id, request=None):
    # 获取从 current_id 到 now 的群组照片列表
    group_photos = GroupPhotoInfo.objects.filter(
        group_id=group_id,
        status=True,
        pk__gt=current_id,
    )
    # 最新照片
    group_tmp_photos = group_photos.order_by(
        '-pk',
    )
    latest_photo = group_tmp_photos.first()
    # 照片按照 session_id 分组
    group_photos = group_photos.order_by(
        '-session_id',
        '-pk',
    )
    if request and request.weixin:
        group_photos = group_photos[:30]
    group_photos = map(lambda x: {'session_id': x[0], 'photos': [y.photo_info(user_id) for y in x[1]]},
                       itertools.groupby(group_photos, lambda x: x.session_id))
    return {
        'current_id': latest_photo and latest_photo.pk or current_id,
        'photos': group_photos,
    }