BatchUpdateUtils.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package com.gyee.benchmarkinghive.util;
  2. import com.gyee.benchmarkinghive.model.auto.Equipmentdaydetailed;
  3. import com.gyee.benchmarkinghive.model.auto.Equipmentdayinfo;
  4. import com.gyee.benchmarkinghive.service.GenericService;
  5. import org.springframework.jdbc.core.JdbcOperations;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. /**
  9. * @ClassName : BatchUpdateUtils
  10. * @Author : xieshengjie
  11. * @Date: 2021/6/15 23:47
  12. * @Description : 批量保存工具类
  13. */
  14. public class BatchUpdateUtils extends GenericService {
  15. private static final Integer BATCH_SIZE = 100;
  16. private JdbcOperations jdbcTemplate;
  17. public void batchEquipmentinfoInsert(List<Equipmentdayinfo> list) {
  18. String sql =" INSERT INTO gyee_test.equipmentdayinfo (id, windturbineid, projectid, lineid, windpowerstationid, " +
  19. " recorddate, genecapacity, therogenecapacity, speed, daynhwhssdl, daynhgzssdl, " +
  20. " daynhxdssdl, daynhqfdl, daynhcfdl) " +
  21. " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ";
  22. List<Object[]> args = transformFlowCarReportDayBoToObjects(list);
  23. int fromIndex = 0; int toIndex = BATCH_SIZE;
  24. while (fromIndex != args.size()) {
  25. if (toIndex > args.size()) {
  26. toIndex = args.size();
  27. }
  28. jdbcTemplate.batchUpdate(sql,args.subList(fromIndex, toIndex));
  29. fromIndex = toIndex;
  30. toIndex += BATCH_SIZE;
  31. if (toIndex > args.size())
  32. toIndex = args.size();
  33. }
  34. }
  35. private List<Object[]> transformFlowCarReportDayBoToObjects(List<Equipmentdayinfo> equipmentdayinfoList) {
  36. List<Object[]> list = new ArrayList<>();
  37. Object[] object = null;
  38. for(Equipmentdayinfo equipmentdayinfo :equipmentdayinfoList){
  39. object = new Object[]{
  40. equipmentdayinfo.getId(),
  41. equipmentdayinfo.getWindturbineid(),
  42. equipmentdayinfo.getProjectid(),
  43. equipmentdayinfo.getLineid(),
  44. equipmentdayinfo.getWindpowerstationid(),
  45. equipmentdayinfo.getRecorddate(),
  46. equipmentdayinfo.getGenecapacity(),
  47. equipmentdayinfo.getTherogenecapacity(),
  48. equipmentdayinfo.getSpeed(),
  49. equipmentdayinfo.getDaynhwhssdl(),
  50. equipmentdayinfo.getDaynhgzssdl(),
  51. equipmentdayinfo.getDaynhxdssdl(),
  52. equipmentdayinfo.getDaynhqfdl(),
  53. equipmentdayinfo.getDaynhcfdl()
  54. };
  55. list.add(object);
  56. }
  57. return list ;
  58. }
  59. public void batchEquipmentdetailedInsert(List<Equipmentdaydetailed> list) {
  60. String sql =" INSERT INTO gyee_test.equipmentdaydetailed (id, windturbineid, projectid, lineid, windpowerstationid, " +
  61. " recorddate, genecapacity, therogenecapacity, speed, daynhwhssdl1, daynhwhssdl2,daynhgzssdl1,daynhgzssdl2, " +
  62. " daynhxdssdl1, daynhxdssdl2,daynhqfdl1, daynhqfdl12,daynhqfdl3,daynhqfdl4, daynhcfdl1,daynhcfdl2 ) " +
  63. " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ";
  64. List<Object[]> args = transformFlowCarReportDetailedBoToObjects(list);
  65. int fromIndex = 0; int toIndex = BATCH_SIZE;
  66. while (fromIndex != args.size()) {
  67. if (toIndex > args.size()) {
  68. toIndex = args.size();
  69. }
  70. jdbcTemplate.batchUpdate(sql,args.subList(fromIndex, toIndex));
  71. fromIndex = toIndex;
  72. toIndex += BATCH_SIZE;
  73. if (toIndex > args.size())
  74. toIndex = args.size();
  75. }
  76. }
  77. private List<Object[]> transformFlowCarReportDetailedBoToObjects(List<Equipmentdaydetailed> equipmentdayinfoList) {
  78. List<Object[]> list = new ArrayList<>();
  79. Object[] object = null;
  80. for(Equipmentdaydetailed equipmentdayinfo :equipmentdayinfoList){
  81. object = new Object[]{
  82. equipmentdayinfo.getId(),
  83. equipmentdayinfo.getWindturbineid(),
  84. equipmentdayinfo.getProjectid(),
  85. equipmentdayinfo.getLineid(),
  86. equipmentdayinfo.getWindpowerstationid(),
  87. equipmentdayinfo.getRecorddate(),
  88. equipmentdayinfo.getGenecapacity(),
  89. equipmentdayinfo.getTherogenecapacity(),
  90. equipmentdayinfo.getSpeed(),
  91. equipmentdayinfo.getDaynhwhssdl1(),
  92. equipmentdayinfo.getDaynhwhssdl2(),
  93. equipmentdayinfo.getDaynhgzssdl1(),
  94. equipmentdayinfo.getDaynhgzssdl2(),
  95. equipmentdayinfo.getDaynhxdssdl1(),
  96. equipmentdayinfo.getDaynhxdssdl2(),
  97. equipmentdayinfo.getDaynhqfdl1(),
  98. equipmentdayinfo.getDaynhqfdl2(),
  99. equipmentdayinfo.getDaynhcfdl1(),
  100. equipmentdayinfo.getDaynhcfdl2()
  101. };
  102. list.add(object);
  103. }
  104. return list ;
  105. }
  106. }