package com.ruoyi.project.base.controller; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import javax.servlet.http.HttpServletResponse; import com.alibaba.fastjson2.JSON; import com.ruoyi.common.annotation.RepeatSubmit; import com.ruoyi.common.core.domain.entity.SysDictData; import com.ruoyi.common.utils.DateUtils; import com.ruoyi.common.utils.file.ExcelUtils; import com.ruoyi.project.base.domain.TBasePlant; import com.ruoyi.project.base.domain.TBaseRegion; import com.ruoyi.project.base.service.ITBasePlantService; import com.ruoyi.project.base.service.ITBaseRegionService; import com.ruoyi.system.service.ISysDictTypeService; import org.apache.commons.collections4.CollectionUtils; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import com.ruoyi.common.annotation.Log; import com.ruoyi.common.core.controller.BaseController; import com.ruoyi.common.core.domain.AjaxResult; import com.ruoyi.common.enums.BusinessType; import com.ruoyi.project.base.domain.TBaseDevice; import com.ruoyi.project.base.service.ITBaseDeviceService; import com.ruoyi.common.utils.poi.ExcelUtil; import com.ruoyi.common.core.page.TableDataInfo; import org.springframework.web.multipart.MultipartFile; /** * 设备/管线Controller * * @author ruoyi * @date 2022-11-15 */ @RestController @RequestMapping("/base/device") public class TBaseDeviceController extends BaseController { @Autowired private ITBaseDeviceService tBaseDeviceService; @Autowired private ITBasePlantService tBasePlantService; @Autowired private ITBaseRegionService tBaseRegionService; /** * 查询设备/管线列表 */ @PreAuthorize("@ss.hasPermi('base:device:list')") @GetMapping("/list") public TableDataInfo list(TBaseDevice tBaseDevice) { startPage(); List list = tBaseDeviceService.selectTBaseDeviceList(tBaseDevice); return getDataTable(list); } @GetMapping("/allDevice/{regionId}") public AjaxResult allDevice(@PathVariable("regionId") Long regionId) { return AjaxResult.success(tBaseDeviceService.selectAllDeviceByRegionId(regionId)); } /** * 导出设备/管线列表 */ @PreAuthorize("@ss.hasPermi('base:device:export')") @Log(title = "设备/管线导出", businessType = BusinessType.EXPORT) @RepeatSubmit @PostMapping("/export") public void export(HttpServletResponse response, TBaseDevice tBaseDevice) { List list = tBaseDeviceService.selectTBaseDeviceList(tBaseDevice); ExcelUtil util = new ExcelUtil(TBaseDevice.class); util.exportExcel(response, list, "受控设备清单"); } /** * 获取设备/管线详细信息 */ @PreAuthorize("@ss.hasPermi('base:device:query')") @GetMapping(value = "/{id}") public AjaxResult getInfo(@PathVariable("id") Long id) { return AjaxResult.success(tBaseDeviceService.selectTBaseDeviceById(id)); } /** * 新增设备/管线 */ @PreAuthorize("@ss.hasPermi('base:device:add')") @Log(title = "设备/管线新增", businessType = BusinessType.INSERT) @RepeatSubmit @PostMapping public AjaxResult add(@RequestBody TBaseDevice tBaseDevice) { TBaseDevice device = new TBaseDevice(); device.setDevCode(tBaseDevice.getDevCode()); device.setPlantId(tBaseDevice.getPlantId()); List tBaseDevices = tBaseDeviceService.selectTBaseDeviceList(device); if (CollectionUtils.isNotEmpty(tBaseDevices)){ return AjaxResult.error("当前装置下已存在该设备!"); } tBaseDevice.setUpdaterCode(getUserId()); tBaseDevice.setCreaterCode(getUserId()); tBaseDevice.setUpdatedate(new Date()); return toAjax(tBaseDeviceService.insertTBaseDevice(tBaseDevice)); } /** * 修改设备/管线 */ @PreAuthorize("@ss.hasPermi('base:device:edit')") @Log(title = "设备/管线修改", businessType = BusinessType.UPDATE) @RepeatSubmit @PutMapping public AjaxResult edit(@RequestBody TBaseDevice tBaseDevice) { tBaseDevice.setUpdaterCode(getUserId()); tBaseDevice.setUpdatedate(new Date()); return toAjax(tBaseDeviceService.updateTBaseDevice(tBaseDevice)); } @PutMapping("/handleApprove") @RepeatSubmit @Log(title = "设备/管线审核", businessType = BusinessType.APPROVE) public AjaxResult handleApprove(@RequestBody TBaseDevice tBaseDevice) { tBaseDevice.setApproveTime(new Date()); return toAjax(tBaseDeviceService.updateTBaseDeviceByDevIds(tBaseDevice)); } /** * 删除设备/管线 */ @PreAuthorize("@ss.hasPermi('base:device:remove')") @Log(title = "设备/管线删除", businessType = BusinessType.DELETE) @DeleteMapping("/{ids}") public AjaxResult remove(@PathVariable Long[] ids) { return toAjax(tBaseDeviceService.deleteTBaseDeviceByIds(ids)); } @PreAuthorize("@ss.hasPermi('base:region:remove')") @Log(title = "设备/管线作废", businessType = BusinessType.DELETE) @DeleteMapping("/handleDisabled/{id}") public AjaxResult handleDisabled(@PathVariable Long id) { return toAjax(tBaseDeviceService.disabledDevice(id)); } @Log(title = "受控设备台账导入", businessType = BusinessType.INSERT) @RepeatSubmit @PostMapping("/importData") public AjaxResult importData(@RequestParam("file") MultipartFile file) throws IOException { //获取操作人员ID Long userId = getUserId(); //报错行数统计 List failRow = new ArrayList<>(); Workbook workbook = ExcelUtils.getWorkBook(file); Sheet sheet = workbook.getSheetAt(0); List list = new ArrayList<>(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); int rowNum = sheet.getPhysicalNumberOfRows(); int failNumber = 0; aa:for (int i = 1; i < rowNum; i++) { try { logger.info("读取行数:" + i); Row row = sheet.getRow(i); int cellNum = row.getLastCellNum(); TBaseDevice entity = new TBaseDevice(); Long plantId =0L; for (int j = 0; j < cellNum; j++) { Cell cell = row.getCell(j); if (cell == null) { continue; } String cellValue = ExcelUtils.getCellValue(cell); logger.info("cellValue:" + cellValue); if (j == 0) { //装置名称 TBasePlant tBasePlant = tBasePlantService.selectTBasePlantByName(cellValue); if (tBasePlant == null) { failNumber++; logger.info("未找到装置"); failRow.add(i + 1); continue aa; } plantId = tBasePlant.getPlantId(); entity.setPlantId(plantId); } else if (j == 1) { // 区域名称 TBaseRegion tBaseRegion = tBaseRegionService.selectTBaseRegionByName(cellValue, plantId); if (tBaseRegion == null) { failNumber++; logger.info("未找到区域"); failRow.add(i + 1); continue aa; } entity.setRegionId(tBaseRegion.getRegionId()); } else if (j == 2) { // 设备描述 entity.setDevDescribe(cellValue); } else if (j == 3) { //设备编号 entity.setDevCode(cellValue); } else if (j == 4) { // 主要物料 entity.setMaterial(cellValue); } else if (j == 5) { // 物料状态 entity.setMaterialStatus(cellValue); } else if (j == 6) { // 合成响应因子 entity.setResponseFactor(cellValue); } else if (j == 7) { // 响应因子来源 entity.setResponseFactorFrom(cellValue); } else if (j == 8) { // 维护日期 entity.setUpdatedate(new SimpleDateFormat(DateUtils.getDateFormat(cellValue)).parse(cellValue)); } else if (j == 9) { // 备注 entity.setRemarks(cellValue); } } entity.setCreaterCode(userId); entity.setApproveStatus(0L); logger.info("entity:" + entity); list.add(entity); } catch (Exception e) { failNumber++; logger.info("e:" + JSON.toJSONString(e)); failRow.add(i + 1); } } int successNumber = 0; int failNum = 0; for (TBaseDevice t : list ) { failNum++; try { //根据使用证、注册编号、位号,进行数据更新 add(t); successNumber++; } catch (Exception e) { failNumber++; logger.info("e:" + e); failRow.add(failNum + 1); } } logger.info("list:" + JSON.toJSONString(list)); logger.info("successNumber:" + String.valueOf(successNumber)); logger.info("failNumber:" + String.valueOf(failNumber)); logger.info("failRow:" + String.valueOf(failRow)); return AjaxResult.success(String.valueOf(successNumber), failRow); } }