123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- package com.ruoyi.system.service.impl;
- import cn.hutool.json.JSONUtil;
- import com.alibaba.excel.EasyExcel;
- import com.alibaba.excel.ExcelWriter;
- import com.alibaba.excel.context.AnalysisContext;
- import com.alibaba.excel.read.listener.ReadListener;
- import com.alibaba.excel.write.metadata.WriteSheet;
- import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
- import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
- import com.ruoyi.common.exception.base.BaseException;
- import com.ruoyi.system.domain.*;
- import com.ruoyi.system.dto.*;
- import com.ruoyi.system.mapper.*;
- import com.ruoyi.system.paramet.BerthingPointQuery;
- import com.ruoyi.system.paramet.FloatPointQuery;
- import com.ruoyi.system.paramet.StopPointQuery;
- import com.ruoyi.system.paramet.TimePointQuery;
- import com.ruoyi.system.service.BerthingService;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.stereotype.Service;
- import org.springframework.web.multipart.MultipartFile;
- import javax.annotation.Resource;
- import javax.servlet.http.HttpServletResponse;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Comparator;
- import java.util.Date;
- import java.util.List;
- /**
- * classname: 断面信息
- * description 服务实现类
- */
- @Service
- @Slf4j
- public class BerthingServiceImpl implements BerthingService {
- @Resource
- private SiteInfoMapper siteInfoMapper;
- @Resource
- private BerthingPointMapper berthingPointMapper;
- @Resource
- private BerthingPointConfigMapper berthingPointConfigMapper;
- @Resource
- private BerthingTimeConfigMapper berthingTimeConfigMapper;
- @Resource
- private BerthingFloatConfigMapper berthingFloatConfigMapper;
- @Override
- public ArrayList<PrintExcelDTO> uploadFile(MultipartFile file) {
- ArrayList<PrintExcelDTO> excelDateList = new ArrayList<>();
- try {
- if (file.isEmpty()) {
- log.error("文件为空");
- throw new BaseException("文件为空");
- }
- EasyExcel.read(file.getInputStream(), PrintExcelDTO.class, new ReadListener<PrintExcelDTO>() {
- @Override
- public void invoke(PrintExcelDTO printExcelDto, AnalysisContext analysisContext) {
- excelDateList.add(printExcelDto);
- }
- @Override
- public void doAfterAllAnalysed(AnalysisContext context) {}
- }).sheet().doRead();
- //排序
- excelDateList.sort(Comparator.comparingDouble(PrintExcelDTO::getX));
- return excelDateList;
- } catch (BaseException e){
- throw new BaseException(e.getMessage());
- } catch (Exception e){
- log.error("上传断面excel错误", e);
- throw new BaseException("上传断面excel错误");
- }
- }
- @Override
- public boolean insertBerthing(BerthingPointDTO berthingPointDTO){
- SiteInfo siteInfo = siteInfoMapper.queryById(berthingPointDTO.getSiteId());
- if (siteInfo == null) {
- throw new BaseException("站点不存在");
- }
- try {
- ArrayList<PrintExcelDTO> positions = berthingPointDTO.getPositions();
- positions.sort(Comparator.comparingDouble(PrintExcelDTO::getX));
- //设置他断面状态为无效
- berthingPointMapper.setBerthingStatus(siteInfo.getSiteId());
- BerthingPoint berthingPoint = new BerthingPoint();
- berthingPoint.setBerthingName(berthingPointDTO.getBerthingName());
- berthingPoint.setPositions(JSONUtil.toJsonStr(positions));
- berthingPoint.setSiteId(siteInfo.getSiteId());
- berthingPoint.setBerthingId(0L);
- berthingPoint.setCreateTime(new Date());
- berthingPoint.setIsDel(0);
- berthingPoint.setId(siteInfo.getId());
- berthingPoint.setStatus(1);
- return berthingPointMapper.insert(berthingPoint) > 0;
- //TODO 配置下发
- } catch (Exception e){
- log.error("新增断面错误", e);
- throw new BaseException("新增断面错误");
- }
- }
- @Override
- public Page<BerthingPoint> queryBerthing(BerthingPointQuery berthingPointQuery) {
- try {
- if (berthingPointQuery.getPage() == null || berthingPointQuery.getSize() == null) {
- berthingPointQuery.setPage(1L);
- berthingPointQuery.setSize(10L);
- }
- Page<SiteInfo> page = new Page<>(berthingPointQuery.getPage(), berthingPointQuery.getSize());
- return berthingPointMapper.queryAllByLimit(berthingPointQuery, page);
- } catch (Exception e) {
- log.error("查询站点错误", e);
- throw new BaseException("查询站点错误");
- }
- }
- @Override
- public boolean downFile(Long siteId, HttpServletResponse response) {
- if (siteId == null) {
- throw new BaseException("站点ID不能为空");
- }
- BerthingPoint berthingPoint = berthingPointMapper.queryBySiteId(siteId);
- if (berthingPoint == null) {
- throw new BaseException("没有断面数据");
- }
- try {
- List<PrintExcelDTO> positions = JSONUtil.toList(berthingPoint.getPositions(), PrintExcelDTO.class);
- response.setContentType("application/octet-stream; charset=utf-8");
- String today = new SimpleDateFormat("yyyyMMdd").format(new Date());
- String fileName = "downFile_".concat(today);
- // 设置导出头信息,文件名放在里面
- response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
- // 设置自适应列宽,注册一个handler: LongestMatchColumnWidthStyleStrategy
- ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).build();
- // 创建sheet并设置好标题
- WriteSheet detailSheet = EasyExcel.writerSheet(0, "断面数据").head(PrintExcelDTO.class).build();
- // 向sheet中写入数据
- excelWriter.write(positions, detailSheet);
- // 数据下载,完成后会自动关闭流
- excelWriter.finish();
- return true;
- } catch (Exception e){
- log.error("下载文件错误", e);
- throw new BaseException("下载文件错误");
- }
- }
- @Override
- public boolean addStopPrint(BerthingPointConfigDTO berthingPointConfigDTO) {
- SiteInfo siteInfo = siteInfoMapper.queryById(berthingPointConfigDTO.getSiteId());
- if (siteInfo == null) {
- throw new BaseException("站点不存在");
- }
- if (berthingPointConfigDTO.getStopId() == null) {
- berthingPointConfigDTO.setStopId(0L);
- }
- if(berthingPointConfigDTO.getPositions().size() != berthingPointConfigDTO.getFactors().size()){
- throw new BaseException("修正系数与停泊点位长度不匹配");
- }
- boolean res = false;
- BerthingPointConfig berthingPointConfig = berthingPointConfigMapper.queryById(berthingPointConfigDTO.getStopId());
- try {
- ArrayList<PrintExcelDTO> positions = berthingPointConfigDTO.getPositions();
- if (berthingPointConfig == null) {
- //新增
- berthingPointConfig = new BerthingPointConfig();
- berthingPointConfig.setStopId(0L);
- berthingPointConfig.setSiteId(siteInfo.getSiteId());
- berthingPointConfig.setBerthingId(0L);
- berthingPointConfig.setPositions(JSONUtil.toJsonStr(positions));
- berthingPointConfig.setPlanid(berthingPointConfigDTO.getPlanid());
- berthingPointConfig.setWlevel(berthingPointConfigDTO.getWlevel());
- berthingPointConfig.setFactors(JSONUtil.toJsonStr(berthingPointConfigDTO.getFactors()));
- berthingPointConfig.setCreateTime(new Date());
- berthingPointConfig.setIsDel(0);
- berthingPointConfig.setStatus(1);
- berthingPointConfig.setId(siteInfo.getId());
- res = berthingPointConfigMapper.insert(berthingPointConfig) > 0;
- } else {
- //更新
- berthingPointConfig.setPositions(JSONUtil.toJsonStr(positions));
- berthingPointConfig.setPlanid(berthingPointConfigDTO.getPlanid());
- berthingPointConfig.setWlevel(berthingPointConfigDTO.getWlevel());
- berthingPointConfig.setFactors(JSONUtil.toJsonStr(berthingPointConfigDTO.getFactors()));
- res = berthingPointConfigMapper.update(berthingPointConfig) > 0;
- }
- } catch (Exception e){
- log.error("新增或修改停泊点错误", e);
- throw new BaseException("新增或修改停泊点错误");
- }
- //TODO 下发配置
- return res;
- }
- @Override
- public Page<BerthingPointConfig> queryStopPrint(StopPointQuery stopPointQuery) {
- try {
- if (stopPointQuery.getPage() == null || stopPointQuery.getSize() == null) {
- stopPointQuery.setPage(1L);
- stopPointQuery.setSize(10L);
- }
- return berthingPointConfigMapper.queryAllByLimit(stopPointQuery, new Page<>(stopPointQuery.getPage(), stopPointQuery.getSize()));
- } catch (Exception e) {
- log.error("查询停泊点错误", e);
- throw new BaseException("查询停泊点错误");
- }
- }
- @Override
- public boolean addTimePrint(BerthingTimeDTO berthingTimeDTO) {
- SiteInfo siteInfo = siteInfoMapper.queryById(berthingTimeDTO.getSiteId());
- if (siteInfo == null) {
- throw new BaseException("站点不存在");
- }
- if (berthingTimeDTO.getTimeId() == null) {
- berthingTimeDTO.setTimeId(0L);
- }
- boolean res = false;
- BerthingTimeConfig berthingTime = berthingTimeConfigMapper.queryById(berthingTimeDTO.getTimeId());
- try {
- ArrayList<String> times = berthingTimeDTO.getTimes();
- if (berthingTime == null) {
- //新增
- berthingTime = new BerthingTimeConfig();
- berthingTime.setTimeId(0L);
- berthingTime.setSiteId(siteInfo.getSiteId());
- berthingTime.setPlanid(berthingTimeDTO.getPlanid());
- berthingTime.setTimes(JSONUtil.toJsonStr(times));
- berthingTime.setWlevel(berthingTimeDTO.getWlevel());
- berthingTime.setCreateTime(new Date());
- berthingTime.setIsDel(0);
- berthingTime.setStatus(1);
- berthingTime.setId(siteInfo.getId());
- berthingTime.setType(berthingTimeDTO.getType());
- res = berthingTimeConfigMapper.insert(berthingTime) > 0;
- } else {
- //更新
- berthingTime.setPlanid(berthingTimeDTO.getPlanid());
- berthingTime.setWlevel(berthingTimeDTO.getWlevel());
- berthingTime.setTimes(JSONUtil.toJsonStr(times));
- berthingTime.setType(berthingTimeDTO.getType());
- res = berthingTimeConfigMapper.update(berthingTime) > 0;
- }
- } catch (Exception e){
- log.error("新增或修改时间策略错误", e);
- throw new BaseException("新增或修改时间策略错误");
- }
- //TODO 下发配置
- return res;
- }
- @Override
- public Page<BerthingTimeConfig> queryTimePrint(TimePointQuery timePointQuery) {
- try {
- if (timePointQuery.getPage() == null || timePointQuery.getSize() == null) {
- timePointQuery.setPage(1L);
- timePointQuery.setSize(10L);
- }
- return berthingTimeConfigMapper.queryAllByLimit(timePointQuery, new Page<>(timePointQuery.getPage(), timePointQuery.getSize()));
- } catch (Exception e) {
- log.error("查询停泊点错误", e);
- throw new BaseException("查询停泊点错误");
- }
- }
- @Override
- public boolean addFloatPrint(BerthingFloatDTO berthingFloatDTO) {
- SiteInfo siteInfo = siteInfoMapper.queryById(berthingFloatDTO.getSiteId());
- if (siteInfo == null) {
- throw new BaseException("站点不存在");
- }
- if (berthingFloatDTO.getFloatId() == null) {
- berthingFloatDTO.setFloatId(0L);
- }
- boolean res = false;
- BerthingFloatConfig berthingFloat = berthingFloatConfigMapper.queryById(berthingFloatDTO.getFloatId());
- try {
- if (berthingFloat == null) {
- //新增
- berthingFloat = new BerthingFloatConfig();
- berthingFloat.setFloatId(0L);
- berthingFloat.setSiteId(siteInfo.getSiteId());
- berthingFloat.setPlanid(berthingFloatDTO.getPlanid());
- berthingFloat.setWlevel(berthingFloatDTO.getWlevel());
- berthingFloat.setCreateTime(new Date());
- berthingFloat.setIsDel(0);
- berthingFloat.setStatus(1);
- berthingFloat.setId(siteInfo.getId());
- berthingFloat.setTimespan(berthingFloatDTO.getTimespan());
- berthingFloat.setWlevelchange(berthingFloatDTO.getWlevelchange());
- res = berthingFloatConfigMapper.insert(berthingFloat) > 0;
- } else {
- //更新
- berthingFloat.setPlanid(berthingFloatDTO.getPlanid());
- berthingFloat.setWlevel(berthingFloatDTO.getWlevel());
- berthingFloat.setTimespan(berthingFloatDTO.getTimespan());
- berthingFloat.setWlevelchange(berthingFloatDTO.getWlevelchange());
- res = berthingFloatConfigMapper.update(berthingFloat) > 0;
- }
- } catch (Exception e){
- log.error("新增或修改水位变幅策略错误", e);
- throw new BaseException("新增或修改水位变幅策略错误");
- }
- //TODO 下发配置
- return res;
- }
- @Override
- public Page<BerthingFloatConfig> queryFloatPrint(FloatPointQuery floatPointQuery) {
- try {
- if (floatPointQuery.getPage() == null || floatPointQuery.getSize() == null) {
- floatPointQuery.setPage(1L);
- floatPointQuery.setSize(10L);
- }
- return berthingFloatConfigMapper.queryAllByLimit(floatPointQuery, new Page<>(floatPointQuery.getPage(), floatPointQuery.getSize()));
- } catch (Exception e) {
- log.error("查询水位变幅策略错误", e);
- throw new BaseException("查询水位变幅策略错误");
- }
- }
- }
|