From 0fc8c9d43de7e74e192300fca708e2b1eb904775 Mon Sep 17 00:00:00 2001 From: tyty Date: Wed, 12 Apr 2023 18:56:19 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=80=BC=E7=8F=AD=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E7=9A=84=E5=AF=BC=E5=87=BA=E7=9B=B8=E5=85=B3=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../duty/controller/ImsDutyRecController.java | 11 +++ .../duty/service/IImsAnalyseExampleService.java | 6 +- .../service/impl/ImsAnalyseExampleServiceImpl.java | 89 ++++++++++++++++++++-- .../duty/service/impl/ImsDutyMainServiceImpl.java | 2 +- .../duty/service/impl/ImsDutyRecServiceImpl.java | 2 + 5 files changed, 101 insertions(+), 9 deletions(-) diff --git a/hzims-service/operational/src/main/java/com/hnac/hzims/operational/duty/controller/ImsDutyRecController.java b/hzims-service/operational/src/main/java/com/hnac/hzims/operational/duty/controller/ImsDutyRecController.java index bc36ed1..ce66e89 100644 --- a/hzims-service/operational/src/main/java/com/hnac/hzims/operational/duty/controller/ImsDutyRecController.java +++ b/hzims-service/operational/src/main/java/com/hnac/hzims/operational/duty/controller/ImsDutyRecController.java @@ -31,6 +31,7 @@ import org.springblade.flow.core.entity.BladeFlow; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; +import javax.servlet.http.HttpServletResponse; import javax.validation.Valid; @@ -268,6 +269,16 @@ public class ImsDutyRecController extends BladeController { public R queryAnalyseExampleData( AnalyzeDataCondition analyzeDataCondition) { return iImsAnalyseExampleService.queryAnalyseExampleData(analyzeDataCondition); } + /** + * 导出分析实例数据 + */ + @PostMapping("/outPutAnalyseExampleData") + @ApiOperationSupport(order = 6) + @ApiOperation(value = "导出分析实例数据", notes = "传入analyseExample") + public void outPutAnalyseExampleData(HttpServletResponse response, @Valid @RequestBody AnalyzeDataCondition analyzeDataCondition) { + iImsAnalyseExampleService.outPutAnalyseExampleData(response, analyzeDataCondition); + } + /** * 查询:分析实例模板 diff --git a/hzims-service/operational/src/main/java/com/hnac/hzims/operational/duty/service/IImsAnalyseExampleService.java b/hzims-service/operational/src/main/java/com/hnac/hzims/operational/duty/service/IImsAnalyseExampleService.java index 830a136..a2c2cc6 100644 --- a/hzims-service/operational/src/main/java/com/hnac/hzims/operational/duty/service/IImsAnalyseExampleService.java +++ b/hzims-service/operational/src/main/java/com/hnac/hzims/operational/duty/service/IImsAnalyseExampleService.java @@ -6,6 +6,8 @@ import org.springblade.core.mp.base.BaseService; import org.springblade.core.tool.api.R; import org.springblade.core.mp.support.Query; +import javax.servlet.http.HttpServletResponse; + /** * 服务类 * @@ -14,8 +16,6 @@ import org.springblade.core.mp.support.Query; public interface IImsAnalyseExampleService extends BaseService { - - R queryAnalyseExampleData(AnalyzeDataCondition analyzeDataCondition); R queryAnalyseExample(AnalyseExample analyseExample,Query query); @@ -23,4 +23,6 @@ public interface IImsAnalyseExampleService extends BaseService { R getAnalyseId(String projectId); R updateAnalyseExample(AnalyseExample analyseExample); + + void outPutAnalyseExampleData(HttpServletResponse response, AnalyzeDataCondition analyzeDataCondition); } diff --git a/hzims-service/operational/src/main/java/com/hnac/hzims/operational/duty/service/impl/ImsAnalyseExampleServiceImpl.java b/hzims-service/operational/src/main/java/com/hnac/hzims/operational/duty/service/impl/ImsAnalyseExampleServiceImpl.java index 02d69f8..46dfbd9 100644 --- a/hzims-service/operational/src/main/java/com/hnac/hzims/operational/duty/service/impl/ImsAnalyseExampleServiceImpl.java +++ b/hzims-service/operational/src/main/java/com/hnac/hzims/operational/duty/service/impl/ImsAnalyseExampleServiceImpl.java @@ -1,16 +1,22 @@ package com.hnac.hzims.operational.duty.service.impl; +import com.alibaba.druid.support.json.JSONUtils; +import com.alibaba.excel.EasyExcel; +import com.alibaba.excel.ExcelWriter; +import com.alibaba.excel.converters.longconverter.LongStringConverter; +import com.alibaba.excel.write.metadata.WriteSheet; +import com.alibaba.excel.write.style.column.SimpleColumnWidthStyleStrategy; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.hnac.hzims.operational.duty.entity.AnalyseExample; import com.hnac.hzims.operational.duty.mapper.ImsAnalyseExampleMapper; -import com.hnac.hzims.operational.duty.mapper.ImsDutyMainMapper; import com.hnac.hzims.operational.duty.service.IImsAnalyseExampleService; import com.hnac.hzims.operational.station.entity.StationEntity; import com.hnac.hzims.operational.station.feign.StationClient; import com.hnac.hzinfo.datasearch.analyse.IAnalyseDataSearchClient; import com.hnac.hzinfo.datasearch.analyse.domain.AnalyzeDataCondition; import com.hnac.hzinfo.datasearch.analyse.domain.AnalyzeDataList; +import com.hnac.hzinfo.datasearch.analyse.domain.FieldsData; import com.hnac.hzinfo.sdk.core.response.HzPage; import com.hnac.hzinfo.sdk.core.response.Result; import com.hnac.hzinfo.sdk.v5.analyse.AnalyseDataHandlerClient; @@ -18,16 +24,20 @@ import com.hnac.hzinfo.sdk.v5.analyse.vo.AnalseInstanceVO; import lombok.extern.slf4j.Slf4j; import net.logstash.logback.encoder.org.apache.commons.lang3.ObjectUtils; import net.logstash.logback.encoder.org.apache.commons.lang3.StringUtils; +import org.springblade.core.log.exception.ServiceException; import org.springblade.core.mp.base.BaseServiceImpl; import org.springblade.core.mp.support.Condition; import org.springblade.core.mp.support.Query; import org.springblade.core.tool.api.R; +import org.springblade.core.tool.utils.DateUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import javax.annotation.Resource; -import java.util.ArrayList; -import java.util.List; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.net.URLEncoder; +import java.util.*; +import java.util.stream.Collectors; /** @@ -38,8 +48,6 @@ import java.util.List; @Slf4j @Service public class ImsAnalyseExampleServiceImpl extends BaseServiceImpl implements IImsAnalyseExampleService { - @Resource - private ImsDutyMainMapper imsDutyMainMapper; @Autowired private IAnalyseDataSearchClient searchClient; @Autowired @@ -119,6 +127,75 @@ public class ImsAnalyseExampleServiceImpl extends BaseServiceImpl page = (HzPage) analyseExampleData.getData(); + response.setContentType("application/vnd.ms-excel"); + response.setCharacterEncoding("UTF-8"); + // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系 + String fileName = URLEncoder.encode( name, "UTF-8"); + response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx"); + if(page.getRecords()!=null){ + List> head=new ArrayList<>(); + List title = new ArrayList<>(Arrays.asList("时间")); + head.add(title); + List records = page.getRecords(); + AnalyzeDataList analyzeDataList = records.get(0); + //把时间加属性名称作为表头 + List stringList=analyzeDataList.getFields().stream().map(FieldsData::getFieldName).collect(Collectors.toList()); + for (String s : stringList) { + head.add(Arrays.asList(s)); + } + //把数据填充 + List> data=new ArrayList<>(); + for (int i = 0; i < records.size(); i++) { + List arrayData=new ArrayList<>(); + String date = DateUtil.format(records.get(i).getTs(), DateUtil.PATTERN_DATETIME); + arrayData.add(date); + List dataBefore = records.get(i).getFields().stream().map(FieldsData::getValue).collect(Collectors.toList()); + arrayData.addAll(dataBefore); + data.add(arrayData); + } + System.out.println("---------------- 数据详情 = " + records.toString()); + //ExcelWriter初始化 + ExcelWriter excelWriter = EasyExcel + .write(response.getOutputStream()) + .autoCloseStream(Boolean.TRUE) + .registerConverter(new LongStringConverter()) + .registerWriteHandler(new SimpleColumnWidthStyleStrategy(45)) + .build(); + WriteSheet writeSheet = EasyExcel.writerSheet(1, "值班日志").head(head).build(); + excelWriter.write(data, writeSheet); + // 这里需要设置关闭流 + excelWriter.finish(); + + + } + } catch (Exception e) { + System.out.println("exportExcel = " + e.toString()); + // 重置response + response.reset(); + response.setContentType("application/json"); + response.setCharacterEncoding("utf-8"); + Map map = new HashMap<>(); + map.put("success", "false"); + map.put("msg", "值班日志信息导出失败" + e.getMessage()); + try { + response.getWriter().println(JSONUtils.toJSONString(map)); + } catch (IOException e3) { + throw new ServiceException( "发生异常 :" + e.toString()); + } + } + + } + private QueryWrapper getQueryWrapper(AnalyseExample analyseExample) { QueryWrapper queryWrapper = new QueryWrapper(); diff --git a/hzims-service/operational/src/main/java/com/hnac/hzims/operational/duty/service/impl/ImsDutyMainServiceImpl.java b/hzims-service/operational/src/main/java/com/hnac/hzims/operational/duty/service/impl/ImsDutyMainServiceImpl.java index e90ebfe..8bb0812 100644 --- a/hzims-service/operational/src/main/java/com/hnac/hzims/operational/duty/service/impl/ImsDutyMainServiceImpl.java +++ b/hzims-service/operational/src/main/java/com/hnac/hzims/operational/duty/service/impl/ImsDutyMainServiceImpl.java @@ -176,7 +176,7 @@ public class ImsDutyMainServiceImpl extends BaseServiceImpl() {{ - eq(ImsDutyMainPersonEntity::getDutyMainId, id); + eq(ImsDutyMainPersonEntity::getDutyMainId, imsSchedulingVo.getId()); last("limit 1"); }}); imsSchedulingVo.setManagerId(imsDutyMainPersonEntity.getDutyChargePerson()); diff --git a/hzims-service/operational/src/main/java/com/hnac/hzims/operational/duty/service/impl/ImsDutyRecServiceImpl.java b/hzims-service/operational/src/main/java/com/hnac/hzims/operational/duty/service/impl/ImsDutyRecServiceImpl.java index 3bafe24..fb42004 100644 --- a/hzims-service/operational/src/main/java/com/hnac/hzims/operational/duty/service/impl/ImsDutyRecServiceImpl.java +++ b/hzims-service/operational/src/main/java/com/hnac/hzims/operational/duty/service/impl/ImsDutyRecServiceImpl.java @@ -338,6 +338,8 @@ public class ImsDutyRecServiceImpl extends BaseServiceImpl() {{ eq(ImsDutyMainPersonEntity::getDutyMainId, entity.getId()); eq(ImsDutyMainPersonEntity::getDutyChargePerson, AuthUtil.getUserId()); + last(" limit 1"); + }}); if (ObjectUtil.isEmpty(groupEntity)&&ObjectUtil.isEmpty(personEntity)) { return R.success("您不是该班组负责人,不可交班!");