package com.dd.admin.business.note.controller;

import cn.hutool.core.bean.BeanUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.dd.admin.common.model.UpdateGroup;
import com.dd.admin.common.model.result.ResultBean;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.NotBlank;
import com.dd.admin.business.note.entity.Note;
import com.dd.admin.business.note.domain.NoteVo;
import com.dd.admin.business.note.domain.NoteDto;
import com.dd.admin.business.note.service.NoteService;

import java.util.List;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 笔记表 前端控制器
 * </p>
 *
 * @author 727869402@qq.com
 * @since 2024-12-09
 */
@Api(tags = "笔记表")
@RestController
public class NoteController {

    @Autowired
    NoteService noteService;

    @ApiOperation(value = "笔记表-分页列表")
    @ApiOperationSupport(order = 1)
    @GetMapping("/admin/note/page")
    public ResultBean<IPage<NoteVo>> page(NoteDto noteDto) {
        IPage<NoteVo> pageInfo = noteService.selectNotePage(noteDto);
        return ResultBean.success(pageInfo);
    }

    @ApiOperation(value = "笔记表-列表")
    @ApiOperationSupport(order = 2)
    @GetMapping("/admin/note/list")
    public ResultBean<List<NoteVo>> list(NoteDto noteDto) {
        List<NoteVo> list = noteService.selectNoteList(noteDto);
        return ResultBean.success(list);
    }

    @ApiOperation(value = "笔记表-添加")
    @ApiOperationSupport(order = 3)
    @PostMapping("/admin/note/add")
    public ResultBean<Note> add(@RequestBody @Validated NoteDto noteDto) {
        Note note = BeanUtil.copyProperties(noteDto, Note.class);
        noteService.save(note);
        return ResultBean.success(note);
    }

    @ApiOperation(value = "笔记表-查询")
    @ApiOperationSupport(order = 4)
    @GetMapping("/admin/note/{noteId}")
    public ResultBean<NoteVo> get(@PathVariable @NotBlank String noteId) {
        Note note = noteService.getById(noteId);
        NoteVo noteVo = BeanUtil.copyProperties(note,NoteVo.class);
        return ResultBean.success(noteVo);
    }

    @ApiOperation(value = "笔记表-修改")
    @ApiOperationSupport(order = 5)
    @PostMapping("/admin/note/update")
    public ResultBean<Note> update(@RequestBody @Validated(UpdateGroup.class) NoteDto noteDto) {
        Note note = BeanUtil.copyProperties(noteDto, Note.class);
        noteService.updateById(note);
        return ResultBean.success(note);
    }

    @ApiOperation(value = "笔记表-删除")
    @ApiOperationSupport(order = 6)
    @GetMapping("/admin/note/delete/{noteId}")
    public ResultBean<Note> delete(@PathVariable @NotBlank String noteId) {
        Boolean b = noteService.removeById(noteId);
        return ResultBean.success(b);
    }
}
