package com.ruoyi.web.controller.api;
import com.ruoyi.common.basic.PageDTO;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.BeanUtils;
import com.ruoyi.common.utils.ExcelUtil;
import com.ruoyi.system.domain.TbDept;
import com.ruoyi.system.dto.update.DeptFocusDTO;
import com.ruoyi.system.dto.update.DeptUpdateDTO;
import com.ruoyi.system.query.DeptQuery;
import com.ruoyi.system.service.TbDeptService;
import com.ruoyi.system.vo.DeptVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
/**
*
* 部门管理表 前端控制器
*
*
* @author mitao
* @since 2024-03-13
*/
@Slf4j
@RestController
@RequestMapping("/dept")
@RequiredArgsConstructor
@Api(tags = "部门管理相关接口")
public class TbDeptController {
private final HttpServletResponse response;
private final TbDeptService tbDeptService;
/**
* 导入模板下载
*/
@GetMapping("/download")
@ApiOperation("模板下载")
public void download() {
try {
ArrayList list = new ArrayList<>();
ExcelUtil.exportExcel(list, "部门导入模板", "部门导入模板", TbDept.class, "部门导入模板", response);
} catch (Exception e) {
log.error("模板下载异常",e);
throw new ServiceException("模板下载失败,请联系管理员!");
}
}
/**
* 导入
* @param file 文件
* @return R
*/
@PostMapping("/import")
@ApiOperation("导入")
public R importExcel(@RequestPart("file")MultipartFile file) {
try {
tbDeptService.importExcel(file);
} catch (Exception e) {
if (e instanceof ServiceException) {
return R.fail(e.getMessage());
}
log.error("导入失败", e);
throw new RuntimeException(e.getMessage());
}
return R.ok();
}
/**
* 分页条件查询
* @param query 查询条件
* @return R>
*/
@PostMapping("/page")
@ApiOperation("分页条件查询")
public R> page(@RequestBody DeptQuery query) {
try {
return R.ok(tbDeptService.queryPage(query));
} catch (Exception e) {
if (e instanceof ServiceException) {
return R.fail(e.getMessage());
}
log.error("分页条件查询异常", e);
return R.fail();
}
}
/**
* 根据id查询部门详情
* @param id 部门id
* @return DeptVO
*/
@GetMapping("/get-details")
@ApiOperation("根据id查询部门详情")
public R getDetails(@RequestParam Integer id) {
try {
TbDept dept = tbDeptService.getById(id);
return R.ok(BeanUtils.copyBean(dept,DeptVO.class));
} catch (Exception e) {
if (e instanceof ServiceException) {
return R.fail(e.getMessage());
}
log.error("根据id查询部门详情异常", e);
return R.fail();
}
}
/**
* 编辑
* @param dto 部门更新实体
* @return 响应成功
*/
@PostMapping("/edit")
@ApiOperation("编辑")
public R