package com.fs.fastGpt;


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.fastGpt.domain.FastGptCollection;
import com.fs.fastGpt.service.IFastGptCollectionService;
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.text.ParseException;
import java.util.List;

/**
 * 集合Controller
 *
 * @author fs
 * @date 2024-09-30
 */
@RestController
@RequestMapping("/fastGpt/fastGptCollection")
public class FastGptCollectionController extends BaseController
{
    @Autowired
    private IFastGptCollectionService fastGptCollectionService;
    @Autowired
    private TokenService tokenService;
    /**
     * 查询集合列表
     */
    @PreAuthorize("@ss.hasPermi('fastGpt:fastGptCollection:list')")
    @GetMapping("/list")
    public TableDataInfo list(FastGptCollection fastGptCollection)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        fastGptCollection.setCompanyId(loginUser.getCompany().getCompanyId());
        List<FastGptCollection> list = fastGptCollectionService.selectFastGptCollectionList(fastGptCollection);
        return getDataTable(list);
    }

    /**
     * 获取集合详细信息
     */
    @PreAuthorize("@ss.hasPermi('fastGpt:fastGptCollection:query')")
    @GetMapping(value = "/{collectionId}")
    public AjaxResult getInfo(@PathVariable("collectionId") String collectionId)
    {
        return AjaxResult.success(fastGptCollectionService.selectFastGptCollectionByCollectionId(collectionId));
    }

    /**
     * 新增集合
     */
    @PreAuthorize("@ss.hasPermi('fastGpt:fastGptCollection:add')")
    @Log(title = "集合", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody FastGptCollection fastGptCollection) throws Exception {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        fastGptCollection.setCompanyId(loginUser.getCompany().getCompanyId());
        return fastGptCollectionService.insertFastGptCollection(fastGptCollection);
    }

    /**
     * 修改集合
     */
    @PreAuthorize("@ss.hasPermi('fastGpt:fastGptCollection:edit')")
    @Log(title = "集合", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody FastGptCollection fastGptCollection)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        fastGptCollection.setCompanyId(loginUser.getCompany().getCompanyId());
        return fastGptCollectionService.updateFastGptCollection(fastGptCollection);
    }

    /**
     * 删除集合
     */
    @PreAuthorize("@ss.hasPermi('fastGpt:fastGptCollection:remove')")
    @Log(title = "集合", businessType = BusinessType.DELETE)
	@DeleteMapping("/{collectionIds}")
    public R remove(@PathVariable String[] collectionIds)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        return fastGptCollectionService.deleteFastGptCollectionByCollectionIds(collectionIds,loginUser.getCompany().getCompanyId());
    }

    /**
    * 同步集合
     * datasetId 知识库id
    */
    @PreAuthorize("@ss.hasPermi('fastGpt:fastGptCollection:sync')")
    @GetMapping("/syncCollection/{datasetId}")
    public R syncCollection(@PathVariable String datasetId) throws ParseException {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        return fastGptCollectionService.syncCollection(datasetId,loginUser.getCompany().getCompanyId());
    }
}
