change - 添加传感器类型导入功能

main
wenjy 3 years ago
parent fbb42c700d
commit 4e3b4523ca

@ -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<BaseSensorType> util = new ExcelUtil<BaseSensorType>(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<BaseSensorType> util = new ExcelUtil<BaseSensorType>(BaseSensorType.class);
//读取file文件将文件内容转为list集合
List<BaseSensorType> baseSensorTypeList = util.importExcel(file.getInputStream());
String message = baseSensorTypeService.importMould(baseSensorTypeList, updateSupport);
return AjaxResult.success(message);
}
/**
*
*/

@ -44,6 +44,9 @@
<a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="base:sensorType:remove">
<i class="fa fa-remove"></i> 删除
</a>
<a class="btn btn-info" onclick="$.table.importExcel() " shiro:hasPermission="base:sensorType:import">
<i class="fa fa-upload"></i> 导入
</a>
<a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="base:sensorType: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>

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

@ -27,6 +27,16 @@ public interface IBaseSensorTypeService
*/
public List<BaseSensorType> selectBaseSensorTypeList(BaseSensorType baseSensorType);
/**
*
* @author WenJY
* @date 2022/2/7 10:36
* @param baseSensorTypes
* @param updateSupport
* @return java.lang.String
*/
public String importMould(List<BaseSensorType> baseSensorTypes, boolean updateSupport);
/**
*
*

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

Loading…
Cancel
Save