TBaseDeviceController.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. package com.ruoyi.project.base.controller;
  2. import java.io.IOException;
  3. import java.text.SimpleDateFormat;
  4. import java.util.ArrayList;
  5. import java.util.Date;
  6. import java.util.List;
  7. import javax.servlet.http.HttpServletResponse;
  8. import com.alibaba.fastjson2.JSON;
  9. import com.ruoyi.common.annotation.RepeatSubmit;
  10. import com.ruoyi.common.core.domain.entity.SysDictData;
  11. import com.ruoyi.common.utils.DateUtils;
  12. import com.ruoyi.common.utils.file.ExcelUtils;
  13. import com.ruoyi.project.base.domain.TBasePlant;
  14. import com.ruoyi.project.base.domain.TBaseRegion;
  15. import com.ruoyi.project.base.service.ITBasePlantService;
  16. import com.ruoyi.project.base.service.ITBaseRegionService;
  17. import com.ruoyi.system.service.ISysDictTypeService;
  18. import org.apache.commons.collections4.CollectionUtils;
  19. import org.apache.poi.ss.usermodel.Cell;
  20. import org.apache.poi.ss.usermodel.Row;
  21. import org.apache.poi.ss.usermodel.Sheet;
  22. import org.apache.poi.ss.usermodel.Workbook;
  23. import org.springframework.security.access.prepost.PreAuthorize;
  24. import org.springframework.beans.factory.annotation.Autowired;
  25. import org.springframework.web.bind.annotation.*;
  26. import com.ruoyi.common.annotation.Log;
  27. import com.ruoyi.common.core.controller.BaseController;
  28. import com.ruoyi.common.core.domain.AjaxResult;
  29. import com.ruoyi.common.enums.BusinessType;
  30. import com.ruoyi.project.base.domain.TBaseDevice;
  31. import com.ruoyi.project.base.service.ITBaseDeviceService;
  32. import com.ruoyi.common.utils.poi.ExcelUtil;
  33. import com.ruoyi.common.core.page.TableDataInfo;
  34. import org.springframework.web.multipart.MultipartFile;
  35. /**
  36. * 设备/管线Controller
  37. *
  38. * @author ruoyi
  39. * @date 2022-11-15
  40. */
  41. @RestController
  42. @RequestMapping("/base/device")
  43. public class TBaseDeviceController extends BaseController
  44. {
  45. @Autowired
  46. private ITBaseDeviceService tBaseDeviceService;
  47. @Autowired
  48. private ITBasePlantService tBasePlantService;
  49. @Autowired
  50. private ITBaseRegionService tBaseRegionService;
  51. /**
  52. * 查询设备/管线列表
  53. */
  54. @PreAuthorize("@ss.hasPermi('base:device:list')")
  55. @GetMapping("/list")
  56. public TableDataInfo list(TBaseDevice tBaseDevice)
  57. {
  58. startPage();
  59. List<TBaseDevice> list = tBaseDeviceService.selectTBaseDeviceList(tBaseDevice);
  60. return getDataTable(list);
  61. }
  62. @GetMapping("/allDevice/{regionId}")
  63. public AjaxResult allDevice(@PathVariable("regionId") Long regionId)
  64. {
  65. return AjaxResult.success(tBaseDeviceService.selectAllDeviceByRegionId(regionId));
  66. }
  67. /**
  68. * 导出设备/管线列表
  69. */
  70. @PreAuthorize("@ss.hasPermi('base:device:export')")
  71. @Log(title = "设备/管线导出", businessType = BusinessType.EXPORT)
  72. @RepeatSubmit
  73. @PostMapping("/export")
  74. public void export(HttpServletResponse response, TBaseDevice tBaseDevice)
  75. {
  76. List<TBaseDevice> list = tBaseDeviceService.selectTBaseDeviceList(tBaseDevice);
  77. ExcelUtil<TBaseDevice> util = new ExcelUtil<TBaseDevice>(TBaseDevice.class);
  78. util.exportExcel(response, list, "受控设备清单");
  79. }
  80. /**
  81. * 获取设备/管线详细信息
  82. */
  83. @PreAuthorize("@ss.hasPermi('base:device:query')")
  84. @GetMapping(value = "/{id}")
  85. public AjaxResult getInfo(@PathVariable("id") Long id)
  86. {
  87. return AjaxResult.success(tBaseDeviceService.selectTBaseDeviceById(id));
  88. }
  89. /**
  90. * 新增设备/管线
  91. */
  92. @PreAuthorize("@ss.hasPermi('base:device:add')")
  93. @Log(title = "设备/管线新增", businessType = BusinessType.INSERT)
  94. @RepeatSubmit
  95. @PostMapping
  96. public AjaxResult add(@RequestBody TBaseDevice tBaseDevice)
  97. {
  98. TBaseDevice device = new TBaseDevice();
  99. device.setDevCode(tBaseDevice.getDevCode());
  100. device.setPlantId(tBaseDevice.getPlantId());
  101. List<TBaseDevice> tBaseDevices = tBaseDeviceService.selectTBaseDeviceList(device);
  102. if (CollectionUtils.isNotEmpty(tBaseDevices)){
  103. return AjaxResult.error("当前装置下已存在该设备!");
  104. }
  105. tBaseDevice.setUpdaterCode(getUserId());
  106. tBaseDevice.setCreaterCode(getUserId());
  107. tBaseDevice.setUpdatedate(new Date());
  108. return toAjax(tBaseDeviceService.insertTBaseDevice(tBaseDevice));
  109. }
  110. /**
  111. * 修改设备/管线
  112. */
  113. @PreAuthorize("@ss.hasPermi('base:device:edit')")
  114. @Log(title = "设备/管线修改", businessType = BusinessType.UPDATE)
  115. @RepeatSubmit
  116. @PutMapping
  117. public AjaxResult edit(@RequestBody TBaseDevice tBaseDevice)
  118. {
  119. tBaseDevice.setUpdaterCode(getUserId());
  120. tBaseDevice.setUpdatedate(new Date());
  121. return toAjax(tBaseDeviceService.updateTBaseDevice(tBaseDevice));
  122. }
  123. @PutMapping("/handleApprove")
  124. @RepeatSubmit
  125. @Log(title = "设备/管线审核", businessType = BusinessType.APPROVE)
  126. public AjaxResult handleApprove(@RequestBody TBaseDevice tBaseDevice)
  127. {
  128. tBaseDevice.setApproveTime(new Date());
  129. return toAjax(tBaseDeviceService.updateTBaseDeviceByDevIds(tBaseDevice));
  130. }
  131. /**
  132. * 删除设备/管线
  133. */
  134. @PreAuthorize("@ss.hasPermi('base:device:remove')")
  135. @Log(title = "设备/管线删除", businessType = BusinessType.DELETE)
  136. @DeleteMapping("/{ids}")
  137. public AjaxResult remove(@PathVariable Long[] ids)
  138. {
  139. return toAjax(tBaseDeviceService.deleteTBaseDeviceByIds(ids));
  140. }
  141. @PreAuthorize("@ss.hasPermi('base:region:remove')")
  142. @Log(title = "设备/管线作废", businessType = BusinessType.DELETE)
  143. @DeleteMapping("/handleDisabled/{id}")
  144. public AjaxResult handleDisabled(@PathVariable Long id) {
  145. return toAjax(tBaseDeviceService.disabledDevice(id));
  146. }
  147. @Log(title = "受控设备台账导入", businessType = BusinessType.INSERT)
  148. @RepeatSubmit
  149. @PostMapping("/importData")
  150. public AjaxResult importData(@RequestParam("file") MultipartFile file) throws IOException {
  151. //获取操作人员ID
  152. Long userId = getUserId();
  153. //报错行数统计
  154. List<Integer> failRow = new ArrayList<>();
  155. Workbook workbook = ExcelUtils.getWorkBook(file);
  156. Sheet sheet = workbook.getSheetAt(0);
  157. List<TBaseDevice> list = new ArrayList<>();
  158. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  159. int rowNum = sheet.getPhysicalNumberOfRows();
  160. int failNumber = 0;
  161. aa:for (int i = 1; i < rowNum; i++) {
  162. try {
  163. logger.info("读取行数:" + i);
  164. Row row = sheet.getRow(i);
  165. int cellNum = row.getLastCellNum();
  166. TBaseDevice entity = new TBaseDevice();
  167. Long plantId =0L;
  168. for (int j = 0; j < cellNum; j++) {
  169. Cell cell = row.getCell(j);
  170. if (cell == null) {
  171. continue;
  172. }
  173. String cellValue = ExcelUtils.getCellValue(cell);
  174. logger.info("cellValue:" + cellValue);
  175. if (j == 0) {
  176. //装置名称
  177. TBasePlant tBasePlant = tBasePlantService.selectTBasePlantByName(cellValue);
  178. if (tBasePlant == null) {
  179. failNumber++;
  180. logger.info("未找到装置");
  181. failRow.add(i + 1);
  182. continue aa;
  183. }
  184. plantId = tBasePlant.getPlantId();
  185. entity.setPlantId(plantId);
  186. } else if (j == 1) {
  187. // 区域名称
  188. TBaseRegion tBaseRegion = tBaseRegionService.selectTBaseRegionByName(cellValue, plantId);
  189. if (tBaseRegion == null) {
  190. failNumber++;
  191. logger.info("未找到区域");
  192. failRow.add(i + 1);
  193. continue aa;
  194. }
  195. entity.setRegionId(tBaseRegion.getRegionId());
  196. } else if (j == 2) {
  197. // 设备描述
  198. entity.setDevDescribe(cellValue);
  199. } else if (j == 3) {
  200. //设备编号
  201. entity.setDevCode(cellValue);
  202. } else if (j == 4) {
  203. // 主要物料
  204. entity.setMaterial(cellValue);
  205. } else if (j == 5) {
  206. // 物料状态
  207. entity.setMaterialStatus(cellValue);
  208. } else if (j == 6) {
  209. // 合成响应因子
  210. entity.setResponseFactor(cellValue);
  211. } else if (j == 7) {
  212. // 响应因子来源
  213. entity.setResponseFactorFrom(cellValue);
  214. } else if (j == 8) {
  215. // 维护日期
  216. entity.setUpdatedate(new SimpleDateFormat(DateUtils.getDateFormat(cellValue)).parse(cellValue));
  217. } else if (j == 9) {
  218. // 备注
  219. entity.setRemarks(cellValue);
  220. }
  221. }
  222. entity.setCreaterCode(userId);
  223. entity.setApproveStatus(0L);
  224. logger.info("entity:" + entity);
  225. list.add(entity);
  226. } catch (Exception e) {
  227. failNumber++;
  228. logger.info("e:" + JSON.toJSONString(e));
  229. failRow.add(i + 1);
  230. }
  231. }
  232. int successNumber = 0;
  233. int failNum = 0;
  234. for (TBaseDevice t : list
  235. ) {
  236. failNum++;
  237. try {
  238. //根据使用证、注册编号、位号,进行数据更新
  239. add(t);
  240. successNumber++;
  241. } catch (Exception e) {
  242. failNumber++;
  243. logger.info("e:" + e);
  244. failRow.add(failNum + 1);
  245. }
  246. }
  247. logger.info("list:" + JSON.toJSONString(list));
  248. logger.info("successNumber:" + String.valueOf(successNumber));
  249. logger.info("failNumber:" + String.valueOf(failNumber));
  250. logger.info("failRow:" + String.valueOf(failRow));
  251. return AjaxResult.success(String.valueOf(successNumber), failRow);
  252. }
  253. }