package com.fs.fastGpt.service.impl;


import com.alibaba.fastjson.JSON;
import com.fs.common.BeanCopyUtils;
import com.fs.common.core.domain.R;
import com.fs.company.domain.CompanyConfig;
import com.fs.company.service.ICompanyConfigService;
import com.fs.fastGpt.domain.AiCompanyConfig;
import com.fs.fastGpt.domain.FastGptDataset;
import com.fs.fastGpt.mapper.FastGptDatasetMapper;
import com.fs.fastGpt.service.IFastGptDatasetService;
import com.fs.fastgptApi.param.CreateKnowledgeBaseParam;
import com.fs.fastgptApi.param.GetKnowledgeBaseListParam;
import com.fs.fastgptApi.result.GetKnowledgeBaseListResult;
import com.fs.fastgptApi.result.KnowledgeBaseResult;
import com.fs.fastgptApi.service.DataSetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 知识库列Service业务层处理
 * 
 * @author fs
 * @date 2024-09-30
 */
@Service
public class FastGptDatasetServiceImpl implements IFastGptDatasetService
{
    @Autowired
    private FastGptDatasetMapper fastGptDatasetMapper;

    @Autowired
    private DataSetService dataSetService;

    @Autowired
    private ICompanyConfigService companyConfigService;

    /**
     * 查询知识库列
     * 
     * @param datasetId 知识库列主键
     * @return 知识库列
     */
    @Override
    public FastGptDataset selectFastGptDatasetByDatasetId(String datasetId)
    {
        return fastGptDatasetMapper.selectFastGptDatasetByDatasetId(datasetId);
    }

    /**
     * 查询知识库列列表
     * 
     * @param fastGptDataset 知识库列
     * @return 知识库列
     */
    @Override
    public List<FastGptDataset> selectFastGptDatasetList(FastGptDataset fastGptDataset)
    {
        return fastGptDatasetMapper.selectFastGptDatasetList(fastGptDataset);
    }

    @Override
    public R syncDataset(Long companyId) {

        CompanyConfig companyConfig = companyConfigService.selectCompanyConfigByKey(companyId, "sys:AiKf:config");
        AiCompanyConfig aiCompanyConfig = JSON.parseObject(companyConfig.getConfigValue(), AiCompanyConfig.class);
        GetKnowledgeBaseListParam getKnowledgeBaseListParam = new GetKnowledgeBaseListParam();
        GetKnowledgeBaseListResult knowledgeBaseList = dataSetService.getKnowledgeBaseList(getKnowledgeBaseListParam,aiCompanyConfig.getUrl(), aiCompanyConfig.getKey());
        if (knowledgeBaseList != null && knowledgeBaseList.getCode()==200 && !knowledgeBaseList.getData().isEmpty()) {
            knowledgeBaseList.getData().forEach(item -> {
                        FastGptDataset fastGptDataset = new FastGptDataset();
                        fastGptDataset.setDatasetId(item.get_id());
                        fastGptDataset.setParentId(item.getParentId());
                        fastGptDataset.setAvatar(item.getAvatar());
                        fastGptDataset.setName(item.getName());
                        fastGptDataset.setIntro(item.getIntro());
                        fastGptDataset.setPermission(item.getPermission());
                        fastGptDataset.setCanWrite(item.isCanWrite() ? 1L : 0L);
                        fastGptDataset.setIsOwner(item.isOwner() ? 1L : 0L);
                        fastGptDataset.setVectorModel(JSON.toJSONString(item.getVectorModel()));
                        fastGptDataset.setCompanyId(companyId);
                        fastGptDataset.setUpdateTime(new Date());
                        fastGptDatasetMapper.insertOrUpdateFastGptDataset(fastGptDataset);
                    }
            );

        }
        return R.ok();
    }

    /**
     * 新增知识库列
     * 
     * @param fastGptDataset 知识库列
     * @return 结果
     */
    @Override
    public R insertFastGptDataset(FastGptDataset fastGptDataset)
    {

        //创建知识库
        CreateKnowledgeBaseParam param=new CreateKnowledgeBaseParam();
        BeanCopyUtils.copy(fastGptDataset,param);

        CompanyConfig companyConfig = companyConfigService.selectCompanyConfigByKey(fastGptDataset.getCompanyId(), "sys:AiKf:config");
        AiCompanyConfig aiCompanyConfig = JSON.parseObject(companyConfig.getConfigValue(), AiCompanyConfig.class);

        fastGptDataset.setCreateTime(new Date());
        KnowledgeBaseResult knowledgeBase = dataSetService.createKnowledgeBase(param,aiCompanyConfig.getUrl(),aiCompanyConfig.getKey());
        if(knowledgeBase!=null&& knowledgeBase.getCode()==200){
            fastGptDataset.setDatasetId(knowledgeBase.getData());
            int i = fastGptDatasetMapper.insertFastGptDataset(fastGptDataset);
            if(i>0){
                return R.ok();
            }else {
                return R.error("创建知识库失败");
            }
        }else {
            return R.error().put("msg",knowledgeBase.getMessage());
        }
    }


    /**
     * 修改知识库列
     *
     * @param fastGptDataset 知识库列
     * @return 结果
     */
    @Override
    public int updateFastGptDataset(FastGptDataset fastGptDataset)
    {
        return fastGptDatasetMapper.updateFastGptDataset(fastGptDataset);
    }

    /**
     * 批量删除知识库列
     * 
     * @param datasetIds 需要删除的知识库列主键
     * @return 结果
     */
    @Override
    public R deleteFastGptDatasetByDatasetIds(String[] datasetIds,Long companyId)
    {
        ArrayList<String> list=new ArrayList<>();

        CompanyConfig companyConfig = companyConfigService.selectCompanyConfigByKey(companyId, "sys:AiKf:config");
        AiCompanyConfig aiCompanyConfig = JSON.parseObject(companyConfig.getConfigValue(), AiCompanyConfig.class);

        for (String datasetId : datasetIds) {
            KnowledgeBaseResult knowledgeBaseResult = dataSetService.deleteKnowledgeBase(datasetId,aiCompanyConfig.getUrl(),aiCompanyConfig.getKey());
            if (knowledgeBaseResult.getCode()==200){
                fastGptDatasetMapper.deleteFastGptDatasetByDatasetId(datasetId);
            }else {
                list.add(datasetId);
            }

        }

        return R.ok().put("data",list);
    }

    /**
     * 删除知识库列信息
     * 
     * @param datasetId 知识库列主键
     * @return 结果
     */
    @Override
    public int deleteFastGptDatasetByDatasetId(String datasetId)
    {
        return fastGptDatasetMapper.deleteFastGptDatasetByDatasetId(datasetId);
    }
}
