package shop.alien.lawyer.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.store.LawyerChatMessage;
import shop.alien.lawyer.service.LawyerChatMessageService;
import shop.alien.util.myBaticsPlus.QueryBuilder;

import java.util.List;

/**
 * 聊天消息 前端控制器
 *
 * @author system
 * @since 2025-01-XX
 */
@Slf4j
@Api(tags = {"律师平台-聊天消息"})
@ApiSort(16)
@CrossOrigin
@RestController
@RequestMapping("/lawyer/chatMessage")
@RequiredArgsConstructor
public class LawyerChatMessageController {

    private final LawyerChatMessageService chatMessageService;

    @ApiOperation("新增聊天消息")
    @ApiOperationSupport(order = 1)
    @PostMapping("/addChatMessage")
    public R<LawyerChatMessage> addChatMessage(@RequestBody LawyerChatMessage chatMessage) {
        log.info("LawyerChatMessageController.addChatMessage?chatMessage={}", chatMessage);
        return chatMessageService.addChatMessage(chatMessage);
    }

    @ApiOperation("编辑聊天消息")
    @ApiOperationSupport(order = 2)
    @PostMapping("/editChatMessage")
    public R<LawyerChatMessage> editChatMessage(@RequestBody LawyerChatMessage chatMessage) {
        log.info("LawyerChatMessageController.editChatMessage?chatMessage={}", chatMessage);
        return chatMessageService.editChatMessage(chatMessage);
    }

    @ApiOperation("删除聊天消息")
    @ApiOperationSupport(order = 3)
    @DeleteMapping("/deleteChatMessage")
    public R<Boolean> deleteChatMessage(@RequestParam(value = "id") Integer id) {
        log.info("LawyerChatMessageController.deleteChatMessage?id={}", id);
        return chatMessageService.deleteChatMessage(id);
    }

    @ApiOperation("保存或更新聊天消息")
    @ApiOperationSupport(order = 4)
    @PostMapping("/saveOrUpdate")
    public R<LawyerChatMessage> saveOrUpdate(@RequestBody LawyerChatMessage chatMessage) {
        log.info("LawyerChatMessageController.saveOrUpdate?chatMessage={}", chatMessage);
        boolean result = chatMessageService.saveOrUpdate(chatMessage);
        if (result) {
            return R.data(chatMessage);
        }
        return R.fail("操作失败");
    }

    @ApiOperation("通用列表查询")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "chatSessionId", value = "聊天会话ID", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "consultationOrderId", value = "咨询订单ID", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "senderType", value = "发送者类型", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "createdTime_Start", value = "创建时间开始（范围查询）", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "createdTime_End", value = "创建时间结束（范围查询）", dataType = "String", paramType = "query")
    })
    @GetMapping("/getList")
    public R<List<LawyerChatMessage>> getList(@ModelAttribute LawyerChatMessage chatMessage) {
        log.info("LawyerChatMessageController.getList?chatMessage={}", chatMessage);
        List<LawyerChatMessage> list = QueryBuilder.of(chatMessage)
                .build()
                .list(chatMessageService);
        return R.data(list);
    }

    @ApiOperation("通用分页查询")
    @ApiOperationSupport(order = 6)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数（默认1）", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "页容（默认10）", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "id", value = "主键", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "chatSessionId", value = "聊天会话ID", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "consultationOrderId", value = "咨询订单ID", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "senderType", value = "发送者类型", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "createdTime_Start", value = "创建时间开始（范围查询）", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "createdTime_End", value = "创建时间结束（范围查询）", dataType = "String", paramType = "query")
    })
    @GetMapping("/getPage")
    public R<IPage<LawyerChatMessage>> getPage(@ModelAttribute LawyerChatMessage chatMessage,
                                               @RequestParam(defaultValue = "1") int page,
                                               @RequestParam(defaultValue = "10") int size) {
        log.info("LawyerChatMessageController.getPage?chatMessage={},page={},size={}", chatMessage, page, size);
        int pageNum = page > 0 ? page : 1;
        int pageSize = size > 0 ? size : 10;
        IPage<LawyerChatMessage> pageResult = QueryBuilder.of(chatMessage)
                .page(pageNum, pageSize)
                .build()
                .page(chatMessageService);
        return R.data(pageResult);
    }
}

