package com.auth.service.base.controller;

import com.auth.common.model.common.result.PageResult;
import com.auth.common.model.common.result.Result;
import com.auth.common.model.common.result.ResultCodeEnum;
import com.auth.dao.base.entity.DeptEntity;
import com.auth.model.base.dto.DeptDto;
import com.auth.model.base.vo.DeptVo;
import com.auth.service.base.service.DeptService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 部门表 前端控制器
 * </p>
 *
 * @author AuthoritySystem
 * @since 2025-07-22 23:14:35
 */
@Tag(name = "部门表", description = "部门表相关接口")
@RestController
@RequestMapping("/api/v1/base/dept")
@RequiredArgsConstructor
public class DeptController {

    private final DeptService deptService;

    @PreAuthorize("auth.decide('dept:query')")
    @Operation(summary = "分页查询部门表", description = "分页查询部门表")
    @GetMapping()
    public Result<PageResult<DeptVo>> getDeptPage(DeptDto dto) {
        Page<DeptEntity> pageParams = new Page<>(dto.getPageIndex(), dto.getPageSize());
        PageResult<DeptVo> pageResult = deptService.getDeptPage(pageParams, dto);
        return Result.success(pageResult);
    }

    @PreAuthorize("auth.decide('dept:query')")
    @Operation(summary = "根据id查询部门表详情", description = "根据id查询部门表详情")
    @GetMapping("{id}")
    public Result<DeptVo> getById(@PathVariable("id") Long id) {
        DeptVo deptVo = deptService.getById(id);

        return Result.success(deptVo);
    }

    @PreAuthorize("auth.decide('dept:create')")
    @Operation(summary = "添加部门表", description = "添加部门表")
    @PostMapping()
    public Result<String> create(@Valid @RequestBody DeptDto dto) {
        deptService.create(dto);
        return Result.success(ResultCodeEnum.ADD_SUCCESS);
    }

    @PreAuthorize("auth.decide('dept:update')")
    @Operation(summary = "更新部门表", description = "更新部门表")
    @PutMapping()
    public Result<String> update(@Valid @RequestBody DeptDto dto) {
        deptService.update(dto);
        return Result.success(ResultCodeEnum.UPDATE_SUCCESS);
    }

    @PreAuthorize("auth.decide('dept:delete')")
    @Operation(summary = "删除部门表", description = "删除部门表")
    @DeleteMapping()
    public Result<String> batchDelete(@RequestBody List<Long> ids) {
        deptService.batchDelete(ids);
        return Result.success(ResultCodeEnum.DELETE_SUCCESS);
    }
}