package com.gxwebsoft.common.system.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.service.ChatConversationService;
import com.gxwebsoft.common.system.entity.ChatConversation;
import com.gxwebsoft.common.system.param.ChatConversationParam;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.core.annotation.OperationLog;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 聊天消息表控制器
 *
 * @author 科技小王子
 * @since 2024-04-27 15:57:27
 */
@Tag(name = "聊天")
@RestController
@RequestMapping("/api/system/chat-conversation")
public class ChatConversationController extends BaseController {
    @Resource
    private ChatConversationService chatConversationService;

    @PreAuthorize("hasAuthority('sys:chatConversation:list')")
    @OperationLog
    @Operation(summary = "分页查询聊天消息表")
    @GetMapping("/page")
    public ApiResult<PageResult<ChatConversation>> page(ChatConversationParam param) {
        // 使用关联查询
        return success(chatConversationService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('sys:chatConversation:list')")
    @OperationLog
    @Operation(summary = "查询全部聊天消息表")
    @GetMapping()
    public ApiResult<List<ChatConversation>> list(ChatConversationParam param) {
        // 使用关联查询
        return success(chatConversationService.listRel(param));
    }

    @PreAuthorize("hasAuthority('sys:chatConversation:list')")
    @OperationLog
    @Operation(summary = "根据id查询聊天消息表")
    @GetMapping("/{id}")
    public ApiResult<ChatConversation> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(chatConversationService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('sys:chatConversation:save')")
    @OperationLog
    @Operation(summary = "添加聊天消息表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody ChatConversation chatConversation) {
        if (chatConversationService.save(chatConversation)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('sys:chatConversation:update')")
    @OperationLog
    @Operation(summary = "修改聊天消息表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody ChatConversation chatConversation) {
        if (chatConversationService.updateById(chatConversation)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('sys:chatConversation:remove')")
    @OperationLog
    @Operation(summary = "删除聊天消息表")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (chatConversationService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('sys:chatConversation:save')")
    @OperationLog
    @Operation(summary = "批量添加聊天消息表")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<ChatConversation> list) {
        if (chatConversationService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('sys:chatConversation:update')")
    @OperationLog
    @Operation(summary = "批量修改聊天消息表")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<ChatConversation> batchParam) {
        if (batchParam.update(chatConversationService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('sys:chatConversation:remove')")
    @OperationLog
    @Operation(summary = "批量删除聊天消息表")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (chatConversationService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
