package com.gxwebsoft.open.controller;

import com.gxwebsoft.cms.entity.Docs;
import com.gxwebsoft.cms.param.DocsParam;
import com.gxwebsoft.cms.service.DocsService;
import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.common.core.web.*;
import com.gxwebsoft.common.system.entity.User;
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 2022-11-16 11:40:27
 */
@Api(tags = "文档管理记录表管理")
@RestController
@RequestMapping("/api/open/docs")
public class OpenDocsController extends BaseController {
    @Resource
    private DocsService docsService;

    @ApiOperation("分页查询文档管理记录表")
    @GetMapping("/page")
    public ApiResult<PageResult<Docs>> page(DocsParam param) {
        // 验证签名
        isCheckSign();
        PageParam<Docs, DocsParam> page = new PageParam<>(param);
        page.setDefaultOrder("sort_number asc,create_time asc");
        return success(docsService.page(page, page.getWrapper()));
        // 使用关联查询
//        return success(docsService.pageRel(param));
    }

    @ApiOperation("查询全部文档管理记录表")
    @GetMapping()
    public ApiResult<List<Docs>> list(DocsParam param) {
        // 验证签名
        isCheckSign();
//        PageParam<Docs, DocsParam> page = new PageParam<>(param);
//        page.setDefaultOrder("sort_number asc,create_time asc");
//        return success(docsService.list(page.getOrderWrapper()));
        // 使用关联查询
        return success(docsService.listRel(param));
    }

    @ApiOperation("根据id查询文档管理记录表")
    @GetMapping("/{id}")
    public ApiResult<Docs> get(@PathVariable("id") Integer id) {
        // 验证签名
        isCheckSign();
        return success(docsService.getById(id));
        // 使用关联查询
        //return success(docsService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('cms:docs:save')")
    @OperationLog
    @ApiOperation("添加文档管理记录表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody Docs docs) {
        // 验证签名
        isCheckSign();
        // 记录当前登录用户id、租户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
          docs.setUserId(loginUser.getUserId());
        }
        if (docsService.save(docs)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('cms:docs:update')")
    @OperationLog
    @ApiOperation("修改文档管理记录表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody Docs docs) {
        // 验证签名
        isCheckSign();
        if (docsService.updateById(docs)) {
            return success("保存成功");
        }
        return fail("保存失败");
    }

    @PreAuthorize("hasAuthority('cms:docs:remove')")
    @OperationLog
    @ApiOperation("删除文档管理记录表")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        // 验证签名
        isCheckSign();
        if (docsService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('cms:docs:save')")
    @OperationLog
    @ApiOperation("批量添加文档管理记录表")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<Docs> list) {
        // 验证签名
        isCheckSign();
        if (docsService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('cms:docs:update')")
    @OperationLog
    @ApiOperation("批量修改文档管理记录表")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<Docs> batchParam) {
        // 验证签名
        isCheckSign();
        if (batchParam.update(docsService, "docs_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('cms:docs:remove')")
    @OperationLog
    @ApiOperation("批量删除文档管理记录表")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        // 验证签名
        isCheckSign();
        if (docsService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
