package com.qiaoba.module.system.controller;

import com.qiaoba.api.system.entity.SysPost;
import com.qiaoba.api.system.entity.param.SysPostParam;
import com.qiaoba.common.base.result.AjaxResult;
import com.qiaoba.common.base.validate.AddGroup;
import com.qiaoba.common.base.validate.EditGroup;
import com.qiaoba.common.database.entity.PageQuery;
import com.qiaoba.common.database.entity.TableDataInfo;
import com.qiaoba.common.poi.util.ExcelUtil;
import com.qiaoba.module.system.service.SysPostService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.Size;
import java.util.List;

/**
 * 岗位管理 web层
 *
 * @author ailanyin
 * @version 1.0
 * @since 2022-09-22 04:20:28
 */
@Validated
@RestController
@RequestMapping("/system/post")
@RequiredArgsConstructor
@Tag(name = "岗位管理")
public class SysPostController {

    private final SysPostService sysPostService;

    @GetMapping("/list")
    @PreAuthorize("hasAuthority('system:post:list')")
    @Operation(summary = "获取列表")
    public TableDataInfo<SysPost> list(@Validated SysPostParam param, PageQuery pageQuery) {
        return sysPostService.selectPageList(param, pageQuery);
    }

    @PostMapping("/export")
    @PreAuthorize("hasAuthority('system:post:export')")
    @Operation(summary = "导出岗位")
    public void export(HttpServletResponse response, @Validated SysPostParam param) {
        List<SysPost> list = sysPostService.selectList(param);
        ExcelUtil.exportExcel(list, SysPost.class, "岗位数据", response);
    }

    @GetMapping(value = "/{postId}")
    @PreAuthorize("hasAuthority('system:post:query')")
    @Operation(summary = "获取详情")
    public AjaxResult getInfo(@Size(min = 19, max = 20, message = "ID不合法") @PathVariable("postId") String postId) {
        return AjaxResult.success(sysPostService.selectById(postId));
    }

    @PostMapping
    @PreAuthorize("hasAuthority('system:post:add')")
    @Operation(summary = "新增岗位")
    public AjaxResult add(@Validated(AddGroup.class) @RequestBody SysPost post) {
        return AjaxResult.toAjax(sysPostService.insert(post));
    }

    @PutMapping
    @PreAuthorize("hasAuthority('system:post:edit')")
    @Operation(summary = "修改岗位")
    public AjaxResult edit(@Validated(EditGroup.class) @RequestBody SysPost post) {
        return AjaxResult.toAjax(sysPostService.updateById(post));
    }

    @DeleteMapping("/{ids}")
    @PreAuthorize("hasAuthority('system:post:remove')")
    @Operation(summary = "删除岗位")
    public AjaxResult remove(@PathVariable List<String> ids) {
        return AjaxResult.toAjax(sysPostService.deleteByIds(ids));
    }

}
