package com.tanpu.community.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.tanpu.biz.common.enums.RelTypeEnum;
import com.tanpu.biz.common.enums.community.ReportStatusEnum;
import com.tanpu.common.exception.BizException;
import com.tanpu.common.redis.RedisHelper;
import com.tanpu.common.util.JsonUtil;
import com.tanpu.common.uuid.UuidGenHelper;
import com.tanpu.community.api.beans.req.comment.CreateCommentReq;
import com.tanpu.community.api.beans.req.theme.ThemeContentReq;
import com.tanpu.community.api.enums.DeleteTagEnum;
import com.tanpu.community.api.enums.ThemeTypeEnum;
import com.tanpu.community.dao.entity.community.ThemeEntity;
import com.tanpu.community.dao.entity.community.TimesCountEntity;
import com.tanpu.community.dao.mapper.community.ThemeMapper;
import com.tanpu.community.util.TimeUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class ThemeService {

    @Resource
    private ThemeMapper themeMapper;

    @Autowired
    private UuidGenHelper uuidGenHelper;

    @Autowired
    private RedisHelper redisHelper;

    @Transactional
    public void insertTheme(ThemeEntity themeEntity) {
        if (StringUtils.isBlank(themeEntity.getThemeId())){
            themeEntity.setThemeId(uuidGenHelper.getUuidStr());
        }

        themeMapper.insert(themeEntity);
    }

    @Transactional
    public void update(ThemeEntity themeEntity, String themeId) {
        themeMapper.update(themeEntity, new LambdaUpdateWrapper<ThemeEntity>().eq(ThemeEntity::getThemeId, themeId));
    }

    //n天内发表的所有主题
    public List<ThemeEntity> queryRecentdays(Integer days) {
        LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<ThemeEntity>()
                .eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode())
                .gt(ThemeEntity::getCreateTime, TimeUtils.getDaysBefore(days))
                .orderByDesc(ThemeEntity::getId);

        return themeMapper.selectList(queryWrapper);
    }

    //最新的n条主题
    public List<ThemeEntity> queryLatestThemes(Integer n) {
        LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<ThemeEntity>()
                .eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode())
                .last("limit " + n)
                .orderByDesc(ThemeEntity::getId);

        return themeMapper.selectList(queryWrapper);
    }

    //根据id返回主题详情(未删)
    public ThemeEntity queryByThemeId(String themeId) {
        return themeMapper.selectOne(new LambdaQueryWrapper<ThemeEntity>()
                .eq(ThemeEntity::getThemeId, themeId)
                .eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode()));
    }

    //根据id返回主题详情(未删)
    public ThemeEntity queryByThemeIdIgnoreDelete(String themeId) {
        ThemeEntity themeEntity = themeMapper.selectOne(new LambdaQueryWrapper<ThemeEntity>()
                .eq(ThemeEntity::getThemeId, themeId));
        return themeEntity;
    }

    //根据用户id查询主题list
    public List<ThemeEntity> queryThemesByUserIdCreateDesc(String userId, String lastId, Integer pageSize) {
        LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<ThemeEntity>()
                .eq(ThemeEntity::getAuthorId, userId)
                .eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode())
                .orderByDesc(ThemeEntity::getCreateTime);
        if (StringUtils.isNotEmpty(lastId)) {
            ThemeEntity lastEntity = queryByThemeId(lastId);
            if (lastEntity == null) throw new BizException("主题未找到,id:" + lastId);
            queryWrapper.lt(ThemeEntity::getCreateTime, lastEntity.getCreateTime());
        }
        if (pageSize != null) {
            queryWrapper.last("limit " + pageSize);
        }
        return themeMapper.selectList(queryWrapper);
    }

    //根据ids返回主题详情,带分页
    public List<ThemeEntity> queryByThemeIds(List<String> themeIds, String lastId, Integer pageSize) {
        if (CollectionUtils.isEmpty(themeIds)) {
            return Collections.emptyList();
        }
        LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<ThemeEntity>()
                .in(ThemeEntity::getThemeId, themeIds)
                .eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode());
        if (StringUtils.isNotEmpty(lastId)) {
            ThemeEntity lastEntity = queryByThemeId(lastId);
            if (lastEntity == null) throw new BizException("主题未找到,id:" + lastId);
            queryWrapper.lt(ThemeEntity::getCreateTime, lastEntity.getCreateTime());
        }
        if (pageSize != null) {
            queryWrapper.last("limit " + pageSize);
        }
        return themeMapper.selectList(queryWrapper);
    }

    /**
     * 根据主题Id查询列表
     *
     * @param themeIds
     * @return
     */
    public List<ThemeEntity> queryByThemeIds(List<String> themeIds) {
        if (CollectionUtils.isEmpty(themeIds)) {
            return Collections.emptyList();
        }
        LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<ThemeEntity>()
                .in(ThemeEntity::getThemeId, themeIds)
                .eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode());

        List<ThemeEntity> themeEntities = themeMapper.selectList(queryWrapper);
        return themeEntities;
    }




    /**
     * 根据话题查询最新主题(可分页)
     *
     * @param topidId  话题Id
     * @param pageSize 查询数量
     * @return
     */
    public List<ThemeEntity> queryNewestByTopic(String topidId, Integer pageNo, Integer pageSize, List<String> excludeIds) {
        LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<ThemeEntity>()
                .eq(ThemeEntity::getTopicId, topidId);
        if (!excludeIds.isEmpty()) {
            queryWrapper.notIn(ThemeEntity::getThemeId, excludeIds);
        }
        queryWrapper.last("limit " + pageNo + ", " + pageSize)
                .orderByDesc(ThemeEntity::getCreateTime)
                .eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode());
        return themeMapper.selectList(queryWrapper);
    }

    //根据话题查询所有的主题Id,包括已删除的主题
    public List<String> queryThemeIdsByTopic(String topidId) {
        if (StringUtils.isEmpty(topidId)) {
            return Collections.emptyList();
        }
        LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<ThemeEntity>()
                .eq(ThemeEntity::getTopicId, topidId);
        queryWrapper.select(ThemeEntity::getThemeId);
        return themeMapper.selectList(queryWrapper).stream().map(ThemeEntity::getThemeId).collect(Collectors.toList());
    }

    /**
     * 根据作者查询主题分页列表
     * @param userIds
     * @param pageStart
     * @param pageSize
     * @return
     */
    public List<ThemeEntity> queryByUserIdsCreateDesc(List<String> userIds, Integer pageStart, Integer pageSize) {
        if (CollectionUtils.isEmpty(userIds)){
            return Collections.emptyList();
        }
        LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<ThemeEntity>()
                .in(ThemeEntity::getAuthorId, userIds)
                .last("limit " + pageStart + ", " + pageSize)
                .orderByDesc(ThemeEntity::getCreateTime)
                .eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode());
        return themeMapper.selectList(queryWrapper);

    }

    public Integer getForwardCountById(String themeId) {
        return themeMapper.selectCount(new LambdaQueryWrapper<ThemeEntity>()
                .eq(ThemeEntity::getFormerThemeId, themeId)
                .eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED));
    }

    public boolean judgeForwardByUser(String themeId, String userId) {
        return themeMapper.selectCount(new LambdaQueryWrapper<ThemeEntity>()
                .eq(ThemeEntity::getFormerThemeId, themeId)
                .eq(ThemeEntity::getAuthorId, userId)
                .eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED))>0;
    }

    public Set<String> getForwardUsers(List<String> themeIds) {
        if (CollectionUtils.isEmpty(themeIds)){
            return new HashSet<>();
        }
        return themeMapper.selectList(new LambdaQueryWrapper<ThemeEntity>()
                .in(ThemeEntity::getFormerThemeId, themeIds)
                .eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED))
                .stream().map(ThemeEntity::getAuthorId).collect(Collectors.toSet());
    }



    @Transactional
    public void deleteById(String themeId,String userId) {
        ThemeEntity themeEntity = themeMapper.selectOne(new LambdaQueryWrapper<ThemeEntity>()
                .eq(ThemeEntity::getThemeId, themeId));
        if (themeEntity == null || !themeEntity.getAuthorId().equals(userId)) {
            throw new BizException("主题与用户不匹配,id:" + themeId+",userId"+userId);
        }
        themeEntity.setDeleteTag(DeleteTagEnum.DELETED.getCode());
        themeMapper.updateById(themeEntity);
    }

    /**
     * 查询更新节点后的用户新建主题数
     *
     * @param userIds      用户ids
     * @param lastId 更新时间节点
     * @return
     */
    public Integer queryCountFromLastId(List<String> userIds, Long lastId) {
        if (CollectionUtils.isEmpty(userIds)) {
            return 0;
        }
        LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<ThemeEntity>()
                .in(ThemeEntity::getAuthorId, userIds)
                .gt(ThemeEntity::getId, lastId)
                .eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode());
        return themeMapper.selectCount(queryWrapper);
    }


    public void updateReportStatus(String themeId) {
        ThemeEntity themeEntity = queryByThemeId(themeId);
        if (themeEntity == null) {
            throw new BizException("主题未找到,id:"+themeId);
        }
        themeEntity.setReportStatus(ReportStatusEnum.REPORTED.getCode());
        themeMapper.updateById(themeEntity);
    }

    //统计主题集合的浏览量
    public Map<String, Integer> getForwardCountMap(List<String> themeIds) {
        if (CollectionUtils.isEmpty(themeIds)){
            return new HashMap<String, Integer>();
        }
        LambdaQueryWrapper<ThemeEntity> wrapper = new LambdaQueryWrapper<ThemeEntity>()
                .eq(ThemeEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED)
                .in(ThemeEntity::getFormerThemeId,themeIds)
                .groupBy(ThemeEntity::getFormerThemeId);
        return themeMapper.selectCountByThemeIds(wrapper).stream()
                .collect(Collectors.toMap(TimesCountEntity::getId, TimesCountEntity::getTimes));
    }

    public List<ThemeEntity> queryAllForward() {
        return themeMapper.selectList(new LambdaQueryWrapper<ThemeEntity>()
                .eq(ThemeEntity::getThemeType, ThemeTypeEnum.FORWARD.getCode())
                .eq(ThemeEntity::getDeleteTag,DeleteTagEnum.NOT_DELETED.getCode())
                .orderByAsc(ThemeEntity::getCreateTime));
    }

    public String commentSyncForward(CreateCommentReq req, String userId) {
        // 评论构造theme content
        List<ThemeContentReq> themeContentReqs = Arrays.asList(ThemeContentReq.builder().type(RelTypeEnum.TEXT.type).value(req.getComment()).build());
        ThemeContentReq.builder().type(RelTypeEnum.TEXT.type).value(req.getComment()).build();
        ThemeEntity themeEntity = ThemeEntity.builder()
                .content(JsonUtil.toJson(themeContentReqs))
                .topicId("")
                .formerThemeId(req.getThemeId())
                .authorId(userId)
                .themeType(ThemeTypeEnum.FORWARD.getCode())
                .build();
        this.insertTheme(themeEntity);
        return themeEntity.getThemeId();
    }
}