package com.fs.chat.controller;

import java.util.List;

import com.fs.chat.domain.ChatMsg;
import com.fs.chat.param.ChatSessionListCParam;
import com.fs.chat.service.IChatMsgService;
import com.fs.chat.vo.ChatMsgVO;
import com.fs.chat.vo.ChatSessionCVO;
import com.fs.chat.vo.ChatSessionListCVO;
import com.fs.common.core.domain.R;
import com.fs.common.utils.ServletUtils;
import com.fs.framework.security.LoginUser;
import com.fs.framework.service.TokenService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.enums.BusinessType;
import com.fs.chat.domain.ChatSession;
import com.fs.chat.service.IChatSessionService;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.core.page.TableDataInfo;

/**
 * 会话Controller
 *
 * @author fs
 * @date 2024-05-11
 */
@RestController
@RequestMapping("/chat/chatSession")
public class ChatSessionController extends BaseController
{
    @Autowired
    private IChatSessionService chatSessionService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private IChatMsgService chatMsgService;

    /**
     * 查询会话列表
     */
    @PreAuthorize("@ss.hasPermi('chat:chatSession:list')")
    @GetMapping("/list")
    public TableDataInfo list(ChatSessionListCParam param)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setCompanyId(loginUser.getCompany().getCompanyId());
        List<ChatSessionListCVO> list = chatSessionService.selectChatSessionListCVO(param);
        return getDataTable(list);
    }

    /**
     * 导出会话列表
     */
    @PreAuthorize("@ss.hasPermi('chat:chatSession:export')")
    @Log(title = "会话", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ChatSession chatSession)
    {
        List<ChatSession> list = chatSessionService.selectChatSessionList(chatSession);
        ExcelUtil<ChatSession> util = new ExcelUtil<ChatSession>(ChatSession.class);
        return util.exportExcel(list, "会话数据");
    }

    /**
     * 获取会话详细信息
     */
    @PreAuthorize("@ss.hasPermi('chat:chatSession:query')")
    @GetMapping(value = "/{sessionId}")
    public R getInfo(@PathVariable("sessionId") Long sessionId)
    {
        ChatSessionCVO sessionCVO = chatSessionService.selectChatSessionCVOBySessionId(sessionId);
        List<ChatMsgVO> list = chatMsgService.selectChatMsgBySessionId(sessionId);
        return R.ok().put("data",sessionCVO).put("list",list);
    }

    /**
     * 新增会话
     */
    @PreAuthorize("@ss.hasPermi('chat:chatSession:add')")
    @Log(title = "会话", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ChatSession chatSession)
    {
        return toAjax(chatSessionService.insertChatSession(chatSession));
    }

    /**
     * 修改会话
     */
    @PreAuthorize("@ss.hasPermi('chat:chatSession:edit')")
    @Log(title = "会话", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ChatSession chatSession)
    {
        return toAjax(chatSessionService.updateChatSession(chatSession));
    }

    /**
     * 删除会话
     */
    @PreAuthorize("@ss.hasPermi('chat:chatSession:remove')")
    @Log(title = "会话", businessType = BusinessType.DELETE)
	@DeleteMapping("/{sessionIds}")
    public AjaxResult remove(@PathVariable Long[] sessionIds)
    {
        return toAjax(chatSessionService.deleteChatSessionBySessionIds(sessionIds));
    }
}
