Procházet zdrojové kódy

日志台账导出接口

yanliming před 5 roky
rodič
revize
7b40a22a40

+ 169 - 0
web/src/main/java/com/jpsoft/smart/modules/base/controller/PersonDeviceLogController.java

@@ -3,19 +3,30 @@ package com.jpsoft.smart.modules.base.controller;
 
 import com.github.pagehelper.Page;
 import com.github.pagehelper.util.StringUtil;
+import com.jpsoft.smart.config.OSSConfig;
 import com.jpsoft.smart.modules.base.entity.PersonDeviceLog;
 import com.jpsoft.smart.modules.base.service.PersonDeviceLogService;
 import com.jpsoft.smart.modules.common.dto.MessageResult;
 import com.jpsoft.smart.modules.common.dto.Sort;
+import com.jpsoft.smart.modules.common.utils.OSSUtil;
 import com.jpsoft.smart.modules.common.utils.PojoUtils;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiImplicitParam;
 import io.swagger.annotations.ApiImplicitParams;
 import io.swagger.annotations.ApiOperation;
 import org.apache.commons.lang3.StringUtils;
+import org.apache.poi.hssf.usermodel.HSSFCell;
+import org.apache.poi.hssf.usermodel.HSSFRow;
+import org.apache.poi.hssf.usermodel.HSSFSheet;
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -25,9 +36,13 @@ import java.util.Map;
 @RequestMapping("/personDeviceLog")
 @Api(description = "日志台账")
 public class PersonDeviceLogController {
+    private Logger logger = LoggerFactory.getLogger(getClass());
     @Autowired
     private PersonDeviceLogService personDeviceLogService;
 
+    @Autowired
+    private OSSConfig ossConfig;
+
     @ApiOperation(value="列表")
     @RequestMapping(value = "pageList",method = RequestMethod.POST)
     @ApiImplicitParams({
@@ -96,4 +111,158 @@ public class PersonDeviceLogController {
 
         return msgResult;
     }
+
+
+    @ApiOperation(value="导出日志台账")
+    @PostMapping("exportXls")
+    @ApiImplicitParams({
+            @ApiImplicitParam(name = "companyId",value = "企业ID", paramType = "query"),
+            @ApiImplicitParam(name = "deviceNo",value = "设备编号", paramType = "query"),
+            @ApiImplicitParam(name = "aliasName",value = "设备别名", paramType = "query"),
+            @ApiImplicitParam(name = "personName",value = "人员姓名", paramType = "query"),
+            @ApiImplicitParam(name = "matchStatus",value = "开门类型", paramType = "query"),
+            @ApiImplicitParam(name = "timeRanges",value = "时间范围", paramType = "query")
+    })
+    public MessageResult<String> exportXls(
+                                String companyId,
+                                String deviceNo,String aliasName,String personName,
+                                String matchStatus,String timeRanges,
+                                @RequestAttribute String subject) {
+        MessageResult<String> msgResult = new MessageResult<>();
+
+        try {
+            HSSFWorkbook workbook = new HSSFWorkbook();
+            HSSFSheet sheet = workbook.createSheet();
+            //表头
+            HSSFRow rowTitle = sheet.createRow(0);
+
+            HSSFCell cellTitle1 = rowTitle.createCell(0);
+            cellTitle1.setCellValue("序号");
+            HSSFCell cellTitle2 = rowTitle.createCell(1);
+            cellTitle2.setCellValue("检测时间");
+            HSSFCell cellTitle3 = rowTitle.createCell(2);
+            cellTitle3.setCellValue("人员姓名");
+            HSSFCell cellTitle4 = rowTitle.createCell(3);
+            cellTitle4.setCellValue("测温度数");
+            HSSFCell cellTitle5 = rowTitle.createCell(4);
+            cellTitle5.setCellValue("设备编号");
+            HSSFCell cellTitle6 = rowTitle.createCell(5);
+            cellTitle6.setCellValue("设备别称");
+            HSSFCell cellTitle7 = rowTitle.createCell(6);
+            cellTitle7.setCellValue("拍照图片");
+
+
+            //表内容
+            Map<String,Object> searchParams = new HashMap<>();
+
+            List<Sort> sortList = new ArrayList<>();
+            sortList.add(new Sort("a.record_time","desc"));
+
+            if(StringUtil.isNotEmpty(companyId)){
+                searchParams.put("companyId",companyId);
+            }
+            if(StringUtil.isNotEmpty(deviceNo)){
+                searchParams.put("deviceNo","%"+deviceNo+"%");
+            }
+            if(StringUtil.isNotEmpty(aliasName)){
+                searchParams.put("aliasName","%"+aliasName+"%");
+            }
+            if(StringUtil.isNotEmpty(personName)){
+                searchParams.put("personName","%"+personName+"%");
+            }
+            if(StringUtil.isNotEmpty(matchStatus)){
+                searchParams.put("matchStatus",matchStatus);
+            }
+
+            if(StringUtil.isNotEmpty(timeRanges)){
+                String[] timeRangeArray = timeRanges.split(",");
+                String beginTime = "";
+                String endTime = "";
+                if(timeRangeArray.length==1){
+                    beginTime = timeRangeArray[0];
+                }
+                else if(timeRangeArray.length==2){
+                    beginTime = timeRangeArray[0];
+                    endTime = timeRangeArray[1];
+                }
+
+                searchParams.put("beginTime",beginTime);
+                searchParams.put("endTime",endTime);
+            }
+
+            Page<PersonDeviceLog> page = personDeviceLogService.pageSearch(searchParams,1,10000,false,sortList);
+
+            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+
+            for(int i=0; i<page.size(); i++){
+
+                PersonDeviceLog personDeviceLog = page.get(i);
+
+                HSSFRow rowContent = sheet.createRow(i+1);
+
+                HSSFCell cellContent1 = rowContent.createCell(0);
+                cellContent1.setCellValue(i+1);
+
+                String recordTime ="";
+                if(personDeviceLog.getRecordTime()!=null){
+                    recordTime = sdf.format(personDeviceLog.getRecordTime());
+                }
+
+                HSSFCell cellContent2 = rowContent.createCell(1);
+                cellContent2.setCellValue(recordTime);
+
+                String name = "";
+                if(personDeviceLog.getPerson()!=null){
+                    name = personDeviceLog.getPerson().getName();
+                }
+                else{
+                    name = "匿名用户";
+                }
+                HSSFCell cellContent3 = rowContent.createCell(2);
+                cellContent3.setCellValue(name);
+                HSSFCell cellContent4 = rowContent.createCell(3);
+                cellContent4.setCellValue(personDeviceLog.getTemperature().toString());
+                HSSFCell cellContent5 = rowContent.createCell(4);
+                cellContent5.setCellValue(personDeviceLog.getDeviceNo());
+                HSSFCell cellContent6 = rowContent.createCell(5);
+                cellContent6.setCellValue(personDeviceLog.getDevice().getAliasName());
+                HSSFCell cellContent7 = rowContent.createCell(6);
+                cellContent7.setCellValue(personDeviceLog.getFaceImage());
+
+                sheet.autoSizeColumn(i);
+            }
+
+            //todo 将wb保存到oss
+            ByteArrayOutputStream output = new ByteArrayOutputStream();
+            workbook.write(output);
+
+            byte[] buffer = output.toByteArray();
+            ByteArrayInputStream input = new ByteArrayInputStream(buffer);
+
+            String downloadUrl = OSSUtil.upload(ossConfig,"import","error.xls",input);
+
+            //todo 返回导入失败报表下载链接
+            msgResult.setData(downloadUrl);
+
+            //将exal输出到哪个文件夹中
+//            ByteArrayOutputStream out = new ByteArrayOutputStream();
+//            workbook.write(out);
+//            workbook.close();
+//
+//            String fileName = new String("导出数据".getBytes("UTF-8"), "iso-8859-1");
+//            response.setContentType("application/x-msdownload");
+//            response.setHeader("Content-Disposition", "attachment;fileName=" + fileName + ".xls");
+//            response.getOutputStream().write(out.toByteArray());
+//            response.getOutputStream().flush();
+
+        }
+        catch(Exception ex){
+            logger.error(ex.getMessage(),ex);
+
+            msgResult.setResult(false);
+            msgResult.setMessage(ex.getMessage());
+        }
+
+        return msgResult;
+    }
 }