package org.dromara.main.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.main.domain.bo.MainPositionBo;
import org.dromara.main.domain.vo.MainPositionVo;
import org.dromara.main.service.IMainPositionService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 岗位管理
 *
 *
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/main/position")
public class MainPositionController extends BaseController {

    private final IMainPositionService mainPositionService;
    private Long id;

    /**
     * 查询岗位列表
     */
    @SaCheckPermission("main:position:list")
    @GetMapping("/list")
    public TableDataInfo<MainPositionVo> list(MainPositionBo bo, PageQuery pageQuery) {
        return TableDataInfo.build(mainPositionService.queryPageList(bo, pageQuery));
    }

    /**
     * 导出岗位列表
     */
    @SaCheckPermission("main:position:export")
    @Log(title = "岗位管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(MainPositionBo bo, HttpServletResponse response) {
        // 如果 IMainPositionService 有 queryList 方法则取消注释，否则可根据需要实现
        // List<MainPositionVo> list = mainPositionService.queryList(bo);
        // ExcelUtil.exportExcel(list, "岗位信息", MainPositionVo.class, response);
    }

    /**
     * 获取岗位详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("main:position:query")
    @GetMapping("/{id}")
    public R<MainPositionVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        this.id = id;
        return R.ok(mainPositionService.queryById(id));
    }

    /**
     * 新增岗位
     */
    @SaCheckPermission("main:position:add")
    @Log(title = "岗位管理", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody MainPositionBo bo) {
        return toAjax(mainPositionService.insertPosition(bo));
    }

    /**
     * 修改岗位
     */
    @SaCheckPermission("main:position:edit")
    @Log(title = "岗位管理", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody MainPositionBo bo) {
        return toAjax(mainPositionService.updatePosition(bo));
    }

    /**
     * 删除岗位
     *
     * @param ids 主键串
     */
    @SaCheckPermission("main:position:remove")
    @Log(title = "岗位管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(mainPositionService.removeByIds(List.of(ids)));
    }
}

