package com.jeesharp.business.modules.ai.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jeesharp.business.modules.ai.entity.AiFaqCategory;
import com.jeesharp.business.modules.ai.mapper.AiFaqCategoryMapper;
import com.jeesharp.business.modules.ai.service.AiFaqCategoryService;
import com.jeesharp.business.modules.ai.vo.AiFaqCategoryQueryVO;
import com.jeesharp.framework.mybatis.core.service.CrudService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(readOnly = true)
public class AiFaqCategoryServiceImpl extends CrudService<AiFaqCategoryMapper, AiFaqCategory, AiFaqCategoryQueryVO>
        implements AiFaqCategoryService {

    @Override
    @Transactional(readOnly = false)
    public void createCategory(AiFaqCategory entity) {
        save(entity);
    }

    @Override
    @Transactional(readOnly = false)
    public void updateCategory(AiFaqCategory entity) {
        update(entity);
    }

    @Override
    public List<AiFaqCategory> listByIds(List<String> ids) {
        if (ids == null || ids.isEmpty()) {
            return List.of();
        }
        List<String> idList = ids.stream()
                .filter(StrUtil::isNotBlank)
                .map(String::trim)
                .distinct()
                .toList();
        if (idList.isEmpty()) {
            return List.of();
        }
        return getBaseMapper().selectList(new LambdaQueryWrapper<AiFaqCategory>().in(AiFaqCategory::getId, idList));
    }

    @Override
    public List<String> findIdsByCategoryName(String categoryName) {
        if (StrUtil.isBlank(categoryName)) {
            return List.of();
        }
        AiFaqCategoryQueryVO query = new AiFaqCategoryQueryVO();
        query.setCategoryName(categoryName.trim());
        return findList(query).stream()
                .map(AiFaqCategory::getId)
                .filter(StrUtil::isNotBlank)
                .distinct()
                .toList();
    }

}
