package com.gyee.benchmarkinghive.util; import com.gyee.benchmarkinghive.model.auto.Equipmentdaydetailed; import com.gyee.benchmarkinghive.model.auto.Equipmentdayinfo; import com.gyee.benchmarkinghive.service.GenericService; import org.springframework.jdbc.core.JdbcOperations; import java.util.ArrayList; import java.util.List; /** * @ClassName : BatchUpdateUtils * @Author : xieshengjie * @Date: 2021/6/15 23:47 * @Description : 批量保存工具类 */ public class BatchUpdateUtils extends GenericService { private static final Integer BATCH_SIZE = 100; private JdbcOperations jdbcTemplate; public void batchEquipmentinfoInsert(List list) { String sql =" INSERT INTO gyee_test.equipmentdayinfo (id, windturbineid, projectid, lineid, windpowerstationid, " + " recorddate, genecapacity, therogenecapacity, speed, daynhwhssdl, daynhgzssdl, " + " daynhxdssdl, daynhqfdl, daynhcfdl) " + " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) "; List args = transformFlowCarReportDayBoToObjects(list); int fromIndex = 0; int toIndex = BATCH_SIZE; while (fromIndex != args.size()) { if (toIndex > args.size()) { toIndex = args.size(); } jdbcTemplate.batchUpdate(sql,args.subList(fromIndex, toIndex)); fromIndex = toIndex; toIndex += BATCH_SIZE; if (toIndex > args.size()) toIndex = args.size(); } } private List transformFlowCarReportDayBoToObjects(List equipmentdayinfoList) { List list = new ArrayList<>(); Object[] object = null; for(Equipmentdayinfo equipmentdayinfo :equipmentdayinfoList){ object = new Object[]{ equipmentdayinfo.getId(), equipmentdayinfo.getWindturbineid(), equipmentdayinfo.getProjectid(), equipmentdayinfo.getLineid(), equipmentdayinfo.getWindpowerstationid(), equipmentdayinfo.getRecorddate(), equipmentdayinfo.getGenecapacity(), equipmentdayinfo.getTherogenecapacity(), equipmentdayinfo.getSpeed(), equipmentdayinfo.getDaynhwhssdl(), equipmentdayinfo.getDaynhgzssdl(), equipmentdayinfo.getDaynhxdssdl(), equipmentdayinfo.getDaynhqfdl(), equipmentdayinfo.getDaynhcfdl() }; list.add(object); } return list ; } public void batchEquipmentdetailedInsert(List list) { String sql =" INSERT INTO gyee_test.equipmentdaydetailed (id, windturbineid, projectid, lineid, windpowerstationid, " + " recorddate, genecapacity, therogenecapacity, speed, daynhwhssdl1, daynhwhssdl2,daynhgzssdl1,daynhgzssdl2, " + " daynhxdssdl1, daynhxdssdl2,daynhqfdl1, daynhqfdl12,daynhqfdl3,daynhqfdl4, daynhcfdl1,daynhcfdl2 ) " + " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) "; List args = transformFlowCarReportDetailedBoToObjects(list); int fromIndex = 0; int toIndex = BATCH_SIZE; while (fromIndex != args.size()) { if (toIndex > args.size()) { toIndex = args.size(); } jdbcTemplate.batchUpdate(sql,args.subList(fromIndex, toIndex)); fromIndex = toIndex; toIndex += BATCH_SIZE; if (toIndex > args.size()) toIndex = args.size(); } } private List transformFlowCarReportDetailedBoToObjects(List equipmentdayinfoList) { List list = new ArrayList<>(); Object[] object = null; for(Equipmentdaydetailed equipmentdayinfo :equipmentdayinfoList){ object = new Object[]{ equipmentdayinfo.getId(), equipmentdayinfo.getWindturbineid(), equipmentdayinfo.getProjectid(), equipmentdayinfo.getLineid(), equipmentdayinfo.getWindpowerstationid(), equipmentdayinfo.getRecorddate(), equipmentdayinfo.getGenecapacity(), equipmentdayinfo.getTherogenecapacity(), equipmentdayinfo.getSpeed(), equipmentdayinfo.getDaynhwhssdl1(), equipmentdayinfo.getDaynhwhssdl2(), equipmentdayinfo.getDaynhgzssdl1(), equipmentdayinfo.getDaynhgzssdl2(), equipmentdayinfo.getDaynhxdssdl1(), equipmentdayinfo.getDaynhxdssdl2(), equipmentdayinfo.getDaynhqfdl1(), equipmentdayinfo.getDaynhqfdl2(), equipmentdayinfo.getDaynhcfdl1(), equipmentdayinfo.getDaynhcfdl2() }; list.add(object); } return list ; } }