package com.fs.chat.controller;

import com.fs.chat.domain.ChatMsg;
import com.fs.chat.domain.ChatMsgLogs;
import com.fs.chat.param.ChatMsgListCParam;
import com.fs.chat.service.IChatMsgLogsService;
import com.fs.chat.service.IChatMsgService;
import com.fs.chat.vo.ChatMsgListCVO;
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.model.LoginUser;
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.framework.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-19
 */
@RestController
@RequestMapping("/chat/chatMsg")
public class ChatMsgController extends BaseController
{
    @Autowired
    private IChatMsgService chatMsgService;


    @Autowired
    private IChatMsgLogsService chatMsgLogsService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询聊天消息记录列表
     */
    @PreAuthorize("@ss.hasPermi('chat:chatMsg:list')")
    @GetMapping("/list")
    public TableDataInfo list(ChatMsgListCParam param)
    {
        startPage();
        List<ChatMsgListCVO> list = chatMsgService.selectChatMsgListCVO(param);
        return getDataTable(list);
    }

    /**
     * 导出聊天消息记录列表
     */
    @PreAuthorize("@ss.hasPermi('chat:chatMsg:export')")
    @Log(title = "聊天消息记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ChatMsgListCParam param)
    {
        List<ChatMsgListCVO> list = chatMsgService.selectChatMsgListCVO(param);
        ExcelUtil<ChatMsgListCVO> util = new ExcelUtil<ChatMsgListCVO>(ChatMsgListCVO.class);
        return util.exportExcel(list, "聊天消息记录数据");
    }

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

    /**
     * 新增聊天消息记录
     */
    @PreAuthorize("@ss.hasPermi('chat:chatMsg:add')")
    @Log(title = "聊天消息记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ChatMsg chatMsg)
    {
        chatMsg.setCompanyId(0L);
        return toAjax(chatMsgService.insertChatMsg(chatMsg));
    }

    /**
     * 修改聊天消息记录
     */
    @PreAuthorize("@ss.hasPermi('chat:chatMsg:edit')")
    @Log(title = "聊天消息记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ChatMsg chatMsg)
    {
        if (chatMsg.getStatus()!=null){
            return toAjax(chatMsgService.updateChatMsg(chatMsg));
        }
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        ChatMsg map = chatMsgService.selectChatMsgByMsgId(chatMsg.getMsgId());
        ChatMsgLogs logs = new ChatMsgLogs();
        logs.setMsgId(chatMsg.getMsgId());
        logs.setLogsType(2);
        if (chatMsg.getContent()!=null&&!chatMsg.getContent().equals("")){
            logs.setContent(chatMsg.getContent());
        }
        String userContent = chatMsgService.selectUserContent(map.getMsgId(), map.getUserId(),map.getRoleId());
        logs.setSContent(map.getContent());
        logs.setUserContent(userContent);
        logs.setCreateBy(loginUser.getUser().getNickName());
        logs.setCompanyId(0L);
        chatMsgLogsService.insertChatMsgLogs(logs);

        return toAjax(chatMsgService.updateChatMsg(chatMsg));
    }

    /**
     * 删除聊天消息记录
     */
    @PreAuthorize("@ss.hasPermi('chat:chatMsg:remove')")
    @Log(title = "聊天消息记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{msgIds}")
    public AjaxResult remove(@PathVariable Long[] msgIds)
    {
        return toAjax(chatMsgService.deleteChatMsgByMsgIds(msgIds));
    }
}
