package com.xindazhou.business.service.community.topic;

import com.xindazhou.business.controller.admin.community.topic.vo.TopicPageReqVO;
import com.xindazhou.business.controller.admin.community.topic.vo.TopicRespVO;
import com.xindazhou.business.controller.admin.community.topic.vo.TopicSaveReqVO;
import com.xindazhou.business.convert.community.topic.TopicConvert;
import com.xindazhou.business.dal.community.dataobject.topic.TopicDO;
import com.xindazhou.business.dal.community.mysql.topic.CommunityTopicMapper;
import com.xindazhou.framework.common.pojo.PageResult;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import java.util.List;

import static com.xindazhou.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.xindazhou.framework.common.exception.enums.GlobalErrorCodeConstants.NOT_FOUND;

/**
 * 话题 Service 实现类
 *
 * @author xindazhou
 */
@Service("communityTopicServiceImpl")
@Slf4j
@Validated
public class TopicServiceImpl implements TopicService {

    @Resource
    private CommunityTopicMapper topicMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createTopic(TopicSaveReqVO createReqVO) {
        // 校验话题名称唯一性
        TopicDO existingTopic = topicMapper.selectByName(createReqVO.getName());
        if (existingTopic != null) {
            throw exception(NOT_FOUND, "话题名称已存在");
        }

        // 插入话题
        TopicDO topic = TopicConvert.INSTANCE.convert(createReqVO);
        topic.setPostCount(0);
        topic.setSort(createReqVO.getSort() != null ? createReqVO.getSort() : 0);
        topic.setStatus(createReqVO.getStatus() != null ? createReqVO.getStatus() : 1);
        topicMapper.insert(topic);
        return topic.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateTopic(TopicSaveReqVO updateReqVO) {
        // 校验话题存在
        validateTopicExists(updateReqVO.getId());

        // 如果修改了名称，校验唯一性
        TopicDO existingTopic = topicMapper.selectByName(updateReqVO.getName());
        if (existingTopic != null && !existingTopic.getId().equals(updateReqVO.getId())) {
            throw exception(NOT_FOUND, "话题名称已存在");
        }

        // 更新话题
        TopicDO updateObj = TopicConvert.INSTANCE.convert(updateReqVO);
        topicMapper.updateById(updateObj);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteTopic(Long id) {
        // 校验话题存在
        validateTopicExists(id);
        // 删除（逻辑删除）
        topicMapper.deleteById(id);
    }

    @Override
    public TopicRespVO getTopic(Long id) {
        TopicDO topic = validateTopicExists(id);
        return TopicConvert.INSTANCE.convert(topic);
    }

    @Override
    public PageResult<TopicRespVO> getTopicPage(TopicPageReqVO reqVO) {
        PageResult<TopicDO> pageResult = topicMapper.selectPage(reqVO);
        return TopicConvert.INSTANCE.convertPage(pageResult);
    }

    @Override
    public List<TopicRespVO> getEnabledTopicList() {
        List<TopicDO> topics = topicMapper.selectEnabledList();
        return TopicConvert.INSTANCE.convertList(topics);
    }

    private TopicDO validateTopicExists(Long id) {
        TopicDO topic = topicMapper.selectById(id);
        if (topic == null) {
            throw exception(NOT_FOUND, "话题不存在");
        }
        return topic;
    }

}

