package com.agileboot.system.post.controller;

import com.agileboot.common.core.core.R;
import com.agileboot.common.core.utils.poi.CustomExcelUtil;
import com.agileboot.common.mybatis.core.page.PageR;
import com.agileboot.system.post.dto.AddPostDTO;
import com.agileboot.system.post.dto.PostQuery;
import com.agileboot.system.post.dto.PostVO;
import com.agileboot.system.post.dto.UpdatePostDTO;
import com.agileboot.system.post.service.ISysPostService;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 岗位信息操作处理
 */
@RestController
@RequestMapping("/system/post")
@Validated
@RequiredArgsConstructor
public class SysPostController {

    private final ISysPostService sysPostService;

    /**
     * 获取岗位列表
     */
    @GetMapping("/list")
    public R<PageR<PostVO>> list(PostQuery query) {
        PageR<PostVO> page = sysPostService.getPostList(query);
        return R.ok(page);
    }

    /**
     * 导出查询到的所有岗位信息到excel文件
     *
     * @param response http响应
     * @param query    查询参数
     * @author Kevin Zhang
     * @date 2023-10-02
     */
    @GetMapping("/excel")
    public void export(HttpServletResponse response, PostQuery query) {
        List<PostVO> all = sysPostService.getPostListAll(query);
        CustomExcelUtil.writeToResponse(all, PostVO.class, response);
    }

    /**
     * 根据岗位编号获取详细信息
     */
    @GetMapping(value = "/{postId}")
    public R<PostVO> getInfo(@PathVariable Long postId) {
        PostVO post = sysPostService.getPostInfo(postId);
        return R.ok(post);
    }

    /**
     * 新增岗位
     */
    @PostMapping
    public R<Void> add(@RequestBody AddPostDTO addDTO) {
        sysPostService.addPost(addDTO);
        return R.ok();
    }

    /**
     * 修改岗位
     */
    @PostMapping("/update")
    public R<Void> edit(@RequestBody UpdatePostDTO updateCommand) {
        sysPostService.updatePost(updateCommand);
        return R.ok();
    }

    /**
     * 删除岗位
     */
    @PostMapping("/del")
    public R<Void> remove(@RequestBody @NotNull @NotEmpty List<Long> ids) {
        sysPostService.deletePost(ids);
        return R.ok();
    }

}
