diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/base/BaseSensorTypeController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/base/BaseSensorTypeController.java index cb05f4f..f04d7fe 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/base/BaseSensorTypeController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/base/BaseSensorTypeController.java @@ -4,6 +4,7 @@ import java.util.Date; import java.util.List; import com.ruoyi.common.utils.ShiroUtils; +import com.ruoyi.system.domain.BaseMonitorunitInfo; import org.apache.shiro.authz.annotation.RequiresPermissions; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; @@ -21,6 +22,7 @@ import com.ruoyi.common.core.controller.BaseController; import com.ruoyi.common.core.domain.AjaxResult; import com.ruoyi.common.utils.poi.ExcelUtil; import com.ruoyi.common.core.page.TableDataInfo; +import org.springframework.web.multipart.MultipartFile; /** * 传感器类型Controller @@ -57,6 +59,39 @@ public class BaseSensorTypeController extends BaseController return getDataTable(list); } + /** + * 获取导入模板 + * @author WenJY + * @date 2022/2/7 9:57 + * @return com.ruoyi.common.core.domain.AjaxResult + */ + @RequiresPermissions("base:sensorType:importTemplate") + @GetMapping("/importTemplate") + @ResponseBody + public AjaxResult importTemplate() { + ExcelUtil util = new ExcelUtil(BaseSensorType.class); + return util.importTemplateExcel("传感器类型"); + } + + /** + * 导入传感器类型信息 + * @author WenJY + * @date 2022/2/7 10:41 + * @param file + * @param updateSupport + * @return com.ruoyi.common.core.domain.AjaxResult + */ + @RequiresPermissions("base:sensorType:importData") + @PostMapping("/importData") + @ResponseBody + public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception { + ExcelUtil util = new ExcelUtil(BaseSensorType.class); + //读取file文件将文件内容转为list集合 + List baseSensorTypeList = util.importExcel(file.getInputStream()); + String message = baseSensorTypeService.importMould(baseSensorTypeList, updateSupport); + return AjaxResult.success(message); + } + /** * 导出传感器类型列表 */ diff --git a/ruoyi-admin/src/main/resources/templates/base/sensorType/sensorType.html b/ruoyi-admin/src/main/resources/templates/base/sensorType/sensorType.html index 65f4aac..bc8b17d 100644 --- a/ruoyi-admin/src/main/resources/templates/base/sensorType/sensorType.html +++ b/ruoyi-admin/src/main/resources/templates/base/sensorType/sensorType.html @@ -44,6 +44,9 @@ 删除 + + 导入 + 导出 @@ -67,6 +70,8 @@ updateUrl: prefix + "/edit/{id}", removeUrl: prefix + "/remove", exportUrl: prefix + "/export", + importUrl: prefix + "/importData", + importTemplateUrl: prefix + "/importTemplate", modalName: "传感器类型", columns: [{ checkbox: true @@ -121,5 +126,21 @@ $.table.init(options); }); + + \ No newline at end of file diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/domain/BaseSensorType.java b/ruoyi-system/src/main/java/com/ruoyi/system/domain/BaseSensorType.java index 6a4151a..c21c2d3 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/domain/BaseSensorType.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/domain/BaseSensorType.java @@ -15,6 +15,13 @@ public class BaseSensorType extends BaseEntity { private static final long serialVersionUID = 1L; + public BaseSensorType() { + } + + public BaseSensorType(String sensortypeId) { + this.sensortypeId = sensortypeId; + } + /** 主键标识 */ private Long ObjId; diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/IBaseSensorTypeService.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/IBaseSensorTypeService.java index ab8197c..fcebc2e 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/IBaseSensorTypeService.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/IBaseSensorTypeService.java @@ -27,6 +27,16 @@ public interface IBaseSensorTypeService */ public List selectBaseSensorTypeList(BaseSensorType baseSensorType); + /** + * 导入传感器类型信息 + * @author WenJY + * @date 2022/2/7 10:36 + * @param baseSensorTypes + * @param updateSupport + * @return java.lang.String + */ + public String importMould(List baseSensorTypes, boolean updateSupport); + /** * 新增传感器类型 * diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/BaseSensorTypeServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/BaseSensorTypeServiceImpl.java index 0f8d8bd..39eafb9 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/BaseSensorTypeServiceImpl.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/BaseSensorTypeServiceImpl.java @@ -1,7 +1,13 @@ package com.ruoyi.system.service.impl; +import java.util.Date; import java.util.List; + +import com.ruoyi.common.exception.BusinessException; import com.ruoyi.common.utils.DateUtils; +import com.ruoyi.common.utils.ShiroUtils; +import com.ruoyi.common.utils.StringUtils; +import com.ruoyi.system.domain.BaseMonitorunitInfo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.ruoyi.system.mapper.BaseSensorTypeMapper; @@ -45,6 +51,62 @@ public class BaseSensorTypeServiceImpl implements IBaseSensorTypeService return baseSensorTypeMapper.selectBaseSensorTypeList(baseSensorType); } + /** + * 导入传感器类型信息 + * @author WenJY + * @date 2022/2/7 10:36 + * @param baseSensorTypes + * @param updateSupport + * @return java.lang.String + */ + @Override + public String importMould(List baseSensorTypes, boolean updateSupport) { + if (StringUtils.isNull(baseSensorTypes) || baseSensorTypes.size() == 0) { + throw new BusinessException("导入标准数据不能为空!"); + } + int successNum = 0; + int failureNum = 0; + StringBuilder successMsg = new StringBuilder(); + StringBuilder failureMsg = new StringBuilder(); + for (BaseSensorType baseSensorType : baseSensorTypes) { + try { + List baseSensorTypeList = + baseSensorTypeMapper.selectBaseSensorTypeList(new BaseSensorType(baseSensorType.getSensortypeId())); + + if (baseSensorTypeList.size() == 0) { + baseSensorType.setCreateBy(ShiroUtils.getLoginName()); + baseSensorType.setCreateTime(new Date()); + baseSensorTypeMapper.insertBaseSensorType(baseSensorType); + successNum++; + successMsg.append("
" + successNum + "、 " + baseSensorType.getSensortypeName() + " 导入成功"); + } else if (updateSupport) { + for (BaseSensorType sensorType : baseSensorTypeList) { + baseSensorType.setObjId(sensorType.getObjId()); + baseSensorType.setUpdateBy(ShiroUtils.getLoginName()); + baseSensorType.setUpdateTime(new Date()); + baseSensorTypeMapper.updateBaseSensorType(baseSensorType); + successNum++; + } + successMsg.append("
" + successNum + "、" + baseSensorType.getSensortypeName() + " 更新成功"); + } else { + failureNum++; + failureMsg.append("
" + failureNum + "、" + baseSensorType.getSensortypeId() + "、" + baseSensorType.getSensortypeName() + " 已存在"); + } + } catch (Exception e) { + failureNum++; + String msg = "
" + failureNum + "、标准: " + baseSensorType.getSensortypeId() + "、" + baseSensorType.getSensortypeName() + " 导入失败:"; + failureMsg.append(msg + e.getMessage()); + } + } + if (failureNum > 0) { + failureMsg.insert(0, "很抱歉,导入失败!共 " + failureNum + " 条数据格式不正确,错误如下:"); + throw new BusinessException(failureMsg.toString()); + } else { + successMsg.insert(0, "恭喜您,数据已全部导入成功!共 " + successNum + " 条,数据如下:"); + } + return successMsg.toString(); + } + /** * 新增传感器类型 *