浏览代码

2021/6/28 阈值管理Excel导入导出

yinhao 4 年之前
父节点
当前提交
233190de5e

+ 32 - 0
huimv-smart-management/pom.xml

@@ -17,6 +17,26 @@
         <java.version>1.8</java.version>
         <spring-cloud.version>2020.0.2</spring-cloud.version>
     </properties>
+
+    <repositories>
+        <repository>
+            <id>aliyun</id>
+            <name>aliyun Repository</name>
+            <url>http://maven.aliyun.com/nexus/content/groups/public</url>
+            <snapshots>
+                <enabled>false</enabled>
+            </snapshots>
+        </repository>
+        <repository>
+            <id>jeecg</id>
+            <name>jeecg Repository</name>
+            <url>http://maven.jeecg.org/nexus/content/repositories/jeecg</url>
+            <snapshots>
+                <enabled>false</enabled>
+            </snapshots>
+        </repository>
+    </repositories>
+
     <dependencies>
         <dependency>
             <groupId>com.huimv</groupId>
@@ -33,6 +53,18 @@
         </dependency>
 
         <dependency>
+            <groupId>org.jeecgframework</groupId>
+            <artifactId>autopoi-web</artifactId>
+            <version>1.0.4</version>
+            <exclusions>
+                <exclusion>
+                    <groupId>commons-codec</groupId>
+                    <artifactId>commons-codec</artifactId>
+                </exclusion>
+            </exclusions>
+        </dependency>
+
+        <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-test</artifactId>
             <scope>test</scope>

+ 96 - 10
huimv-smart-management/src/main/java/com/huimv/management/controller/ThresholdManagementController.java

@@ -1,16 +1,33 @@
- package com.huimv.management.controller;
+package com.huimv.management.controller;
 
+import java.io.IOException;
 import java.util.Arrays;
+import java.util.List;
 import java.util.Map;
 
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import io.swagger.annotations.Api;
+import lombok.extern.slf4j.Slf4j;
+import org.jeecgframework.poi.excel.ExcelImportUtil;
+import org.jeecgframework.poi.excel.def.NormalExcelConstants;
+import org.jeecgframework.poi.excel.entity.ExportParams;
+import org.jeecgframework.poi.excel.entity.ImportParams;
+import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.transaction.annotation.Transactional;
 import org.springframework.web.bind.annotation.*;
 
 import com.huimv.management.entity.ThresholdManagementEntity;
 import com.huimv.management.service.ThresholdManagementService;
 import com.huimv.common.utils.PageUtils;
 import com.huimv.common.utils.R;
+import org.springframework.web.multipart.MultipartFile;
+import org.springframework.web.multipart.MultipartHttpServletRequest;
+import org.springframework.web.servlet.ModelAndView;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
 
 /**
  * <p>
@@ -21,6 +38,7 @@ import com.huimv.common.utils.R;
  * @version 1.0
  * @date 2021-06-15 13:01:37
  */
+@Slf4j
 @Api(tags = "报警阈值管理Controller")
 @RestController
 @RequestMapping("management/thresholdManagement")
@@ -33,7 +51,7 @@ public class ThresholdManagementController {
      * 列表
      */
     @GetMapping("/list")
-    public R list(@RequestParam Map<String, Object> params){
+    public R list(@RequestParam Map<String, Object> params) {
         PageUtils page = thresholdManagementService.queryPage(params);
 
         return R.ok().put("page", page);
@@ -44,8 +62,8 @@ public class ThresholdManagementController {
      * 信息
      */
     @GetMapping("/info/{id}")
-    public R info(@PathVariable("id") Integer id){
-		ThresholdManagementEntity thresholdManagement = thresholdManagementService.getById(id);
+    public R info(@PathVariable("id") Integer id) {
+        ThresholdManagementEntity thresholdManagement = thresholdManagementService.getById(id);
 
         return R.ok().put("thresholdManagement", thresholdManagement);
     }
@@ -54,8 +72,8 @@ public class ThresholdManagementController {
      * 保存
      */
     @PostMapping("/save")
-    public R save(@RequestBody ThresholdManagementEntity thresholdManagement){
-		thresholdManagementService.save(thresholdManagement);
+    public R save(@RequestBody ThresholdManagementEntity thresholdManagement) {
+        thresholdManagementService.save(thresholdManagement);
 
         return R.ok();
     }
@@ -64,8 +82,8 @@ public class ThresholdManagementController {
      * 修改
      */
     @PostMapping("/update")
-    public R update(@RequestBody ThresholdManagementEntity thresholdManagement){
-		thresholdManagementService.updateById(thresholdManagement);
+    public R update(@RequestBody ThresholdManagementEntity thresholdManagement) {
+        thresholdManagementService.updateById(thresholdManagement);
 
         return R.ok();
     }
@@ -74,10 +92,78 @@ public class ThresholdManagementController {
      * 删除
      */
     @PostMapping("/delete")
-    public R delete(@RequestBody Integer[] ids){
-		thresholdManagementService.removeByIds(Arrays.asList(ids));
+    public R delete(@RequestBody Integer[] ids) {
+        thresholdManagementService.removeByIds(Arrays.asList(ids));
 
         return R.ok();
     }
 
+    @GetMapping("/exportXls")
+    public ModelAndView exportXls(ThresholdManagementEntity thresholdManagementEntity, HttpServletRequest request) {
+
+        LambdaQueryWrapper<ThresholdManagementEntity> queryWrapper = Wrappers.lambdaQuery(thresholdManagementEntity);
+        List<ThresholdManagementEntity> list = thresholdManagementService.list(queryWrapper);
+
+        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
+        mv.addObject(NormalExcelConstants.FILE_NAME, "阈值列表");
+        mv.addObject(NormalExcelConstants.CLASS, ThresholdManagementEntity.class);
+        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("阈值列表数据", "导出信息"));
+        mv.addObject(NormalExcelConstants.DATA_LIST, list);
+
+        return mv;
+    }
+
+
+    @PostMapping("/importExcel")
+    @Transactional(rollbackFor = Throwable.class)
+    public R importExcel(HttpServletRequest request, HttpServletResponse response) {
+        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
+        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
+        for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
+            // 获取上传文件对象
+            MultipartFile file = entity.getValue();
+            ImportParams params = new ImportParams();
+            params.setTitleRows(1);
+            params.setHeadRows(1);
+            params.setNeedSave(true);
+            try {
+                List<ThresholdManagementEntity> list = ExcelImportUtil.importExcel(file.getInputStream(), ThresholdManagementEntity.class, params);
+                for (ThresholdManagementEntity thresholdManagementEntity : list) {
+                    Integer thresholdType = thresholdManagementEntity.getThresholdType();
+                    switch (thresholdType) {
+                        case 1:
+                        case 2:
+                        case 3:
+                            thresholdManagementEntity.setBelongTo((short) 1);
+                            break;
+                        case 4:
+                            thresholdManagementEntity.setBelongTo((short) 4);
+                            break;
+                        case 5:
+                        case 6:
+                        case 7:
+                            thresholdManagementEntity.setBelongTo((short) 2);
+                            break;
+                        case 8:
+                        case 9:
+                        case 10:
+                            thresholdManagementEntity.setBelongTo((short) 3);
+                            break;
+                    }
+                    thresholdManagementService.save(thresholdManagementEntity);
+                }
+            } catch (Exception e) {
+                log.error(e.getMessage(),e);
+            } finally {
+                try {
+                    file.getInputStream().close();
+                } catch (IOException e) {
+                    log.error(e.getMessage(),e);
+                }
+            }
+        }
+        return R.ok();
+
+    }
+
 }

+ 4 - 0
huimv-smart-management/src/main/java/com/huimv/management/entity/ThresholdManagementEntity.java

@@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.annotation.*;
 import java.io.Serializable;
 import java.util.Date;
 import lombok.Data;
+import org.jeecgframework.poi.excel.annotation.Excel;
 
 /**
  * 
@@ -26,10 +27,12 @@ public class ThresholdManagementEntity implements Serializable {
 	/**
 	 * 牧场id
 	 */
+	@Excel(name="牧场名称",width = 15,replace = {"小猪认养牧场_144","海盐牧场_145"})
 	private Integer farmId;
 	/**
 	 * 阈值类型
 	 */
+	@Excel(name = "阈值类型",width = 15,orderNum = "1",replace = {"室内高温_1","室内低温_2","室内空气污染_3","饮用水污染_4","室外高温_5","室外低温_6","室外空气污染_7","体温高温_8","体温低温_9","耳标电量不足_10"})
 	private Integer thresholdType;
 	/**
 	 * 所属范围
@@ -42,6 +45,7 @@ public class ThresholdManagementEntity implements Serializable {
 	/**
 	 * 阈值数值
 	 */
+	@Excel(name = "阈值数值",orderNum = "2")
 	private Double thresholdValue;
 	/**
 	 * 删除状态 0:未删除 其他:已删除