package com.fs.company.controller.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.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.FastGptChatMsg;
import com.fs.fastGpt.domain.FastGptChatMsgLogs;
import com.fs.fastGpt.param.FastGptChatMsgListCParam;
import com.fs.fastGpt.service.IFastGptChatMsgLogsService;
import com.fs.fastGpt.service.IFastGptChatMsgService;
import com.fs.fastGpt.vo.FastGptChatMsgListCVO;
import com.fs.framework.security.LoginUser;
import com.fs.framework.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/fastGptChatMsg")
public class FastGptChatMsgController extends BaseController
{
    @Autowired
    private IFastGptChatMsgService fastGptChatMsgService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private IFastGptChatMsgLogsService iFastGptChatMsgLogsService;

    /**
     * 查询聊天记录列表
     */
    @PreAuthorize("@ss.hasPermi('fastGpt:fastGptChatMsg:list')")
    @GetMapping("/list")
    public TableDataInfo list(FastGptChatMsgListCParam param)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setCompanyId(loginUser.getCompany().getCompanyId());

        List<FastGptChatMsgListCVO> list = fastGptChatMsgService.selectFastGptChatMsgListCVO(param);
        return getDataTable(list);
    }

    /**
     * 导出聊天记录列表
     */
    @PreAuthorize("@ss.hasPermi('fastGpt:fastGptChatMsg:export')")
    @Log(title = "聊天记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FastGptChatMsg fastGptChatMsg)
    {
        List<FastGptChatMsg> list = fastGptChatMsgService.selectFastGptChatMsgList(fastGptChatMsg);
        ExcelUtil<FastGptChatMsg> util = new ExcelUtil<FastGptChatMsg>(FastGptChatMsg.class);
        return util.exportExcel(list, "聊天记录数据");
    }

    /**
     * 获取聊天记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('fastGpt:fastGptChatMsg:query')")
    @GetMapping(value = "/{msgId}")
    public AjaxResult getInfo(@PathVariable("msgId") Long msgId)
    {
        return AjaxResult.success(fastGptChatMsgService.selectFastGptChatMsgVOByMsgId(msgId));
    }

    /**
     * 新增聊天记录
     */
    @PreAuthorize("@ss.hasPermi('fastGpt:fastGptChatMsg:add')")
    @Log(title = "聊天记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FastGptChatMsg fastGptChatMsg)
    {
        return toAjax(fastGptChatMsgService.insertFastGptChatMsg(fastGptChatMsg));
    }

    /**
     * 修改聊天记录
     */
    @PreAuthorize("@ss.hasPermi('fastGpt:fastGptChatMsg:edit')")
    @Log(title = "聊天记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FastGptChatMsg param)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        FastGptChatMsg fastGptChatMsg = fastGptChatMsgService.selectFastGptChatMsgByMsgId(param.getMsgId());

        FastGptChatMsgLogs logs = new FastGptChatMsgLogs();

        logs.setMsgId(param.getMsgId());
        logs.setLogsType(2);
        if (param.getContent()!=null&&!param.getContent().equals("")){
            logs.setContent(param.getContent());
        }
        if (param.getStatus()!=null){
            logs.setLogsType(1);
        }
        String userContent = fastGptChatMsgService.selectUserContent(fastGptChatMsg.getMsgId(), fastGptChatMsg.getUserId(),fastGptChatMsg.getRoleId());
        logs.setSContent(fastGptChatMsg.getContent());
        logs.setUserContent(userContent);
        logs.setCompanyUserId(loginUser.getUser().getUserId());
        logs.setCreateBy(loginUser.getUser().getNickName());
        logs.setCompanyId(loginUser.getCompany().getCompanyId());

        iFastGptChatMsgLogsService.insertFastGptChatMsgLogs(logs);

        return toAjax(fastGptChatMsgService.updateFastGptChatMsg(param));
    }

    /**
     * 删除聊天记录
     */
    @PreAuthorize("@ss.hasPermi('fastGpt:fastGptChatMsg:remove')")
    @Log(title = "聊天记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{msgIds}")
    public AjaxResult remove(@PathVariable Long[] msgIds)
    {
        return toAjax(fastGptChatMsgService.deleteFastGptChatMsgByMsgIds(msgIds));
    }
}
