package com.ruoyi.jjh.ent.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.jjh.ent.entity.JMemorandum;
import com.ruoyi.jjh.ent.entity.request.JMemorandumRequest;
import com.ruoyi.jjh.ent.service.JMemorandumService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * 备忘录(JMemorandum)表控制层
 *
 * @author makejava
 * @since 2024-03-26 10:34:03
 */
@RestController
@RequestMapping("/jjh/jMemorandum")
@Api(tags = "备忘录表")
public class JMemorandumController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private JMemorandumService jMemorandumService;

    /**
     * 分页查询所有数据
     *
     * @param page        分页对象
     * @param jMemorandumRequest 查询实体
     * @return 所有数据
     */
    @ApiOperation(value = "分页查询所有数据", response = JMemorandum.class)
    @GetMapping
    public AjaxResult selectAll(Page<JMemorandum> page, JMemorandumRequest jMemorandumRequest) {
        return success(jMemorandumService.page(page,jMemorandumRequest));
    }

    /**
     * 根据关联项目库ID查询该ID下的备忘录
     *
     * @param jMemorandumRequest 查询实体
     * @return 所有数据
     */
    @ApiOperation(value = "根据关联项目库ID查询该ID下的备忘录", response = JMemorandum.class)
    @GetMapping("/getIdToList")
    public AjaxResult selectIdToAll(JMemorandumRequest jMemorandumRequest) {
        if(jMemorandumRequest.getProjectId() == null){
            throw new ServiceException("项目库ID不能为空");
        }
        return success(jMemorandumService.selectIdToAll(jMemorandumRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation(value = "通过主键查询单条数据", response = JMemorandum.class)
    @GetMapping("{id}")
    public AjaxResult selectOne(@PathVariable Serializable id) {
        return success(this.jMemorandumService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param jMemorandum 实体对象
     * @return 新增结果
     */
    @ApiOperation(value = "新增数据")
    @PostMapping
    public AjaxResult insert(@RequestBody JMemorandum jMemorandum) {
        jMemorandum.setCreateTime(new Date());
        return success(this.jMemorandumService.save(jMemorandum));
    }

    /**
     * 修改数据
     *
     * @param jMemorandum 实体对象
     * @return 修改结果
     */
    @ApiOperation(value = "修改数据")
    @PutMapping
    public AjaxResult update(@RequestBody JMemorandum jMemorandum) {
        jMemorandum.setUpdateTime(new Date());
        return success(this.jMemorandumService.updateById(jMemorandum));
    }

    /**
     * 删除数据
     *
     * @param id 主键结合
     * @return 删除结果
     */
    @ApiOperation(value = "删除数据")
    @DeleteMapping
    public AjaxResult delete(@RequestParam("id") Long id) {
        return success(this.jMemorandumService.removeById(id));
    }
}

