package com.fs.chat.controller;

import com.fs.ai.service.IBaiduAIService;
import com.fs.chat.domain.ChatDataset;
import com.fs.chat.param.ChatDatasetCParam;
import com.fs.chat.service.IChatDatasetService;
import com.fs.chat.service.IChatRoleService;
import com.fs.chat.vo.ChatDatasetListCVO;
import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.domain.R;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.ServletUtils;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.core.security.LoginUser;
import com.fs.core.web.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 知识库Controller
 *
 * @author fs
 * @date 2024-04-21
 */
@RestController
@RequestMapping("/chat/chatDataset")
public class ChatDatasetController extends BaseController
{
    @Autowired
    private IChatDatasetService chatDatasetService;
    @Autowired
    private IChatRoleService chatRoleService;
    @Autowired
    private IBaiduAIService baiduAIService;

    @Autowired
    private TokenService tokenService;
    /**
     * 查询知识库列表
     */
    @PreAuthorize("@ss.hasPermi('chat:chatDataset:list')")
    @GetMapping("/list")
    public TableDataInfo list(ChatDatasetCParam chatDataset)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        chatDataset.setCompanyId(loginUser.getCompany().getCompanyId());
        List<ChatDatasetListCVO> list = chatDatasetService.selectChatDatasetListCVO(chatDataset);
        return getDataTable(list);
    }

    /**
     * 导出知识库列表
     */
    @PreAuthorize("@ss.hasPermi('chat:chatDataset:export')")
    @Log(title = "知识库", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ChatDataset chatDataset)
    {
        List<ChatDataset> list = chatDatasetService.selectChatDatasetList(chatDataset);
        ExcelUtil<ChatDataset> util = new ExcelUtil<ChatDataset>(ChatDataset.class);
        return util.exportExcel(list, "知识库数据");
    }

    /**
     * 获取知识库详细信息
     */
    @PreAuthorize("@ss.hasPermi('chat:chatDataset:query')")
    @GetMapping(value = "/{datasetId}")
    public AjaxResult getInfo(@PathVariable("datasetId") Long datasetId)
    {
        return AjaxResult.success(chatDatasetService.selectChatDatasetByDatasetId(datasetId));
    }

    /**
     * 新增知识库
     */
    @PreAuthorize("@ss.hasPermi('chat:chatDataset:add')")
    @Log(title = "知识库", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody ChatDataset chatDataset)
    {
        //判断角色是否已创建知识库
       if(chatDatasetService.check(chatDataset.getRoleId())>0){
           return R.error("此客服角色已创建知识库");
       }
        String id=baiduAIService.createDataset(chatDataset.getDatasetName());
        chatDataset.setExtId(id);
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        chatDataset.setCompanyId(loginUser.getCompany().getCompanyId());
        return chatDatasetService.insertChatDataset(chatDataset)>0?R.ok():R.error();
    }

    /**
     * 修改知识库
     */
    @PreAuthorize("@ss.hasPermi('chat:chatDataset:edit')")
    @Log(title = "知识库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ChatDataset chatDataset)
    {
        ChatDataset chatDataset1=chatDatasetService.selectChatDatasetByDatasetId(chatDataset.getDatasetId());
        if(!chatDataset1.getRoleId().equals(chatDataset.getRoleId())){
            if(chatDatasetService.check(chatDataset.getRoleId())>0){
                return AjaxResult.error("此客服角色已创建知识库");
            }
        }


        return toAjax(chatDatasetService.updateChatDataset(chatDataset));
    }

    /**
     * 删除知识库
     */
    @PreAuthorize("@ss.hasPermi('chat:chatDataset:remove')")
    @Log(title = "知识库", businessType = BusinessType.DELETE)
	@DeleteMapping("/{datasetIds}")
    public AjaxResult remove(@PathVariable Long[] datasetIds)
    {

        return toAjax(chatDatasetService.deleteChatDatasetByDatasetIds(datasetIds));
    }
}
