package com.yonge.cooleshow.biz.dal.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yonge.cooleshow.biz.dal.entity.Subject;
import com.yonge.cooleshow.biz.dal.entity.UserTenantAlbumRecord;
import com.yonge.cooleshow.biz.dal.entity.UserTenantBindRecord;
import com.yonge.cooleshow.biz.dal.mapper.UserTenantAlbumRecordMapper;
import com.yonge.cooleshow.biz.dal.mapper.UserTenantBindRecordMapper;
import com.yonge.cooleshow.biz.dal.service.SubjectService;
import com.yonge.cooleshow.biz.dal.service.UserTenantBindRecordService;
import com.yonge.cooleshow.biz.dal.wrapper.UserTenantBindRecordWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 用户机构绑定解绑记录
 * 2023-12-04 15:30:02
 */
@Slf4j
@Service
public class UserTenantBindRecordServiceImpl extends ServiceImpl<UserTenantBindRecordMapper, UserTenantBindRecord> implements UserTenantBindRecordService {

    @Autowired
    private UserTenantAlbumRecordMapper userTenantAlbumRecordMapper;

    @Autowired
    private SubjectService subjectService;

    /**
     * 查询详情
     *
     * @param id 详情ID
     * @return UserTenantBindRecord
     */
    @Override
    public UserTenantBindRecord detail(Long id) {

        return baseMapper.selectById(id);
    }

    /**
     * 分页查询
     *
     * @param page  IPage<UserTenantBindRecord>
     * @param query UserTenantBindRecordWrapper.UserTenantBindRecordQuery
     * @return IPage<UserTenantBindRecord>
     */
    @Override
    public IPage<UserTenantBindRecordWrapper.UserTenantBindRecord> selectPage(IPage<UserTenantBindRecordWrapper.UserTenantBindRecord> page,
                                                                              UserTenantBindRecordWrapper.UserTenantBindRecordQuery query) {

        IPage<UserTenantBindRecordWrapper.UserTenantBindRecord> pageRecord = page.setRecords(baseMapper.selectPage(page, query));
        List<UserTenantBindRecordWrapper.UserTenantBindRecord> records = pageRecord.getRecords();
        if (!records.isEmpty() && "STUDENT".equals(query.getUserType())) {
            List<Long> userIds = records.stream().map(UserTenantBindRecordWrapper.UserTenantBindRecord::getUserId).distinct().collect(Collectors.toList());

            QueryWrapper<UserTenantAlbumRecord> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda()
                    .in(UserTenantAlbumRecord::getUserId, userIds)
                    .ge(UserTenantAlbumRecord::getEndTime, new Date());
            Map<String, List<UserTenantAlbumRecord>> groupByAlbum = userTenantAlbumRecordMapper.selectList(queryWrapper).stream()
                    .collect(Collectors.groupingBy(next -> next.getUserId() + "_" + next.getTenantId()));

            List<String> collect = records.stream().map(UserTenantBindRecordWrapper.UserTenantBindRecord::getSubjectId).distinct().collect(Collectors.toList());
            Map<Integer, Subject> mapByIds = subjectService.getMapByIds(collect);
            records.forEach(next -> {
                String key = next.getUserId() + "_" + next.getTenantId();
                if (groupByAlbum.containsKey(key)) {
                    next.setTenantAlbumActiveStatus(true);
                    List<UserTenantAlbumRecord> userTenantAlbumRecords = groupByAlbum.get(key);
                    userTenantAlbumRecords.sort((o1, o2) -> o2.getEndTime().compareTo(o1.getEndTime()));
                    next.setTenantAlbumExpireTime(userTenantAlbumRecords.get(0).getEndTime());
                } else {
                    next.setTenantAlbumActiveStatus(false);
                }
                next.setSubjectName(mapByIds.getOrDefault(Integer.valueOf(next.getSubjectId()), new Subject()).getName());
            });
        }

        return pageRecord;

    }

    /**
     * 添加
     *
     * @param userTenantBindRecord UserTenantBindRecordWrapper.UserTenantBindRecord
     * @return Boolean
     */
    @Override
    public Boolean add(UserTenantBindRecordWrapper.UserTenantBindRecord userTenantBindRecord) {

        return this.save(JSON.parseObject(userTenantBindRecord.jsonString(), UserTenantBindRecord.class));
    }

    /**
     * 更新
     *
     * @param userTenantBindRecord UserTenantBindRecordWrapper.UserTenantBindRecord
     * @return Boolean
     */
    @Override
    public Boolean update(UserTenantBindRecordWrapper.UserTenantBindRecord userTenantBindRecord) {

        return this.updateById(JSON.parseObject(userTenantBindRecord.jsonString(), UserTenantBindRecord.class));
    }
}