package com.gic.haoban.manage.service.service.chat.impl;

import com.gic.api.base.commons.Page;
import com.gic.commons.util.PageHelperUtils;
import com.gic.haoban.manage.api.dto.chat.GroupChatActivityInviteLogDTO;
import com.gic.haoban.manage.api.dto.chat.GroupChatActivityRewardDetailLogDTO;
import com.gic.haoban.manage.api.dto.chat.GroupChatActivityRewardLogDTO;
import com.gic.haoban.manage.api.qdto.chat.GroupChatActivityRewardDetailLogQDTO;
import com.gic.haoban.manage.api.qdto.chat.GroupChatActivityRewardLogQDTO;
import com.gic.haoban.manage.service.dao.mapper.chat.GroupChatActivityRewardLogMapper;
import com.gic.haoban.manage.service.entity.chat.TabGroupChatActivityInviteLog;
import com.gic.haoban.manage.service.entity.chat.TabGroupChatActivityRewardLog;
import com.gic.haoban.manage.service.service.chat.GroupChatActivityRewardDetailLogService;
import com.gic.haoban.manage.service.service.chat.GroupChatActivityRewardLogService;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Maps;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 内容活动完成奖励表(TabHaobanGroupChatActivityRewardLog)表服务实现类
 *
 * @author makejava
 * @since 2024-09-27 16:09:57
 */
@Service("groupChatActivityRewardLogService")
public class GroupChatActivityRewardLogServiceImpl implements GroupChatActivityRewardLogService {

    @Resource
    private GroupChatActivityRewardLogMapper groupChatActivityRewardLogMapper;

    @Resource
    private GroupChatActivityRewardDetailLogService groupChatActivityRewardDetailLogService;

    private GroupChatActivityRewardLogDTO convertToDTO(TabGroupChatActivityRewardLog tabGroupChatActivityRewardLog) {
        GroupChatActivityRewardLogDTO groupChatActivityRewardLogDTO = new GroupChatActivityRewardLogDTO();
        BeanUtils.copyProperties(tabGroupChatActivityRewardLog, groupChatActivityRewardLogDTO);
        return groupChatActivityRewardLogDTO;
    }

    @Override
    public Page<GroupChatActivityRewardLogDTO> queryRewardLogDetails(GroupChatActivityRewardLogQDTO rewardLogQDTO) {
        PageHelper.startPage(rewardLogQDTO);
        List<TabGroupChatActivityRewardLog> rewardLogList = groupChatActivityRewardLogMapper.queryList(rewardLogQDTO);
        if (CollectionUtils.isEmpty(rewardLogList)) {
            return PageHelperUtils.changePageHelperToCurrentPage(rewardLogList, GroupChatActivityRewardLogDTO.class);
        }
        Page<GroupChatActivityRewardLogDTO> page = PageHelperUtils.changePageHelperToCurrentPage(rewardLogList, GroupChatActivityRewardLogDTO.class);

        List<Long> rewardLogIdList = rewardLogList.stream()
                .map(TabGroupChatActivityRewardLog::getChatActivityRewardLogId).collect(Collectors.toList());
        GroupChatActivityRewardDetailLogQDTO rewardDetailLogQDTO = new GroupChatActivityRewardDetailLogQDTO();
        rewardDetailLogQDTO.setRewardLogIdList(rewardLogIdList);
        List<GroupChatActivityRewardDetailLogDTO> rewardDetailLogDTOList = groupChatActivityRewardDetailLogService.queryList(rewardDetailLogQDTO);
        Map<Long, List<GroupChatActivityRewardDetailLogDTO>> rewardDetailLogDTOMap = Maps.newHashMap();
        if (CollectionUtils.isNotEmpty(rewardDetailLogDTOList)) {
            rewardDetailLogDTOMap = rewardDetailLogDTOList.stream()
                    .collect(Collectors.groupingBy(GroupChatActivityRewardDetailLogDTO::getChatActivityRewardId));
        }
        Map<Long, List<GroupChatActivityRewardDetailLogDTO>> finalRewardDetailLogDTOMap = rewardDetailLogDTOMap;

        List<GroupChatActivityRewardLogDTO> rewardLogDTOList = rewardLogList.stream().map(tabGroupChatActivityRewardLog -> {
            GroupChatActivityRewardLogDTO rewardLogDTO = convertToDTO(tabGroupChatActivityRewardLog);
            rewardLogDTO.setRewardDetailLogDTOList(finalRewardDetailLogDTOMap.get(rewardLogDTO.getChatActivityRewardLogId()));
            return rewardLogDTO;

        }).collect(Collectors.toList());
        page.setResult(rewardLogDTOList);
        return page;
    }
}
