1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
| static class MySqlC { AtomicBoolean free = new AtomicBoolean(true); private SqlSession session; private BbItemInfoMapper mapper;
public MySqlC(SqlSession session) { this.session = session; this.mapper = session.getMapper(BbItemInfoMapper.class); } }
private final ArrayList<MySqlC> connectionLists = new ArrayList<>();
private MySqlC getFreeConnection() { for (MySqlC connectionList : connectionLists) { if (connectionList.free.get()) { return connectionList; } } return null; }
public void importPlu(String filePath, Long batchId) { try { File file = new File(filePath); CsvReader reader = CsvUtil.getReader(); //设置文件读取的分隔符 reader.setFieldSeparator('|'); String charSet = FileUtils.getCharSet(new BufferedInputStream(new FileInputStream(file))); //根据特定的编码方式读取File的内容 CsvData data = reader.read(file, Charset.forName(charSet)); if (data == null) { log.error("read csvData got null"); return; } ArrayList<BbItemInfo> nInfoList = new ArrayList<>(); int insertCount = 0; CsvRow firstRow; String storeNum = ""; int batchCount = 0; CountDownLatch countDownLatch = new CountDownLatch(data.getRowCount());
connectionLists.add(new MySqlC(sqlSessionFactory.openSession(ExecutorType.BATCH, false))); connectionLists.add(new MySqlC(sqlSessionFactory.openSession(ExecutorType.BATCH, false))); connectionLists.add(new MySqlC(sqlSessionFactory.openSession(ExecutorType.BATCH, false))); connectionLists.add(new MySqlC(sqlSessionFactory.openSession(ExecutorType.BATCH, false)));
// SqlSession sqlsession = sqlSessionFactory.openSession(ExecutorType.BATCH, false); // BbItemInfoMapper bbItemInfoMapper = sqlsession.getMapper(BbItemInfoMapper.class);
for (int i = 0; i < data.getRowCount(); i++) { if (i == 0) { firstRow = data.getRow(i); log.debug(JSON.toJSONString(firstRow)); storeNum = firstRow.get(6); countDownLatch.countDown(); } else { CsvRow row = data.getRow(i); if (row == null) { log.warn("line 0 is null "); } else { log.debug(JSON.toJSONString(row)); BbItemInfo info = new BbItemInfo(); info.setPlu_btch_nbr(row.get(0)); info.setItm_id(StringUtils.parseStr2Int(row.get(2))); info.setStr_hier_id(StringUtils.parseStr2Int(row.get(3))); info.setDspl_descr(row.get(6)); info.setRcpt_descr(row.get(8)); info.setTaxability_cd(row.get(9)); info.setSls_auth_fg(row.get(19)); info.setRtl_prc(row.get(23)); info.setTax_rate1_fg(row.get(47)); info.setTax_rate3_fg(row.get(48)); info.setTax_rate4_fg(row.get(49)); info.setTax_rate5_fg(row.get(50)); info.setTax_rate6_fg(row.get(51)); info.setTax_rate7_fg(row.get(52)); info.setTax_rate8_fg(row.get(53)); info.setSubdep_id(StringUtils.parseStr2Int(row.get(61))); info.setRcpt_descr1(row.get(78)); info.setRcpt_descr2(row.get(79)); info.setRcpt_descr3(row.get(80)); info.setIntrnl_id(StringUtils.parseStr2Int(row.get(85))); info.setBns_by_descr(row.get(108)); info.setComp_prc(row.get(110)); info.setVnd_itm_id2(row.get(137)); info.setBatch_id(batchId); info.setStatus_id(0); info.setAdd_time(new Date()); info.setOp_code(StringUtils.parseStr2Int(row.get(1))); info.setStore_num(storeNum); batchCount++; nInfoList.add(info); } } if (batchCount > 999) { long startSave = System.currentTimeMillis();
int finalBatchCount = batchCount; final ArrayList<BbItemInfo> insertList = nInfoList; PriorityThreadFactory.getPool().execute(new PriorityThread() { @Override public void run() { MySqlC c; while ((c = getFreeConnection()) != null) { c.free.set(false); commit(insertList, c.session, c.mapper); c.free.set(true); for (int j = 0; j < finalBatchCount; j++) { countDownLatch.countDown(); } break; } }
@Override public String getThreadName() { return "t" + System.currentTimeMillis(); } });
log.info("保存耗费 :" + (System.currentTimeMillis() - startSave) + "ms"); insertCount += batchCount; batchCount = 0; nInfoList = new ArrayList<>(); } }
int finalBatchCount = batchCount; final ArrayList<BbItemInfo> insertList = nInfoList; PriorityThreadFactory.getPool().execute(new PriorityThread() { @Override public void run() { MySqlC c; while ((c = getFreeConnection()) != null) { commit(insertList, c.session, c.mapper); for (int j = 0; j < finalBatchCount; j++) { countDownLatch.countDown(); } break; } }
@Override public String getThreadName() { return "t" + System.currentTimeMillis(); } });
insertCount += batchCount;
countDownLatch.await(); for (int i = 0; i < connectionLists.size(); i++) { connectionLists.get(i).session.close(); }
log.info(String.format("\n导入itemInfo(" + filePath + "), 单据数:%s, 导入数量:%s ", data.getRowCount(), insertCount)); } catch (Exception e) { e.printStackTrace(); log.error("导入itemInfo", e); } }
public synchronized void commit(List<BbItemInfo> bbItemInfos, SqlSession sqlsession, BbItemInfoMapper bbItemInfoMapper) { for (BbItemInfo bbItemInfo : bbItemInfos) { bbItemInfoMapper.insert(bbItemInfo); } sqlsession.commit(); sqlsession.clearCache(); }
|