change - 监控单元添加导入功能

main
wenjy 3 years ago
parent fd473ccf0a
commit 56b9fb6209

@ -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<BaseMonitorunitType> util = new ExcelUtil<BaseMonitorunitType>(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<BaseMonitorunitType> util = new ExcelUtil<BaseMonitorunitType>(BaseMonitorunitType.class);
//读取file文件将文件内容转为list集合
List<BaseMonitorunitType> baseStandardList = util.importExcel(file.getInputStream());
String message = baseMonitorunitTypeService.importMould(baseStandardList,updateSupport);
return AjaxResult.success(message);
}
/** 导出监控单元类型列表 */
@RequiresPermissions("base:monitorUnitType:export")
@Log(title = "监控单元类型", businessType = BusinessType.EXPORT)

@ -44,6 +44,9 @@
<a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="base:monitorUnitType:remove">
<i class="fa fa-remove"></i> 删除
</a>
<a class="btn btn-info" onclick="$.table.importExcel() " shiro:hasPermission="base:monitorUnitType:import">
<i class="fa fa-upload"></i> 导入
</a>
<a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="base:monitorUnitType:export">
<i class="fa fa-download"></i> 导出
</a>
@ -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);
});
</script>
<!-- 导入区域 -->
<script id="importTpl" type="text/template">
<form enctype="multipart/form-data" class="mt20 mb10">
<div class="col-xs-offset-1">
<input type="file" id="file" name="file"/>
<div class="mt10 pt5">
<input type="checkbox" id="updateSupport" name="updateSupport" title="如果登录账户已经存在,更新这条数据。"> 是否更新已经存在的用户数据
&nbsp; <a onclick="$.table.importTemplate()" class="btn btn-default btn-xs"><i
class="fa fa-file-excel-o"></i> 下载模板</a>
</div>
<font color="red" class="pull-left mt10">
提示仅允许导入“xls”或“xlsx”格式文件
</font>
</div>
</form>
</script>
</body>
</html>

@ -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;
}
}

@ -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;

@ -27,6 +27,16 @@ public interface IBaseMonitorunitTypeService
*/
public List<BaseMonitorunitType> selectBaseMonitorunitTypeList(BaseMonitorunitType baseMonitorunitType);
/**
*
* @author WenJY
* @date 2022/2/7 9:35
* @param baseMonitorunitType
* @param updateSupport
* @return java.lang.String
*/
public String importMould(List<BaseMonitorunitType> baseMonitorunitType, boolean updateSupport);
/**
*
*

@ -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<BaseMonitorunitType> 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<BaseMonitorunitType> 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("<br/>" + 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("<br/>" + successNum + "、" + baseMonitorunitType.getMonitorunittypeName() + " 更新成功");
} else {
failureNum++;
failureMsg.append("<br/>" + failureNum + "、" + baseMonitorunitType.getMonitorunittypeId() + "、" + baseMonitorunitType.getMonitorunittypeName() + " 已存在");
}
} catch (Exception e) {
failureNum++;
String msg = "<br/>" + 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();
}
/**
*
*

Loading…
Cancel
Save