package com.yem.ensign.common;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.utils.StringUtils;
import kd.bos.entity.datamodel.IDataModel;

/**
 * 业务对象工具类
 */
public class DynamicObjUtils {
	/**
	 * 重置业务对象分录序号
	 * @param obj 业务对象
	 * @param entryNameMap 分录标识集合
	 */
	public static void resetSeq(DynamicObject obj, Map<String, String> entryNameMap) {
		Set<String> keys = entryNameMap.keySet();
		Iterator<String> arg2 = keys.iterator();

		while (arg2.hasNext()) {
			String entryName = (String) arg2.next();
			DynamicObjectCollection entrys = obj.getDynamicObjectCollection(entryName);

			for (int i = 0; entrys != null && i < entrys.size(); ++i) {
				DynamicObject entry = (DynamicObject) entrys.get(i);
				entry.set("seq", Integer.valueOf(i + 1));
				if (StringUtils.isNotEmpty((CharSequence) entryNameMap.get(entryName))) {
					DynamicObjectCollection hddEntrys = entry
							.getDynamicObjectCollection((String) entryNameMap.get(entryName));

					for (int j = 0; hddEntrys != null && j < hddEntrys.size(); ++j) {
						DynamicObject hddEntry = (DynamicObject) hddEntrys.get(j);
						hddEntry.set("seq", Integer.valueOf(j + 1));
					}
				}
			}
		}

	}

	/**
	 * 重置业务对象分录序号
	 * @param obj 数据模型对象
	 * @param entryNameMap 分录标识集合
	 */
	public static void resetSeq(IDataModel obj, Map<String, String> entryNameMap) {
		Set<String> keys = entryNameMap.keySet();
		Iterator<String> arg2 = keys.iterator();

		while (arg2.hasNext()) {
			String entryName = (String) arg2.next();
			DynamicObjectCollection entrys = obj.getEntryEntity(entryName);

			for (int i = 0; entrys != null && i < entrys.size(); ++i) {
				DynamicObject entry = (DynamicObject) entrys.get(i);
				entry.set("seq", Integer.valueOf(i + 1));
				if (StringUtils.isNotEmpty((CharSequence) entryNameMap.get(entryName))) {
					DynamicObjectCollection hddEntrys = entry
							.getDynamicObjectCollection((String) entryNameMap.get(entryName));

					for (int j = 0; hddEntrys != null && j < hddEntrys.size(); ++j) {
						DynamicObject hddEntry = (DynamicObject) hddEntrys.get(j);
						hddEntry.set("seq", Integer.valueOf(j + 1));
					}
				}
			}
		}

	}
}
