package cn.com.qmth.stmms.biz.utils;

import java.util.List;

public abstract class BatchUpdateDataUtil<P> {
	/**
	 * @param dataList 参数集合
	 * @param batchSize 每批数量
	 */
	public final int setDataForBatch(List<P> dataList, int batchSize) {
		if (dataList == null || dataList.size() == 0) {
			return 0;
		}
		int total=0;
		if (dataList.size() <= batchSize) {
			total=total+updateData(dataList);
		} else {
			int size = dataList.size();
			int len = batchSize;
			int count = (size + len - 1) / len;

			for (int i = 0; i < count; i++) {
				List<P> subList = dataList.subList(i * len, ((i + 1) * len > size ? size : len * (i + 1)));
				total=total+updateData(subList);
			}
		}
		return total;
	}

	protected abstract int updateData(List<P> dataList);
}
