E63UploadFileService.java 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559
  1. package com.jiayue.ipfcst.fileupload.service;
  2. import cn.hutool.core.util.StrUtil;
  3. import com.jiayue.ipfcst.common.core.exception.BusinessException;
  4. import com.jiayue.ipfcst.common.core.util.DateMomentUtil;
  5. import com.jiayue.ipfcst.common.data.abst.equipmentinfo.AbstractEquipmentInfo;
  6. import com.jiayue.ipfcst.common.data.constant.enums.FileTypeEnum;
  7. import com.jiayue.ipfcst.common.data.entity.*;
  8. import com.jiayue.ipfcst.common.data.repository.*;
  9. import com.jiayue.ipfcst.common.data.service.uploadfilerule.E63UploadFileRuleService;
  10. import com.jiayue.ipfcst.console.service.ForecastPowerShortTermService;
  11. import com.jiayue.ipfcst.console.service.ForecastPowerUltraShortTermService;
  12. import com.jiayue.ipfcst.console.util.RedisUtils;
  13. import com.jiayue.ipfcst.fileupload.util.FileUtil;
  14. import lombok.extern.slf4j.Slf4j;
  15. import org.apache.commons.lang.time.DateFormatUtils;
  16. import org.apache.commons.lang.time.DateUtils;
  17. import org.apache.velocity.Template;
  18. import org.apache.velocity.VelocityContext;
  19. import org.apache.velocity.app.VelocityEngine;
  20. import org.springframework.stereotype.Service;
  21. import java.io.File;
  22. import java.io.StringWriter;
  23. import java.math.BigDecimal;
  24. import java.math.RoundingMode;
  25. import java.text.DecimalFormat;
  26. import java.text.SimpleDateFormat;
  27. import java.util.*;
  28. import java.util.stream.Collectors;
  29. /**
  30. * 青海上报文件生成
  31. *
  32. * @author tl
  33. * @version 3.0
  34. */
  35. @Service
  36. @Slf4j
  37. public class E63UploadFileService extends BaseUploadFileService {
  38. private final String vmsPath = FileUtil.getResourceBasePath() + "/vms/E63";
  39. private final VelocityEngine velocityEngine;
  40. private final ForecastPowerShortTermService forecastPowerShortTermService;
  41. private final ForecastPowerUltraShortTermService forecastPowerUltraShortTermService;
  42. private final E63UploadFileRuleService e63UploadFileRuleService;
  43. private final WeatherStationInfoRepository weatherStationInfoRepository;
  44. private final WindTurbineInfoRepository windTurbineInfoRepository;
  45. private final WeatherStationStatusDataRepository weatherStationStatusDataRepository;
  46. private final WindTowerInfoRepository windTowerInfoRepository;
  47. private final WindTowerStatusDataRepository windTowerStatusDataRepository;
  48. private final NwpRepository nwpRepository;
  49. private final InverterInfoRepository inverterInfoRepository;
  50. private final WindTurbineStatusDataRepository windTurbineStatusDataRepository;
  51. private final InverterStatusDataRepository inverterStatusDataRepository;
  52. private final PowerStationStatusDataRepository powerStationStatusDataRepository;
  53. private final RedisUtils redisUtils;
  54. public E63UploadFileService(VelocityEngine velocityEngine, ForecastPowerShortTermService forecastPowerShortTermService, ForecastPowerUltraShortTermService forecastPowerUltraShortTermService, E63UploadFileRuleService e63UploadFileRuleService, WeatherStationInfoRepository weatherStationInfoRepository, WindTurbineInfoRepository windTurbineInfoRepository, WeatherStationStatusDataRepository weatherStationStatusDataRepository, WindTowerInfoRepository windTowerInfoRepository, WindTowerStatusDataRepository windTowerStatusDataRepository, NwpRepository nwpRepository, InverterInfoRepository inverterInfoRepository, WindTurbineStatusDataRepository windTurbineStatusDataRepository, InverterStatusDataRepository inverterStatusDataRepository, PowerStationStatusDataRepository powerStationStatusDataRepository, RedisUtils redisUtils) {
  55. this.velocityEngine = velocityEngine;
  56. this.forecastPowerShortTermService = forecastPowerShortTermService;
  57. this.forecastPowerUltraShortTermService = forecastPowerUltraShortTermService;
  58. this.e63UploadFileRuleService = e63UploadFileRuleService;
  59. this.weatherStationInfoRepository = weatherStationInfoRepository;
  60. this.windTurbineInfoRepository = windTurbineInfoRepository;
  61. this.weatherStationStatusDataRepository = weatherStationStatusDataRepository;
  62. this.windTowerInfoRepository = windTowerInfoRepository;
  63. this.windTowerStatusDataRepository = windTowerStatusDataRepository;
  64. this.nwpRepository = nwpRepository;
  65. this.inverterInfoRepository = inverterInfoRepository;
  66. this.windTurbineStatusDataRepository = windTurbineStatusDataRepository;
  67. this.inverterStatusDataRepository = inverterStatusDataRepository;
  68. this.powerStationStatusDataRepository = powerStationStatusDataRepository;
  69. this.redisUtils = redisUtils;
  70. }
  71. /**
  72. * 生成短期预测上报文件。
  73. */
  74. public void generateDqFile(Date date) {
  75. List<ElectricField> electricFieldList = new ArrayList<>();
  76. try {
  77. electricFieldList = super.getMultipleStation();
  78. } catch (BusinessException e) {
  79. log.error("场站获取失败", e);
  80. }
  81. // 循环场站生成短期
  82. for (ElectricField electricFieldInfo : electricFieldList) {
  83. String stationCode = electricFieldInfo.getStationCode();
  84. try {
  85. Template template;
  86. // 获取短期模板
  87. if (electricFieldInfo.getElectricFieldTypeEnum().equals("E1")) {
  88. template = this.velocityEngine.getTemplate(this.vmsPath + "/DQ.vm");
  89. } else {
  90. template = this.velocityEngine.getTemplate(this.vmsPath + "/DQ_F.vm");
  91. }
  92. if (template != null) {
  93. VelocityContext velocityContext;
  94. StringWriter writer;
  95. String fileName;
  96. File file;
  97. // 获取当前系统时间
  98. Date systemDate = new Date();
  99. if (date != null) {
  100. systemDate = date;
  101. }
  102. // 短期默认天数
  103. int dqDays = super.getTranSysParameter("FILE_FORECAST_DAYS", "3", stationCode);
  104. // 开始时间
  105. Long startTime = DateMomentUtil.getDayStartTime(DateUtils.addDays(systemDate, 1).getTime());
  106. // 结束时间(开始加24小时再减去1秒)
  107. Long endTime = DateMomentUtil.getDayLastTime(DateUtils.addDays(systemDate, dqDays).getTime());
  108. // 生成上报文件名格式
  109. fileName = e63UploadFileRuleService.getFileNameRule(electricFieldInfo, "E1", startTime);
  110. if (getFileName(fileName, "E1", stationCode)) {
  111. // 查询短期预测功率
  112. List<ForecastPowerShortTermHis> forecastPowerShortTermHisList = this.forecastPowerShortTermService.getForecastPowerShortTerm(startTime, endTime, stationCode);
  113. // 按照预测时间进行升序
  114. forecastPowerShortTermHisList.sort(Comparator.comparing(ForecastPowerShortTermHis::getForecastTime));
  115. // 创建上报文件
  116. file = super.createTempFile(fileName);
  117. // 根据模板生成文件内容
  118. velocityContext = new VelocityContext();
  119. velocityContext.put("vList", forecastPowerShortTermHisList);
  120. //场站标识
  121. velocityContext.put("sign", electricFieldInfo.getSign());
  122. //场站装机容量
  123. velocityContext.put("capacity", electricFieldInfo.getCapacity());
  124. //系统当前日期
  125. if (electricFieldInfo.getElectricFieldTypeEnum().equals("E1")) {
  126. velocityContext.put("currentTime", DateFormatUtils.format(systemDate, "yyyy-MM-dd_" + "00:00:00"));
  127. } else {
  128. velocityContext.put("currentTime", DateFormatUtils.format(systemDate, "yyyy-MM-dd " + "00:00"));
  129. }
  130. //上报数据开始日期
  131. velocityContext.put("uploadTime", DateFormatUtils.format(startTime, "yyyy-MM-dd_") + "00:00");
  132. writer = new StringWriter();
  133. template.merge(velocityContext, writer);
  134. // 将文件复制到上报路径中
  135. super.copyUploadFile(writer, file, FileTypeEnum.E1.name(), null, date, stationCode);
  136. }
  137. } else {
  138. log.error(stationCode + "短期上报模板不存在!");
  139. }
  140. } catch (Exception e) {
  141. log.error(stationCode + "生成短期异常", e);
  142. }
  143. }
  144. }
  145. /**
  146. * 生成短期可用预测上报文件。
  147. */
  148. public void generateDqUseFile(Date date) {
  149. List<ElectricField> electricFieldList = new ArrayList<>();
  150. try {
  151. electricFieldList = super.getMultipleStation();
  152. } catch (BusinessException e) {
  153. log.error("场站获取失败", e);
  154. }
  155. // 循环场站生成短期
  156. for (ElectricField electricFieldInfo : electricFieldList) {
  157. String stationCode = electricFieldInfo.getStationCode();
  158. try {
  159. Template template;
  160. // 获取短期模板
  161. if (electricFieldInfo.getElectricFieldTypeEnum().equals("E1")) {
  162. template = this.velocityEngine.getTemplate(this.vmsPath + "/DQ_USE.vm");
  163. } else {
  164. template = this.velocityEngine.getTemplate(this.vmsPath + "/DQ_USE_F.vm");
  165. }
  166. if (template != null) {
  167. VelocityContext velocityContext;
  168. StringWriter writer;
  169. String fileName;
  170. File file;
  171. // 获取当前系统时间
  172. Date systemDate = new Date();
  173. if (date != null) {
  174. systemDate = date;
  175. }
  176. // 短期默认天数
  177. int dqDays = super.getTranSysParameter("FILE_FORECAST_DAYS", "3", stationCode);
  178. // 开始时间
  179. Long startTime = DateMomentUtil.getDayStartTime(DateUtils.addDays(systemDate, 1).getTime());
  180. // 结束时间(开始加24小时再减去1秒)
  181. Long endTime = DateMomentUtil.getDayLastTime(DateUtils.addDays(systemDate, dqDays).getTime());
  182. // 生成上报文件名格式
  183. fileName = e63UploadFileRuleService.getFileNameRule(electricFieldInfo, "E26", startTime);
  184. if (getFileName(fileName, "E26", stationCode)) {
  185. // 查询短期预测功率
  186. List<ForecastPowerShortTermHis> forecastPowerShortTermHisList = this.forecastPowerShortTermService.getForecastPowerShortTerm(startTime, endTime, stationCode);
  187. if (forecastPowerShortTermHisList.size() > 0) {
  188. // 按照预测时间进行升序
  189. forecastPowerShortTermHisList.sort(Comparator.comparing(ForecastPowerShortTermHis::getForecastTime));
  190. }
  191. // 创建上报文件
  192. file = super.createTempFile(fileName);
  193. // 根据模板生成文件内容
  194. velocityContext = new VelocityContext();
  195. velocityContext.put("vList", forecastPowerShortTermHisList);
  196. //场站标识
  197. velocityContext.put("sign", electricFieldInfo.getSign());
  198. //系统当前日期
  199. velocityContext.put("currentTime", DateFormatUtils.format(systemDate, "yyyy-MM-dd_" + "00:00:00"));
  200. //上报数据开始日期
  201. velocityContext.put("uploadTime", DateFormatUtils.format(startTime, "yyyy-MM-dd_") + "00:00");
  202. writer = new StringWriter();
  203. template.merge(velocityContext, writer);
  204. // 将文件复制到上报路径中
  205. super.copyUploadFile(writer, file, FileTypeEnum.E26.name(), null, date, stationCode);
  206. }
  207. } else {
  208. log.error(stationCode + "可用短期上报模板不存在!");
  209. }
  210. } catch (Exception e) {
  211. log.error(stationCode + "生成可用短期异常", e);
  212. }
  213. }
  214. }
  215. /**
  216. * 生成超短期预测上报文件。
  217. */
  218. public void generateCdqFile(Date date) {
  219. List<ElectricField> electricFieldList = new ArrayList<>();
  220. try {
  221. electricFieldList = super.getMultipleStation();
  222. } catch (BusinessException e) {
  223. log.error("场站获取失败", e);
  224. }
  225. // 循环场站生成短期
  226. for (ElectricField electricFieldInfo : electricFieldList) {
  227. String stationCode = electricFieldInfo.getStationCode();
  228. try {
  229. Template template;
  230. // 获取短期模板
  231. if (electricFieldInfo.getElectricFieldTypeEnum().equals("E1")) {
  232. template = this.velocityEngine.getTemplate(this.vmsPath + "/CDQ.vm");
  233. } else {
  234. template = this.velocityEngine.getTemplate(this.vmsPath + "/CDQ_F.vm");
  235. }
  236. if (template != null) {
  237. VelocityContext velocityContext;
  238. StringWriter writer;
  239. String fileName;
  240. File file;
  241. // 获取当前系统时间
  242. Date systemDate = new Date();
  243. if (date != null) {
  244. systemDate = date;
  245. }
  246. // 短期默认天数
  247. int cdqPoint = super.getTranSysParameter("FILE_CDQ_POINT", "16", stationCode);
  248. // 获取提前几分钟生成超短期
  249. int cdqUpMin = super.getTranSysParameter("CDQ_UP_MIN", "0", stationCode);
  250. // 开始时间
  251. long startTime = DateMomentUtil.getMomentTime(systemDate.getTime() + 15 * 60 * 1000 + (long) cdqUpMin * 1000 * 60, 1, 15 * 60 * 1000L);
  252. // 结束时间
  253. long endTime = startTime + (long) cdqPoint * 15 * 60 * 1000 - 1000;
  254. // 生成上报文件名格式
  255. fileName = e63UploadFileRuleService.getFileNameRule(electricFieldInfo, "E2", startTime);
  256. if (getFileName(fileName, "E2", stationCode)) {
  257. // 查询超短期预测功率
  258. List<ForecastPowerUltraShortTermHis> forecastPowerUltraShortTermHisList = this.forecastPowerUltraShortTermService.getForecastPowerUltraShortTerm(startTime, endTime, stationCode);
  259. // 按照预测时间进行升序
  260. forecastPowerUltraShortTermHisList.sort(Comparator.comparing(ForecastPowerUltraShortTermHis::getForecastTime));
  261. // 创建上报文件
  262. file = super.createTempFile(fileName);
  263. // 根据模板生成文件内容
  264. velocityContext = new VelocityContext();
  265. velocityContext.put("vList", forecastPowerUltraShortTermHisList);
  266. // 场站标识
  267. velocityContext.put("sign", electricFieldInfo.getSign());
  268. // 场站装机容量
  269. velocityContext.put("capacity", electricFieldInfo.getCapacity());
  270. // 系统当前日期
  271. if (electricFieldInfo.getElectricFieldTypeEnum().equals("E1")) {
  272. velocityContext.put("currentTime", DateFormatUtils.format(systemDate, "yyyy-MM-dd_HH:mm" + ":00"));
  273. } else {
  274. velocityContext.put("currentTime", DateFormatUtils.format(systemDate, "yyyy-MM-dd HH:mm" + ":00"));
  275. }
  276. // 上报数据开始日期
  277. velocityContext.put("uploadTime", DateFormatUtils.format(startTime, "yyyy-MM-dd_HH:mm"));
  278. writer = new StringWriter();
  279. template.merge(velocityContext, writer);
  280. super.copyUploadFile(writer, file, FileTypeEnum.E2.name(), null, date, stationCode);
  281. }
  282. } else {
  283. log.error(stationCode + "超短期上报模板不存在!");
  284. }
  285. } catch (Exception e) {
  286. log.error(stationCode + "生成超短期异常", e);
  287. }
  288. }
  289. }
  290. /**
  291. * 生成超短期可用预测上报文件。
  292. */
  293. public void generateCdqUseFile(Date date) {
  294. List<ElectricField> electricFieldList = new ArrayList<>();
  295. try {
  296. electricFieldList = super.getMultipleStation();
  297. } catch (BusinessException e) {
  298. log.error("场站获取失败", e);
  299. }
  300. // 循环场站生成短期
  301. for (ElectricField electricFieldInfo : electricFieldList) {
  302. String stationCode = electricFieldInfo.getStationCode();
  303. try {
  304. Template template;
  305. // 获取短期模板
  306. if (electricFieldInfo.getElectricFieldTypeEnum().equals("E1")) {
  307. template = this.velocityEngine.getTemplate(this.vmsPath + "/CDQ_USE.vm");
  308. } else {
  309. template = this.velocityEngine.getTemplate(this.vmsPath + "/CDQ_USE_F.vm");
  310. }
  311. if (template != null) {
  312. VelocityContext velocityContext;
  313. StringWriter writer;
  314. String fileName;
  315. File file;
  316. // 获取当前系统时间
  317. Date systemDate = new Date();
  318. if (date != null) {
  319. systemDate = date;
  320. }
  321. // 短期默认天数
  322. int cdqPoint = super.getTranSysParameter("FILE_CDQ_POINT", "16", stationCode);
  323. // 获取提前几分钟生成超短期
  324. int cdqUpMin = super.getTranSysParameter("CDQ_UP_MIN", "0", stationCode);
  325. // 开始时间
  326. long startTime = DateMomentUtil.getMomentTime(systemDate.getTime() + 15 * 60 * 1000 + (long) cdqUpMin * 1000 * 60, 1, 15 * 60 * 1000L);
  327. // 结束时间
  328. long endTime = startTime + (long) cdqPoint * 15 * 60 * 1000 - 1000;
  329. // 生成上报文件名格式
  330. fileName = e63UploadFileRuleService.getFileNameRule(electricFieldInfo, "E27", startTime);
  331. if (getFileName(fileName, "E27", stationCode)) {
  332. List<ForecastPowerUltraShortTermHis> forecastPowerUltraShortTermHisList = this.forecastPowerUltraShortTermService.getForecastPowerUltraShortTerm(startTime, endTime, stationCode);
  333. // 按照预测时间进行升序
  334. if (forecastPowerUltraShortTermHisList.size() > 0) {
  335. forecastPowerUltraShortTermHisList.sort(Comparator.comparing(ForecastPowerUltraShortTermHis::getForecastTime));
  336. }
  337. // 创建上报文件
  338. file = super.createTempFile(fileName);
  339. // 根据模板生成文件内容
  340. velocityContext = new VelocityContext();
  341. velocityContext.put("vList", forecastPowerUltraShortTermHisList);
  342. // 场站标识
  343. velocityContext.put("sign", electricFieldInfo.getSign());
  344. // 场站装机容量
  345. velocityContext.put("capacity", electricFieldInfo.getCapacity());
  346. // 系统当前日期
  347. if (electricFieldInfo.getElectricFieldTypeEnum().equals("E1")) {
  348. velocityContext.put("currentTime", DateFormatUtils.format(systemDate, "yyyy-MM-dd_HH:mm" + ":00"));
  349. } else {
  350. velocityContext.put("currentTime", DateFormatUtils.format(systemDate, "yyyy-MM-dd HH:mm" + ":00"));
  351. }
  352. // 上报数据开始日期
  353. velocityContext.put("uploadTime", DateFormatUtils.format(startTime - 15 * 60 * 1000, "yyyy-MM-dd_HH:mm"));
  354. writer = new StringWriter();
  355. template.merge(velocityContext, writer);
  356. super.copyUploadFile(writer, file, FileTypeEnum.E27.name(), null, date, stationCode);
  357. }
  358. } else {
  359. log.error(stationCode + "可用超短期上报模板不存在!");
  360. }
  361. } catch (Exception e) {
  362. log.error(stationCode + "生成可用超短期异常", e);
  363. }
  364. }
  365. }
  366. /**
  367. * 生成气象站或者测风塔上报文件
  368. */
  369. public void generateQxzOrCftFile(Date date) {
  370. List<ElectricField> electricFieldList = new ArrayList<>();
  371. try {
  372. electricFieldList = super.getMultipleStation();
  373. } catch (BusinessException e) {
  374. log.error("场站获取失败", e);
  375. }
  376. // 循环场站生成短期
  377. for (ElectricField electricFieldInfo : electricFieldList) {
  378. String stationCode = electricFieldInfo.getStationCode();
  379. try {
  380. String fileName;
  381. // 获取当前系统时间
  382. Date systemDate = new Date();
  383. if (date != null) {
  384. systemDate = date;
  385. }
  386. Long endTime = DateMomentUtil.getMomentTime(systemDate.getTime(), 1, 5 * 60 * 1000L);
  387. Long startTime = endTime - 4 * 60 * 1000;
  388. // 生成上报文件名格式
  389. Template template;
  390. if (electricFieldInfo.getElectricFieldTypeEnum().toString().equals("E1")) {
  391. // 获取气象站模板
  392. template = this.velocityEngine.getTemplate(this.vmsPath + "/QXZ.vm");
  393. fileName = e63UploadFileRuleService.getFileNameRule(electricFieldInfo, "E6", endTime);
  394. if (getFileName(fileName, "E6", stationCode)) {
  395. generateQxzFile(fileName, template, electricFieldInfo, date, startTime, endTime);
  396. }
  397. } else {
  398. template = this.velocityEngine.getTemplate(this.vmsPath + "/CFT.vm");
  399. fileName = e63UploadFileRuleService.getFileNameRule(electricFieldInfo, "E7", endTime);
  400. if (getFileName(fileName, "E7", stationCode)) {
  401. generateCftFile(fileName, template, electricFieldInfo, date, startTime, endTime);
  402. }
  403. }
  404. } catch (Exception e) {
  405. log.error(stationCode + "生成文件异常", e);
  406. }
  407. }
  408. }
  409. /**
  410. * 生成气象站上报文件
  411. */
  412. private void generateQxzFile(String fileName, Template template, ElectricField electricFieldInfo, Date date, Long startTime, Long endTime) {
  413. // 获取气象站信息
  414. List<WeatherStationInfo> weatherStationInfoList = weatherStationInfoRepository.findAll();
  415. // 将气象站信息过滤出上报
  416. List<WeatherStationInfo> weatherStationInfos = weatherStationInfoList.stream().filter(AbstractEquipmentInfo::getReport).collect(Collectors.toList());
  417. if (!weatherStationInfos.isEmpty()) {
  418. String qxzId = String.valueOf(weatherStationInfos.get(0).getId());
  419. Map<String, String> qxzMap = redisUtils.hgetall("qxz-" + electricFieldInfo.getStationCode() + "-" + qxzId);
  420. // 数据格式化
  421. DecimalFormat df = new DecimalFormat("0.00");
  422. BigDecimal globalR = new BigDecimal(0);
  423. BigDecimal directR = new BigDecimal(0);
  424. BigDecimal diffuseR = new BigDecimal(0);
  425. BigDecimal ws = new BigDecimal(0);
  426. BigDecimal wd = new BigDecimal(0);
  427. BigDecimal airT = new BigDecimal(0);
  428. BigDecimal p = new BigDecimal(0);
  429. BigDecimal rh = new BigDecimal(0);
  430. BigDecimal cellT = new BigDecimal(0);
  431. boolean isUseRedis = false;
  432. if (!qxzMap.isEmpty()) {
  433. String time = qxzMap.get("time");
  434. long timestamp = new Date().getTime();
  435. SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
  436. try{
  437. Date d = sf.parse(time);// 日期转换为时间戳
  438. timestamp = d.getTime();
  439. }catch (Exception e){
  440. log.info("日期转换异常");
  441. e.printStackTrace();
  442. }
  443. if (StrUtil.isNotBlank(time)) {
  444. if (timestamp >= startTime && timestamp <= endTime) {
  445. // 从缓存里赋值
  446. globalR = qxzMap.get("globalR") == null ? qxzRandomCreate("globalR") : new BigDecimal(qxzMap.get("globalR"));
  447. directR = qxzMap.get("directR") == null ? qxzRandomCreate("directR") : new BigDecimal(qxzMap.get("directR"));
  448. diffuseR = qxzMap.get("diffuseR") == null ? globalR.subtract(directR) : new BigDecimal(qxzMap.get("diffuseR"));
  449. airT = qxzMap.get("airT") == null ? qxzRandomCreate("airT") : new BigDecimal(qxzMap.get("airT"));
  450. p = qxzMap.get("p") == null ? qxzRandomCreate("p") : new BigDecimal(qxzMap.get("p"));
  451. rh = qxzMap.get("rh") == null ? qxzRandomCreate("rh") : new BigDecimal(qxzMap.get("rh"));
  452. cellT = qxzMap.get("cellT") == null ? qxzRandomCreate("cellT") : new BigDecimal(qxzMap.get("cellT"));
  453. ws = qxzMap.get("ws") == null ? qxzRandomCreate("ws") : new BigDecimal(qxzMap.get("ws"));
  454. wd = qxzMap.get("wd") == null ? qxzRandomCreate("wd") : new BigDecimal(qxzMap.get("wd"));
  455. isUseRedis = true;
  456. log.info(electricFieldInfo.getStationCode() + "生成气象站数据从redis获取:" + qxzMap);
  457. }
  458. }
  459. }
  460. if (!isUseRedis) {
  461. List<WeatherStationStatusData> weatherStationStatusDataList = weatherStationStatusDataRepository.findByStationCodeAndTimeBetweenAndEquipmentId(electricFieldInfo.getStationCode(), new Date(startTime), new Date(endTime), qxzId);
  462. if (weatherStationStatusDataList.isEmpty()) {
  463. // 总辐射产生500-1000随机数
  464. globalR = qxzRandomCreate("globalR");
  465. // 直辐射产生400-500随机数
  466. directR = qxzRandomCreate("directR");
  467. // 散辐射 = 总 - 直
  468. diffuseR = globalR.subtract(directR);
  469. airT = qxzRandomCreate("airT");
  470. p = qxzRandomCreate("p");
  471. rh = qxzRandomCreate("rh");
  472. cellT = qxzRandomCreate("cellT");
  473. ws = qxzRandomCreate("ws");
  474. wd = qxzRandomCreate("wd");
  475. log.info(electricFieldInfo.getStationCode() + "生成气象站数据用随机数");
  476. } else {
  477. weatherStationStatusDataList.stream().sorted(Comparator.comparing(WeatherStationStatusData::getTime).reversed()).collect(Collectors.toList());
  478. WeatherStationStatusData weatherStationStatusData = weatherStationStatusDataList.get(0);
  479. globalR = weatherStationStatusData.getGlobalR();
  480. directR = weatherStationStatusData.getDirectR();
  481. diffuseR = globalR.subtract(directR);
  482. airT = weatherStationStatusData.getAirT();
  483. p = weatherStationStatusData.getP();
  484. rh = weatherStationStatusData.getRh();
  485. cellT = weatherStationStatusData.getCellT();
  486. ws = weatherStationStatusData.getWs();
  487. wd = weatherStationStatusData.getWd();
  488. log.info(electricFieldInfo.getStationCode() + "生成气象站数据从数据表获取");
  489. }
  490. }
  491. // 创建上报文件
  492. File file = super.createTempFile(fileName);
  493. //青海要求 直=总-散;但是当散辐射大于总辐射时直辐射为负数不符合该规则。所以比较总直散三个值,用最大的当总辐射去减去
  494. log.info(electricFieldInfo.getStationCode() + "qxz实际值 总GlobalR:" + globalR + " 直DirectR:" + directR + " 散DiffuseR:" + diffuseR);
  495. if (globalR.compareTo(diffuseR) < 0) {
  496. //当散辐射大于总辐射时,数值互换
  497. BigDecimal a = globalR;
  498. globalR = diffuseR;
  499. diffuseR = a;
  500. }
  501. directR = globalR.subtract(diffuseR);
  502. // 根据模板生成文件内容
  503. VelocityContext velocityContext = new VelocityContext();
  504. velocityContext.put("GlobalR", df.format(globalR));
  505. velocityContext.put("AirT", df.format(airT));
  506. velocityContext.put("P", df.format(p));
  507. velocityContext.put("RH", df.format(rh));
  508. velocityContext.put("CellT", df.format(cellT));
  509. velocityContext.put("DirectR", df.format(directR));
  510. velocityContext.put("DiffuseR", df.format(diffuseR));
  511. velocityContext.put("WS", df.format(ws));
  512. velocityContext.put("WD", df.format(wd));
  513. // 场站标识
  514. velocityContext.put("sign", electricFieldInfo.getSign());
  515. // 场站装机容量
  516. velocityContext.put("capacity", electricFieldInfo.getCapacity());
  517. // 系统当前日期
  518. velocityContext.put("currentTime", DateFormatUtils.format(endTime, "yyyy-MM-dd_HH:mm") + ":00");
  519. // 上报数据开始日期
  520. velocityContext.put("uploadTime", DateFormatUtils.format(endTime, "yyyy-MM-dd_HH:mm"));
  521. StringWriter writer = new StringWriter();
  522. template.merge(velocityContext, writer);
  523. super.copyUploadFile(writer, file, FileTypeEnum.E6.name(), null, date, electricFieldInfo.getStationCode());
  524. }
  525. }
  526. /**
  527. * 生成测风塔上报文件
  528. */
  529. private void generateCftFile(String fileName, Template template, ElectricField electricFieldInfo, Date date, Long startTime, Long endTime) {
  530. // 获取测风塔信息
  531. List<WindTowerInfo> windTowerInfoList = windTowerInfoRepository.findAll();
  532. // 将测风塔信息过滤出上报的塔
  533. List<WindTowerInfo> filterWindTowerInfoList = windTowerInfoList.stream().filter(AbstractEquipmentInfo::getReport).collect(Collectors.toList());
  534. if (!filterWindTowerInfoList.isEmpty()) {
  535. String cftId = String.valueOf(filterWindTowerInfoList.get(0).getId());
  536. Map<String, String> cftMap = redisUtils.hgetall("cft-" + electricFieldInfo.getStationCode() + "-" + cftId);
  537. // 数据格式化
  538. DecimalFormat df = new DecimalFormat("0.00");
  539. BigDecimal ws10 = new BigDecimal(0);
  540. BigDecimal wd10 = new BigDecimal(0);
  541. BigDecimal ws30 = new BigDecimal(0);
  542. BigDecimal wd30 = new BigDecimal(0);
  543. BigDecimal ws50 = new BigDecimal(0);
  544. BigDecimal wd50 = new BigDecimal(0);
  545. BigDecimal ws60 = new BigDecimal(0);
  546. BigDecimal wd60 = new BigDecimal(0);
  547. BigDecimal ws70 = new BigDecimal(0);
  548. BigDecimal wd70 = new BigDecimal(0);
  549. BigDecimal ws80 = new BigDecimal(0);
  550. BigDecimal wd80 = new BigDecimal(0);
  551. BigDecimal ws90 = new BigDecimal(0);
  552. BigDecimal wd90 = new BigDecimal(0);
  553. BigDecimal ws100 = new BigDecimal(0);
  554. BigDecimal wd100 = new BigDecimal(0);
  555. BigDecimal wsHubHeight = new BigDecimal(0);
  556. BigDecimal wdHubHeight = new BigDecimal(0);
  557. BigDecimal t8 = new BigDecimal(0);
  558. BigDecimal p8 = new BigDecimal(0);
  559. BigDecimal rh8 = new BigDecimal(0);
  560. boolean isUseRedis = false;
  561. if (!cftMap.isEmpty()) {
  562. String time = cftMap.get("time");
  563. long timestamp = new Date().getTime();
  564. SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
  565. try{
  566. Date d = sf.parse(time);// 日期转换为时间戳
  567. timestamp = d.getTime();
  568. }catch (Exception e){
  569. log.info("日期转换异常");
  570. e.printStackTrace();
  571. }
  572. if (StrUtil.isNotBlank(time)) {
  573. if (timestamp>= startTime && timestamp <= endTime) {
  574. // 从缓存里赋值
  575. ws10 = cftMap.get("wsInst10") == null ? new BigDecimal("-99") : new BigDecimal(cftMap.get("wsInst10"));
  576. wd10 = cftMap.get("wdInst10") == null ? new BigDecimal("-99") : new BigDecimal(cftMap.get("wdInst10"));
  577. ws30 = cftMap.get("wsInst30") == null ? new BigDecimal("-99") : new BigDecimal(cftMap.get("wsInst30"));
  578. wd30 = cftMap.get("wdInst30") == null ? new BigDecimal("-99") : new BigDecimal(cftMap.get("wdInst30"));
  579. ws50 = cftMap.get("wsInst50") == null ? new BigDecimal("-99") : new BigDecimal(cftMap.get("wsInst50"));
  580. wd50 = cftMap.get("wdInst50") == null ? new BigDecimal("-99") : new BigDecimal(cftMap.get("wdInst50"));
  581. ws60 = cftMap.get("wsInst60") == null ? new BigDecimal("-99") : new BigDecimal(cftMap.get("wsInst60"));
  582. wd60 = cftMap.get("wdInst60") == null ? new BigDecimal("-99") : new BigDecimal(cftMap.get("wdInst60"));
  583. ws70 = cftMap.get("wsInst70") == null ? new BigDecimal("-99") : new BigDecimal(cftMap.get("wsInst70"));
  584. wd70 = cftMap.get("wdInst70") == null ? new BigDecimal("-99") : new BigDecimal(cftMap.get("wdInst70"));
  585. ws80 = cftMap.get("wsInst80") == null ? new BigDecimal("-99") : new BigDecimal(cftMap.get("wsInst80"));
  586. wd80 = cftMap.get("wdInst80") == null ? new BigDecimal("-99") : new BigDecimal(cftMap.get("wdInst80"));
  587. ws90 = cftMap.get("wsInst90") == null ? new BigDecimal("-99") : new BigDecimal(cftMap.get("wsInst90"));
  588. wd90 = cftMap.get("wdInst90") == null ? new BigDecimal("-99") : new BigDecimal(cftMap.get("wdInst90"));
  589. ws100 = cftMap.get("wsInst100") == null ? new BigDecimal("-99") : new BigDecimal(cftMap.get("wsInst100"));
  590. wd100 = cftMap.get("wdInst100") == null ? new BigDecimal("-99") : new BigDecimal(cftMap.get("wdInst100"));
  591. wsHubHeight = cftMap.get("wsInstHubHeight") == null ? cftRandomCreate("wsInstHubHeight") : new BigDecimal(cftMap.get("wsInstHubHeight"));
  592. wdHubHeight = cftMap.get("wdInstHubHeight") == null ? cftRandomCreate("wdInstHubHeight") : new BigDecimal(cftMap.get("wdInstHubHeight"));
  593. t8 = cftMap.get("tInst") == null ? cftRandomCreate("tInst") : new BigDecimal(cftMap.get("tInst"));
  594. p8 = cftMap.get("paInst") == null ? cftRandomCreate("paInst") : new BigDecimal(cftMap.get("paInst"));
  595. rh8 = cftMap.get("rhInst") == null ? cftRandomCreate("rhInst") : new BigDecimal(cftMap.get("rhInst"));
  596. isUseRedis = true;
  597. log.info(electricFieldInfo.getStationCode() + "生成测风塔数据从redis获取:" + cftMap);
  598. }
  599. }
  600. }
  601. if (!isUseRedis) {
  602. List<WindTowerStatusData> windTowerStatusDataList = windTowerStatusDataRepository.findByStationCodeAndEquipmentIdAndTimeBetween(electricFieldInfo.getStationCode(), cftId, new Date(startTime), new Date(endTime));
  603. if (windTowerStatusDataList.isEmpty()) {
  604. // 没有测风塔数据用nwp数据补
  605. long nwpTime;
  606. try {
  607. nwpTime = DateMomentUtil.getMomentTime(endTime, 1, 15 * 60 * 1000L);
  608. } catch (Exception e) {
  609. log.error("获取nwp时刻步长错误", e);
  610. nwpTime = DateMomentUtil.getDayStartTime(new Date().getTime());
  611. }
  612. List<Nwp> nwpList = nwpRepository.findByPreTimeBetween(nwpTime, nwpTime);
  613. if (nwpList.size() > 0) {
  614. Nwp nwpData = nwpList.get(0);
  615. ws10 = nwpData.getWs10();
  616. ws10 = ws10.add(new BigDecimal(df.format(Math.random() * 1 + 0.01))).setScale(2, RoundingMode.HALF_UP);
  617. wd10 = nwpData.getWd10();
  618. wd10 = wd10.add(new BigDecimal(df.format(Math.random() * 1 + 0.01))).setScale(2, RoundingMode.HALF_UP);
  619. ws30 = nwpData.getWs30();
  620. ws30 = ws30.add(new BigDecimal(df.format(Math.random() * 1 + 0.01))).setScale(2, RoundingMode.HALF_UP);
  621. wd30 = nwpData.getWd30();
  622. wd30 = wd30.add(new BigDecimal(df.format(Math.random() * 1 + 0.01))).setScale(2, RoundingMode.HALF_UP);
  623. ws50 = nwpData.getWs50();
  624. ws50 = ws50.add(new BigDecimal(df.format(Math.random() * 1 + 0.01))).setScale(2, RoundingMode.HALF_UP);
  625. wd50 = nwpData.getWd50();
  626. wd50 = wd50.add(new BigDecimal(df.format(Math.random() * 1 + 0.01))).setScale(2, RoundingMode.HALF_UP);
  627. ws60 = ws50.add(new BigDecimal(df.format(Math.random() * 1 + 0.01))).setScale(2, RoundingMode.HALF_UP);
  628. wd60 = wd50.add(new BigDecimal(df.format(Math.random() * 1 + 0.01))).setScale(2, RoundingMode.HALF_UP);
  629. ws70 = nwpData.getWs70();
  630. ws70 = ws70.add(new BigDecimal(df.format(Math.random() * 1 + 0.01))).setScale(2, RoundingMode.HALF_UP);
  631. wd70 = nwpData.getWd70();
  632. wd70 = wd70.add(new BigDecimal(df.format(Math.random() * 1 + 0.01))).setScale(2, RoundingMode.HALF_UP);
  633. ws80 = nwpData.getWs80();
  634. ws80 = ws80.add(new BigDecimal(df.format(Math.random() * 1 + 0.01))).setScale(2, RoundingMode.HALF_UP);
  635. wd80 = nwpData.getWd80();
  636. wd80 = wd80.add(new BigDecimal(df.format(Math.random() * 1 + 0.01))).setScale(2, RoundingMode.HALF_UP);
  637. ws90 = nwpData.getWs90();
  638. ws90 = ws90.add(new BigDecimal(df.format(Math.random() * 1 + 0.01))).setScale(2, RoundingMode.HALF_UP);
  639. wd90 = nwpData.getWd90();
  640. wd90 = wd90.add(new BigDecimal(df.format(Math.random() * 1 + 0.01))).setScale(2, RoundingMode.HALF_UP);
  641. ws100 = nwpData.getWs100();
  642. ws100 = ws100.add(new BigDecimal(df.format(Math.random() * 1 + 0.01))).setScale(2, RoundingMode.HALF_UP);
  643. wd100 = nwpData.getWd100();
  644. wd100 = wd100.add(new BigDecimal(df.format(Math.random() * 1 + 0.01))).setScale(2, RoundingMode.HALF_UP);
  645. wsHubHeight = nwpData.getWs170();
  646. wsHubHeight = wsHubHeight.add(new BigDecimal(df.format(Math.random() * 1 + 0.01))).setScale(2, RoundingMode.HALF_UP);
  647. wdHubHeight = nwpData.getWd170();
  648. wdHubHeight = wdHubHeight.add(new BigDecimal(df.format(Math.random() * 1 + 0.01))).setScale(2, RoundingMode.HALF_UP);
  649. rh8 = nwpData.getRh();
  650. rh8 = rh8.add(new BigDecimal(df.format(Math.random() * 1 + 0.01))).setScale(2, RoundingMode.HALF_UP);
  651. t8 = nwpData.getT();
  652. t8 = t8.add(new BigDecimal(df.format(Math.random() * 1 + 0.01))).setScale(2, RoundingMode.HALF_UP);
  653. p8 = nwpData.getPressure();
  654. p8 = p8.add(new BigDecimal(df.format(Math.random() * 1 + 0.01))).setScale(2, RoundingMode.HALF_UP);
  655. log.info(electricFieldInfo.getStationCode() + "生成测风塔数据用nwp+随机数");
  656. } else {
  657. // 10米风速产生1-5随机数
  658. ws10 = cftRandomCreate("wsInst10");
  659. // 10米风向产生40-60随机数
  660. wd10 = cftRandomCreate("wdInst10");
  661. // 30米风速产生6-10随机数
  662. ws30 = cftRandomCreate("wsInst30");
  663. // 30米风向产生70-90随机数
  664. wd30 = cftRandomCreate("wdInst30");
  665. // 50米风速产生16-20随机数
  666. ws50 = cftRandomCreate("wsInst50");
  667. // 50米风向产生130-150随机数
  668. wd50 = cftRandomCreate("wdInst50");
  669. // 60米风速产生16-20随机数
  670. ws60 = cftRandomCreate("wsInst60");
  671. // 60米风向产生130-150随机数
  672. wd60 = cftRandomCreate("wdInst60");
  673. // 70米风速产生31-35随机数
  674. ws70 = cftRandomCreate("wsInst70");
  675. // 70米风向产生220-240随机数
  676. wd70 = cftRandomCreate("wdInst70");
  677. // 80米风速产生35-40随机数
  678. ws80 = cftRandomCreate("wsInst80");
  679. // 80米风向产生240-250随机数
  680. wd80 = cftRandomCreate("wdInst80");
  681. // 90米风速产生45-50随机数
  682. ws90 = cftRandomCreate("wsInst90");
  683. // 90米风向产生260-270随机数
  684. wd90 = cftRandomCreate("wdInst90");
  685. // 100米风速产生60-70随机数
  686. ws100 = cftRandomCreate("wsInst100");
  687. // 100米风向产生280-290随机数
  688. wd100 = cftRandomCreate("wdInst100");
  689. wsHubHeight = cftRandomCreate("wsInstHubHeight");
  690. wdHubHeight = cftRandomCreate("wdInstHubHeight");
  691. // 湿度产生50-60随机数
  692. rh8 = cftRandomCreate("rhInst");
  693. // 温度产生20-35随机数
  694. t8 = cftRandomCreate("tInst");
  695. // 气压产生75-90随机数
  696. p8 = cftRandomCreate("paInst");
  697. log.info(electricFieldInfo.getStationCode() + "生成测风塔数据用随机数");
  698. }
  699. } else {
  700. windTowerStatusDataList.stream().sorted(Comparator.comparing(WindTowerStatusData::getTime).reversed()).collect(Collectors.toList());
  701. WindTowerStatusData windTowerStatusData = windTowerStatusDataList.get(0);
  702. // 10米风速
  703. if (windTowerStatusData.getWsInst10() != null)
  704. ws10 = windTowerStatusData.getWsInst10();
  705. // 10米风向
  706. if (windTowerStatusData.getWdInst10() != null)
  707. wd10 = windTowerStatusData.getWdInst10();
  708. // 30米风速
  709. if (windTowerStatusData.getWdInst30() != null)
  710. ws30 = windTowerStatusData.getWsInst30();
  711. // 30米风向
  712. if (windTowerStatusData.getWdInst30() != null)
  713. wd30 = windTowerStatusData.getWdInst30();
  714. // 50米风速
  715. if (windTowerStatusData.getWdInst50() != null)
  716. ws50 = windTowerStatusData.getWsInst50();
  717. // 50米风向
  718. if (windTowerStatusData.getWdInst50() != null)
  719. wd50 = windTowerStatusData.getWdInst50();
  720. // 60米风速
  721. if (windTowerStatusData.getWdInst60() != null)
  722. ws60 = windTowerStatusData.getWsInst60();
  723. // 60米风向
  724. if (windTowerStatusData.getWdInst60() != null)
  725. wd60 = windTowerStatusData.getWdInst60();
  726. // 70米风速
  727. if (windTowerStatusData.getWdInst70() != null)
  728. ws70 = windTowerStatusData.getWsInst70();
  729. // 70米风向
  730. if (windTowerStatusData.getWdInst70() != null)
  731. wd70 = windTowerStatusData.getWdInst70();
  732. // 80米风速
  733. if (windTowerStatusData.getWdInst80() != null)
  734. ws80 = windTowerStatusData.getWsInst80();
  735. // 80米风向
  736. if (windTowerStatusData.getWdInst80() != null)
  737. wd80 = windTowerStatusData.getWdInst80();
  738. // 90米风速
  739. if (windTowerStatusData.getWdInst90() != null)
  740. ws90 = windTowerStatusData.getWsInst90();
  741. // 90米风向
  742. if (windTowerStatusData.getWdInst90() != null)
  743. wd90 = windTowerStatusData.getWdInst90();
  744. // 100米风速
  745. if (windTowerStatusData.getWdInst100() != null)
  746. ws100 = windTowerStatusData.getWsInst100();
  747. // 100米风向
  748. if (windTowerStatusData.getWdInst100() != null)
  749. wd100 = windTowerStatusData.getWdInst100();
  750. if (windTowerStatusData.getWsInstHubHeight() != null)
  751. wsHubHeight = windTowerStatusData.getWsInstHubHeight();
  752. if (windTowerStatusData.getWdInstHubHeight() != null)
  753. wdHubHeight = windTowerStatusData.getWdInstHubHeight();
  754. // 湿度产生
  755. if (windTowerStatusData.getRhInst() != null)
  756. rh8 = windTowerStatusData.getRhInst();
  757. // 温度产生
  758. if (windTowerStatusData.getTInst() != null)
  759. t8 = windTowerStatusData.getTInst();
  760. // 气压产生
  761. if (windTowerStatusData.getPaInst() != null)
  762. p8 = windTowerStatusData.getPaInst();
  763. log.info(electricFieldInfo.getStationCode() + "生成测风塔数据用数据表");
  764. }
  765. }
  766. // 创建上报文件
  767. File file = super.createTempFile(fileName);
  768. // 根据模板生成文件内容
  769. VelocityContext velocityContext = new VelocityContext();
  770. velocityContext.put("WS_10", df.format(ws10));
  771. velocityContext.put("WD_10", df.format(wd10));
  772. velocityContext.put("WS_30", df.format(ws30));
  773. velocityContext.put("WD_30", df.format(wd30));
  774. velocityContext.put("WS_50", df.format(ws50));
  775. velocityContext.put("WD_50", df.format(wd50));
  776. velocityContext.put("WS_60", df.format(ws60));
  777. velocityContext.put("WD_60", df.format(wd60));
  778. velocityContext.put("WS_70", df.format(ws70));
  779. velocityContext.put("WD_70", df.format(wd70));
  780. velocityContext.put("WS_80", df.format(ws80));
  781. velocityContext.put("WD_80", df.format(wd80));
  782. velocityContext.put("WS_90", df.format(ws90));
  783. velocityContext.put("WD_90", df.format(wd90));
  784. velocityContext.put("WS_100", df.format(ws100));
  785. velocityContext.put("WD_100", df.format(wd100));
  786. velocityContext.put("WS_HubHeight", df.format(wsHubHeight));
  787. velocityContext.put("WD_HubHeight", df.format(wdHubHeight));
  788. velocityContext.put("T_8", df.format(t8));
  789. velocityContext.put("P_8", df.format(p8));
  790. velocityContext.put("RH_8", df.format(rh8));
  791. // 场站标识
  792. velocityContext.put("sign", electricFieldInfo.getSign());
  793. // 系统当前日期
  794. velocityContext.put("currentTime", DateFormatUtils.format(endTime, "yyyy-MM-dd_HH:mm") + ":00");
  795. // 上报数据开始日期
  796. velocityContext.put("uploadTime", DateFormatUtils.format(endTime, "yyyy-MM-dd_HH:mm"));
  797. StringWriter writer = new StringWriter();
  798. template.merge(velocityContext, writer);
  799. super.copyUploadFile(writer, file, FileTypeEnum.E7.name(), null, date, electricFieldInfo.getStationCode());
  800. }
  801. }
  802. /**
  803. * 生成逆变器上报文件。
  804. */
  805. public void generateNbqOrFjFile(Date date) {
  806. List<ElectricField> electricFieldList = new ArrayList<>();
  807. try {
  808. electricFieldList = super.getMultipleStation();
  809. } catch (BusinessException e) {
  810. log.error("场站获取失败", e);
  811. }
  812. // 循环场站生成短期
  813. for (ElectricField electricFieldInfo : electricFieldList) {
  814. String stationCode = electricFieldInfo.getStationCode();
  815. try {
  816. String fileName;
  817. // 获取当前系统时间
  818. Date systemDate = new Date();
  819. if (date != null) {
  820. systemDate = date;
  821. }
  822. Long endTime = DateMomentUtil.getMomentTime(systemDate.getTime(), 1, 5 * 60 * 1000L);
  823. Long startTime = endTime - 4 * 60 * 1000;
  824. // 生成上报文件名格式
  825. Template template;
  826. if (electricFieldInfo.getElectricFieldTypeEnum().toString().equals("E1")) {
  827. // 获取逆变器模板
  828. template = this.velocityEngine.getTemplate(this.vmsPath + "/NBQ.vm");
  829. fileName = e63UploadFileRuleService.getFileNameRule(electricFieldInfo, "E8", endTime);
  830. if (getFileName(fileName, "E8", stationCode)) {
  831. generateNbqFile(fileName, template, electricFieldInfo, systemDate, startTime, endTime);
  832. }
  833. } else {
  834. template = this.velocityEngine.getTemplate(this.vmsPath + "/FJ.vm");
  835. fileName = e63UploadFileRuleService.getFileNameRule(electricFieldInfo, "E9", endTime);
  836. if (getFileName(fileName, "E9", stationCode)) {
  837. generateFjFile(fileName, template, electricFieldInfo, systemDate, startTime, endTime);
  838. }
  839. }
  840. } catch (Exception e) {
  841. log.error(stationCode + "生成文件异常", e);
  842. }
  843. }
  844. }
  845. /**
  846. * 生成风机上报文件
  847. */
  848. private void generateFjFile(String fileName, Template template, ElectricField electricFieldInfo, Date date, Long startTime, Long endTime) {
  849. // 获取风机信息
  850. List<WindTurbineInfo> windTurbineInfoList = windTurbineInfoRepository.findAll();
  851. // 将测风塔信息过滤出上报的塔
  852. List<WindTurbineInfo> filterWindTurbineInfoList = windTurbineInfoList.stream().filter(AbstractEquipmentInfo::getReport).collect(Collectors.toList());
  853. if (filterWindTurbineInfoList.size() > 0) {
  854. List<Map<String, Object>> fanList = new ArrayList<>();
  855. for (WindTurbineInfo windTurbineInfo : filterWindTurbineInfoList) {
  856. Map<String, Object> map = new HashMap<>();
  857. map.put("name", windTurbineInfo.getName());
  858. map.put("dtaStatus", windTurbineInfo);
  859. map.put("sample", windTurbineInfo.getSample() ? "1" : "0");
  860. map.put("modelNumber", windTurbineInfo.getModelNumber());
  861. map.put("RatedCapacity", div(BigDecimal.valueOf(windTurbineInfo.getMaxPower()), new BigDecimal("1000"), 2));
  862. map.put("elName", electricFieldInfo.getName());
  863. map.put("collectorCircuit", windTurbineInfo.getCollectorCircuit());
  864. String fjId = String.valueOf(windTurbineInfo.getId());
  865. Map<String, String> fjMap = redisUtils.hgetall("fj-" + electricFieldInfo.getStationCode() + "-" + fjId);
  866. DecimalFormat df = new DecimalFormat("0.00");
  867. // 状态
  868. String status = "1";
  869. // 有功
  870. BigDecimal activePower = new BigDecimal("0");
  871. // 无功
  872. BigDecimal reactivePower = new BigDecimal("0");
  873. // 功率因数
  874. BigDecimal powerFactor = new BigDecimal("0");
  875. // 电压
  876. BigDecimal voltage = new BigDecimal("0");
  877. // 电流
  878. BigDecimal electricalCurrent = new BigDecimal("0");
  879. // 当日发电量
  880. BigDecimal dayElectricQuantity = new BigDecimal("0");
  881. // 转速
  882. BigDecimal windWheelRatedSpeed = new BigDecimal("0");
  883. // 风速
  884. BigDecimal ws = new BigDecimal("0");
  885. // 风向
  886. BigDecimal wd = new BigDecimal("0");
  887. // 温度
  888. BigDecimal t = new BigDecimal("0");
  889. // 桨距角
  890. BigDecimal pitchAngle = new BigDecimal("0");
  891. // 累积发电量
  892. BigDecimal cumulativeGeneratedEnergy = new BigDecimal("0");
  893. boolean isUseRedis = false;
  894. if (!fjMap.isEmpty()) {
  895. String time = fjMap.get("time");
  896. long timestamp = new Date().getTime();
  897. SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
  898. try{
  899. Date d = sf.parse(time);// 日期转换为时间戳
  900. timestamp = d.getTime();
  901. }catch (Exception e){
  902. log.info("日期转换异常");
  903. e.printStackTrace();
  904. }
  905. if (StrUtil.isNotBlank(time)) {
  906. if (timestamp >= startTime && timestamp <= endTime) {
  907. if (fjMap.get("status") != null) {
  908. status = fjMap.get("status");
  909. }
  910. if (fjMap.get("activePower") != null) {
  911. activePower = new BigDecimal(fjMap.get("activePower"));
  912. }
  913. if (fjMap.get("reactivePower") != null) {
  914. reactivePower = new BigDecimal(fjMap.get("reactivePower"));
  915. }
  916. if (fjMap.get("powerFactor") != null) {
  917. powerFactor = new BigDecimal(fjMap.get("powerFactor"));
  918. }
  919. if (fjMap.get("voltage") != null) {
  920. voltage = new BigDecimal(fjMap.get("voltage"));
  921. }
  922. if (fjMap.get("electricalCurrent") != null) {
  923. electricalCurrent = new BigDecimal(fjMap.get("electricalCurrent"));
  924. }
  925. if (fjMap.get("dayElectricQuantity") != null) {
  926. dayElectricQuantity = new BigDecimal(fjMap.get("dayElectricQuantity"));
  927. }
  928. if (fjMap.get("cumulativeGeneratedEnergy") != null) {
  929. cumulativeGeneratedEnergy = new BigDecimal(fjMap.get("cumulativeGeneratedEnergy"));
  930. }
  931. if (fjMap.get("windWheelRatedSpeed") != null) {
  932. windWheelRatedSpeed = new BigDecimal(fjMap.get("windWheelRatedSpeed"));
  933. }
  934. if (fjMap.get("ws") != null) {
  935. ws = new BigDecimal(fjMap.get("ws"));
  936. }
  937. if (fjMap.get("wd") != null) {
  938. wd = new BigDecimal(fjMap.get("wd"));
  939. }
  940. if (fjMap.get("t") != null) {
  941. t = new BigDecimal(fjMap.get("t"));
  942. }
  943. if (fjMap.get("pitchAngle") != null) {
  944. pitchAngle = new BigDecimal(fjMap.get("pitchAngle"));
  945. }
  946. isUseRedis = true;
  947. log.info(electricFieldInfo.getStationCode() + "生成风机数据用缓存");
  948. }
  949. }
  950. }
  951. if (!isUseRedis) {
  952. List<WindTurbineStatusData> windTurbineStatusDataList = windTurbineStatusDataRepository.findByTimeBetweenAndEquipmentId(new Date(startTime), new Date(endTime), fjId);
  953. if (!windTurbineStatusDataList.isEmpty()) {
  954. windTurbineStatusDataList.stream().sorted(Comparator.comparing(WindTurbineStatusData::getTime).reversed()).collect(Collectors.toList());
  955. WindTurbineStatusData windTurbineStatusData = windTurbineStatusDataList.get(0);
  956. if (windTurbineStatusData.getStatus() != null) {
  957. status = String.valueOf(windTurbineStatusData.getStatus());
  958. }
  959. if (windTurbineStatusData.getActivePower() != null) {
  960. activePower = windTurbineStatusData.getActivePower();
  961. }
  962. if (windTurbineStatusData.getReactivePower() != null) {
  963. reactivePower = windTurbineStatusData.getReactivePower();
  964. }
  965. if (windTurbineStatusData.getPowerFactor() != null) {
  966. powerFactor = windTurbineStatusData.getPowerFactor();
  967. }
  968. if (windTurbineStatusData.getVoltage() != null) {
  969. voltage = windTurbineStatusData.getVoltage();
  970. }
  971. if (windTurbineStatusData.getElectricalCurrent() != null) {
  972. electricalCurrent = windTurbineStatusData.getElectricalCurrent();
  973. }
  974. if (windTurbineStatusData.getDayElectricQuantity() != null) {
  975. dayElectricQuantity = windTurbineStatusData.getDayElectricQuantity();
  976. }
  977. if (windTurbineStatusData.getCumulativeGeneratedEnergy() != null) {
  978. cumulativeGeneratedEnergy = windTurbineStatusData.getCumulativeGeneratedEnergy();
  979. }
  980. if (windTurbineStatusData.getWindWheelRatedSpeed() != null) {
  981. windWheelRatedSpeed = windTurbineStatusData.getWindWheelRatedSpeed();
  982. }
  983. if (windTurbineStatusData.getWs() != null) {
  984. ws = windTurbineStatusData.getWs();
  985. }
  986. if (windTurbineStatusData.getWd() != null) {
  987. wd = windTurbineStatusData.getWd();
  988. }
  989. if (windTurbineStatusData.getT() != null) {
  990. t = windTurbineStatusData.getT();
  991. }
  992. if (windTurbineStatusData.getPitchAngle() != null) {
  993. pitchAngle = windTurbineStatusData.getPitchAngle();
  994. }
  995. log.info(electricFieldInfo.getStationCode() + "生成风机数据用数据表");
  996. } else {
  997. log.info(electricFieldInfo.getStationCode() + "生成风机数据缓存和数据表都没有值,使用默认0");
  998. }
  999. }
  1000. map.put("ActivePower", div(activePower, new BigDecimal("1000"), 2).toString());
  1001. map.put("ReActivePower", div(reactivePower, new BigDecimal("1000"), 2).toString());
  1002. map.put("RotationlSpeed", df.format(windWheelRatedSpeed));
  1003. map.put("WS", df.format(ws));
  1004. map.put("WD", df.format(wd));
  1005. map.put("T", df.format(t));
  1006. map.put("Voltage", df.format(voltage));
  1007. map.put("GalvanicCurrent", df.format(electricalCurrent));
  1008. map.put("PowerFactor", df.format(powerFactor));
  1009. map.put("GeneratingCap", df.format(cumulativeGeneratedEnergy));
  1010. map.put("dtaStatus", status);
  1011. map.put("DayElectricQuantity", df.format(dayElectricQuantity));
  1012. map.put("PropellerPitchAngle", df.format(pitchAngle));
  1013. fanList.add(map);
  1014. }
  1015. // 创建上报文件
  1016. File file = super.createTempFile(fileName);
  1017. VelocityContext velocityContext = new VelocityContext();
  1018. // 根据模板生成文件内容
  1019. velocityContext.put("vList", fanList);
  1020. // 场站标识
  1021. velocityContext.put("sign", electricFieldInfo.getSign());
  1022. // 场站装机容量
  1023. velocityContext.put("capacity", electricFieldInfo.getCapacity());
  1024. // 系统当前日期
  1025. velocityContext.put("currentTime", DateFormatUtils.format(endTime, "yyyy-MM-dd_HH:mm") + ":00");
  1026. // 上报数据开始日期
  1027. velocityContext.put("uploadTime", DateFormatUtils.format(endTime, "yyyy-MM-dd_HH:mm"));
  1028. StringWriter writer = new StringWriter();
  1029. template.merge(velocityContext, writer);
  1030. super.copyUploadFile(writer, file, FileTypeEnum.E9.name(), null, date, electricFieldInfo.getStationCode());
  1031. }
  1032. }
  1033. /**
  1034. * 生成逆变器上报文件
  1035. */
  1036. private void generateNbqFile(String fileName, Template template, ElectricField electricFieldInfo, Date date, Long startTime, Long endTime) {
  1037. // 获取逆变器
  1038. List<InverterInfo> inverterInfoList = inverterInfoRepository.findAll();
  1039. // 过滤上报的逆变器
  1040. List<InverterInfo> filterInverterInfoList = inverterInfoList.stream().filter(AbstractEquipmentInfo::getReport).collect(Collectors.toList());
  1041. if (filterInverterInfoList.size() > 0) {
  1042. List<Map<String, String>> dtaInverterList = new ArrayList<>();
  1043. for (InverterInfo inverterInfo : filterInverterInfoList) {
  1044. Map<String, String> map = new HashMap<>();
  1045. //场站名称
  1046. map.put("czmc", electricFieldInfo.getName());
  1047. // 名称
  1048. map.put("name", inverterInfo.getName());
  1049. //集电线名称
  1050. map.put("CollectorCircuit", inverterInfo.getCollectorCircuit());
  1051. //额定容量
  1052. map.put("RatedCapacity", div(inverterInfo.getCapacity(), new BigDecimal("1000"), 2).toString());
  1053. // 开机容量
  1054. map.put("nbqCapacity", inverterInfo.getCapacity().toString());
  1055. //型号
  1056. map.put("modelNumber", inverterInfo.getModelNumber());
  1057. // 是否样板
  1058. map.put("sample", inverterInfo.getSample() ? "1" : "0");
  1059. //是否组串
  1060. map.put("IsString", inverterInfo.getGroupSeries() ? "1" : "0");
  1061. String nbqId = String.valueOf(inverterInfo.getId());
  1062. Map<String, String> nbqMap = redisUtils.hgetall("nbq-" + electricFieldInfo.getStationCode() + "-" + nbqId);
  1063. DecimalFormat df = new DecimalFormat("0.00");
  1064. // 状态
  1065. String status = "1";
  1066. // 有功
  1067. BigDecimal activePower = new BigDecimal("0");
  1068. // 无功
  1069. BigDecimal reactivePower = new BigDecimal("0");
  1070. // 功率因数
  1071. BigDecimal powerFactor = new BigDecimal("0");
  1072. // 电压
  1073. BigDecimal voltage = new BigDecimal("0");
  1074. // 电流
  1075. BigDecimal electricalCurrent = new BigDecimal("0");
  1076. // 当日发电量
  1077. BigDecimal dayElectricQuantity = new BigDecimal("0");
  1078. // 累积发电量
  1079. BigDecimal cumulativeGeneratedEnergy = new BigDecimal("0");
  1080. boolean isUseRedis = false;
  1081. if (!nbqMap.isEmpty()) {
  1082. String time = nbqMap.get("time");
  1083. long timestamp = new Date().getTime();
  1084. SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
  1085. try{
  1086. Date d = sf.parse(time);// 日期转换为时间戳
  1087. timestamp = d.getTime();
  1088. }catch (Exception e){
  1089. log.info("日期转换异常");
  1090. e.printStackTrace();
  1091. }
  1092. if (StrUtil.isNotBlank(time)) {
  1093. if (timestamp >= startTime && timestamp <= endTime) {
  1094. if (nbqMap.get("status") != null) {
  1095. status = nbqMap.get("status");
  1096. }
  1097. if (nbqMap.get("activePower") != null) {
  1098. activePower = new BigDecimal(nbqMap.get("activePower"));
  1099. }
  1100. if (nbqMap.get("reactivePower") != null) {
  1101. reactivePower = new BigDecimal(nbqMap.get("reactivePower"));
  1102. }
  1103. if (nbqMap.get("powerFactor") != null) {
  1104. powerFactor = new BigDecimal(nbqMap.get("powerFactor"));
  1105. }
  1106. if (nbqMap.get("voltage") != null) {
  1107. voltage = new BigDecimal(nbqMap.get("voltage"));
  1108. }
  1109. if (nbqMap.get("electricalCurrent") != null) {
  1110. electricalCurrent = new BigDecimal(nbqMap.get("electricalCurrent"));
  1111. }
  1112. if (nbqMap.get("dayElectricQuantity") != null) {
  1113. dayElectricQuantity = new BigDecimal(nbqMap.get("dayElectricQuantity"));
  1114. }
  1115. if (nbqMap.get("cumulativeGeneratedEnergy") != null) {
  1116. cumulativeGeneratedEnergy = new BigDecimal(nbqMap.get("cumulativeGeneratedEnergy"));
  1117. }
  1118. isUseRedis = true;
  1119. log.info(electricFieldInfo.getStationCode() + "生成逆变器数据用缓存");
  1120. }
  1121. }
  1122. }
  1123. if (!isUseRedis) {
  1124. List<InverterStatusData> inverterStatusDataList = inverterStatusDataRepository.findByTimeBetweenAndEquipmentId(new Date(startTime), new Date(endTime), nbqId);
  1125. if (!inverterStatusDataList.isEmpty()) {
  1126. inverterStatusDataList.stream().sorted(Comparator.comparing(InverterStatusData::getTime).reversed()).collect(Collectors.toList());
  1127. InverterStatusData inverterStatusData = inverterStatusDataList.get(0);
  1128. if (inverterStatusData.getStatus() != null) {
  1129. status = String.valueOf(inverterStatusData.getStatus());
  1130. }
  1131. // 有功
  1132. if (inverterStatusData.getActivePower() != null) {
  1133. activePower = inverterStatusData.getActivePower();
  1134. }
  1135. // 无功
  1136. if (inverterStatusData.getReactivePower() != null) {
  1137. reactivePower = inverterStatusData.getReactivePower();
  1138. }
  1139. // 电压
  1140. if (inverterStatusData.getVoltage() != null) {
  1141. voltage = inverterStatusData.getVoltage();
  1142. }
  1143. // 电流
  1144. if (inverterStatusData.getElectricalCurrent() != null) {
  1145. electricalCurrent = inverterStatusData.getElectricalCurrent();
  1146. }
  1147. // 功率因数
  1148. if (inverterStatusData.getPowerFactor() != null) {
  1149. powerFactor = inverterStatusData.getPowerFactor();
  1150. }
  1151. // 日发电量
  1152. if (inverterStatusData.getDayElectricQuantity() != null) {
  1153. dayElectricQuantity = inverterStatusData.getDayElectricQuantity();
  1154. }
  1155. // 累计发电量
  1156. if (inverterStatusData.getCumulativeGeneratedEnergy() != null) {
  1157. cumulativeGeneratedEnergy = inverterStatusData.getCumulativeGeneratedEnergy();
  1158. }
  1159. log.info(electricFieldInfo.getStationCode() + "生成逆变器数据用数据表");
  1160. } else {
  1161. log.info(electricFieldInfo.getStationCode() + "生成逆变器数据缓存和数据表都没有值,使用默认0");
  1162. }
  1163. }
  1164. map.put("status", status);
  1165. // 有功
  1166. map.put("NbqActivePower", div(activePower, new BigDecimal("1000"), 2).toString());
  1167. // 无功
  1168. map.put("NbqReActivePower", div(reactivePower, new BigDecimal("1000"), 2).toString());
  1169. // 电压
  1170. map.put("Voltage", df.format(voltage));
  1171. // 电流
  1172. map.put("GalvanicCurrent", df.format(electricalCurrent));
  1173. // 功率因数
  1174. map.put("PowerFactor", df.format(powerFactor));
  1175. // 日发电量
  1176. map.put("NbqDailyOutPut", df.format(dayElectricQuantity));
  1177. // 累计发电量
  1178. map.put("GeneratingCap", df.format(cumulativeGeneratedEnergy));
  1179. dtaInverterList.add(map);
  1180. }
  1181. // 创建上报文件
  1182. File file = super.createTempFile(fileName);
  1183. VelocityContext velocityContext = new VelocityContext();
  1184. // 根据模板生成文件内容
  1185. velocityContext.put("vList", dtaInverterList);
  1186. // 场站标识
  1187. velocityContext.put("sign", electricFieldInfo.getSign());
  1188. // 场站装机容量
  1189. velocityContext.put("capacity", electricFieldInfo.getCapacity());
  1190. // 系统当前日期
  1191. velocityContext.put("currentTime", DateFormatUtils.format(endTime, "yyyy-MM-dd_HH:mm") + ":00");
  1192. // 上报数据开始日期
  1193. velocityContext.put("uploadTime", DateFormatUtils.format(endTime, "yyyy-MM-dd_HH:mm"));
  1194. StringWriter writer = new StringWriter();
  1195. template.merge(velocityContext, writer);
  1196. super.copyUploadFile(writer, file, FileTypeEnum.E8.name(), null, date, electricFieldInfo.getStationCode());
  1197. }
  1198. }
  1199. /**
  1200. * 生成理论功率上报文件
  1201. */
  1202. public void generateTheroyFile(Date date) {
  1203. List<ElectricField> electricFieldList = new ArrayList<>();
  1204. try {
  1205. electricFieldList = super.getMultipleStation();
  1206. } catch (BusinessException e) {
  1207. log.error("场站获取失败", e);
  1208. }
  1209. // 循环场站生成短期
  1210. for (ElectricField electricFieldInfo : electricFieldList) {
  1211. String stationCode = electricFieldInfo.getStationCode();
  1212. try {
  1213. String fileName;
  1214. // 获取当前系统时间
  1215. Date systemDate = new Date();
  1216. if (date != null) {
  1217. systemDate = date;
  1218. }
  1219. Long endTime = DateMomentUtil.getMomentTime(systemDate.getTime(), 1, 5 * 60 * 1000L);
  1220. Long startTime = endTime - 4 * 60 * 1000;
  1221. // 生成上报文件名格式
  1222. Template template;
  1223. if (electricFieldInfo.getElectricFieldTypeEnum().toString().equals("E1")) {
  1224. fileName = e63UploadFileRuleService.getFileNameRule(electricFieldInfo, "E3", endTime);
  1225. if (getFileName(fileName, "E3", stationCode)) {
  1226. template = this.velocityEngine.getTemplate(this.vmsPath + "/THEROY.vm");
  1227. generatePvTheroyFile(fileName, template, electricFieldInfo, date, startTime, endTime);
  1228. }
  1229. } else {
  1230. fileName = e63UploadFileRuleService.getFileNameRule(electricFieldInfo, "E85", endTime);
  1231. if (getFileName(fileName, "E85", stationCode)) {
  1232. // 理论功率风
  1233. template = this.velocityEngine.getTemplate(this.vmsPath + "/THEROY_F.vm");
  1234. generateWpTheroyFile(fileName, template, electricFieldInfo, date, startTime, endTime);
  1235. }
  1236. }
  1237. } catch (Exception e) {
  1238. log.error(stationCode + "生成文件异常", e);
  1239. }
  1240. }
  1241. }
  1242. /**
  1243. * 生成理论功率光上报文件
  1244. */
  1245. private void generatePvTheroyFile(String fileName, Template template, ElectricField electricFieldInfo, Date date, Long startTime, Long endTime) {
  1246. // 样板机变量
  1247. float ableValueAfter = 0f;
  1248. // 辐照仪变量
  1249. float theoryValueAfter = 0f;
  1250. Map<String, String> powerMap = redisUtils.hgetall("power-" + electricFieldInfo.getStationCode());
  1251. boolean isUseRedis = false;
  1252. if (!powerMap.isEmpty()) {
  1253. String time = powerMap.get("time");
  1254. if (StrUtil.isNotBlank(time)) {
  1255. if (Long.parseLong(time) >= startTime && Long.parseLong(time) <= endTime) {
  1256. // 对预测值保留2位小数显示输出
  1257. if (powerMap.get("ableValue") != null) {
  1258. ableValueAfter = Float.parseFloat(powerMap.get("ableValue"));
  1259. }
  1260. if (powerMap.get("theoryValue") != null) {
  1261. theoryValueAfter = Float.parseFloat(powerMap.get("theoryValue"));
  1262. }
  1263. isUseRedis = true;
  1264. log.info(electricFieldInfo.getStationCode() + "生成理论功率使用缓存");
  1265. }
  1266. }
  1267. }
  1268. if (!isUseRedis) {
  1269. // 从数据表获取
  1270. List<PowerStationStatusData> powerStationStatusDataList = powerStationStatusDataRepository.findByTimeBetweenAndStationCode(new Date(startTime), new Date(endTime), electricFieldInfo.getStationCode());
  1271. if (!powerStationStatusDataList.isEmpty()) {
  1272. powerStationStatusDataList.stream().sorted(Comparator.comparing(PowerStationStatusData::getTime).reversed()).collect(Collectors.toList());
  1273. PowerStationStatusData powerStationStatusData = powerStationStatusDataList.get(0);
  1274. if (powerStationStatusData.getAbleValue() != null)
  1275. ableValueAfter = powerStationStatusData.getAbleValue().floatValue();
  1276. if (powerStationStatusData.getTheoryValue() != null)
  1277. theoryValueAfter = powerStationStatusData.getTheoryValue().floatValue();
  1278. log.info(electricFieldInfo.getStationCode() + "生成理论功率使用数据表");
  1279. }
  1280. }
  1281. DecimalFormat df = new DecimalFormat("0.00");
  1282. // 创建上报文件
  1283. File file = super.createTempFile(fileName);
  1284. // 根据模板生成文件内容
  1285. VelocityContext velocityContext = new VelocityContext();
  1286. // 样板机
  1287. velocityContext.put("ableValueAfter", df.format(ableValueAfter));
  1288. // 辐照仪
  1289. velocityContext.put("theoryValueAfter", df.format(theoryValueAfter));
  1290. // 场站标识
  1291. velocityContext.put("sign", electricFieldInfo.getSign());
  1292. // 系统当前日期
  1293. velocityContext.put("currentTime", DateFormatUtils.format(endTime, "yyyy-MM-dd_HH:mm") + ":00");
  1294. // 上报数据开始日期
  1295. velocityContext.put("uploadTime", DateFormatUtils.format(endTime, "yyyy-MM-dd_HH:mm"));
  1296. StringWriter writer = new StringWriter();
  1297. template.merge(velocityContext, writer);
  1298. super.copyUploadFile(writer, file, FileTypeEnum.E3.name(), null, date, electricFieldInfo.getStationCode());
  1299. }
  1300. /**
  1301. * 生成理论功率风上报文件
  1302. */
  1303. private void generateWpTheroyFile(String fileName, Template template, ElectricField electricFieldInfo, Date date, Long startTime, Long endTime) {
  1304. // 可用
  1305. float ableValueAfter = 0f;
  1306. // 理论
  1307. float theoryValueAfter = 0f;
  1308. //样板机
  1309. float referencePowerBySampleAfter = 0f;
  1310. //测风
  1311. float referencePowerByMeasuringAfter = 0f;
  1312. Map<String, String> powerMap = redisUtils.hgetall("power-" + electricFieldInfo.getStationCode());
  1313. boolean isUseRedis = false;
  1314. if (!powerMap.isEmpty()) {
  1315. String time = powerMap.get("time");
  1316. if (StrUtil.isNotBlank(time)) {
  1317. if (Long.parseLong(time) >= startTime && Long.parseLong(time) <= endTime) {
  1318. if (powerMap.get("ableValue") != null) {
  1319. ableValueAfter = Float.parseFloat(powerMap.get("ableValue"));
  1320. }
  1321. if (powerMap.get("theoryValue") != null) {
  1322. theoryValueAfter = Float.parseFloat(powerMap.get("theoryValue"));
  1323. }
  1324. if (powerMap.get("referencePowerBySample") != null) {
  1325. referencePowerBySampleAfter = Float.parseFloat(powerMap.get("referencePowerBySample"));
  1326. }
  1327. if (powerMap.get("referencePowerByMeasuring") != null) {
  1328. referencePowerByMeasuringAfter = Float.parseFloat(powerMap.get("referencePowerByMeasuring"));
  1329. }
  1330. isUseRedis = true;
  1331. log.info(electricFieldInfo.getStationCode() + "生成理论功率使用缓存");
  1332. }
  1333. }
  1334. }
  1335. if (!isUseRedis) {
  1336. // 从数据表获取
  1337. List<PowerStationStatusData> powerStationStatusDataList = powerStationStatusDataRepository.findByTimeBetweenAndStationCode(new Date(startTime), new Date(endTime), electricFieldInfo.getStationCode());
  1338. if (!powerStationStatusDataList.isEmpty()) {
  1339. powerStationStatusDataList.stream().sorted(Comparator.comparing(PowerStationStatusData::getTime).reversed()).collect(Collectors.toList());
  1340. PowerStationStatusData powerStationStatusData = powerStationStatusDataList.get(0);
  1341. if (powerStationStatusData.getAbleValue() != null)
  1342. ableValueAfter = powerStationStatusData.getAbleValue().floatValue();
  1343. if (powerStationStatusData.getTheoryValue() != null)
  1344. theoryValueAfter = powerStationStatusData.getTheoryValue().floatValue();
  1345. if (powerStationStatusData.getReferencePowerBySample() != null)
  1346. referencePowerBySampleAfter = powerStationStatusData.getReferencePowerBySample().floatValue();
  1347. if (powerStationStatusData.getReferencePowerByMeasuring() != null)
  1348. referencePowerByMeasuringAfter = powerStationStatusData.getReferencePowerByMeasuring().floatValue();
  1349. log.info(electricFieldInfo.getStationCode() + "生成理论功率使用数据表");
  1350. }
  1351. }
  1352. DecimalFormat df = new DecimalFormat("0.00");
  1353. // 创建上报文件
  1354. File file = super.createTempFile(fileName);
  1355. // 根据模板生成文件内容
  1356. VelocityContext velocityContext = new VelocityContext();
  1357. // 样板机
  1358. velocityContext.put("ableValueAfter", df.format(ableValueAfter));
  1359. // 辐照仪
  1360. velocityContext.put("theoryValueAfter", df.format(theoryValueAfter));
  1361. // 样板机
  1362. velocityContext.put("referencePowerBySampleAfter", df.format(referencePowerBySampleAfter));
  1363. // 测风
  1364. velocityContext.put("referencePowerByMeasuringAfter", df.format(referencePowerByMeasuringAfter));
  1365. // 场站标识
  1366. velocityContext.put("sign", electricFieldInfo.getSign());
  1367. // 系统当前日期
  1368. velocityContext.put("currentTime", DateFormatUtils.format(endTime, "yyyy-MM-dd_HH:mm") + ":00");
  1369. // 上报数据开始日期
  1370. velocityContext.put("uploadTime", DateFormatUtils.format(endTime, "yyyy-MM-dd_HH:mm") + ":00");
  1371. StringWriter writer = new StringWriter();
  1372. template.merge(velocityContext, writer);
  1373. super.copyUploadFile(writer, file, FileTypeEnum.E85.name(), null, date, electricFieldInfo.getStationCode());
  1374. }
  1375. /**
  1376. * 除法
  1377. *
  1378. * @param v1
  1379. * @param v2
  1380. * @param scale 精度,到小数点后几位
  1381. * @return
  1382. */
  1383. private BigDecimal div(BigDecimal v1, BigDecimal v2, int scale) {
  1384. if (scale < 0) {
  1385. throw new IllegalArgumentException("The scale must be a positive integer or zero");
  1386. }
  1387. return v1.divide(v2, scale, RoundingMode.HALF_UP);
  1388. }
  1389. private BigDecimal qxzRandomCreate(String type) {
  1390. DecimalFormat df = new DecimalFormat("0.00");
  1391. switch (type) {
  1392. case "globalR":
  1393. // 总辐射产生500-1000随机数
  1394. return new BigDecimal(df.format(Math.random() * 500 + 500));
  1395. case "directR":
  1396. // 直辐射产生400-500随机数
  1397. return new BigDecimal(df.format(Math.random() * 100 + 400));
  1398. case "airT":
  1399. // 温度
  1400. return new BigDecimal(df.format(Math.random() * 4 + 8));
  1401. case "p":
  1402. // 气压
  1403. return new BigDecimal(df.format(Math.random() * 200 + 800));
  1404. case "rh":
  1405. // 湿度
  1406. return new BigDecimal(df.format(Math.random() * 10 + 30));
  1407. case "cellT":
  1408. // 电池板温度
  1409. return new BigDecimal(df.format(Math.random() * 26 + 4));
  1410. case "ws":
  1411. // 风速
  1412. return new BigDecimal(df.format(Math.random() * 9 + 1));
  1413. case "wd":
  1414. // 风向
  1415. return new BigDecimal(df.format(Math.random() * 100 + 200));
  1416. default:
  1417. return new BigDecimal("0");
  1418. }
  1419. }
  1420. private BigDecimal cftRandomCreate(String type) {
  1421. DecimalFormat df = new DecimalFormat("0.00");
  1422. switch (type) {
  1423. case "wsInst10":
  1424. // 10米风速产生1-5随机数
  1425. return new BigDecimal(df.format(Math.random() * 4 + 1));
  1426. case "wdInst10":
  1427. // 10米风向产生40-60随机数
  1428. return new BigDecimal(df.format(Math.random() * 20 + 40));
  1429. case "wsInst30":
  1430. // 30米风速产生6-10随机数
  1431. return new BigDecimal(df.format(Math.random() * 4 + 6));
  1432. case "wdInst30":
  1433. // 30米风向产生70-90随机数
  1434. return new BigDecimal(df.format(Math.random() * 20 + 70));
  1435. case "wsInst50":
  1436. // 50米风速产生16-20随机数
  1437. return new BigDecimal(df.format(Math.random() * 4 + 16));
  1438. case "wdInst50":
  1439. // 50米风向产生130-150随机数
  1440. return new BigDecimal(df.format(Math.random() * 20 + 130));
  1441. case "wsInst60":
  1442. // 60米风速产生25-30随机数
  1443. return new BigDecimal(df.format(Math.random() * 5 + 25));
  1444. case "wdInst60":
  1445. // 60米风向产生160-200随机数
  1446. return new BigDecimal(df.format(Math.random() * 40 + 160));
  1447. case "wsInst70":
  1448. // 70米风速产生31-35随机数
  1449. return new BigDecimal(df.format(Math.random() * 4 + 31));
  1450. case "wdInst70":
  1451. // 70米风向产生220-240随机数
  1452. return new BigDecimal(df.format(Math.random() * 20 + 220));
  1453. case "wsInst80":
  1454. // 80米风速产生35-40随机数
  1455. return new BigDecimal(df.format(Math.random() * 20 + 220));
  1456. case "wdInst80":
  1457. // 80米风向产生240-250随机数
  1458. return new BigDecimal(df.format(Math.random() * 10 + 240));
  1459. case "wsInst90":
  1460. // 90米风速产生45-50随机数
  1461. return new BigDecimal(df.format(Math.random() * 5 + 45));
  1462. case "wdInst90":
  1463. // 90米风向产生260-270随机数
  1464. return new BigDecimal(df.format(Math.random() * 10 + 260));
  1465. case "wsInst100":
  1466. // 100米风速产生60-70随机数
  1467. return new BigDecimal(df.format(Math.random() * 10 + 60));
  1468. case "wdInst100":
  1469. // 100米风向产生280-290随机数
  1470. return new BigDecimal(df.format(Math.random() * 10 + 280));
  1471. case "wsInstHubHeight":
  1472. // 轮毂风速70-74随机数
  1473. return new BigDecimal(df.format(Math.random() * 4 + 70));
  1474. case "wdInstHubHeight":
  1475. // 轮毂风向280-290随机数
  1476. return new BigDecimal(df.format(Math.random() * 20 + 250));
  1477. case "tInst":
  1478. // 温度产生20-35随机数
  1479. return new BigDecimal(df.format(Math.random() * 15 + 20));
  1480. case "paInst":
  1481. // 气压产生75-90随机数
  1482. return new BigDecimal(df.format(Math.random() * 15 + 75));
  1483. case "rhInst":
  1484. // 湿度产生50-60随机数
  1485. return new BigDecimal(df.format(Math.random() * 10 + 50));
  1486. default:
  1487. return new BigDecimal("0");
  1488. }
  1489. }
  1490. }