EveryEightGetController.java 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package io.renren.modules.common;
  2. import io.renren.modules.aspen.entity.TDashboarddayeleceightEntity;
  3. import io.renren.modules.aspen.service.TDashboarddayeleceightService;
  4. import io.renren.modules.sys.controller.AbstractController;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.scheduling.annotation.EnableScheduling;
  7. import org.springframework.stereotype.Component;
  8. import com.opencsv.CSVReader;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.scheduling.annotation.Scheduled;
  11. import java.io.FileReader;
  12. import java.io.IOException;
  13. import java.math.BigDecimal;
  14. import java.util.Calendar;
  15. import java.util.Date;
  16. @Component
  17. @Configuration //1.主要用于标记配置类,兼备Component的效果。
  18. @EnableScheduling // 2.开启定时任务
  19. public class EveryEightGetController extends AbstractController {
  20. @Autowired
  21. private TDashboarddayeleceightService tDashboarddayeleceightService;
  22. // @Scheduled(cron = "0 59 7 * * ?")
  23. //每日8:00触发
  24. @Scheduled(cron = "0 0 8 * * ?")
  25. public void getExcelData1(){
  26. logger.info("0:00获取数据");
  27. // String csvFile = "D://ssyFile/物料.csv";
  28. String csvFile = "C://dashboardAspen/wuliao.csv";
  29. TDashboarddayeleceightEntity tDashboarddayeleceightEntity = new TDashboarddayeleceightEntity();
  30. CSVReader reader = null;
  31. try {
  32. reader = new CSVReader(new FileReader(csvFile));
  33. String[] line;
  34. //定义行数
  35. int i =0;
  36. while ((line = reader.readNext()) != null) {
  37. if (i == 18) {
  38. logger.info("第" + i + "行");
  39. for (int j = 2; j < 6; j++) {
  40. logger.info("读数:" + line[j]);
  41. if (line[j].equals("")) {
  42. line[j] = "0";
  43. }else {
  44. if (!Character.isDigit(line[j].charAt(0))) {
  45. line[j] = "0";
  46. }else {
  47. BigDecimal b = new BigDecimal(Double.parseDouble(line[j]));
  48. double f = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
  49. line[j] = String.valueOf(f);
  50. }
  51. }
  52. logger.info(line[j]);
  53. }
  54. tDashboarddayeleceightEntity.setEvconsume(line[2]);
  55. tDashboarddayeleceightEntity.setElecp(line[3]);
  56. tDashboarddayeleceightEntity.setHechengqi(line[4]);
  57. tDashboarddayeleceightEntity.setU2(line[5]);
  58. }
  59. }
  60. tDashboarddayeleceightEntity.setDataDate(new Date());
  61. Calendar now = Calendar.getInstance();
  62. tDashboarddayeleceightEntity.setDateDay(String.valueOf(now.get(Calendar.DAY_OF_MONTH)));
  63. logger.info("dashboardDay:" + tDashboarddayeleceightEntity);
  64. tDashboarddayeleceightService.save(tDashboarddayeleceightEntity);
  65. } catch (IOException e) {
  66. e.printStackTrace();
  67. }
  68. }
  69. public String rounding(String lineNum) {
  70. try {
  71. logger.info("读数:" + lineNum);
  72. if (lineNum.equals("")) {
  73. lineNum = "0.0";
  74. }else {
  75. if (!Character.isDigit(lineNum.charAt(0))) {
  76. lineNum = "0.0";
  77. }else {
  78. BigDecimal b = new BigDecimal(Double.parseDouble(lineNum));
  79. double f = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
  80. lineNum = String.valueOf(f);
  81. }
  82. }
  83. logger.info(lineNum);
  84. return lineNum;
  85. }catch (Exception e) {
  86. logger.error(String.valueOf(e));
  87. return "0.0";
  88. }
  89. }
  90. }