package com.dl.demo.controller; import cn.dev33.satoken.annotation.SaCheckPermission; import cn.hutool.core.bean.BeanUtil; import com.dl.common.annotation.Log; import com.dl.common.annotation.RepeatSubmit; import com.dl.common.core.controller.BaseController; import com.dl.common.core.domain.PageQuery; import com.dl.common.core.domain.R; import com.dl.common.core.page.TableDataInfo; import com.dl.common.core.validate.AddGroup; import com.dl.common.core.validate.EditGroup; import com.dl.common.core.validate.QueryGroup; import com.dl.common.enums.BusinessType; import com.dl.common.excel.ExcelResult; import com.dl.common.utils.ValidatorUtils; import com.dl.common.utils.poi.ExcelUtil; import com.dl.demo.domain.TestDemo; import com.dl.demo.domain.bo.TestDemoBo; import com.dl.demo.domain.bo.TestDemoImportVo; import com.dl.demo.domain.vo.TestDemoVo; import com.dl.demo.service.ITestDemoService; import lombok.RequiredArgsConstructor; import org.springframework.http.MediaType; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; import javax.validation.constraints.NotEmpty; import javax.validation.constraints.NotNull; import java.util.Arrays; import java.util.List; import java.util.concurrent.TimeUnit; /** * 测试单表Controller * * @author Lion Li * @date 2021-07-26 */ @Validated @RequiredArgsConstructor @RestController @RequestMapping("/demo/demo") public class TestDemoController extends BaseController { private final ITestDemoService iTestDemoService; /** * 查询测试单表列表 */ @SaCheckPermission("demo:demo:list") @GetMapping("/list") public TableDataInfo list(@Validated(QueryGroup.class) TestDemoBo bo, PageQuery pageQuery) { return iTestDemoService.queryPageList(bo, pageQuery); } /** * 自定义分页查询 */ @SaCheckPermission("demo:demo:list") @GetMapping("/page") public TableDataInfo page(@Validated(QueryGroup.class) TestDemoBo bo, PageQuery pageQuery) { return iTestDemoService.customPageList(bo, pageQuery); } /** * 导入数据 * * @param file 导入文件 */ @Log(title = "测试单表", businessType = BusinessType.IMPORT) @SaCheckPermission("demo:demo:import") @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) public R importData(@RequestPart("file") MultipartFile file) throws Exception { ExcelResult excelResult = ExcelUtil.importExcel(file.getInputStream(), TestDemoImportVo.class, true); List volist = excelResult.getList(); List list = BeanUtil.copyToList(volist, TestDemo.class); iTestDemoService.saveBatch(list); return R.ok(excelResult.getAnalysis()); } /** * 导出测试单表列表 */ @SaCheckPermission("demo:demo:export") @Log(title = "测试单表", businessType = BusinessType.EXPORT) @PostMapping("/export") public void export(@Validated TestDemoBo bo, HttpServletResponse response) { List list = iTestDemoService.queryList(bo); // 测试雪花id导出 // for (TestDemoVo vo : list) { // vo.setId(1234567891234567893L); // } ExcelUtil.exportExcel(list, "测试单表", TestDemoVo.class, response); } /** * 获取测试单表详细信息 * * @param id 测试ID */ @SaCheckPermission("demo:demo:query") @GetMapping("/{id}") public R getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) { return R.ok(iTestDemoService.queryById(id)); } /** * 新增测试单表 */ @SaCheckPermission("demo:demo:add") @Log(title = "测试单表", businessType = BusinessType.INSERT) @RepeatSubmit(interval = 2, timeUnit = TimeUnit.SECONDS, message = "{repeat.submit.message}") @PostMapping() public R add(@RequestBody TestDemoBo bo) { // 使用校验工具对标 @Validated(AddGroup.class) 注解 // 用于在非 Controller 的地方校验对象 ValidatorUtils.validate(bo, AddGroup.class); return toAjax(iTestDemoService.insertByBo(bo)); } /** * 修改测试单表 */ @SaCheckPermission("demo:demo:edit") @Log(title = "测试单表", businessType = BusinessType.UPDATE) @RepeatSubmit @PutMapping() public R edit(@Validated(EditGroup.class) @RequestBody TestDemoBo bo) { return toAjax(iTestDemoService.updateByBo(bo)); } /** * 删除测试单表 * * @param ids 测试ID串 */ @SaCheckPermission("demo:demo:remove") @Log(title = "测试单表", businessType = BusinessType.DELETE) @DeleteMapping("/{ids}") public R remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) { return toAjax(iTestDemoService.deleteWithValidByIds(Arrays.asList(ids), true)); } }