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.common.utils.poi.ExcelUtil;
import com.fs.fastGpt.domain.FastGptChatSession;
import com.fs.fastGpt.param.FastGptChatSessionParam;
import com.fs.fastGpt.service.IFastGptChatMsgService;
import com.fs.fastGpt.service.IFastGptChatSessionService;
import com.fs.fastGpt.vo.FastGptChatMsgCVO;
import com.fs.fastGpt.vo.FastGptChatSessionCVO;
import com.fs.fastGpt.vo.FastGptChatSessionListCVO;
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-10-10
 */
@RestController
@RequestMapping("/fastGpt/fastGptChatSession")
public class FastGptChatSessionController extends BaseController
{
    @Autowired
    private IFastGptChatSessionService fastGptChatSessionService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private IFastGptChatMsgService fastGptChatMsgService;


    /**
     * 查询对话关系列表
     */
    @PreAuthorize("@ss.hasPermi('fastGpt:fastGptChatSession:list')")
    @GetMapping("/list")
    public TableDataInfo list(FastGptChatSessionParam param)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setCompanyId(loginUser.getCompany().getCompanyId());
        List<FastGptChatSessionListCVO> list = fastGptChatSessionService.selectFastGptChatSessionListCVO(param);
        return getDataTable(list);
    }

    /**
     * 导出对话关系列表
     */
    @PreAuthorize("@ss.hasPermi('fastGpt:fastGptChatSession:export')")
    @Log(title = "对话关系", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FastGptChatSession fastGptChatSession)
    {
        List<FastGptChatSession> list = fastGptChatSessionService.selectFastGptChatSessionList(fastGptChatSession);
        ExcelUtil<FastGptChatSession> util = new ExcelUtil<FastGptChatSession>(FastGptChatSession.class);
        return util.exportExcel(list, "对话关系数据");
    }

    /**
     * 获取对话关系详细信息
     */
    @PreAuthorize("@ss.hasPermi('fastGpt:fastGptChatSession:query')")
    @GetMapping(value = "/{sessionId}")
    public R getInfo(@PathVariable("sessionId") Long sessionId)
    {
        FastGptChatSessionCVO sessionCVO = fastGptChatSessionService.selectFastGptChatSessionCVOBySessionId(sessionId);

        List<FastGptChatMsgCVO> list = fastGptChatMsgService.selectFastGptChatMsgCVOBySessionId(sessionId);

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

    /**
     * 新增对话关系
     */
    @PreAuthorize("@ss.hasPermi('fastGpt:fastGptChatSession:add')")
    @Log(title = "对话关系", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FastGptChatSession fastGptChatSession)
    {
        return toAjax(fastGptChatSessionService.insertFastGptChatSession(fastGptChatSession));
    }

    /**
     * 修改对话关系
     */
    @PreAuthorize("@ss.hasPermi('fastGpt:fastGptChatSession:edit')")
    @Log(title = "对话关系", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FastGptChatSession fastGptChatSession)
    {
        FastGptChatSession session = new FastGptChatSession();
        session.setSessionId(fastGptChatSession.getSessionId());
        session.setIsArtificial(fastGptChatSession.getIsArtificial());
        return toAjax(fastGptChatSessionService.updateFastGptChatSession(session));
    }

    /**
     * 删除对话关系
     */
    @PreAuthorize("@ss.hasPermi('fastGpt:fastGptChatSession:remove')")
    @Log(title = "对话关系", businessType = BusinessType.DELETE)
	@DeleteMapping("/{sessionIds}")
    public AjaxResult remove(@PathVariable Long[] sessionIds)
    {
        return toAjax(fastGptChatSessionService.deleteFastGptChatSessionBySessionIds(sessionIds));
    }
}
