DqInterveneController.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package com.jiayue.ipp.idp.controller;
  2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  3. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  4. import com.jiayue.ipfcst.common.core.util.DateMomentUtil;
  5. import com.jiayue.ipp.common.data.entity.DqIntervene;
  6. import com.jiayue.ipp.common.data.entity.ForecastPowerShortTerm;
  7. import com.jiayue.ipp.common.data.entity.an.ParsingChannel;
  8. import com.jiayue.ipp.common.data.entity.an.ParsingUrl;
  9. import com.jiayue.ipp.idp.dto.DqInterveneDto;
  10. import com.jiayue.ipp.idp.dto.DqTempDto;
  11. import com.jiayue.ipp.idp.dto.ForecastData;
  12. import com.jiayue.ipp.idp.service.DqInterveneService;
  13. import com.jiayue.ipp.idp.service.an.ParsingChannelService;
  14. import com.jiayue.ipp.idp.service.an.ParsingUrlService;
  15. import com.jiayue.ipp.idp.util.FileUtil;
  16. import com.jiayue.ipp.idp.util.R;
  17. import lombok.extern.slf4j.Slf4j;
  18. import org.apache.commons.lang.time.DateFormatUtils;
  19. import org.apache.commons.lang.time.DateUtils;
  20. import org.apache.velocity.VelocityContext;
  21. import org.apache.velocity.app.VelocityEngine;
  22. import org.springframework.beans.factory.annotation.Autowired;
  23. import org.springframework.web.bind.annotation.*;
  24. import java.io.File;
  25. import java.io.FileOutputStream;
  26. import java.io.IOException;
  27. import java.io.StringWriter;
  28. import java.util.*;
  29. import java.util.stream.Collectors;
  30. /**
  31. * 短期干预
  32. */
  33. @RestController
  34. @RequestMapping("/dqInterveneController")
  35. @Slf4j
  36. public class DqInterveneController {
  37. @Autowired
  38. DqInterveneService dqInterveneService;
  39. @Autowired
  40. ParsingChannelService parsingChannelService;
  41. @Autowired
  42. VelocityEngine velocityEngine;
  43. @Autowired
  44. ParsingUrlService parsingUrlService;
  45. /**
  46. * 获取短期干预
  47. */
  48. @GetMapping(value = "/getShort")
  49. public R getPresetsShort(Long startTime, Long endTime,String stationCode) {
  50. List<DqInterveneDto> presetsShortDtoList = dqInterveneService.getShort(startTime, endTime,stationCode);
  51. return R.ok(presetsShortDtoList);
  52. }
  53. /**
  54. * 获取短期干预
  55. */
  56. @PostMapping(value = "/saveIntervene")
  57. public R savePresets(@RequestBody DqTempDto dqTempDto) {
  58. String stationCode = dqTempDto.getStationCode();
  59. List<DqInterveneDto> list = dqTempDto.getList();
  60. // 先删除干预表的数据记录
  61. list.sort(Comparator.comparing(DqInterveneDto::getForecastTime));
  62. // 干预日期
  63. String gyrq = DateFormatUtils.format(list.get(0).getForecastTime(), "yyyyMMdd");
  64. Date startTime = list.get(0).getForecastTime();
  65. Date endTime = list.get(list.size() - 1).getForecastTime();
  66. dqInterveneService.deleteShort(startTime, endTime,stationCode);
  67. // 封装干预实体,保存数据
  68. List<DqIntervene> presetsShortList = new ArrayList<>();
  69. for (DqInterveneDto dqInterveneDto : list) {
  70. if (dqInterveneDto.getPresetsPower() != null) {
  71. DqIntervene dqIntervene = new DqIntervene();
  72. dqIntervene.setForecastTime(dqInterveneDto.getForecastTime());
  73. dqIntervene.setActivePower(dqInterveneDto.getPresetsPower());
  74. dqIntervene.setStationCode(stationCode);
  75. presetsShortList.add(dqIntervene);
  76. }
  77. }
  78. dqInterveneService.saveBatch(presetsShortList);
  79. // 下发短期干预
  80. log.info(stationCode+"下发嘉越DQ数据开始:"+gyrq);
  81. LambdaQueryWrapper<ParsingUrl> urlWrapper = new LambdaQueryWrapper<>();
  82. urlWrapper.eq(ParsingUrl::getUrlStatus, "1");
  83. urlWrapper.eq(ParsingUrl::getStationCode, stationCode);
  84. List<ParsingUrl> parsingUrlList = parsingUrlService.list(urlWrapper);
  85. if (parsingUrlList.size()>0){
  86. // 获取ftp通道
  87. LambdaQueryWrapper<ParsingChannel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
  88. lambdaQueryWrapper.eq(ParsingChannel::getAnChannelType, "E1");
  89. lambdaQueryWrapper.eq(ParsingChannel::getUseStatus, "E1");
  90. lambdaQueryWrapper.eq(ParsingChannel::getId, parsingUrlList.get(0).getCId());
  91. ParsingChannel parsingChannel = parsingChannelService.getOne(lambdaQueryWrapper);
  92. String distPath = "/home"+ File.separator+parsingChannel.getUsername()+File.separator+"download";
  93. File dirFile = new File(distPath);
  94. if (!dirFile.exists()) {
  95. dirFile.mkdirs();
  96. }
  97. VelocityContext velocityContext = new VelocityContext();
  98. List<ForecastData> vList = new ArrayList<>();
  99. for (DqIntervene d1:presetsShortList) {
  100. ForecastData forecastData = new ForecastData();
  101. forecastData.setTime(DateFormatUtils.format(d1.getForecastTime(), "yyyy-MM-dd HH:mm:ss"));
  102. forecastData.setPower(d1.getActivePower().toString());
  103. vList.add(forecastData);
  104. }
  105. velocityContext.put("vList", vList);
  106. // 系统当前日期
  107. velocityContext.put("currentTime", DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss"));
  108. StringWriter writer = new StringWriter();
  109. org.apache.velocity.Template template = this.velocityEngine.getTemplate(FileUtil.getResourceBasePath() + "/templates/send_dq.vm");
  110. template.merge(velocityContext, writer);
  111. String vmFileName = "send_"+stationCode+"_dq_"+gyrq+".WPD";
  112. createSendDqFile(writer,new File(distPath + File.separator+vmFileName));
  113. log.info(stationCode+"下发嘉越DQ数据成功:"+gyrq);
  114. }
  115. return R.ok();
  116. }
  117. /**
  118. * 生成上报文件
  119. *
  120. * @param writer
  121. * @param file
  122. */
  123. protected void createSendDqFile(StringWriter writer, File file) {
  124. FileOutputStream os = null;
  125. try {
  126. os = new FileOutputStream(file);
  127. // 采用UTF-8字符集
  128. os.write(writer.toString().getBytes("UTF-8"));
  129. os.flush();
  130. } catch (IOException e) {
  131. throw new RuntimeException(e);
  132. } finally {
  133. if (os != null) {
  134. try {
  135. os.close();
  136. } catch (IOException e) {
  137. log.error("文件生成关闭流失败", e);
  138. }
  139. }
  140. }
  141. }
  142. }