TPssrRegionController.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package com.ruoyi.project.pssr.controller;
  2. import com.alibaba.fastjson.JSON;
  3. import com.ruoyi.common.utils.file.ExcelUtils;
  4. import com.ruoyi.common.utils.poi.ExcelUtil;
  5. import com.ruoyi.framework.aspectj.lang.annotation.Log;
  6. import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
  7. import com.ruoyi.framework.web.controller.BaseController;
  8. import com.ruoyi.framework.web.domain.AjaxResult;
  9. import com.ruoyi.framework.web.page.TableDataInfo;
  10. import com.ruoyi.project.pssr.domain.TPssrRegion;
  11. import com.ruoyi.project.pssr.domain.TPssrRegion;
  12. import com.ruoyi.project.pssr.service.ITPssrRegionService;
  13. import org.apache.poi.ss.usermodel.Cell;
  14. import org.apache.poi.ss.usermodel.Row;
  15. import org.apache.poi.ss.usermodel.Sheet;
  16. import org.apache.poi.ss.usermodel.Workbook;
  17. import org.springframework.beans.factory.annotation.Autowired;
  18. import org.springframework.security.access.prepost.PreAuthorize;
  19. import org.springframework.web.bind.annotation.*;
  20. import org.springframework.web.multipart.MultipartFile;
  21. import java.io.IOException;
  22. import java.util.ArrayList;
  23. import java.util.Date;
  24. import java.util.List;
  25. import java.util.Map;
  26. import java.util.stream.Collectors;
  27. /**
  28. * pssr区域管理Controller
  29. *
  30. * @author ssy
  31. * @date 2024-09-18
  32. */
  33. @RestController
  34. @RequestMapping("/pssr/region")
  35. public class TPssrRegionController extends BaseController {
  36. @Autowired
  37. private ITPssrRegionService tPssrRegionService;
  38. /**
  39. * 查询pssr区域管理列表
  40. */
  41. @PreAuthorize("@ss.hasPermi('pssr:region:list')")
  42. @GetMapping("/list")
  43. public TableDataInfo list(TPssrRegion tPssrRegion) {
  44. startPage();
  45. List<TPssrRegion> list = tPssrRegionService.selectTPssrRegionList(tPssrRegion);
  46. return getDataTable(list);
  47. }
  48. @PreAuthorize("@ss.hasPermi('pssr:region:list')")
  49. @GetMapping("/groupList")
  50. public AjaxResult groupList(TPssrRegion tPssrRegion) {
  51. List<TPssrRegion> list = tPssrRegionService.selectTPssrRegionList(tPssrRegion);
  52. //根据区域分组
  53. Map<String, List<TPssrRegion>> collect = list.stream().collect(Collectors.groupingBy(TPssrRegion::getRegion));
  54. return AjaxResult.success(collect);
  55. }
  56. /**
  57. * 导出pssr区域管理列表
  58. */
  59. @PreAuthorize("@ss.hasPermi('pssr:region:export')")
  60. @Log(title = "pssr区域管理", businessType = BusinessType.EXPORT)
  61. @GetMapping("/export")
  62. public AjaxResult export(TPssrRegion tPssrRegion) {
  63. List<TPssrRegion> list = tPssrRegionService.selectTPssrRegionList(tPssrRegion);
  64. ExcelUtil<TPssrRegion> util = new ExcelUtil<TPssrRegion>(TPssrRegion.class);
  65. return util.exportExcel(list, "region");
  66. }
  67. /**
  68. * 获取pssr区域管理详细信息
  69. */
  70. @PreAuthorize("@ss.hasPermi('pssr:region:query')")
  71. @GetMapping(value = "/{id}")
  72. public AjaxResult getInfo(@PathVariable("id") Long id) {
  73. return AjaxResult.success(tPssrRegionService.selectTPssrRegionById(id));
  74. }
  75. /**
  76. * 新增pssr区域管理
  77. */
  78. @PreAuthorize("@ss.hasPermi('pssr:region:add')")
  79. @Log(title = "pssr区域管理", businessType = BusinessType.INSERT)
  80. @PostMapping
  81. public AjaxResult add(@RequestBody TPssrRegion tPssrRegion) {
  82. return toAjax(tPssrRegionService.insertTPssrRegion(tPssrRegion));
  83. }
  84. /**
  85. * 修改pssr区域管理
  86. */
  87. @PreAuthorize("@ss.hasPermi('pssr:region:edit')")
  88. @Log(title = "pssr区域管理", businessType = BusinessType.UPDATE)
  89. @PutMapping
  90. public AjaxResult edit(@RequestBody TPssrRegion tPssrRegion) {
  91. return toAjax(tPssrRegionService.updateTPssrRegion(tPssrRegion));
  92. }
  93. /**
  94. * 删除pssr区域管理
  95. */
  96. @PreAuthorize("@ss.hasPermi('pssr:region:remove')")
  97. @Log(title = "pssr区域管理", businessType = BusinessType.DELETE)
  98. @DeleteMapping("/{ids}")
  99. public AjaxResult remove(@PathVariable Long[] ids) {
  100. return toAjax(tPssrRegionService.deleteTPssrRegionByIds(ids));
  101. }
  102. @PreAuthorize("@ss.hasPermi('pssr:region:add')")
  103. @Log(title = "分离培训模版", businessType = BusinessType.INSERT)
  104. @PostMapping("/importData")
  105. public AjaxResult importData(@RequestParam("file") MultipartFile file) throws IOException {
  106. //获取操作人员ID
  107. Long userId = getUserId();
  108. //报错行数统计
  109. List<Integer> failRow = new ArrayList<Integer>();
  110. Workbook workbook = ExcelUtils.getWorkBook(file);
  111. Sheet sheet = workbook.getSheetAt(0);
  112. List<TPssrRegion> list = new ArrayList<>();
  113. int rowNum = sheet.getLastRowNum();
  114. int failNumber = 0;
  115. String topic = "";
  116. boolean flag = false;
  117. for (int i = 1; i <= rowNum; i++) {
  118. try {
  119. logger.info("读取行数:" + i);
  120. Row row = sheet.getRow(i);
  121. if (row == null) {
  122. sheet.removeRow(row);
  123. rowNum++;
  124. continue;
  125. }
  126. int cellNum = row.getLastCellNum();
  127. TPssrRegion entity = new TPssrRegion();
  128. for (int j = 0; j < cellNum; j++) {
  129. Cell cell = row.getCell(j);
  130. if (cell == null) {
  131. continue;
  132. }
  133. String cellValue = ExcelUtils.getCellValue(cell);
  134. logger.info("cellValue:" + cellValue);
  135. if (j == 0) {
  136. entity.setPlant(cellValue);
  137. } else if (j == 1) {
  138. entity.setRegion(cellValue);
  139. } else if (j == 2) {
  140. entity.setUnit(cellValue);
  141. } else if (j == 3) {
  142. entity.setRemarks(cellValue);
  143. }
  144. }
  145. entity.setCreaterCode(userId.toString());
  146. entity.setCreatedate(new Date());
  147. logger.info("entity:" + entity);
  148. list.add(entity);
  149. } catch (Exception e) {
  150. failNumber++;
  151. failRow.add(i + 1);
  152. }
  153. }
  154. int successNumber = 0;
  155. int failNum = 0;
  156. for (TPssrRegion t : list) {
  157. failNum++;
  158. try {
  159. add(t);
  160. successNumber++;
  161. } catch (Exception e) {
  162. e.printStackTrace();
  163. failNumber++;
  164. failRow.add(failNum + 1);
  165. }
  166. }
  167. logger.info("list:" + JSON.toJSONString(list));
  168. logger.info("successNumber:{}", successNumber);
  169. logger.info("failNumber:{}", failNumber);
  170. logger.info("failRow:{}", failRow);
  171. return AjaxResult.success(String.valueOf(successNumber), failRow);
  172. }
  173. }