package org.jeecg.modules.test.controller; import java.io.UnsupportedEncodingException; import java.io.IOException; import java.net.URLDecoder; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.stream.Collectors; import java.util.HashMap; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.jeecgframework.poi.excel.ExcelImportUtil; import org.jeecgframework.poi.excel.def.NormalExcelConstants; import org.jeecgframework.poi.excel.entity.ExportParams; import org.jeecgframework.poi.excel.entity.ImportParams; import org.jeecgframework.poi.excel.view.JeecgEntityExcelView; import org.jeecg.common.system.vo.LoginUser; import org.apache.shiro.SecurityUtils; import org.jeecg.common.api.vo.Result; import org.jeecg.common.system.query.QueryGenerator; import org.jeecg.common.system.query.QueryRuleEnum; import org.jeecg.common.util.oConvertUtils; import org.jeecg.modules.test.entity.TestSonTable; import org.jeecg.modules.test.entity.TestMainTable; import org.jeecg.modules.test.vo.TestMainTablePage; import org.jeecg.modules.test.service.ITestMainTableService; import org.jeecg.modules.test.service.ITestSonTableService; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import lombok.extern.slf4j.Slf4j; import com.alibaba.fastjson.JSON; import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.Operation; import org.jeecg.common.aspect.annotation.AutoLog; import org.apache.shiro.authz.annotation.RequiresPermissions; /** * @Description: test * @Author: jeecg-boot * @Date: 2026-04-03 * @Version: V1.0 */ @Tag(name="test") @RestController @RequestMapping("/test/testMainTable") @Slf4j public class TestMainTableController { @Autowired private ITestMainTableService testMainTableService; @Autowired private ITestSonTableService testSonTableService; /** * 分页列表查询 * * @param testMainTable * @param pageNo * @param pageSize * @param req * @return */ //@AutoLog(value = "test-分页列表查询") @Operation(summary="test-分页列表查询") @GetMapping(value = "/list") public Result> queryPageList(TestMainTable testMainTable, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo, @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, HttpServletRequest req) { QueryWrapper queryWrapper = QueryGenerator.initQueryWrapper(testMainTable, req.getParameterMap()); Page page = new Page(pageNo, pageSize); IPage pageList = testMainTableService.page(page, queryWrapper); return Result.OK(pageList); } /** * 添加 * * @param testMainTablePage * @return */ @AutoLog(value = "test-添加") @Operation(summary="test-添加") @RequiresPermissions("test:test_main_table:add") @PostMapping(value = "/add") public Result add(@RequestBody TestMainTablePage testMainTablePage) { TestMainTable testMainTable = new TestMainTable(); BeanUtils.copyProperties(testMainTablePage, testMainTable); testMainTableService.saveMain(testMainTable, testMainTablePage.getTestSonTableList()); return Result.OK("添加成功!"); } /** * 编辑 * * @param testMainTablePage * @return */ @AutoLog(value = "test-编辑") @Operation(summary="test-编辑") @RequiresPermissions("test:test_main_table:edit") @RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST}) public Result edit(@RequestBody TestMainTablePage testMainTablePage) { TestMainTable testMainTable = new TestMainTable(); BeanUtils.copyProperties(testMainTablePage, testMainTable); TestMainTable testMainTableEntity = testMainTableService.getById(testMainTable.getId()); if(testMainTableEntity==null) { return Result.error("未找到对应数据"); } testMainTableService.updateMain(testMainTable, testMainTablePage.getTestSonTableList()); return Result.OK("编辑成功!"); } /** * 通过id删除 * * @param id * @return */ @AutoLog(value = "test-通过id删除") @Operation(summary="test-通过id删除") @RequiresPermissions("test:test_main_table:delete") @DeleteMapping(value = "/delete") public Result delete(@RequestParam(name="id",required=true) String id) { testMainTableService.delMain(id); return Result.OK("删除成功!"); } /** * 批量删除 * * @param ids * @return */ @AutoLog(value = "test-批量删除") @Operation(summary="test-批量删除") @RequiresPermissions("test:test_main_table:deleteBatch") @DeleteMapping(value = "/deleteBatch") public Result deleteBatch(@RequestParam(name="ids",required=true) String ids) { this.testMainTableService.delBatchMain(Arrays.asList(ids.split(","))); return Result.OK("批量删除成功!"); } /** * 通过id查询 * * @param id * @return */ //@AutoLog(value = "test-通过id查询") @Operation(summary="test-通过id查询") @GetMapping(value = "/queryById") public Result queryById(@RequestParam(name="id",required=true) String id) { TestMainTable testMainTable = testMainTableService.getById(id); if(testMainTable==null) { return Result.error("未找到对应数据"); } return Result.OK(testMainTable); } /** * 通过id查询 * * @param id * @return */ //@AutoLog(value = "test通过主表ID查询") @Operation(summary="test主表ID查询") @GetMapping(value = "/queryTestSonTableByMainId") public Result> queryTestSonTableListByMainId(@RequestParam(name="id",required=true) String id) { List testSonTableList = testSonTableService.selectByMainId(id); return Result.OK(testSonTableList); } /** * 导出excel * * @param request * @param testMainTable */ @RequiresPermissions("test:test_main_table:exportXls") @RequestMapping(value = "/exportXls") public ModelAndView exportXls(HttpServletRequest request, TestMainTable testMainTable) { // Step.1 组装查询条件查询数据 QueryWrapper queryWrapper = QueryGenerator.initQueryWrapper(testMainTable, request.getParameterMap()); LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal(); //配置选中数据查询条件 String selections = request.getParameter("selections"); if(oConvertUtils.isNotEmpty(selections)) { List selectionList = Arrays.asList(selections.split(",")); queryWrapper.in("id",selectionList); } //Step.2 获取导出数据 List testMainTableList = testMainTableService.list(queryWrapper); // Step.3 组装pageList List pageList = new ArrayList(); for (TestMainTable main : testMainTableList) { TestMainTablePage vo = new TestMainTablePage(); BeanUtils.copyProperties(main, vo); List testSonTableList = testSonTableService.selectByMainId(main.getId()); vo.setTestSonTableList(testSonTableList); pageList.add(vo); } // Step.4 AutoPoi 导出Excel ModelAndView mv = new ModelAndView(new JeecgEntityExcelView()); mv.addObject(NormalExcelConstants.FILE_NAME, "test列表"); mv.addObject(NormalExcelConstants.CLASS, TestMainTablePage.class); mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("test数据", "导出人:"+sysUser.getRealname(), "test")); mv.addObject(NormalExcelConstants.DATA_LIST, pageList); return mv; } /** * 通过excel导入数据 * * @param request * @param response * @return */ @RequiresPermissions("test:test_main_table:importExcel") @RequestMapping(value = "/importExcel", method = RequestMethod.POST) public Result importExcel(HttpServletRequest request, HttpServletResponse response) { MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; Map fileMap = multipartRequest.getFileMap(); for (Map.Entry entity : fileMap.entrySet()) { // 获取上传文件对象 MultipartFile file = entity.getValue(); ImportParams params = new ImportParams(); params.setTitleRows(2); params.setHeadRows(1); params.setNeedSave(true); try { List list = ExcelImportUtil.importExcel(file.getInputStream(), TestMainTablePage.class, params); for (TestMainTablePage page : list) { TestMainTable po = new TestMainTable(); BeanUtils.copyProperties(page, po); testMainTableService.saveMain(po, page.getTestSonTableList()); } return Result.OK("文件导入成功!数据行数:" + list.size()); } catch (Exception e) { log.error(e.getMessage(),e); return Result.error("文件导入失败:"+e.getMessage()); } finally { try { file.getInputStream().close(); } catch (IOException e) { e.printStackTrace(); } } } return Result.OK("文件导入失败!"); } }