package org.xyzh.ai.service.impl;

import java.util.Arrays;
import java.util.List;

import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.xyzh.ai.mapper.TbKnowledgeFileLogMapper;
import org.xyzh.api.ai.dto.TbKnowledgeFileLog;
import org.xyzh.api.ai.service.KnowledgeFileLogService;
import org.xyzh.common.core.domain.ResultDomain;
import org.xyzh.common.core.page.PageDomain;
import org.xyzh.common.core.page.PageParam;
import org.xyzh.common.core.page.PageRequest;
import org.xyzh.common.utils.NonUtils;
import org.xyzh.common.utils.id.IdUtil;
import org.xyzh.common.utils.validation.ValidationResult;
import org.xyzh.common.utils.validation.ValidationUtils;

@DubboService(version="1.0.0", group="ai", timeout=3000, retries=0)
public class KnowledgeFileLogServiceImpl implements KnowledgeFileLogService{
    
    private static final Logger logger = LoggerFactory.getLogger(KnowledgeServiceImpl.class);

    @Autowired
    private TbKnowledgeFileLogMapper knowledgeFileLogMapper;

    /**
     * @description    新增知识库文件操作日志
     * @param knowledgeFileLog
     * @return    日志
     * @author    yslg
     * @since 2025-12-31
     */
    @Override
    public ResultDomain<TbKnowledgeFileLog>  addKnowledgeFileLog(TbKnowledgeFileLog knowledgeFileLog){
        knowledgeFileLog.setOptsn(IdUtil.getOptsn());
        knowledgeFileLog.setLogId(IdUtil.generateID());
        ValidationResult rt = ValidationUtils.validate(knowledgeFileLog, Arrays.asList(
            ValidationUtils.requiredString("fileId",  "文件id")
        ));
        if(!rt.isValid()){
            return ResultDomain.failure("日志参数校验失败");
        }
        int result = knowledgeFileLogMapper.addKnowledgeFileLog(knowledgeFileLog);
        if(result >0){
            return ResultDomain.success("添加知识库文件日志成功", knowledgeFileLog);
        }else {
            return ResultDomain.failure("添加知识库文件日志失败");
        }
    }

    /**
     * @description    查询知识库日志操作列表
     * @param    filter
     * @return    日志列表
     * @author    yslg
     * @since 2025-12-31
     */
    @Override
    public ResultDomain<TbKnowledgeFileLog>  getKnowledgeFileLogList(TbKnowledgeFileLog filter){
        List<TbKnowledgeFileLog> logs = knowledgeFileLogMapper.getKnowledgeFileLogList(filter);
        return ResultDomain.success("查询知识库日志成功",logs);
    }

    /**
     * @description    查询知识库日志操作分页
     * @param    pageRequest
     * @return    日志分页数据
     * @author    yslg
     * @since 2025-12-31
     */
    @Override
    public ResultDomain<TbKnowledgeFileLog>  getKnowledgeFileLogPage(PageRequest<TbKnowledgeFileLog> pageRequest){
        List<TbKnowledgeFileLog> logs = knowledgeFileLogMapper.getKnowledgeFileLogPage(pageRequest.getPageParam(), pageRequest.getFilter());
        int total = knowledgeFileLogMapper.countKnowledgeFileLog(pageRequest.getFilter());
        PageDomain<TbKnowledgeFileLog> pageDomain = new PageDomain<>();
        pageDomain.setDataList(logs);
        PageParam pageParam = pageRequest.getPageParam();
        pageParam.setTotal(total);
        pageDomain.setPageParam(pageParam);
        return ResultDomain.success("查询知识库日志成功", pageDomain);
    }

    
}
