package com.fs.chat.controller;

import java.util.List;
import java.util.Map;

import com.fs.chat.domain.ChatMsgLogs;
import com.fs.chat.param.ChatMsgListCParam;
import com.fs.chat.service.IChatMsgLogsService;
import com.fs.chat.vo.ChatMsgListCVO;
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.ChatMsg;
import com.fs.chat.service.IChatMsgService;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.core.page.TableDataInfo;

/**
 * 聊天消息记录Controller
 *
 * @author fs
 * @date 2024-04-19
 */
@RestController
@RequestMapping("/chat/chatMsg")
public class ChatMsgController extends BaseController
{
    @Autowired
    private IChatMsgService chatMsgService;
    @Autowired
    private TokenService tokenService;

    @Autowired
    private IChatMsgLogsService chatMsgLogsService;
    /**
     * 查询聊天消息记录列表
     */
    @PreAuthorize("@ss.hasPermi('chat:chatMsg:list')")
    @GetMapping("/list")
    public TableDataInfo list(ChatMsgListCParam param)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setCompanyId(loginUser.getCompany().getCompanyId());
        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(ChatMsg chatMsg)
    {
        List<ChatMsg> list = chatMsgService.selectChatMsgList(chatMsg);
        ExcelUtil<ChatMsg> util = new ExcelUtil<ChatMsg>(ChatMsg.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)
    {
        return toAjax(chatMsgService.insertChatMsg(chatMsg));
    }

    /**
     * 修改聊天消息记录
     */
    @PreAuthorize("@ss.hasPermi('chat:chatMsg:edit')")
    @Log(title = "聊天消息记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ChatMsg 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());
        }
        if (chatMsg.getStatus()!=null){
            logs.setLogsType(1);
        }
        String userContent = chatMsgService.selectUserContent(map.getMsgId(), map.getUserId(),map.getRoleId());

        logs.setSContent(map.getContent());
        logs.setUserContent(userContent);
        logs.setCompanyUserId(loginUser.getUser().getUserId());
        logs.setCreateBy(loginUser.getUser().getNickName());
        logs.setCompanyId(loginUser.getCompany().getCompanyId());
        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));
    }
}
