package com.gxwebsoft.cms.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gxwebsoft.cms.entity.Docs;
import com.gxwebsoft.cms.entity.DocsBook;
import com.gxwebsoft.cms.entity.DocsContent;
import com.gxwebsoft.cms.param.DocsBookParam;
import com.gxwebsoft.cms.service.DocsBookService;
import com.gxwebsoft.cms.service.DocsContentService;
import com.gxwebsoft.cms.service.DocsService;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.core.web.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 书籍记录表控制器
 *
 * @author 科技小王子
 * @since 2024-03-23 19:40:02
 */
@Api(tags = "文档(书籍)")
@RestController
@RequestMapping("/api/cms/docs-book")
public class DocsBookController extends BaseController {
    @Resource
    private DocsBookService docsBookService;
    @Resource
    private DocsService docsService;
    @Resource
    private DocsContentService contentService;

    @PreAuthorize("hasAuthority('cms:docsBook:list')")
    @ApiOperation("分页查询书籍记录表")
    @GetMapping("/page")
    public ApiResult<PageResult<DocsBook>> page(DocsBookParam param) {
      // 使用关联查询
        return success(docsBookService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('cms:docsBook:list')")
    @ApiOperation("查询全部书籍记录表")
    @GetMapping()
    public ApiResult<List<DocsBook>> list(DocsBookParam param) {
        // 使用关联查询
        return success(docsBookService.listRel(param));
    }

    @PreAuthorize("hasAuthority('cms:docsBook:list')")
    @ApiOperation("根据id查询书籍记录表")
    @GetMapping("/{id}")
    public ApiResult<DocsBook> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(docsBookService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('cms:docsBook:save')")
    @ApiOperation("添加书籍记录表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody DocsBook docsBook) {
        if (docsBookService.count(new LambdaQueryWrapper<DocsBook>().eq(DocsBook::getCode,docsBook.getCode())) > 0) {
          return fail("该标识已存在");
        }
        if (docsBookService.save(docsBook)) {
            // 添加默认章节
            final Docs docs = new Docs();
            final DocsContent content = new DocsContent();
            docs.setTitle("序言");
            docs.setBookId(docsBook.getBookId());
            docsService.save(docs);
            content.setDocsId(docs.getDocsId());
            content.setContent("撰写中...");
            contentService.save(content);
            docs.setTitle("系统简介");
            docs.setBookId(docsBook.getBookId());
            docsService.save(docs);
            content.setDocsId(docs.getDocsId());
            content.setContent("撰写中...");
            contentService.save(content);
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('cms:docsBook:update')")
    @ApiOperation("修改书籍记录表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody DocsBook docsBook) {
        if (docsBookService.updateById(docsBook)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('cms:docsBook:remove')")
    @ApiOperation("删除书籍记录表")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (docsBookService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('cms:docsBook:save')")
    @ApiOperation("批量添加书籍记录表")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<DocsBook> list) {
        if (docsBookService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('cms:docsBook:update')")
    @ApiOperation("批量修改书籍记录表")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<DocsBook> batchParam) {
        if (batchParam.update(docsBookService, "book_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('cms:docsBook:remove')")
    @ApiOperation("批量删除书籍记录表")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (docsBookService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
