123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- 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<TBaseDevice> 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<TBaseDevice> list = tBaseDeviceService.selectTBaseDeviceList(tBaseDevice);
- ExcelUtil<TBaseDevice> util = new ExcelUtil<TBaseDevice>(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<TBaseDevice> 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<Integer> failRow = new ArrayList<>();
- Workbook workbook = ExcelUtils.getWorkBook(file);
- Sheet sheet = workbook.getSheetAt(0);
- List<TBaseDevice> 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);
- }
- }
|