package com.fs.tag.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.fs.common.utils.DateUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fs.course.vo.FsUserCourseVideoVO;
import com.fs.qw.domain.QwTag;
import com.fs.qw.domain.QwTagGroup;
import com.fs.qw.mapper.QwTagGroupMapper;
import com.fs.qw.mapper.QwTagMapper;
import com.fs.tag.domain.FsVideoCourseTag;
import com.fs.tag.mapper.FsVideoCourseTagMapper;
import com.fs.tag.service.IFsVideoCourseTagService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

/**
 * 视频小节看课标签关联Service业务层处理
 *
 * @author fs
 * @date 2025-11-05
 */
@Slf4j
@Service
public class FsVideoCourseTagServiceImpl extends ServiceImpl<FsVideoCourseTagMapper, FsVideoCourseTag> implements IFsVideoCourseTagService {

    /**
     * 查询视频小节看课标签关联
     *
     * @param id 视频小节看课标签关联主键
     * @return 视频小节看课标签关联
     */
    @Override
    public FsVideoCourseTag selectFsVideoCourseTagById(Long id)
    {
        return baseMapper.selectFsVideoCourseTagById(id);
    }


    @Autowired
    private QwTagGroupMapper qwTagGroupMapper;

    @Autowired
    private QwTagMapper qwTagMapper;
    /**
     * 查询视频小节看课标签关联列表
     *
     * @param fsVideoCourseTag 视频小节看课标签关联
     * @return 视频小节看课标签关联
     */
    @Override
    public List<FsVideoCourseTag> selectFsVideoCourseTagList(FsVideoCourseTag fsVideoCourseTag)
    {
        List<FsVideoCourseTag> fsVideoCourseTags = baseMapper.selectFsVideoCourseTagList(fsVideoCourseTag);
        for (FsVideoCourseTag item : fsVideoCourseTags) {
            if(ObjectUtils.isNotNull(item.getWatchingGroupId())){
                QwTagGroup qwTagGroup = qwTagGroupMapper.selectQwTagGroupById(item.getWatchingGroupId());
                if(ObjectUtils.isNotNull(qwTagGroup)){
                    item.setWatchingGroupName(qwTagGroup.getName());
                }
            }
            if(ObjectUtils.isNotNull(item.getWatchedGroupId())){
                QwTagGroup qwTagGroup = qwTagGroupMapper.selectQwTagGroupById(item.getWatchedGroupId());
                if(ObjectUtils.isNotNull(qwTagGroup)){
                    item.setWatchedGroupName(qwTagGroup.getName());
                }
            }

            if(ObjectUtils.isNotNull(item.getWatchingTgId())){
                QwTag qwTag = qwTagMapper.selectQwTagById(item.getWatchingTgId());
                if(ObjectUtils.isNotNull(qwTag)){
                    item.setWatchingTgName(qwTag.getName());
                }
            }

            if(ObjectUtils.isNotNull(item.getWatchedTgId())) {
                QwTag qwTag = qwTagMapper.selectQwTagById(item.getWatchedTgId());
                if(ObjectUtils.isNotNull(qwTag)){
                    item.setWatchedTgName(qwTag.getName());
                }
            }
        }

        return fsVideoCourseTags;
    }

    /**
     * 新增视频小节看课标签关联
     *
     * @param fsVideoCourseTag 视频小节看课标签关联
     * @return 结果
     */
    @Override
    public int insertFsVideoCourseTag(FsVideoCourseTag fsVideoCourseTag)
    {
        try{
            fsVideoCourseTag.setCreateTime(DateUtils.getNowDate());
            return baseMapper.insertFsVideoCourseTag(fsVideoCourseTag);
        }catch (DuplicateKeyException e){
            log.info("同一主体的小节添加标签不允许重复！");
            throw new RuntimeException("同一主体的小节添加标签不允许重复！");
        }
    }

    /**
     * 修改视频小节看课标签关联
     *
     * @param fsVideoCourseTag 视频小节看课标签关联
     * @return 结果
     */
    @Override
    public int updateFsVideoCourseTag(FsVideoCourseTag fsVideoCourseTag)
    {
        fsVideoCourseTag.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateFsVideoCourseTag(fsVideoCourseTag);
    }

    /**
     * 批量删除视频小节看课标签关联
     *
     * @param ids 需要删除的视频小节看课标签关联主键
     * @return 结果
     */
    @Override
    public int deleteFsVideoCourseTagByIds(Long[] ids)
    {
        return baseMapper.deleteFsVideoCourseTagByIds(ids);
    }

    /**
     * 删除视频小节看课标签关联信息
     *
     * @param id 视频小节看课标签关联主键
     * @return 结果
     */
    @Override
    public int deleteFsVideoCourseTagById(Long id)
    {
        return baseMapper.deleteFsVideoCourseTagById(id);
    }
}
