package com.ruoyi.scientific.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import com.ruoyi.scientific.domain.vo.DepartmentVo;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;

import com.ruoyi.scientific.domain.bo.DepartmentBo;
import com.ruoyi.scientific.service.IDepartmentService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * department
 *
 * @author zqjia
 * @date 2024-08-13
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/scientific/department")
public class DepartmentController extends BaseController {

    private final IDepartmentService iDepartmentService;

    /**
     * 查询department列表
     */
    @SaCheckPermission("scientific:department:list")
    @GetMapping("/list")
    public TableDataInfo<DepartmentVo> list(DepartmentBo bo, PageQuery pageQuery) {
        return iDepartmentService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出department列表
     */
    @SaCheckPermission("scientific:department:export")
    @Log(title = "department", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(DepartmentBo bo, HttpServletResponse response) {
        List<DepartmentVo> list = iDepartmentService.queryList(bo);
        ExcelUtil.exportExcel(list, "department", DepartmentVo.class, response);
    }

    /**
     * 获取department详细信息
     *
     * @param departmentId 主键
     */
    @SaCheckPermission("scientific:department:query")
    @GetMapping("/{departmentId}")
    public R<DepartmentVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long departmentId) {
        return R.ok(iDepartmentService.queryById(departmentId));
    }

    /**
     * 新增department
     */
    @SaCheckPermission("scientific:department:add")
    @Log(title = "department", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody DepartmentBo bo) {
        return toAjax(iDepartmentService.insertByBo(bo));
    }

    /**
     * 修改department
     */
    @SaCheckPermission("scientific:department:edit")
    @Log(title = "department", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody DepartmentBo bo) {
        return toAjax(iDepartmentService.updateByBo(bo));
    }

    /**
     * 删除department
     *
     * @param departmentIds 主键串
     */
    @SaCheckPermission("scientific:department:remove")
    @Log(title = "department", businessType = BusinessType.DELETE)
    @DeleteMapping("/{departmentIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] departmentIds) {
        return toAjax(iDepartmentService.deleteWithValidByIds(Arrays.asList(departmentIds), true));
    }
}
