package com.kexue.skills.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kexue.skills.entity.CmsCategoryTag;
import com.kexue.skills.entity.CmsTag;
import com.kexue.skills.entity.dto.CmsCategoryTagDto;
import com.kexue.skills.entity.dto.CmsTagDto;
import com.kexue.skills.mapper.CmsCategoryTagMapper;
import com.kexue.skills.service.CmsCategoryTagService;
import com.kexue.skills.service.CmsTagService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * (CmsCategoryTag)表服务实现类
 *
 * @author 王志维
 * @since 2025-02-21 23:01:48
 */
@Service("cmsCategoryTagService")
@Transactional(rollbackFor = Exception.class)
public class CmsCategoryTagServiceImpl implements CmsCategoryTagService {
    @Resource
    private CmsCategoryTagMapper cmsCategoryTagMapper;
    
    @Resource
    private CmsTagService cmsTagService;

    /**
     * 分页查询
     *
     * @param queryDto 筛选条件
     * @return 查询结果
     */
    @Override
    public PageInfo<CmsCategoryTag> getPageList(CmsCategoryTagDto queryDto) {
        PageHelper.startPage(queryDto.getPageNum(), queryDto.getPageSize());
        List<CmsCategoryTag> list = this.cmsCategoryTagMapper.getPageList(queryDto);
        return new PageInfo<>(list);
    }

    /**
     * 查询列表
     *
     * @param queryDto 筛选条件
     * @return 查询结果
     */
    @Override
    public List<CmsCategoryTag> getList(CmsCategoryTagDto queryDto) {
        return this.cmsCategoryTagMapper.getList(queryDto);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public CmsCategoryTag queryById(Long id) {
        return this.cmsCategoryTagMapper.queryById(id);
    }

    /**
     * 新增数据
     *
     * @param cmsCategoryTag 实例对象
     * @return 实例对象
     */
    @Override
    public CmsCategoryTag insert(CmsCategoryTag cmsCategoryTag) {
        // 设置创建时间和更新时间
        Date now = new Date();
        cmsCategoryTag.setCreateTime(now);
        cmsCategoryTag.setUpdateTime(now);
        // 设置默认值
        cmsCategoryTag.setDeleteFlag(0);
        // 保存数据
        this.cmsCategoryTagMapper.insert(cmsCategoryTag);
        return cmsCategoryTag;
    }

    /**
     * 更新数据
     *
     * @param cmsCategoryTag 实例对象
     * @return 实例对象
     */
    @Override
    public CmsCategoryTag update(CmsCategoryTag cmsCategoryTag) {
        // 设置更新时间
        cmsCategoryTag.setUpdateTime(new Date());
        // 更新数据
        this.cmsCategoryTagMapper.update(cmsCategoryTag);
        return this.queryById(cmsCategoryTag.getId());
    }

    /**
     * 通过主键逻辑删除
     *
     * @param id 主键
     * @param updateBy 更新人
     * @return 影响行数
     */
    @Override
    public int logicDeleteById(Long id, String updateBy) {
        return this.cmsCategoryTagMapper.logicDeleteById(id, updateBy);
    }

    /**
     * 通过主键物理删除
     *
     * @param id 主键
     * @return 影响行数
     */
    @Override
    public int deleteById(Long id) {
        return this.cmsCategoryTagMapper.deleteById(id);
    }

    /**
     * 根据分类ID删除关联
     *
     * @param categoryId 分类ID
     * @param updateBy 更新人
     * @return 影响行数
     */
    @Override
    public int deleteByCategoryId(Long categoryId, String updateBy) {
        return this.cmsCategoryTagMapper.deleteByCategoryId(categoryId, updateBy);
    }

    /**
     * 根据标签ID删除关联
     *
     * @param tagId 标签ID
     * @param updateBy 更新人
     * @return 影响行数
     */
    @Override
    public int deleteByTagId(Long tagId, String updateBy) {
        return this.cmsCategoryTagMapper.deleteByTagId(tagId, updateBy);
    }

    /**
     * 批量插入关联
     *
     * @param categoryTagList 关联列表
     * @return 影响行数
     */
    @Override
    public int batchInsert(List<CmsCategoryTag> categoryTagList) {
        if (categoryTagList == null || categoryTagList.isEmpty()) {
            return 0;
        }
        // 设置默认值
        Date now = new Date();
        for (CmsCategoryTag tag : categoryTagList) {
            tag.setCreateTime(now);
            tag.setUpdateTime(now);
            tag.setDeleteFlag(0);
        }
        return this.cmsCategoryTagMapper.batchInsert(categoryTagList);
    }

    /**
     * 批量设置分类标签关联
     *
     * @param categoryId 分类ID
     * @param tagIds 标签ID列表
     * @param updateBy 更新人
     * @return 影响行数
     */
    @Override
    public int batchSetCategoryTags(Long categoryId, List<Long> tagIds, String updateBy) {
        // 先删除该分类的所有关联
        int deleteCount = this.deleteByCategoryId(categoryId, updateBy);
        
        // 然后批量插入新的关联
        if (tagIds == null || tagIds.isEmpty()) {
            return deleteCount;
        }
        
        List<CmsCategoryTag> categoryTagList = new ArrayList<>();
        Date now = new Date();
        for (Long tagId : tagIds) {
            CmsCategoryTag categoryTag = new CmsCategoryTag();
            categoryTag.setCategoryId(categoryId);
            categoryTag.setTagId(tagId);
            categoryTag.setCreateTime(now);
            categoryTag.setUpdateTime(now);
            categoryTag.setCreateBy(updateBy);
            categoryTag.setUpdateBy(updateBy);
            categoryTag.setDeleteFlag(0);
            categoryTagList.add(categoryTag);
        }
        
        int insertCount = this.batchInsert(categoryTagList);
        return deleteCount + insertCount;
    }

    /**
     * 根据分类ID获取标签列表
     *
     * @param categoryId 分类ID
     * @return 标签列表
     */
    @Override
    public List<CmsTag> getTagsByCategoryId(Long categoryId) {
        // 直接使用SQL联合查询获取标签列表，提高效率
        return cmsTagService.getTagsByCategoryId(categoryId);
    }
}
