package com.ruoyi.aicall.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.aicall.domain.CcLlmKbCat;
import com.ruoyi.aicall.utils.XSSFUtils;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.aicall.mapper.CcLlmKbMapper;
import com.ruoyi.aicall.domain.CcLlmKb;
import com.ruoyi.aicall.service.ICcLlmKbService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

/**
 * 知识库内容Service业务层处理
 * 
 * @author ruoyi
 * @date 2026-01-19
 */
@Service
public class CcLlmKbServiceImpl implements ICcLlmKbService 
{
    @Autowired
    private CcLlmKbMapper ccLlmKbMapper;

    /**
     * 查询知识库内容
     * 
     * @param id 知识库内容主键
     * @return 知识库内容
     */
    @Override
    public CcLlmKb selectCcLlmKbById(Long id)
    {
        return ccLlmKbMapper.selectCcLlmKbById(id);
    }

    /**
     * 查询知识库内容列表
     * 
     * @param ccLlmKb 知识库内容
     * @return 知识库内容
     */
    @Override
    public List<CcLlmKb> selectCcLlmKbList(CcLlmKb ccLlmKb)
    {
        return ccLlmKbMapper.selectCcLlmKbList(ccLlmKb);
    }

    /**
     * 新增知识库内容
     * 
     * @param ccLlmKb 知识库内容
     * @return 结果
     */
    @Override
    public int insertCcLlmKb(CcLlmKb ccLlmKb)
    {
        return ccLlmKbMapper.insertCcLlmKb(ccLlmKb);
    }

    /**
     * 修改知识库内容
     * 
     * @param ccLlmKb 知识库内容
     * @return 结果
     */
    @Override
    public int updateCcLlmKb(CcLlmKb ccLlmKb)
    {
        return ccLlmKbMapper.updateCcLlmKb(ccLlmKb);
    }

    /**
     * 批量删除知识库内容
     * 
     * @param ids 需要删除的知识库内容主键
     * @return 结果
     */
    @Override
    public int deleteCcLlmKbByIds(String ids)
    {
        return ccLlmKbMapper.deleteCcLlmKbByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除知识库内容信息
     * 
     * @param id 知识库内容主键
     * @return 结果
     */
    @Override
    public int deleteCcLlmKbById(Long id)
    {
        return ccLlmKbMapper.deleteCcLlmKbById(id);
    }

    @Override
    public Integer selectCountByCatId(Long catId) {
        return ccLlmKbMapper.selectCountByCatId(catId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String importData(MultipartFile file, Long fixedCatId) throws Exception {
        Workbook wb = WorkbookFactory.create(file.getInputStream());
        Sheet sheet = wb.getSheetAt(0);
        if (sheet == null) throw new ServiceException("Excel 为空");
        List<CcLlmKb> ccLlmKbList = new ArrayList<>();
        // 跳过表头
        for (int i = 1; i <= sheet.getLastRowNum(); i++) {
            Row row = sheet.getRow(i);
            if (row == null) continue;
            CcLlmKb e = new CcLlmKb();
            String title = XSSFUtils.getCellString(row.getCell(0));
            e.setTitle(title);
            e.setContent(XSSFUtils.getCellString(row.getCell(1)));
            if (StringUtils.isBlank(e.getTitle())) continue;
            e.setCatId(fixedCatId);
            ccLlmKbList.add(e);
        }
        wb.close();
        ccLlmKbMapper.deleteCcLlmKbByCatId(fixedCatId);
        ccLlmKbMapper.insertBatch(ccLlmKbList);
        return "导入成功";
    }


    @Override
    public CcLlmKb selectCcLlmKbContentByTitle(Long id, String title, Long catId) {
        List<CcLlmKb> list = ccLlmKbMapper.selectCcLlmKbList(new CcLlmKb().setTitle(title).setCatId(catId));
        if (!CollectionUtils.isEmpty(list)) {
            if (null == id) {
                return list.get(0);
            } else {
                for (CcLlmKb data: list) {
                    if (data.getId() != id) {
                        return data;
                    }
                }
            }
        }
        return null;
    }
}
