package com.jeeplus.test.knowledge.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.test.knowledge.domain.KnowledgeShareType;
import com.jeeplus.test.knowledge.mapper.KnowledgeShareTypeMapper;
import com.jeeplus.test.knowledge.service.TypeService;
import com.jeeplus.test.knowledge.service.dto.KnowledgeShareTypeDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

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

@RestController
@Api("/知识分享类型")
@RequestMapping(value = "/knowledgeShare/type")
public class TypeController {

    @Resource
    private TypeService service;

    @Resource
    private KnowledgeShareTypeMapper mapper;

    /**
     * 列表查询
     * @param type
     * @return
     */
    @ApiOperation(value = "列表")
    @GetMapping("/list")
    public ResponseEntity<IPage<KnowledgeShareTypeDto>> list(Page<KnowledgeShareTypeDto> page, KnowledgeShareType type) {
        return ResponseEntity.ok(service.list(page, type));
    }

    /**
     * 新增/修改
     * @param type
     * @return
     */
    @ApiOperation(value = "新增/修改")
    @PostMapping("/save")
    public ResponseEntity<String> save(@RequestBody KnowledgeShareType type) {
        Boolean b = service.save(type);
        if (b) {
            return ResponseEntity.ok("操作成功");
        } else {
            return ResponseEntity.badRequest().body("该类型已存在,请重新输入");
        }
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping("/findById")
    public ResponseEntity<KnowledgeShareType> findById(String id) {
        KnowledgeShareType knowledgeShareType = mapper.selectById(id);
        return ResponseEntity.ok(knowledgeShareType);
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id删除")
    @GetMapping("/remove")
    public ResponseEntity<String> remove(String id) {
        mapper.deleteById(id);
        return ResponseEntity.ok("操作完成");
    }

}
