TSpecdevDtController.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. package com.ruoyi.project.sems.controller;
  2. import java.io.FileOutputStream;
  3. import java.io.IOException;
  4. import java.io.OutputStream;
  5. import java.text.SimpleDateFormat;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. import com.alibaba.fastjson.JSON;
  9. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  10. import com.ruoyi.common.utils.DateUtils;
  11. import com.ruoyi.common.utils.file.ExcelUtils;
  12. import com.ruoyi.project.sems.domain.*;
  13. import com.ruoyi.project.sems.service.ITSpecCheckService;
  14. import com.ruoyi.project.system.domain.SysDept;
  15. import com.ruoyi.project.system.domain.SysDictData;
  16. import com.ruoyi.project.system.service.ISysDeptService;
  17. import com.ruoyi.project.system.service.ISysDictTypeService;
  18. import org.apache.poi.ss.usermodel.*;
  19. import org.apache.poi.xssf.streaming.SXSSFWorkbook;
  20. import org.springframework.security.access.prepost.PreAuthorize;
  21. import org.springframework.beans.factory.annotation.Autowired;
  22. import org.springframework.web.bind.annotation.*;
  23. import com.ruoyi.framework.aspectj.lang.annotation.Log;
  24. import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
  25. import com.ruoyi.project.sems.domain.TSpecdevDt;
  26. import com.ruoyi.project.sems.service.ITSpecdevDtService;
  27. import com.ruoyi.framework.web.controller.BaseController;
  28. import com.ruoyi.framework.web.domain.AjaxResult;
  29. import com.ruoyi.common.utils.poi.ExcelUtil;
  30. import com.ruoyi.framework.web.page.TableDataInfo;
  31. import org.springframework.web.multipart.MultipartFile;
  32. /**
  33. * 特种设备电梯台账Controller
  34. *
  35. * @author ruoyi
  36. * @date 2021-07-26
  37. */
  38. @RestController
  39. @RequestMapping("/sems/specDt")
  40. public class TSpecdevDtController extends BaseController
  41. {
  42. @Autowired
  43. private ITSpecdevDtService tSpecdevDtService;
  44. @Autowired
  45. private ITSpecCheckService tSpecCheckService;
  46. @Autowired
  47. private ISysDeptService iSysDeptService;
  48. @Autowired
  49. private ISysDictTypeService iSysDictTypeService;
  50. /**
  51. * 查询特种设备电梯台账列表
  52. */
  53. @PreAuthorize("@ss.hasPermi('sems:specDt:list')")
  54. @GetMapping("/list")
  55. public TableDataInfo list(TSpecdevDt tSpecdevDt)
  56. {
  57. startPage();
  58. List<TSpecdevDt> list = tSpecdevDtService.selectTSpecdevDtList(tSpecdevDt);
  59. return getDataTable(list);
  60. }
  61. /**
  62. * 导出特种设备电梯台账列表
  63. */
  64. @PreAuthorize("@ss.hasPermi('sems:specDt:export')")
  65. @Log(title = "特种设备电梯台账", businessType = BusinessType.EXPORT)
  66. @GetMapping("/export")
  67. public AjaxResult export(TSpecdevDt tSpecdevDt)
  68. {
  69. List<TSpecdevDt> list = tSpecdevDtService.selectTSpecdevDtList(tSpecdevDt);
  70. ExcelUtil<TSpecdevDt> util = new ExcelUtil<TSpecdevDt>(TSpecdevDt.class);
  71. return util.exportExcel(list, "specDt");
  72. }
  73. /**
  74. * 获取特种设备电梯台账详细信息
  75. */
  76. @PreAuthorize("@ss.hasPermi('sems:specDt:query')")
  77. @GetMapping(value = "/{id}")
  78. public AjaxResult getInfo(@PathVariable("id") Long id)
  79. {
  80. return AjaxResult.success(tSpecdevDtService.selectTSpecdevDtById(id));
  81. }
  82. /**
  83. * 新增特种设备电梯台账
  84. */
  85. @PreAuthorize("@ss.hasPermi('sems:specDt:add')")
  86. @Log(title = "特种设备电梯台账", businessType = BusinessType.INSERT)
  87. @PostMapping
  88. public AjaxResult add(@RequestBody TSpecdevDt tSpecdevDt)
  89. {
  90. return toAjax(tSpecdevDtService.insertTSpecdevDt(tSpecdevDt));
  91. }
  92. /**
  93. * 修改特种设备电梯台账
  94. */
  95. @PreAuthorize("@ss.hasPermi('sems:specDt:edit')")
  96. @Log(title = "特种设备电梯台账", businessType = BusinessType.UPDATE)
  97. @PutMapping
  98. public AjaxResult edit(@RequestBody TSpecdevDt tSpecdevDt)
  99. {
  100. TSpecdevDt old = tSpecdevDtService.selectTSpecdevDtById(tSpecdevDt.getId());
  101. if (!old.getReportNo().equals(tSpecdevDt.getReportNo())) {
  102. TSpecCheck tc = new TSpecCheck();
  103. tc.setDevType(5l);
  104. tc.setCheckUnit(tSpecdevDt.getCheckUnit());
  105. tc.setDevId(tSpecdevDt.getId());
  106. tc.setNextWarnDate(tSpecdevDt.getNextWarnDate());
  107. tc.setReportNo(tSpecdevDt.getReportNo());
  108. tc.setWarnDate(tSpecdevDt.getWarnDate());
  109. tSpecCheckService.insertTSpecCheck(tc);
  110. }
  111. return toAjax(tSpecdevDtService.updateTSpecdevDt(tSpecdevDt));
  112. }
  113. /**
  114. * 删除特种设备电梯台账
  115. */
  116. @PreAuthorize("@ss.hasPermi('sems:specDt:remove')")
  117. @Log(title = "特种设备电梯台账", businessType = BusinessType.DELETE)
  118. @DeleteMapping("/{ids}")
  119. public AjaxResult remove(@PathVariable Long[] ids)
  120. {
  121. return toAjax(tSpecdevDtService.deleteTSpecdevDtByIds(ids));
  122. }
  123. /**
  124. * 去重
  125. */
  126. @PreAuthorize("@ss.hasPermi('sems:plant:remove')")
  127. @GetMapping("/duplicate")
  128. public AjaxResult duplicate()
  129. {
  130. tSpecdevDtService.duplicateTSpecdevDt();
  131. return AjaxResult.success();
  132. }
  133. /**
  134. * 批量导入
  135. */
  136. @PreAuthorize("@ss.hasPermi('sems:specDt:add')")
  137. @Log(title = "特种设备批量导入", businessType = BusinessType.INSERT)
  138. @PostMapping("/importData")
  139. public AjaxResult importData(@RequestParam("file") MultipartFile file) throws IOException
  140. {
  141. //获取操作人员ID
  142. Long userId = getUserId();
  143. //报错行数统计
  144. List<Integer> failRow =new ArrayList<Integer>();
  145. Workbook workbook = ExcelUtils.getWorkBook(file);
  146. Sheet sheet = workbook.getSheetAt(0);
  147. List<TSpecdevDt> list = new ArrayList<TSpecdevDt>();
  148. //字典查询
  149. List<SysDictData> plant = iSysDictTypeService.selectDictDataByType("PLANT_DIVIDE");
  150. //部门查询
  151. List<SysDept> dept = iSysDeptService.selectDeptList(new SysDept());
  152. int rowNum = sheet.getPhysicalNumberOfRows();
  153. int failNumber = 0;
  154. for (int i = 1; i < rowNum; i++) {
  155. try {
  156. logger.info("读取行数:" + i);
  157. Row row = sheet.getRow(i);
  158. int cellNum = row.getLastCellNum();
  159. TSpecdevDt entity = new TSpecdevDt();
  160. for (int j = 0; j < cellNum; j++) {
  161. Cell cell = row.getCell(j);
  162. if (cell == null) {
  163. continue;
  164. }
  165. String cellValue = ExcelUtils.getCellValue(cell);
  166. logger.info("cellValue:" + cellValue);
  167. if (j == 0) {
  168. //序号
  169. } else if (j == 1) {
  170. entity.setPlantCode(cellValue);//装置名称
  171. } else if (j == 2) {
  172. entity.setUnit(cellValue);//单元
  173. }else if (j == 3) {
  174. entity.setDocno(cellValue);//档案号
  175. } else if (j == 4) {
  176. entity.setDevname(cellValue);//名称
  177. } else if (j == 5) {
  178. entity.setRegno(cellValue);//设备注册编号
  179. } else if (j == 6) {
  180. entity.setModel(cellValue);//型号
  181. } else if (j == 7) {
  182. entity.setCapacity(cellValue);//额定载重
  183. } else if (j == 8) {
  184. entity.setFloor(cellValue);//层站数
  185. } else if (j == 9) {
  186. entity.setCreateUnit(cellValue);//制造单位
  187. } else if (j == 10) {
  188. entity.setLocation(cellValue);//使用地点
  189. } else if (j == 11) {
  190. entity.setMaintContractor(cellValue);//维保承包商
  191. } else if (j == 12) {
  192. if (cellValue.length() > 3) {//初检日期
  193. entity.setFirstWarnDate(new SimpleDateFormat(DateUtils.getDateFormat(cellValue)).parse(cellValue));
  194. }
  195. } else if (j == 13) {
  196. if (cellValue.length() > 3) {//检验日期
  197. entity.setWarnDate(new SimpleDateFormat(DateUtils.getDateFormat(cellValue)).parse(cellValue));
  198. }
  199. } else if (j == 14) {
  200. entity.setCheckUnit(cellValue);//检验单位
  201. } else if (j == 15) {
  202. entity.setReportNo(cellValue);//报告编号
  203. } else if (j == 16) {
  204. if (cellValue.length() > 3) {//下次年检日期
  205. entity.setNextWarnDate(new SimpleDateFormat(DateUtils.getDateFormat(cellValue)).parse(cellValue));
  206. }
  207. } else if (j == 17) {
  208. entity.setPerTestConclusion(cellValue);//备注
  209. } else if (j == 18) {
  210. entity.setRemarks(cellValue);//备注
  211. }/*else if (j == 17) {
  212. for (SysDept d : dept) {
  213. if (d.getDeptName().equals(cellValue)) {
  214. entity.setDeptId(d.getDeptId());//部门编号
  215. }
  216. }
  217. }*/
  218. }
  219. entity.setCreaterCode(userId);
  220. logger.info("entity:" + entity);
  221. list.add(entity);
  222. }catch (Exception e){
  223. failNumber++;
  224. logger.info("e:" + JSON.toJSONString(e));
  225. failRow.add(i + 1);
  226. }
  227. }
  228. int successNumber = 0;
  229. int failNum = 0;
  230. for (TSpecdevDt t : list
  231. ) {
  232. failNum++;
  233. try {
  234. tSpecdevDtService.insertTSpecdevDt(t);
  235. successNumber++;
  236. }catch (Exception e){
  237. failNumber++;
  238. logger.info("e:" + e);
  239. failRow.add(failNum + 1);
  240. }
  241. }
  242. logger.info("list:" + JSON.toJSONString(list));
  243. logger.info("successNumber:" +String.valueOf(successNumber));
  244. logger.info("failNumber:" +String.valueOf(failNumber));
  245. logger.info("failRow:" +String.valueOf(failRow));
  246. return AjaxResult.success(String.valueOf(successNumber), failRow);
  247. }
  248. /**
  249. * 批量导入
  250. * 检验更新中的批量导入
  251. */
  252. @PreAuthorize("@ss.hasPermi('sems:specDt:add')")
  253. @Log(title = "特种设备批量导入", businessType = BusinessType.INSERT)
  254. @PostMapping("/updateData")
  255. public AjaxResult updateData(@RequestParam("file") MultipartFile file)throws IOException{
  256. //获取操作人员ID
  257. Long userId = getUserId();
  258. //报错行数统计
  259. List<Integer> failRow =new ArrayList<Integer>();
  260. Workbook workbook = ExcelUtils.getWorkBook(file);
  261. Sheet sheet = workbook.getSheetAt(0);
  262. List<TSpecdevDt> list = new ArrayList<TSpecdevDt>();
  263. List<TSpecdevDt> oldList = new ArrayList<TSpecdevDt>();
  264. TSpecdevDt oldEntity = new TSpecdevDt();
  265. //字典查询
  266. List<SysDictData> plant = iSysDictTypeService.selectDictDataByType("PLANT_DIVIDE");
  267. //部门查询
  268. List<SysDept> dept = iSysDeptService.selectDeptList(new SysDept());
  269. int rowNum = sheet.getPhysicalNumberOfRows();
  270. int failNumber = 0;
  271. for (int i = 1; i < rowNum; i++) {
  272. try {
  273. logger.info("读取行数:" + i);
  274. Row row = sheet.getRow(i);
  275. int cellNum = row.getLastCellNum();
  276. TSpecdevDt entity = new TSpecdevDt();
  277. for (int j = 0; j < cellNum; j++) {
  278. Cell cell = row.getCell(j);
  279. if (cell == null) {
  280. continue;
  281. }
  282. String cellValue = ExcelUtils.getCellValue(cell);
  283. logger.info("cellValue:" + cellValue);
  284. if (j == 0) {
  285. //序号
  286. Double d = Double.parseDouble(cellValue);
  287. long s = new Double(d).longValue();
  288. entity.setId(s);
  289. } else if (j == 1) { //装置名称
  290. entity.setPlantCode(cellValue);
  291. } else if (j == 2) { //档案号
  292. entity.setDocno(cellValue);
  293. } else if (j == 3) { //型号
  294. entity.setModel(cellValue);
  295. } else if (j == 4) { //使用地点
  296. entity.setLocation(cellValue);
  297. } else if (j == 5) { //检验日期
  298. logger.info("日期格式:" + cellValue);
  299. if (cellValue.length() > 3) {
  300. entity.setWarnDate(new SimpleDateFormat(DateUtils.getDateFormat(cellValue)).parse(cellValue));
  301. }
  302. } else if (j == 6) { //检验单位
  303. entity.setCheckUnit(cellValue);
  304. } else if (j == 7) { //报告编号
  305. entity.setReportNo(cellValue);
  306. } else if (j == 8) { //下次年度检查日期
  307. logger.info("日期格式:" + cellValue);
  308. if (cellValue.length() > 3) {
  309. entity.setNextWarnDate(new SimpleDateFormat(DateUtils.getDateFormat(cellValue)).parse(cellValue));
  310. }
  311. } else if (j == 9) { //定期结论
  312. entity.setPerTestConclusion(cellValue);
  313. }
  314. }
  315. logger.info("entity:" + JSON.toJSONString(entity));
  316. list.add(entity);
  317. oldEntity = entity;
  318. oldList.add(oldEntity);
  319. }catch (Exception e){
  320. failNumber++;
  321. logger.info("e:" + JSON.toJSONString(e));
  322. failRow.add(i + 1);
  323. }
  324. }
  325. int successNumber = 0;
  326. int failNum = 0;
  327. for (TSpecdevDt t : list) {
  328. failNum++;
  329. try {
  330. tSpecdevDtService
  331. .update(t,
  332. new QueryWrapper<TSpecdevDt>()
  333. .eq("DOCNO", t.getDocno())
  334. );
  335. successNumber++;
  336. }catch (Exception e){
  337. failNumber++;
  338. logger.info("e:" + e);
  339. failRow.add(failNum + 1);
  340. e.printStackTrace();
  341. }
  342. }
  343. for (TSpecdevDt t :oldList) {
  344. TSpecCheck tc = new TSpecCheck();
  345. tc.setDevType(5l);
  346. tc.setCheckUnit(t.getCheckUnit());
  347. tc.setDevId(t.getId());
  348. tc.setNextWarnDate(t.getNextWarnDate());
  349. tc.setReportNo(t.getReportNo());
  350. tc.setWarnDate(t.getWarnDate());
  351. tSpecCheckService.insertTSpecCheck(tc);
  352. }
  353. logger.info("list:" + JSON.toJSONString(list));
  354. logger.info("successNumber:" +String.valueOf(successNumber));
  355. logger.info("failNumber:" +String.valueOf(failNumber));
  356. logger.info("failRow:" +String.valueOf(failRow));
  357. return AjaxResult.success(String.valueOf(successNumber),failRow);
  358. }
  359. @GetMapping("/exportDevList")
  360. public AjaxResult exportbmy(ParamData params) throws IOException {
  361. logger.info(JSON.toJSONString(params));
  362. String id = params.getIds();
  363. String[] ids = id.split(",");
  364. List<TSpecdevDt> list = new ArrayList<>();
  365. for (String i : ids
  366. ) {
  367. TSpecdevDt t = tSpecdevDtService.getById(i);
  368. list.add(t);
  369. }
  370. SXSSFWorkbook wb = new SXSSFWorkbook(1000);
  371. CellStyle wrapStyle = wb.createCellStyle();
  372. wrapStyle.setWrapText(true); //设置自动换行
  373. //创建sheet页
  374. Sheet sheet = wb.createSheet("sheet1");
  375. //设置列的宽度,第一个参数为列的序号,从0开始,第二参数为列宽,单位1/256个字节
  376. sheet.setColumnWidth(0, 12*256);
  377. sheet.setColumnWidth(1, 26*256);
  378. sheet.setColumnWidth(2, 26*256);
  379. sheet.setColumnWidth(3, 26*256);
  380. sheet.setColumnWidth(4, 26*256);
  381. sheet.setColumnWidth(5, 26*256);
  382. sheet.setColumnWidth(6, 26*256);
  383. sheet.setColumnWidth(7, 26*256);
  384. sheet.setColumnWidth(8, 40*256);
  385. sheet.setColumnWidth(9, 40*256);
  386. //设置开始行和开始列
  387. Row row0 = sheet.createRow(0);
  388. CellStyle style = wb.createCellStyle();
  389. style.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
  390. style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  391. // Font headerFont = wb.createFont();
  392. // headerFont.setFontName("Arial");
  393. // headerFont.setFontHeightInPoints((short) 12);
  394. // headerFont.setBold(false);
  395. // headerFont.setColor(IndexedColors.BLACK.getIndex());
  396. // style.setFont(headerFont);
  397. Cell cell0 = row0.createCell(0);
  398. row0.createCell(0).setCellValue("序号");
  399. row0.createCell(1).setCellValue("装置");
  400. row0.createCell(2).setCellValue("档案号");
  401. row0.createCell(3).setCellValue("型号");
  402. row0.createCell(4).setCellValue("使用地点");
  403. row0.createCell(5).setCellValue("检验日期");row0.getCell(5).setCellStyle(style);
  404. row0.createCell(6).setCellValue("检验单位");row0.getCell(6).setCellStyle(style);
  405. row0.createCell(7).setCellValue("报告编号");row0.getCell(7).setCellStyle(style);
  406. row0.createCell(8).setCellValue("下次检验日期");row0.getCell(8).setCellStyle(style);
  407. row0.createCell(9).setCellValue("定期检验结论");row0.getCell(9).setCellStyle(style);
  408. //填充数据
  409. int rowIndex = 1;
  410. int columnIndex = 1;
  411. for (TSpecdevDt t: list
  412. ) {
  413. Row row = sheet.createRow(rowIndex);
  414. row.createCell(0).setCellValue(t.getId());
  415. row.createCell(1).setCellValue(t.getPlantCode());
  416. row.createCell(2).setCellValue(t.getDocno());
  417. row.createCell(3).setCellValue(t.getModel());
  418. row.createCell(4).setCellValue(t.getLocation());
  419. if (t.getWarnDate()!= null){
  420. row.createCell(5).setCellValue(new SimpleDateFormat("yyyy-MM-dd").format(t.getWarnDate()));
  421. }
  422. row.createCell(6).setCellValue(t.getCheckUnit());
  423. row.createCell(7).setCellValue(t.getReportNo());
  424. if (t.getNextWarnDate()!= null){
  425. row.createCell(8).setCellValue(new SimpleDateFormat("yyyy-MM-dd").format(t.getNextWarnDate()));
  426. }
  427. row.createCell(9).setCellValue(t.getPerTestConclusion());
  428. rowIndex++;
  429. }
  430. OutputStream out = null;
  431. String filename = ExcelUtil.encodingFilename("电梯批量更新");
  432. out = new FileOutputStream(ExcelUtil.getAbsoluteFile(filename));
  433. wb.write(out);
  434. wb.close();
  435. out.close();
  436. return AjaxResult.success(filename);
  437. }
  438. }