diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/base/BaseMonitorunitTypeController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/base/BaseMonitorunitTypeController.java index 488f9f9..0c2f104 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/base/BaseMonitorunitTypeController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/base/BaseMonitorunitTypeController.java @@ -21,6 +21,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 @@ -52,6 +53,38 @@ public class BaseMonitorunitTypeController extends BaseController { return getDataTable(list); } + /** + * 获取导入模板 + * @author WenJY + * @date 2021/8/16 17:25 + * @return com.ruoyi.common.core.domain.AjaxResult + */ + @RequiresPermissions("mould:mouldFont:view") + @GetMapping("/importTemplate") + @ResponseBody + public AjaxResult importTemplate() { + ExcelUtil util = new ExcelUtil(BaseMonitorunitType.class); + return util.importTemplateExcel("监控单元类型"); + } + + /** + * 导入活字块台账信息 + * + * @param file + * @return + * @throws Exception + */ + @RequiresPermissions("mould:mouldFont:import") + @PostMapping("/importData") + @ResponseBody + public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception { + ExcelUtil util = new ExcelUtil(BaseMonitorunitType.class); + //读取file文件将文件内容转为list集合 + List baseStandardList = util.importExcel(file.getInputStream()); + String message = baseMonitorunitTypeService.importMould(baseStandardList,updateSupport); + return AjaxResult.success(message); + } + /** 导出监控单元类型列表 */ @RequiresPermissions("base:monitorUnitType:export") @Log(title = "监控单元类型", businessType = BusinessType.EXPORT) diff --git a/ruoyi-admin/src/main/resources/templates/base/monitorUnitType/monitorUnitType.html b/ruoyi-admin/src/main/resources/templates/base/monitorUnitType/monitorUnitType.html index 297936c..74d30aa 100644 --- a/ruoyi-admin/src/main/resources/templates/base/monitorUnitType/monitorUnitType.html +++ b/ruoyi-admin/src/main/resources/templates/base/monitorUnitType/monitorUnitType.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-common/src/main/java/com/ruoyi/common/exception/BusinessException.java b/ruoyi-common/src/main/java/com/ruoyi/common/exception/BusinessException.java new file mode 100644 index 0000000..d490675 --- /dev/null +++ b/ruoyi-common/src/main/java/com/ruoyi/common/exception/BusinessException.java @@ -0,0 +1,30 @@ +package com.ruoyi.common.exception; + +/** + * 业务异常 + * + * @author ruoyi + */ +public class BusinessException extends RuntimeException +{ + private static final long serialVersionUID = 1L; + + protected final String message; + + public BusinessException(String message) + { + this.message = message; + } + + public BusinessException(String message, Throwable e) + { + super(message, e); + this.message = message; + } + + @Override + public String getMessage() + { + return message; + } +} diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/domain/BaseMonitorunitType.java b/ruoyi-system/src/main/java/com/ruoyi/system/domain/BaseMonitorunitType.java index a6e0222..e54a500 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/domain/BaseMonitorunitType.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/domain/BaseMonitorunitType.java @@ -15,6 +15,13 @@ public class BaseMonitorunitType extends BaseEntity { private static final long serialVersionUID = 1L; + public BaseMonitorunitType() { + } + + public BaseMonitorunitType(String monitorunittypeId) { + this.monitorunittypeId = monitorunittypeId; + } + /** 主键标识 */ private Long objId; diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/IBaseMonitorunitTypeService.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/IBaseMonitorunitTypeService.java index 427d507..2ea392a 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/IBaseMonitorunitTypeService.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/IBaseMonitorunitTypeService.java @@ -27,6 +27,16 @@ public interface IBaseMonitorunitTypeService */ public List selectBaseMonitorunitTypeList(BaseMonitorunitType baseMonitorunitType); + /** + * 导入监控单元类型 + * @author WenJY + * @date 2022/2/7 9:35 + * @param baseMonitorunitType + * @param updateSupport + * @return java.lang.String + */ + public String importMould(List baseMonitorunitType, boolean updateSupport); + /** * 新增监控单元类型 * diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/BaseMonitorunitTypeServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/BaseMonitorunitTypeServiceImpl.java index e9e3f93..2c8ab51 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/BaseMonitorunitTypeServiceImpl.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/BaseMonitorunitTypeServiceImpl.java @@ -1,7 +1,12 @@ 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 org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.ruoyi.system.mapper.BaseMonitorunitTypeMapper; @@ -55,6 +60,62 @@ public class BaseMonitorunitTypeServiceImpl implements IBaseMonitorunitTypeServi return baseMonitorunitTypeMapper.selectBaseMonitorunitTypeList(baseMonitorunitType); } + /** + * 导入监控单元类型 + * @author WenJY + * @date 2022/2/7 9:27 + * @param baseMonitorunitTypes + * @param updateSupport + * @return java.lang.String + */ + @Override + public String importMould(List baseMonitorunitTypes, boolean updateSupport) { + if (StringUtils.isNull(baseMonitorunitTypes) || baseMonitorunitTypes.size() == 0) { + throw new BusinessException("导入标准数据不能为空!"); + } + int successNum = 0; + int failureNum = 0; + StringBuilder successMsg = new StringBuilder(); + StringBuilder failureMsg = new StringBuilder(); + for (BaseMonitorunitType baseMonitorunitType : baseMonitorunitTypes) { + try { + List baseMonitorunitTypeList = + baseMonitorunitTypeMapper.selectBaseMonitorunitTypeList(new BaseMonitorunitType(baseMonitorunitType.getMonitorunittypeId())); + + if (baseMonitorunitTypeList.size() == 0) { + baseMonitorunitType.setCreateBy(ShiroUtils.getLoginName()); + baseMonitorunitType.setCreateTime(new Date()); + baseMonitorunitTypeMapper.insertBaseMonitorunitType(baseMonitorunitType); + successNum++; + successMsg.append("
" + successNum + "、 " + baseMonitorunitType.getMonitorunittypeName() + " 导入成功"); + } else if (updateSupport) { + for (BaseMonitorunitType unitType : baseMonitorunitTypeList) { + baseMonitorunitType.setObjId(unitType.getObjId()); + baseMonitorunitType.setUpdateBy(ShiroUtils.getLoginName()); + baseMonitorunitType.setUpdateTime(new Date()); + baseMonitorunitTypeMapper.updateBaseMonitorunitType(baseMonitorunitType); + successNum++; + } + successMsg.append("
" + successNum + "、" + baseMonitorunitType.getMonitorunittypeName() + " 更新成功"); + } else { + failureNum++; + failureMsg.append("
" + failureNum + "、" + baseMonitorunitType.getMonitorunittypeId() + "、" + baseMonitorunitType.getMonitorunittypeName() + " 已存在"); + } + } catch (Exception e) { + failureNum++; + String msg = "
" + failureNum + "、标准: " + baseMonitorunitType.getMonitorunittypeId() + "、" + baseMonitorunitType.getMonitorunittypeName() + " 导入失败:"; + 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(); + } + /** * 新增监控单元类型 *