package com.tangguo.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tangguo.common.core.redis.RedisCache;
import com.tangguo.common.exception.ServiceException;
import com.tangguo.common.mauth.MobileTokenHelper;
import com.tangguo.common.utils.StringUtils;
import com.tangguo.constant.TopicConstant;
import com.tangguo.domain.BbsTopic;
import com.tangguo.domain.bo.CreateTopicBO;
import com.tangguo.domain.vo.BbsMomentListVO;
import com.tangguo.domain.vo.BbsTopicListVO;
import com.tangguo.enums.TopicTopStatus;
import com.tangguo.mapper.BbsTopicMapper;
import com.tangguo.service.IBbsTopicService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * 话题Service业务层处理
 *
 * @author ruoyi
 * @date 2025-08-27
 */
@Service
public class BbsTopicServiceImpl extends ServiceImpl<BbsTopicMapper, BbsTopic> implements IBbsTopicService {

	@Resource
	private BbsTopicMapper bbsTopicMapper;

	@Resource
	private RedisCache redisCache;


	/**
	 * 查询话题列表
	 *
	 * @param topic 话题
	 * @return 话题
	 */
	@Override
	public List<BbsTopic> selectBbsTopicList(BbsTopic topic) {
		LambdaQueryWrapper<BbsTopic> lqw = Wrappers.lambdaQuery();
		lqw.like(StringUtils.isNotBlank(topic.getSource()), BbsTopic::getSource, topic.getSource());
		lqw.like(StringUtils.isNotBlank(topic.getName()), BbsTopic::getName, topic.getName());
		lqw.orderByDesc(BbsTopic::getIsTop);
		lqw.orderByDesc(BbsTopic::getTopTime);
		lqw.orderByAsc(BbsTopic::getSort);
		return bbsTopicMapper.selectList(lqw);
	}


	/**
	 * 新增话题
	 *
	 * @param topic 话题
	 */
	@Override
	public void addTopic(BbsTopic topic) {
		long nameCount = this.count(
			Wrappers.lambdaQuery(BbsTopic.class).eq(BbsTopic::getName, topic.getName())
		);
		if (nameCount > 0) {
			throw new ServiceException("新增失败，当前话题名称已存在。");
		}

		BbsTopic newTopic = new BbsTopic();
		newTopic.setSource("后台管理");
		newTopic.setName(topic.getName());
		newTopic.setSort(topic.getSort());
		this.save(newTopic);
	}


	/**
	 * 修改话题
	 *
	 * @param topic 话题
	 */
	@Override
	public void editeTopic(BbsTopic topic) {
		BbsTopic dbTopic = this.getById(topic.getId());
		if (Objects.isNull(dbTopic)) {
			throw new ServiceException("修改失败，当前话题数据不存在。");
		}

		if (!dbTopic.getName().equals(topic.getName())) {
			long nameCount = this.count(
				Wrappers.lambdaQuery(BbsTopic.class).eq(BbsTopic::getName, topic.getName())
			);
			if (nameCount > 0) {
				throw new ServiceException("修改失败，当前话题名称已存在。");
			}
		}

		BbsTopic updTopic = new BbsTopic();
		updTopic.setId(dbTopic.getId());
		updTopic.setName(topic.getName());
		updTopic.setSort(topic.getSort());
		this.updateById(updTopic);
	}


	/**
	 * 置顶话题
	 *
	 * @param topic 话题
	 */
	@Override
	public void topTopic(BbsTopic topic) {
		BbsTopic dbTopic = this.getById(topic.getId());
		if (Objects.isNull(dbTopic)) {
			throw new ServiceException("置顶失败，当前话题数据不存在。");
		}

		Integer isTop = topic.getIsTop();
		if (!dbTopic.getIsTop().equals(isTop)) {
			LambdaUpdateWrapper<BbsTopic> wrapper = Wrappers.lambdaUpdate(BbsTopic.class);
			wrapper.eq(BbsTopic::getId, dbTopic.getId());
			if (TopicTopStatus.ZD.getValue() == isTop) {
				wrapper.set(BbsTopic::getIsTop, TopicTopStatus.ZD.getValue());
				wrapper.set(BbsTopic::getTopTime, new Date());
			} else {
				wrapper.set(BbsTopic::getIsTop, TopicTopStatus.BZD.getValue());
				wrapper.set(BbsTopic::getTopTime, null);
			}
			this.update(wrapper);
		}
 	}


	/**
	 * 删除话题
	 *
	 * @param topicId 话题Id
	 */
	@Override
	public void deleteTopic(Long topicId) {
		this.removeById(topicId);
	}


	/**
	 * 查询选择话题
	 *
	 * @param name 话题名称
	 * @return 话题
	 */
	@Override
	public List<BbsTopicListVO> getSelectTopics(String name) {
		return this.baseMapper.selectSelectTopics(name);
	}


	/**
	 * 刷新热搜话题
	 */
	@Override
	public void refreshRankingTopic() {
		this.baseMapper.refreshRankingTopic();
		this.redisCache.deleteObject(TopicConstant.HOT_SCORE_CACHE_KEY);
	}


	/**
	 * 查询热搜话题
	 *
	 * @return 话题
	 */
	@Override
	public List<BbsTopicListVO> getRankingTopic() {
		List<BbsTopicListVO> topics = this.redisCache.getCacheObject(TopicConstant.HOT_SCORE_CACHE_KEY);
		if (CollUtil.isEmpty(topics)) {
			topics = this.baseMapper.selectRankingTopic();
			this.redisCache.setCacheObject(TopicConstant.HOT_SCORE_CACHE_KEY, topics, 1, TimeUnit.HOURS);
		}
		return topics;
	}


	/**
	 * 查询话题详情
	 *
	 * @param topicId 话题Id
	 * @return 话题明细
	 */
	@Override
	public List<BbsMomentListVO> getTopicDetails(Long topicId) {
		return Collections.emptyList();
	}


	/**
	 * 创建话题
	 *
	 * @param bo 话题
	 */
	@Override
	public BbsTopicListVO createTopic(CreateTopicBO bo) {
		synchronized (bo.getName().intern()) {
			long nameCount = this.count(
				Wrappers.lambdaQuery(BbsTopic.class).eq(BbsTopic::getName, bo.getName())
			);
			if (nameCount > 0) {
				throw new ServiceException("创建失败，当前话题名称已存在。");
			}

			BbsTopic newTopic = new BbsTopic();
			newTopic.setCreateBy(MobileTokenHelper.getUserName());
			newTopic.setSource("移动端用户");
			newTopic.setName(bo.getName());
			this.save(newTopic);

			BbsTopicListVO topicVO = new BbsTopicListVO();
			topicVO.setId(newTopic.getId());
			topicVO.setName(newTopic.getName());
			return topicVO;
		}
	}

}
