| | |
| | | package com.ruoyi.web.controller.api; |
| | | |
| | | |
| | | import org.springframework.web.bind.annotation.RequestMapping; |
| | | import cn.afterturn.easypoi.excel.ExcelExportUtil; |
| | | import cn.afterturn.easypoi.excel.ExcelImportUtil; |
| | | import cn.afterturn.easypoi.excel.entity.ExportParams; |
| | | import cn.afterturn.easypoi.excel.entity.ImportParams; |
| | | import cn.hutool.json.JSONObject; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.ruoyi.common.annotation.Log; |
| | | import com.ruoyi.common.basic.PageInfo; |
| | | import com.ruoyi.common.core.domain.R; |
| | | import com.ruoyi.common.core.domain.entity.SysUser; |
| | | import com.ruoyi.common.core.exception.ServiceException; |
| | | import com.ruoyi.common.enums.BusinessType; |
| | | import com.ruoyi.common.utils.WebUtils; |
| | | import com.ruoyi.system.export.OldsterImport; |
| | | import com.ruoyi.system.model.TOldster; |
| | | import com.ruoyi.system.query.BannerListQuery; |
| | | import com.ruoyi.system.query.OldsterListQuery; |
| | | import com.ruoyi.system.service.TOldsterService; |
| | | import com.ruoyi.system.service.TOldsterService; |
| | | import io.swagger.annotations.Api; |
| | | import io.swagger.annotations.ApiOperation; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.poi.ss.usermodel.Workbook; |
| | | import org.springframework.beans.BeanUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.transaction.annotation.Propagation; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.util.CollectionUtils; |
| | | import org.springframework.util.StringUtils; |
| | | import org.springframework.web.bind.annotation.*; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import org.springframework.web.bind.annotation.RestController; |
| | | import javax.servlet.ServletOutputStream; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.net.URLEncoder; |
| | | import java.time.LocalDate; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * <p> |
| | |
| | | * @since 2025-11-06 |
| | | */ |
| | | @RestController |
| | | @Slf4j |
| | | @RequestMapping("/t-oldster") |
| | | @Api(tags = "老人户籍信息") |
| | | public class TOldsterController { |
| | | @Autowired |
| | | private TOldsterService oldsterService; |
| | | @ApiOperation(value = "老人户籍信息分页列表") |
| | | @PostMapping(value = "/pageList") |
| | | public R<PageInfo<TOldster>> pageList(@RequestBody OldsterListQuery query) { |
| | | return R.ok(oldsterService.pageList(query)); |
| | | } |
| | | |
| | | @ApiOperation(value = "添加") |
| | | @Transactional |
| | | @Log(title = "老人户籍信息-添加", businessType = BusinessType.INSERT) |
| | | @PostMapping(value = "/add") |
| | | public R<Boolean> save(@RequestBody TOldster entity) { |
| | | oldsterService.save(entity); |
| | | return R.ok(); |
| | | } |
| | | @ApiOperation(value = "修改") |
| | | @Log(title = "老人户籍信息-修改", businessType = BusinessType.UPDATE) |
| | | @PostMapping(value = "/edit") |
| | | public R<Boolean> edit(@RequestBody TOldster entity) { |
| | | oldsterService.updateById(entity); |
| | | return R.ok(); |
| | | } |
| | | @Log(title = "老人户籍信息-删除", businessType = BusinessType.DELETE) |
| | | @ApiOperation(value = "老人户籍信息-删除") |
| | | @DeleteMapping(value = "/delete") |
| | | public R delete(@RequestParam String ids) { |
| | | List<Integer> res = Arrays.stream(ids.split(",")).map(Integer::parseInt).collect(Collectors.toList()); |
| | | // 转换成Integer |
| | | oldsterService.removeBatchByIds(res); |
| | | return R.ok(); |
| | | } |
| | | @ApiOperation(value = "导入模板下载") |
| | | @GetMapping("/import-template") |
| | | public void importTemplate() { |
| | | List<OldsterImport> assetHouseInspectionImports = new ArrayList<>(); |
| | | OldsterImport temp = new OldsterImport(); |
| | | assetHouseInspectionImports.add(temp); |
| | | Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), OldsterImport.class, assetHouseInspectionImports); |
| | | HttpServletResponse response = WebUtils.response(); |
| | | ServletOutputStream outputStream = null; |
| | | try { |
| | | String fileName = URLEncoder.encode("老人户籍信息导入模板.xls", "utf-8"); |
| | | response.setHeader("Content-Disposition", "attachment;filename=" + fileName); |
| | | response.setHeader("content-Type", "application/vnd.ms-excel"); |
| | | response.setHeader("Pragma", "no-cache"); |
| | | response.setHeader("Cache-Control", "no-cache"); |
| | | outputStream = response.getOutputStream(); |
| | | workbook.write(outputStream); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | System.out.println("老人户籍信息导入模板下载失败!"); |
| | | } finally { |
| | | try { |
| | | outputStream.close(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | @Log(title = "老人户籍信息导入", businessType = BusinessType.INSERT) |
| | | @ApiOperation(value = "老人户籍信息导入") |
| | | @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW) |
| | | @PostMapping("/import") |
| | | public R<String> importLocation(@RequestPart("file") MultipartFile file) { |
| | | List<TOldster> oldsters = new ArrayList<>(); |
| | | ImportParams params = new ImportParams(); |
| | | params.setHeadRows(1); //表头行数 |
| | | InputStream inputStream = null; |
| | | List<OldsterImport> assetHouseInspectionImports; |
| | | try { |
| | | inputStream = file.getInputStream(); |
| | | assetHouseInspectionImports = ExcelImportUtil.importExcel(inputStream, |
| | | OldsterImport.class, params); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | log.error("老人户籍信息导入失败:{}", e.getMessage()); |
| | | throw new ServiceException("老人户籍信息导入失败!"); |
| | | } finally { |
| | | try { |
| | | inputStream.close(); |
| | | } catch (IOException e) { |
| | | throw new ServiceException(e.getMessage()); |
| | | } |
| | | } |
| | | if (CollectionUtils.isEmpty(assetHouseInspectionImports)) { |
| | | throw new ServiceException("老人户籍信息数据为空!"); |
| | | } |
| | | JSONObject result = new JSONObject(); |
| | | int i = 1; |
| | | for (OldsterImport oldsterImport : assetHouseInspectionImports) { |
| | | if (!StringUtils.hasLength(oldsterImport.getOldsterName())){ |
| | | result.append("第"+i+"行:[", "老人姓名"+"]不能为空"); |
| | | } |
| | | if (oldsterImport.getOldsterAge()!=null){ |
| | | result.append("第"+i+"行:[", "老人年龄"+"]不能为空"); |
| | | } |
| | | if (oldsterImport.getOldsterSex()!=null){ |
| | | result.append("第"+i+"行:[", "老人性别"+"]不能为空"); |
| | | } |
| | | if (!StringUtils.hasLength(oldsterImport.getContactPhone())){ |
| | | result.append("第"+i+"行:[", "联系方式"+"]不能为空"); |
| | | } |
| | | if (oldsterImport.getOldsterType()!=null){ |
| | | result.append("第"+i+"行:[", "人员类型"+"]不能为空"); |
| | | } |
| | | if (!StringUtils.hasLength(oldsterImport.getAddress())){ |
| | | result.append("第"+i+"行:[", "居住地址"+"]不能为空"); |
| | | } |
| | | TOldster tOldster = new TOldster(); |
| | | BeanUtils.copyProperties(oldsterImport, tOldster); |
| | | oldsters.add(tOldster); |
| | | i++; |
| | | } |
| | | oldsterService.saveBatch(oldsters); |
| | | if(!result.isEmpty()){ |
| | | return R.fail(result.toString()); |
| | | } |
| | | return R.ok(); |
| | | } |
| | | } |
| | | |