DataProcess.java 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. package com.jjt;
  2. import cn.hutool.json.JSONArray;
  3. import cn.hutool.json.JSONObject;
  4. import com.ruoyi.RuoYiApplication;
  5. import com.ruoyi.biz.service.IIotService;
  6. import com.ruoyi.common.utils.DateUtils;
  7. import org.apache.poi.ss.usermodel.Cell;
  8. import org.apache.poi.ss.usermodel.Row;
  9. import org.apache.poi.ss.usermodel.Sheet;
  10. import org.apache.poi.ss.usermodel.Workbook;
  11. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  12. import org.junit.jupiter.api.Test;
  13. import org.springframework.boot.test.context.SpringBootTest;
  14. import org.springframework.jdbc.core.JdbcTemplate;
  15. import javax.annotation.Resource;
  16. import java.io.FileOutputStream;
  17. import java.io.IOException;
  18. import java.time.LocalDate;
  19. import java.time.LocalDateTime;
  20. import java.time.LocalTime;
  21. import java.time.ZoneOffset;
  22. import java.util.ArrayList;
  23. import java.util.Date;
  24. import java.util.List;
  25. import java.util.concurrent.atomic.AtomicInteger;
  26. /**
  27. * DataProcess$
  28. *
  29. * @author wukai
  30. * @date 2024/5/7 11:49
  31. */
  32. @SpringBootTest(classes = RuoYiApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
  33. public class DataProcess {
  34. @Resource
  35. private JdbcTemplate jdbcTemplate;
  36. @Resource
  37. private IIotService iotService;
  38. public static void main(String[] args) {
  39. String date = "2024-06-25";
  40. LocalDate localDate = LocalDate.parse(date);
  41. //获取当天0点
  42. LocalDateTime ldt = LocalDateTime.of(localDate, LocalTime.MIN);
  43. LocalDateTime aTime = ldt.plusHours(7);
  44. //获取7点-19点为A班
  45. int times = 1;
  46. do {
  47. System.err.println("A班\t"+aTime);
  48. aTime = aTime.plusHours(1);
  49. } while (times++ < 12);
  50. LocalDateTime bTime=ldt.plusHours(19);
  51. //获取19点-第二天6点为B班
  52. times = 1;
  53. do {
  54. System.err.println("B班\t"+bTime);
  55. bTime = bTime.plusHours(1);
  56. } while (times++ < 12);
  57. }
  58. @Test
  59. void test() {
  60. String code = "135";
  61. String table = "root.tl.suxi.knittings" + code + "_plc1";
  62. Workbook workbook = new XSSFWorkbook();
  63. String date = "2025-02-24";
  64. System.err.println(date);
  65. List<String[]> list = new ArrayList<>();
  66. for (int j = 7; j < 19; j++) {
  67. List<String[]> pList = getNew(table, date, j);
  68. list.addAll(pList);
  69. }
  70. // String date="2024-06-25";
  71. // for (int j = 0; j < 7; j++) {
  72. // List<String[]> pList = getNew(table, date, j);
  73. // list.addAll(pList);
  74. // }
  75. Sheet sheet = workbook.createSheet("A班");
  76. toExcelNew(list, sheet);
  77. try (FileOutputStream outputStream = new FileOutputStream("D:\\SYSTEM\\Desktop\\temp\\excel\\小经编机" + code + "号+" + date + "-明细.xlsx")) {
  78. workbook.write(outputStream);
  79. } catch (IOException e) {
  80. e.printStackTrace();
  81. } finally {
  82. try {
  83. workbook.close();
  84. } catch (IOException e) {
  85. e.printStackTrace();
  86. }
  87. }
  88. }
  89. @Test
  90. void excel() {
  91. String code = "3";
  92. String table = "root.tl.suxi.knittings" + code + "_plc1";
  93. Workbook workbook = new XSSFWorkbook();
  94. for (int i = 21; i <= 26; i++) {
  95. String date = "2024-06-" + i;
  96. System.err.println(date);
  97. List<String[]> list = new ArrayList<>();
  98. for (int j = 1; j <= 12; j++) {
  99. List<String[]> pList = get(table, date, j);
  100. list.addAll(pList);
  101. }
  102. Sheet sheet = workbook.createSheet(date);
  103. toExcel(list, sheet);
  104. }
  105. try (FileOutputStream outputStream = new FileOutputStream("D:\\SYSTEM\\Desktop\\temp\\excel\\小经编机" + code + "号.xlsx")) {
  106. workbook.write(outputStream);
  107. } catch (IOException e) {
  108. e.printStackTrace();
  109. } finally {
  110. try {
  111. workbook.close();
  112. } catch (IOException e) {
  113. e.printStackTrace();
  114. }
  115. }
  116. }
  117. public List<String[]> get(String table, String date, int period) {
  118. List<String[]> list = new ArrayList<>();
  119. LocalDate localDate = LocalDate.parse(date);
  120. LocalDateTime ldt = LocalDateTime.of(localDate, LocalTime.MIN);
  121. LocalDateTime start = ldt.plusHours(2 * period).minusHours(2);
  122. LocalDateTime end = start.plusHours(2);
  123. start = start.minusSeconds(1);
  124. Long startTime = start.toInstant(ZoneOffset.of("+8")).toEpochMilli();
  125. Long endTime = end.toInstant(ZoneOffset.of("+8")).toEpochMilli();
  126. String sql = "select Capacity_data_5,Capacity_data_6,Capacity_data_7,Capacity_data_8,Capacity_data_9,Capacity_data_10,Capacity_data_11,Capacity_data_12,Capacity_data_13,Capacity_data_14,Capacity_data_15,Capacity_data_16,Capacity_data_17,Capacity_data_18,Capacity_data_19" +
  127. " from %s where time>%s and time <=%s";
  128. sql = String.format(sql, table, startTime, endTime);
  129. iotService.query(sql);
  130. JSONObject jsonObject = iotService.query(sql);
  131. JSONObject data = jsonObject.getJSONObject("data");
  132. JSONArray values = data.getJSONArray("values");
  133. JSONArray timestamps = data.getJSONArray("timestamps");
  134. for (int i = 0; i < values.size(); i++) {
  135. String[] temp = new String[16];
  136. temp[0] = timestamps.getStr(i);
  137. JSONArray da = values.getJSONArray(i);
  138. for (int j = 1; j < temp.length; j++) {
  139. temp[j] = da.getStr(j - 1);
  140. }
  141. list.add(temp);
  142. }
  143. return list;
  144. }
  145. public void toExcel(List<String[]> list, Sheet sheet) {
  146. String[] names = {"时间", "GB1剩余时间", "GB2剩余时间", "GB3剩余时间", "GB4剩余时间", "GB5剩余时间", "GB1盘头剩余周长", "GB2盘头剩余周长", "GB3盘头剩余周长", "GB4盘头剩余周长", "GB5盘头剩余周长", "GB1盘头剩余圈数", "GB2盘头剩余圈数", "GB3盘头剩余圈数", "GB4盘头剩余圈数", "GB5盘头剩余圈数"};
  147. Row title = sheet.createRow(0);
  148. for (int i = 0; i < names.length; i++) {
  149. Cell cell = title.createCell(i);
  150. cell.setCellValue(names[i]);
  151. }
  152. AtomicInteger index = new AtomicInteger(1);
  153. list.forEach(d -> {
  154. Row row = sheet.createRow(index.get());
  155. Long time = Long.parseLong(d[0]);
  156. String date = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, new Date(time));
  157. Cell cell = row.createCell(0);
  158. cell.setCellValue(date);
  159. for (int j = 1; j <= 10; j++) {
  160. cell = row.createCell(j);
  161. cell.setCellValue(Float.parseFloat(d[j]));
  162. }
  163. for (int j = 11; j < d.length; j++) {
  164. cell = row.createCell(j);
  165. cell.setCellValue(Integer.parseInt(d[j]));
  166. }
  167. index.getAndIncrement();
  168. });
  169. }
  170. public List<String[]> getNew(String table, String date, int period) {
  171. List<String[]> list = new ArrayList<>();
  172. LocalDate localDate = LocalDate.parse(date);
  173. LocalDateTime ldt = LocalDateTime.of(localDate, LocalTime.MIN);
  174. LocalDateTime start = ldt.plusHours(period);
  175. LocalDateTime end = start.plusHours(1);
  176. start = start.minusSeconds(1);
  177. Long startTime = start.toInstant(ZoneOffset.of("+8")).toEpochMilli();
  178. Long endTime = end.toInstant(ZoneOffset.of("+8")).toEpochMilli();
  179. String sql = "select Capacity_data_2,Capacity_data_33" +
  180. " from %s where time>%s and time <=%s";
  181. sql = String.format(sql, table, startTime, endTime);
  182. iotService.query(sql);
  183. JSONObject jsonObject = iotService.query(sql);
  184. JSONObject data = jsonObject.getJSONObject("data");
  185. JSONArray values = data.getJSONArray("values");
  186. JSONArray timestamps = data.getJSONArray("timestamps");
  187. for (int i = 0; i < values.size(); i++) {
  188. String[] temp = new String[3];
  189. temp[0] = timestamps.getStr(i);
  190. JSONArray da = values.getJSONArray(i);
  191. for (int j = 1; j < temp.length; j++) {
  192. temp[j] = da.getStr(j - 1);
  193. }
  194. list.add(temp);
  195. }
  196. return list;
  197. }
  198. public void toExcelNew(List<String[]> list, Sheet sheet) {
  199. String[] names = {"时间", "织造米数", "功率-W"};
  200. Row title = sheet.createRow(0);
  201. for (int i = 0; i < names.length; i++) {
  202. Cell cell = title.createCell(i);
  203. cell.setCellValue(names[i]);
  204. }
  205. AtomicInteger index = new AtomicInteger(1);
  206. list.forEach(d -> {
  207. Row row = sheet.createRow(index.get());
  208. Long time = Long.parseLong(d[0]);
  209. String date = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, new Date(time));
  210. Cell cell = row.createCell(0);
  211. cell.setCellValue(date);
  212. for (int j = 1; j <= 2; j++) {
  213. cell = row.createCell(j);
  214. cell.setCellValue(Float.parseFloat(d[j]));
  215. }
  216. index.getAndIncrement();
  217. });
  218. }
  219. }