package com.yem.ensign.weboffice.dynamic;

import java.io.InputStream;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.EventObject;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.yem.ensign.common.BigDecimalUtils;
import com.yem.ensign.common.ConstantUtils;
import com.yem.ensign.weboffice.WebOfficeComUtils;
import kd.bos.algo.DataSet;
import kd.bos.algo.Row;
import kd.bos.cache.CacheFactory;
import kd.bos.cache.TempFileCache;
import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.entity.LocaleString;
import kd.bos.db.DB;
import kd.bos.entity.EntityMetadataCache;
import kd.bos.entity.EntityTypeUtil;
import kd.bos.entity.EntryEntityDto;
import kd.bos.entity.MainEntityType;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.entity.datamodel.events.ChangeData;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.fileservice.FileItem;
import kd.bos.fileservice.FileService;
import kd.bos.fileservice.FileServiceFactory;
import kd.bos.form.CloseCallBack;
import kd.bos.form.FormShowParameter;
import kd.bos.form.ShowType;
import kd.bos.form.control.Button;
import kd.bos.form.control.Control;
import kd.bos.form.control.EntryGrid;
import kd.bos.form.control.SplitContainer;
import kd.bos.form.control.SplitDirection;
import kd.bos.form.control.WebOffice;
import kd.bos.form.control.events.ItemClickListener;
import kd.bos.form.control.events.RowClickEvent;
import kd.bos.form.control.events.RowClickEventListener;
import kd.bos.form.control.events.UploadEvent;
import kd.bos.form.control.events.UploadListener;
import kd.bos.form.control.events.webOffice.WebOfficeDataListener;
import kd.bos.form.control.events.webOffice.event.WebOfficeBookmarkCheckEvent;
import kd.bos.form.control.events.webOffice.event.WebOfficeBookmarkEvent;
import kd.bos.form.control.events.webOffice.event.WebOfficeBookmarkValueEvent;
import kd.bos.form.control.events.webOffice.event.WebOfficeCheckboxEvent;
import kd.bos.form.control.events.webOffice.event.WebOfficeDocChangeEvent;
import kd.bos.form.control.events.webOffice.event.WebOfficeIsFieldReviseEvent;
import kd.bos.form.control.events.webOffice.event.WebOfficeOpenStatusEvent;
import kd.bos.form.control.model.WebOfficeMark;
import kd.bos.form.events.ClosedCallBackEvent;
import kd.bos.form.field.ComboEdit;
import kd.bos.form.field.ComboItem;
import kd.bos.form.field.TextEdit;
import kd.bos.form.field.events.BeforeF7SelectEvent;
import kd.bos.form.field.events.BeforeF7SelectListener;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.form.plugin.IFormPlugin;
import kd.bos.orm.ORM;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.AttachmentServiceHelper;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import kd.bos.servicehelper.user.UserServiceHelper;
import kd.bos.util.StringUtils;

/**
 * webOffice 表单插件
 *
 * @author hdd
 * @date 2020-12-01 下午2:53:04
 */
public class TPWebofficeNew extends AbstractFormPlugin implements ItemClickListener, WebOfficeDataListener,
		BeforeF7SelectListener, RowClickEventListener, UploadListener {

	// 界面标识
	private final String yem_buttonap = "yem_buttonap";// 重新获取书签
	private final String yem_buttonap1 = "yem_buttonap1";// 书签赋值
	private final String yem_buttonap2 = "yem_buttonap2";// 保存关联明细

	private final String yem_entryentity = "yem_entryentity";// 关联明细
	private final String yem_bookmark = "yem_bookmark";// 书签标识
	private final String yem_fieldtype = "yem_fieldtype";// 数据源类型
	private final String yem_dyquery = "yem_dyquery";// 动态数据源选择
	private final String yem_dyqueryfieldcn = "yem_dyqueryfieldcn";// 动态数据源字段
	private final String yem_dyqueryfield = "yem_dyqueryfield";// 动态数据源字段
	private final String yem_fieldchoice = "yem_fieldchoice";// 实体数据源字段
	private final String yem_field = "yem_field";// 字段标识
	private final String yem_fieldname = "yem_fieldname";// 字段名称
	private final String yem_entity = "yem_entity";// 分录标识
	private final String yem_entityname = "yem_entityname";// 分录名称
	private final String yem_bill = "yem_bill";// 单据标识
	private final String yem_billname = "yem_billname";// 单据名称
	private final String yem_isentity = "yem_isentity";// 是否分录标识
	private final String yem_parententry = "yem_parententry";// 关联分录书签标识
	private final String yem_colindex = "yem_colindex";// 分录列序号
	private final String yem_datatype = "yem_datatype";// 数据类型
	private final String yem_precision = "yem_precision";// 精度
	private final String yem_invoiceseq = "yem_invoiceseq";// 发票数据源序号

	private final String yem_fieldtype2 = "yem_fieldtype2";// 数据源类型
	private final String yem_dyquery2 = "yem_dyquery2";// 动态数据源选择
	private final String yem_dyqueryfieldcn2 = "yem_dyqueryfieldcn2";// 动态数据源字段
	private final String yem_dyqueryfield2 = "yem_dyqueryfield2";// 动态数据源字段
	private final String yem_fieldchoice2 = "yem_fieldchoice2";// 实体数据源字段
	private final String yem_field2 = "yem_field2";// 字段标识
	private final String yem_fieldname2 = "yem_fieldname2";// 字段名称
	private final String yem_entity2 = "yem_entity2";// 分录标识
	private final String yem_entityname2 = "yem_entityname2";// 分录名称
	private final String yem_bill2 = "yem_bill2";// 单据标识
	private final String yem_billname2 = "yem_billname2";// 单据名称
	private final String yem_isentity2 = "yem_isentity2";// 是否分录标识
	private final String yem_parententry2 = "yem_parententry2";// 关联分录书签标识
	private final String yem_colindex2 = "yem_colindex2";// 分录列序号
	private final String yem_datatype2 = "yem_datatype2";// 数据类型
	private final String yem_precision2 = "yem_precision2";// 精度
	private final String yem_invoiceseq2 = "yem_invoiceseq2";// 发票数据源序号
	private final String yem_buttonap3 = "yem_buttonap3";// 保存
	private final String yem_buttonap4 = "yem_buttonap4";// 保存附件
	private final String yem_webofficeap = "yem_webofficeap";// WebOffice控件标识

	public void registerListener(EventObject e) {
		super.registerListener(e);
		Button yem_buttonap = this.getView().getControl(this.yem_buttonap);
		if (null != yem_buttonap) {
			this.addClickListeners(new String[] { this.yem_buttonap });
		}
		Button yem_buttonap1 = this.getView().getControl(this.yem_buttonap1);
		if (null != yem_buttonap1) {
			this.addClickListeners(new String[] { this.yem_buttonap1 });
		}
		Button yem_buttonap2 = this.getView().getControl(this.yem_buttonap2);
		if (null != yem_buttonap2) {
			this.addClickListeners(new String[] { this.yem_buttonap2 });
		}
		WebOffice webOffice = getControl(this.yem_webofficeap);
		webOffice.addDataListener(this);
		webOffice.addUploadListener(this);

		TextEdit yem_dyqueryfieldcn = this.getView().getControl(this.yem_dyqueryfieldcn2);
		yem_dyqueryfieldcn.addClickListener(this);

		Button yem_buttonap3 = this.getView().getControl(this.yem_buttonap3);
		if (null != yem_buttonap3) {
			this.addClickListeners(new String[] { this.yem_buttonap3 });
		}

		Button yem_buttonap4 = this.getView().getControl(this.yem_buttonap4);
		if (null != yem_buttonap4) {
			this.addClickListeners(new String[] { this.yem_buttonap4 });
		}

		EntryGrid yem_entryentity = this.getView().getControl(this.yem_entryentity);
		yem_entryentity.addRowClickListener(this);
	}

	@Override
	public void click(EventObject evt) {
		super.click(evt);
		IDataModel model = this.getModel();
		FormShowParameter showParameter = this.getView().getFormShowParameter();
		Map<String, Object> cusMap = showParameter.getCustomParams();
		String yem_bill = cusMap.containsKey("yem_bill")?cusMap.get("yem_bill").toString():"";
		String yem_billId = cusMap.containsKey("yem_billId")?cusMap.get("yem_billId").toString():"";
		String yem_modelid = cusMap.containsKey("yem_modelid")?cusMap.get("yem_modelid").toString():"";
		String fileName = cusMap.containsKey("fileName")?cusMap.get("fileName").toString():"";
		Control source = (Control) evt.getSource();
		String key = source.getKey();
		if (this.yem_buttonap.equals(key)) {
			WebOffice webOffice = getControl(this.yem_webofficeap);
			webOffice.getAllBookmarks();
		} else if (this.yem_buttonap1.equals(key)) {
			boolean isSuccess = verification(model);
			if (isSuccess) {
				setBookmarkData(model, yem_billId, yem_bill);
			}
		} else if (this.yem_buttonap2.equals(key)) {
			boolean isSuccess = verification(model);
			if (isSuccess) {
				saveBookmarkRelation(model, yem_modelid);
			}
		} else if (this.yem_dyqueryfieldcn2.equals(key)) {
			String yem_fieldtype = model.getValue(this.yem_fieldtype2) == null ? ""
					: model.getValue(this.yem_fieldtype2).toString();
			if (yem_fieldtype == null || "".equals(yem_fieldtype)) {
				this.getView().showErrorNotification("请先选择数据源类型！");
			} else if (!"B".equals(yem_fieldtype)) {
				this.getView().showErrorNotification("数据源类型不是动态数据源，无法选择字段！");
			} else {
				DynamicObject yem_dyquery = (DynamicObject) model.getValue(this.yem_dyquery2);
				if (yem_dyquery != null) {
					FormShowParameter showParameter2 = new FormShowParameter();
					showParameter2.setFormId("yem_dyquerydatasouchoice");
					showParameter2.getOpenStyle().setShowType(ShowType.Modal);
					Map<String, Object> mapParam = new HashMap<String, Object>();
					mapParam.put("dyqueryId", yem_dyquery.getPkValue());
					showParameter2.setCustomParams(mapParam);
					CloseCallBack closeCallBack = new CloseCallBack((IFormPlugin) this, "dyquerydatasouchoice");
					showParameter2.setCloseCallBack(closeCallBack);
					this.getView().showForm(showParameter2);
				} else {
					this.getView().showErrorNotification("请先选择动态数据源！");
				}
			}
		} else if (this.yem_buttonap3.equals(key)) {
			int rowIndex = this.getModel().getEntryCurrentRowIndex(this.yem_entryentity);
			this.getModel().setValue(this.yem_fieldtype, this.getModel().getValue(this.yem_fieldtype2), rowIndex);
			this.getModel().setValue(this.yem_dyquery, this.getModel().getValue(this.yem_dyquery2), rowIndex);
			this.getModel().setValue(this.yem_dyqueryfieldcn, this.getModel().getValue(this.yem_dyqueryfieldcn2),
					rowIndex);
			this.getModel().setValue(this.yem_dyqueryfield, this.getModel().getValue(this.yem_dyqueryfield2), rowIndex);
			this.getModel().setValue(this.yem_fieldchoice, this.getModel().getValue(this.yem_fieldchoice2), rowIndex);
			this.getModel().setValue(this.yem_field, this.getModel().getValue(this.yem_field2), rowIndex);
			this.getModel().setValue(this.yem_fieldname, this.getModel().getValue(this.yem_fieldname2), rowIndex);
			this.getModel().setValue(this.yem_entity, this.getModel().getValue(this.yem_entity2), rowIndex);
			this.getModel().setValue(this.yem_entityname, this.getModel().getValue(this.yem_entityname2), rowIndex);
			this.getModel().setValue(this.yem_bill, this.getModel().getValue(this.yem_bill2), rowIndex);
			this.getModel().setValue(this.yem_billname, this.getModel().getValue(this.yem_billname2), rowIndex);
			this.getModel().setValue(this.yem_isentity, this.getModel().getValue(this.yem_isentity2), rowIndex);
			this.getModel().setValue(this.yem_parententry, this.getModel().getValue(this.yem_parententry2), rowIndex);
			this.getModel().setValue(this.yem_colindex, this.getModel().getValue(this.yem_colindex2), rowIndex);
			this.getModel().setValue(this.yem_datatype, this.getModel().getValue(this.yem_datatype2), rowIndex);
			this.getModel().setValue(this.yem_precision, this.getModel().getValue(this.yem_precision2), rowIndex);
			this.getModel().setValue(this.yem_invoiceseq, this.getModel().getValue(this.yem_invoiceseq2), rowIndex);
		} else if (this.yem_buttonap4.equals(key)) {
			WebOffice webOffice = getControl(this.yem_webofficeap);
			fileName = fileName.substring(0, fileName.lastIndexOf("."));
			if(cusMap.containsKey("invoiceNo")) {
				String invoiceNo = cusMap.get("invoiceNo").toString();
				String c = invoiceNo.replaceAll("/", "-");
				webOffice.save(c + "_" + fileName+".docx");
			}else {
				webOffice.save(fileName + "_" + new Date().getTime()+".docx");
			}
		}
	}

	@Override
	public void afterUpload(UploadEvent evt) {
		Object[] urls = evt.getUrls();
		if (null == urls || urls.length == 0) {
			getView().showErrorNotification("请上传文档");
			return;
		}
		try {
			FormShowParameter showParameter = this.getView().getFormShowParameter();
			Map<String, Object> cusMap = showParameter.getCustomParams();
			String yem_bill = cusMap.get("yem_bill").toString();
			String yem_billId = cusMap.get("yem_billId").toString();
			String attachmentpanel = cusMap.get("attachmentpanel").toString();
			String yem_fileName = cusMap.get("fileName").toString();
			boolean isInvoice = false;//是否发票打印
			if(cusMap.containsKey("isInvoice")) {
				isInvoice = true;
			}
			yem_fileName = yem_fileName.substring(0, yem_fileName.lastIndexOf("."));
			Object urlData = urls[0];
			String filePath = "";
			String fileName = "";
			String url = "";
			StringBuffer msg = new StringBuffer();
			if (urlData instanceof String) {
				filePath = urlData.toString();
				fileName = filePath.substring(filePath.lastIndexOf("/"));
				msg.append("name1：" + fileName).append("\n");
				String[] files = fileName.split("\\.");
				String tFileName = files.length == 2 ? files[0] : UUID.randomUUID().toString();
				tFileName = URLEncoder.encode(tFileName, "utf-8");
				String tExtend = files.length == 2 ? files[1] : "docx";
				fileName = tFileName + "." + tExtend;
				url = filePath;
			} else if (urlData instanceof Map) {
				filePath = ((Map<String, Object>) urlData).get("url").toString();
				filePath = filePath.replaceAll("cmnmovevpn.minmetals.com.cn/ierp", "cmnerp.minmetals.com.cn");
				if(cusMap.containsKey("invoiceNo")) {
					String invoiceNo = cusMap.get("invoiceNo").toString();
					if (invoiceNo.contains("/")) {
						String c = invoiceNo.replaceAll("/", "-");
						fileName = c + "_" + yem_fileName;
					} else {
						fileName = invoiceNo + "_" + yem_fileName;
					}
				}else {
					fileName = yem_fileName + "_" + new Date().getTime();
				}
				msg.append("name2：" + fileName).append("\n");
				url = filePath;
			}

			if (url.contains("tempfile")) {
				// 文件上传服务器
				TempFileCache cache = CacheFactory.getCommonCacheFactory().getTempFileCache();
				InputStream in = cache.getInputStream(url);
				int size = in.available();
				FileService service = FileServiceFactory.getAttachmentFileService();
				String suffix = ".docx";
				String path = "/" + fileName + suffix;
				fileName = URLEncoder.encode(fileName, "utf-8");
				msg.append("name3：" + fileName).append("\n");
				fileName = fileName + suffix;
				FileItem fileItem = new FileItem(fileName, path, in);
				fileItem.setCreateNewFileWhenExists(true);
				String turl = service.upload(fileItem);
				// 文件保存至附件
				ORM orm = ORM.create();
				DynamicObject dynamicObject = orm.newDynamicObject("bos_attachment");
				dynamicObject.set("id", Long.valueOf(orm.genLongId("bos_attachment")));
				dynamicObject.set("FNUMBER", "rc-upload-" + new Date().getTime());
				dynamicObject.set("FBillType", yem_bill);
				dynamicObject.set("FInterID", yem_billId);
				Date today = new Date();
				dynamicObject.set("FModifyTime", today);
				dynamicObject.set("fcreatetime", today);
				fileName = URLDecoder.decode(fileName, "utf-8");
				msg.append("name4：" + fileName).append("\n");
				dynamicObject.set("FaliasFileName", fileName);
				dynamicObject.set("FAttachmentName", fileName);
				dynamicObject.set("FExtName", "docx");
				dynamicObject.set("FATTACHMENTSIZE", size);
				dynamicObject.set("FCREATEMEN", RequestContext.get().getUserId());
				dynamicObject.set("fattachmentpanel", attachmentpanel);
				dynamicObject.set("fdescription", "");
				dynamicObject.set("FFileId", turl);
				Object[] obj = SaveServiceHelper.save(new DynamicObject[] { dynamicObject });

				DynamicObject bill = BusinessDataServiceHelper.loadSingle(yem_billId, yem_bill);
				SaveServiceHelper.save(new DynamicObject[] { bill });
				this.getView().showMessage("保存成功！");
			}
		} catch (Exception e) {
			getView().showErrorNotification("保存失败："+ e.getMessage());
			e.printStackTrace();
		}
	}

	/**
	 * 根据关联明细分录数据赋值
	 *
	 * @param model
	 * @param yem_billId
	 *            单据内码ID
	 * @param yem_bill
	 *            单据标识
	 */
	public void setBookmarkData(IDataModel model, String yem_billId, String yem_bill) {
		DynamicObjectCollection entryentitys = model.getEntryEntity(this.yem_entryentity);
		if (entryentitys != null && entryentitys.size() > 0) {
			if (!verification(this.getModel())) {
				return;
			}
			int size = entryentitys.size();
			DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingle(yem_billId, yem_bill);
			// 获取实体分录标识
			Map entityKeyMap = getEntityKeyMap(yem_bill);
			// 获取分录书签集合
			Map entityBookMarkMap = getEntityBookMarkMap(size, entryentitys);
			// 获取分录字段书签排序集合
			Map entityFieldBookMarkSortMap = getEntityFieldBookMarkSortMap(size, entryentitys);
			// 获取动态数据源ID集合List
			List dyqueryIdsList = getDyqueryIdsList(size, entryentitys);
			// 获取动态数据源分录级次集合
			Map dyLevelMap = getDyLevleMap(dyqueryIdsList, yem_billId);
			// 获取动态数据源对应值
			Map dyValsMap = getDyValsMap(dyqueryIdsList, yem_billId);

			// 获取非动态数据源单据表头书签对应值
			List billMarks = getBillMarks(yem_bill, dynamicObject, size, entryentitys);
			// 获取非动态数据源一级单据体书签对应值
			Map oneLeveLEntityValMap = getOneLeveLEntityValMap(entityKeyMap, entityBookMarkMap, size, entryentitys,
					dynamicObject);
			// 获取非动态数据源二级单据体书签对应值
			Map twoLeveLEntityValMap = getTwoLeveLEntityValMap(entityKeyMap, entityBookMarkMap, size, entryentitys,
					dynamicObject);

			// 获取动态数据源单据表头书签对应值
			List dyBillMarks = getDyBillMarks(dyLevelMap, dyValsMap, size, entryentitys);
			// 获取动态数据源一级单据体书签对应值
			oneLeveLEntityValMap = getDyOneLeveLEntityValMap(entityBookMarkMap, size, entryentitys, dyLevelMap,
					dyValsMap, oneLeveLEntityValMap);
			// 获取动态数据源二级单据体书签对应值
			twoLeveLEntityValMap = getDyTwoLeveLEntityValMap(entityBookMarkMap, size, entryentitys, dyLevelMap,
					dyValsMap, twoLeveLEntityValMap);

			// 书签赋值
			setData(billMarks, dyBillMarks, oneLeveLEntityValMap, twoLeveLEntityValMap, entityBookMarkMap,
					entityFieldBookMarkSortMap);
		}
	}

	/**
	 * 获取实体分录标识
	 *
	 * @param yem_bill
	 *            单据标识
	 * @return
	 */
	private Map getEntityKeyMap(String yem_bill) {
		// 获取分录、子分录标识集合
		Map entityKeyMap = new HashMap();
		MainEntityType entityType = EntityMetadataCache.getDataEntityType(yem_bill);
		EntityTypeUtil helper = new EntityTypeUtil();
		List entityCols = helper.getEntryEntities(entityType);
		Iterator entityModel = entityCols.iterator();
		while (entityModel.hasNext()) {
			EntryEntityDto indexs = (EntryEntityDto) entityModel.next();
			String key = indexs.getKey();
			String fieldName = indexs.getName().toString();
			String parentKey = indexs.getParentKey();
			if (parentKey == null || parentKey.trim().equals("")) {
				continue;
			}
			entityKeyMap.put(key, parentKey);
		}
		return entityKeyMap;
	}

	/**
	 * 获取分录书签集合
	 *
	 * @param size
	 * @param entryentitys
	 */
	private Map getEntityBookMarkMap(int size, DynamicObjectCollection entryentitys) {
		Map entityBookMarkMap = new HashMap();
		for (int i = 0; i < size; i++) {
			DynamicObject entryentity = entryentitys.get(i);
			boolean entry_yem_isentity = entryentity.getBoolean(this.yem_isentity);// 是否分录标识
			String entry_yem_entity = entryentity.getString(this.yem_entity);// 分录标识
			String entry_yem_bookmark = entryentity.getString(this.yem_bookmark);// 书签标识
			if (entry_yem_isentity) {
				entityBookMarkMap.put(entry_yem_bookmark, entry_yem_entity);
			}
		}
		return entityBookMarkMap;
	}

	/**
	 * 获取分录字段书签排序集合
	 *
	 * @param size
	 * @param entryentitys
	 */
	private Map getEntityFieldBookMarkSortMap(int size, DynamicObjectCollection entryentitys) {
		Map entityFieldBookMarkSortMap = new HashMap();
		for (int i = 0; i < size; i++) {
			DynamicObject entryentity = entryentitys.get(i);
			boolean entry_yem_isentity = entryentity.getBoolean(this.yem_isentity);// 是否分录标识
			String entry_yem_bookmark = entryentity.getString(this.yem_bookmark);// 书签标识
			if (!entry_yem_isentity) {
				String entry_yem_parententry = entryentity.getString(this.yem_parententry);// 关联分录书签标识
				if (entry_yem_parententry == null || "".equals(entry_yem_parententry.trim())) {
					continue;
				}
				int yem_colindex = entryentity.getInt(this.yem_colindex);// 分录列序号
				if (entityFieldBookMarkSortMap.containsKey(entry_yem_parententry)) {
					Map sortMap = (Map) entityFieldBookMarkSortMap.get(entry_yem_parententry);
					sortMap.put(entry_yem_bookmark, yem_colindex - 1);
					entityFieldBookMarkSortMap.put(entry_yem_parententry, sortMap);
				} else {
					Map sortMap = new HashMap();
					sortMap.put(entry_yem_bookmark, yem_colindex - 1);
					entityFieldBookMarkSortMap.put(entry_yem_parententry, sortMap);
				}
			}
		}
		return entityFieldBookMarkSortMap;
	}

	/**
	 * 获取动态数据源ID集合List
	 *
	 * @param size
	 * @param entryentitys
	 */
	private List getDyqueryIdsList(int size, DynamicObjectCollection entryentitys) {
		Map dyqueryIdsMap = new HashMap();
		List dyqueryIdsList = new ArrayList();
		for (int i = 0; i < size; i++) {
			DynamicObject entryentity = entryentitys.get(i);
			boolean entry_yem_isentity = entryentity.getBoolean(this.yem_isentity);// 是否分录标识
			if (!entry_yem_isentity) {
				String entry_yem_fieldtype = entryentity.getString(this.yem_fieldtype);// 数据源类型
				if ("B".equals(entry_yem_fieldtype)) {
					DynamicObject entry_yem_dyquery = entryentity.getDynamicObject(this.yem_dyquery);// 动态数据源选择
					long dyqueryId = entry_yem_dyquery.getLong("id");
					if (!dyqueryIdsMap.containsKey(dyqueryId)) {
						dyqueryIdsList.add(dyqueryId);
						dyqueryIdsMap.put(dyqueryId, dyqueryId);
					}
				}
			}
		}
		return dyqueryIdsList;
	}

	/**
	 * 获取非动态数据源单据表头书签对应值
	 *
	 * @param dynamicObject
	 * @param size
	 * @param entryentitys
	 * @return
	 */
	private List getBillMarks(String yem_bill, DynamicObject dynamicObject, int size,
							  DynamicObjectCollection entryentitys) {
		List<WebOfficeMark> billMarks = new ArrayList<>();
		for (int i = 0; i < size; i++) {
			DynamicObject entryentity = entryentitys.get(i);
			boolean entry_yem_isentity = entryentity.getBoolean(this.yem_isentity);// 是否分录标识
			String entry_yem_field = entryentity.getString(this.yem_field);// 字段标识
			String entry_yem_bookmark = entryentity.getString(this.yem_bookmark);// 书签标识
			String entry_yem_parententry = entryentity.getString(this.yem_parententry);// 关联分录书签标识
			String yem_datatype = entryentity.getString(this.yem_datatype);// 数据类型
			int yem_precision = entryentity.getInt(this.yem_precision);// 精度
			if (!entry_yem_isentity) {
				String value = "";
				String[] field = entry_yem_field.split("\\.");
				if (yem_bill.equals(field[0]) && "".equals(entry_yem_parententry)) {
					if (field.length == 2) {
						if ("G".equals(yem_datatype)) {
							value = WebOfficeComUtils.objecttransformation(
									dynamicObject.get(field[field.length - 1] + "_tag"), yem_datatype, yem_precision);
						} else {
							value = WebOfficeComUtils.objecttransformation(dynamicObject.get(field[field.length - 1]),
									yem_datatype, yem_precision);
						}
					} else {
						DynamicObject valueObj = dynamicObject;
						for (int j = 1; j < field.length - 1; j++) {
							if (valueObj != null) {
								valueObj = valueObj.getDynamicObject(field[j]);
							}
						}
						if (valueObj != null) {
							if ("G".equals(yem_datatype)) {
								value = WebOfficeComUtils.objecttransformation(
										valueObj.get(field[field.length - 1] + "_tag"), yem_datatype, yem_precision);
							} else {
								value = WebOfficeComUtils.objecttransformation(valueObj.get(field[field.length - 1]),
										yem_datatype, yem_precision);
							}
						}
					}
					billMarks.add(new WebOfficeMark(entry_yem_bookmark, value));
				}
			}
		}
		return billMarks;
	}

	/**
	 * 获取动态数据源分录级次集合
	 *
	 * @param dyqueryIdsList
	 * @param yem_billId
	 */
	private Map getDyLevleMap(List dyqueryIdsList, String yem_billId) {
		Map dyLevelMap = new HashMap();
		if (dyqueryIdsList.size() > 0) {
			QFilter filter1 = new QFilter("id", "in", dyqueryIdsList);
			DynamicObject[] dyquerydatasous = BusinessDataServiceHelper.load("yem_dyquerydatasou",
					"id,yem_sqleditor,yem_entryentity.yem_textfield,yem_entryentity.yem_textfieldcn",
					new QFilter[] { filter1 });
			if (dyquerydatasous != null && dyquerydatasous.length > 0) {
				for (int i = 0; i < dyquerydatasous.length; i++) {
					DynamicObject dyquerydatasou = dyquerydatasous[i];
					String dyqueryId = dyquerydatasou.getString("id");
					DynamicObjectCollection dyEntryentitys = dyquerydatasou
							.getDynamicObjectCollection(this.yem_entryentity);
					List dyFields = new ArrayList();
					int level = 1;
					for (int j = 0; j < dyEntryentitys.size(); j++) {
						String yem_textfield = dyEntryentitys.get(j).getString("yem_textfield");
						dyFields.add(yem_textfield);
						if ("FDETAILID".equals(yem_textfield)) {
							level = 3;
						} else if (level == 1 && "FENTRYID".equals(yem_textfield)) {
							level = 2;
						}
					}
					dyLevelMap.put(dyqueryId, level);
				}
			}
		}
		return dyLevelMap;
	}

	/**
	 * 获取动态数据源对应值
	 *
	 * @param dyqueryIdsList
	 * @param yem_billId
	 * @return
	 */
	private Map getDyValsMap(List dyqueryIdsList, String yem_billId) {
		Map dyValsMap = new HashMap();
		if (dyqueryIdsList.size() > 0) {
			QFilter filter1 = new QFilter("id", "in", dyqueryIdsList);
			DynamicObject[] dyquerydatasous = BusinessDataServiceHelper.load("yem_dyquerydatasou",
					"id,yem_isinvoice,yem_sqleditor,yem_entryentity.yem_textfield,yem_entryentity.yem_textfieldcn",
					new QFilter[] { filter1 });
			if (dyquerydatasous != null && dyquerydatasous.length > 0) {
				FormShowParameter showParameter = this.getView().getFormShowParameter();
				Map<String, Object> cusMap = showParameter.getCustomParams();
				for (int i = 0; i < dyquerydatasous.length; i++) {
					DynamicObject dyquerydatasou = dyquerydatasous[i];
					String dyqueryId = dyquerydatasou.getString("id");
					boolean yem_isinvoice = dyquerydatasou.getBoolean("yem_isinvoice");
					DynamicObjectCollection dyEntryentitys = dyquerydatasou
							.getDynamicObjectCollection(this.yem_entryentity);
					List dyFields = new ArrayList();
					for (int j = 0; j < dyEntryentitys.size(); j++) {
						String yem_textfield = dyEntryentitys.get(j).getString("yem_textfield");
						dyFields.add(yem_textfield);
					}
					String sql = dyquerydatasou.getString("yem_sqleditor");
					if(cusMap.containsKey("isInvoice")) {
						sql = sql.replaceFirst("\\?", yem_billId);
						String yem_arrorderentry_id = cusMap.get("yem_arrorderentry_id").toString();
						sql = sql.replaceFirst("\\?", yem_arrorderentry_id);
					}else {
						sql = sql.replaceAll("\\?", yem_billId);
					}
					DataSet data = DB.queryDataSet("", ConstantUtils.KZKF, sql);
					List list = new ArrayList();
					while (data.hasNext()) {
						Row row = data.next();
						Map val = new HashMap();
						for (int j = 0; j < dyFields.size(); j++) {
							String field = dyFields.get(j).toString();
							val.put(field, row.get(field));
						}
						list.add(val);
					}
					if (list.size() > 0) {
						dyValsMap.put(dyqueryId, list);
					}
				}
			}
		}
		return dyValsMap;
	}

	/**
	 * 获取动态数据源单据表头书签对应值
	 *
	 * @param dyLevelMap
	 * @param dyValsMap
	 * @param size
	 * @param entryentitys
	 * @return
	 */
	private List getDyBillMarks(Map dyLevelMap, Map dyValsMap, int size, DynamicObjectCollection entryentitys) {
		List<WebOfficeMark> dyBillMarks = new ArrayList<>();// 单据头书签
		Iterator levelIt = dyLevelMap.keySet().iterator();
		while (levelIt.hasNext()) {
			String dyqueryId = levelIt.next().toString();
			int level = new Integer(dyLevelMap.get(dyqueryId).toString()).intValue();
			if (level == 1 && dyValsMap.containsKey(dyqueryId)) {
				List list = (List) dyValsMap.get(dyqueryId);
				Map valMap = (Map) list.get(0);
				for (int i = 0; i < size; i++) {
					DynamicObject entryentity = entryentitys.get(i);
					String entry_yem_fieldtype = entryentity.getString(this.yem_fieldtype);// 数据源类型
					if ("B".equals(entry_yem_fieldtype)) {
						DynamicObject entry_yem_dyquery = entryentity.getDynamicObject(this.yem_dyquery);// 动态数据源选择
						String entry_dyqueryId = entry_yem_dyquery.getString("id");
						String entry_yem_dyqueryfield = entryentity.getString(this.yem_dyqueryfield);// 动态数据源字段
						String entry_yem_bookmark = entryentity.getString(this.yem_bookmark);// 书签标识
						String yem_datatype = entryentity.getString(this.yem_datatype);// 数据类型
						int yem_precision = entryentity.getInt(this.yem_precision);// 精度
						if (dyqueryId.equals(entry_dyqueryId)) {
							String val = valMap.get(entry_yem_dyqueryfield) == null ? ""
									: WebOfficeComUtils.objecttransformation(valMap.get(entry_yem_dyqueryfield),
									yem_datatype, yem_precision);
							dyBillMarks.add(new WebOfficeMark(entry_yem_bookmark, val));
						}
					}
				}
			}
		}
		return dyBillMarks;
	}

	/**
	 * 获取非动态数据源一级单据体书签对应值
	 *
	 * @param entityKeyMap
	 * @param entityBookMarkMap
	 * @param size
	 * @param entryentitys
	 * @param dynamicObject
	 */
	private Map getOneLeveLEntityValMap(Map entityKeyMap, Map entityBookMarkMap, int size,
										DynamicObjectCollection entryentitys, DynamicObject dynamicObject) {

		FormShowParameter showParameter = this.getView().getFormShowParameter();
		Map<String, Object> cusMap = showParameter.getCustomParams();
		String yem_arrorderentry_id = null;
		Map yem_goodentry_map = new HashMap();
		if(cusMap.containsKey("isInvoice")) {
			yem_arrorderentry_id = cusMap.get("yem_arrorderentry_id").toString();
			yem_goodentry_map = (Map) cusMap.get("yem_goodentry_map");
			String yem_bill =  cusMap.get("yem_bill").toString();
			String yem_billId =  cusMap.get("yem_billId").toString();
			DynamicObject obj = BusinessDataServiceHelper.loadSingle(yem_billId, yem_bill);
			if(null != obj) {
				DynamicObjectCollection entryCol = null;
				if("yem_is_surrenderdoc".equals(yem_bill)) {//交单
					entryCol= obj.getDynamicObjectCollection("yem_yem_goodentry");
				}else if("yem_is_arrorderbill".equals(yem_bill)) {//到单
					entryCol = obj.getDynamicObjectCollection("yem_goodentry");
				}
				if(null != entryCol && entryCol.size()>0) {
					for(int i=0;i<entryCol.size();i++) {
						DynamicObject entry = entryCol.get(i);
						String seq = entry.getString("seq");
						if(yem_goodentry_map.containsKey(seq)) {
							String id = entry.getString("id");
							yem_goodentry_map.put(id, id);
						}
					}
				}
			}
		}
		Map oneLeveLEntityValMap = new HashMap();
		Iterator iterator = entityBookMarkMap.keySet().iterator();
		while (iterator.hasNext()) {
			String entityKey = iterator.next().toString();// 分录书签
			String entityVal = entityBookMarkMap.get(entityKey).toString();// 分录标识
			// 实体数据源
			for (int i = 0; i < size; i++) {
				DynamicObject entryentity = entryentitys.get(i);
				String entry_yem_bill = entryentity.getString(this.yem_bill);// 单据标识
				boolean entry_yem_isentity = entryentity.getBoolean(this.yem_isentity);// 是否分录标识
				String entry_yem_field = entryentity.getString(this.yem_field);// 字段标识
				String entry_yem_bookmark = entryentity.getString(this.yem_bookmark);// 书签标识
				if (!entry_yem_isentity) {
					String entry_yem_fieldtype = entryentity.getString(this.yem_fieldtype);// 数据源类型
					String entry_yem_parententry = entryentity.getString(this.yem_parententry);// 关联分录书签标识
					String yem_datatype = entryentity.getString(this.yem_datatype);// 数据类型
					int yem_precision = entryentity.getInt(this.yem_precision);// 精度
					if ("A".equals(entry_yem_fieldtype) && entry_yem_parententry != null
							&& entry_yem_parententry.equals(entityKey)) {
						String[] field = entry_yem_field.split("\\.");
						String firstKey = field[0];
						if (entityKeyMap.containsKey(firstKey)) {
							String parentKey1 = entityKeyMap.get(firstKey).toString();
							if (entry_yem_bill.equals(parentKey1) && entityVal.equals(firstKey)) {
								DynamicObjectCollection objs = dynamicObject.getDynamicObjectCollection(firstKey);
								Map map = new HashMap();
								if (oneLeveLEntityValMap.containsKey(entityKey)) {
									map = (Map) oneLeveLEntityValMap.get(entityKey);
								}
								for (int j = 0; j < objs.size(); j++) {
									DynamicObject obj = objs.get(j);
									String id = obj.getString("id");
									for (int k = 1; k < field.length - 1; k++) {
										if (obj != null) {
											obj = obj.getDynamicObject(field[k]);
										}
									}
									String value = "";
									if ("G".equals(yem_datatype)) {
										value = obj == null ? ""
												: WebOfficeComUtils.objecttransformation(
												obj.get(field[field.length - 1] + "_tag"), yem_datatype,
												yem_precision);
									} else {
										value = obj == null ? ""
												: WebOfficeComUtils.objecttransformation(
												obj.get(field[field.length - 1]), yem_datatype, yem_precision);
									}
									if (map.size() == objs.size()) {
										Map val = (Map) map.get(id);
										if (StringUtils.isNotEmpty(value) && val != null) {
											val.put(entry_yem_bookmark, value);
										}
									} else {
										Map val = new HashMap();
										val.put(entry_yem_bookmark, value);
										map.put(id, val);
									}
								}
								oneLeveLEntityValMap.put(entityKey, map);
							}
						} else if (!"".equals(entry_yem_parententry)) {
							// 分录中包含表头字段处理
							Map map = new HashMap();
							if (oneLeveLEntityValMap.containsKey(entityKey)) {
								map = (Map) oneLeveLEntityValMap.get(entityKey);
							}
							String value = "";
							if ("G".equals(yem_datatype)) {
								value = dynamicObject.get(field[field.length - 1]) == null ? ""
										: WebOfficeComUtils.objecttransformation(
										dynamicObject.get(field[field.length - 1] + "_tag"), yem_datatype,
										yem_precision);
							} else {
								value = dynamicObject.get(field[field.length - 1]) == null ? ""
										: WebOfficeComUtils.objecttransformation(
										dynamicObject.get(field[field.length - 1]), yem_datatype, yem_precision);
							}
							DynamicObjectCollection objs = dynamicObject
									.getDynamicObjectCollection(entityBookMarkMap.get(entry_yem_parententry).toString());
							for (int j = 0; j < objs.size(); j++) {
								DynamicObject obj = objs.get(j);
								String id = obj.getString("id");
								if (map.size() == objs.size()) {
									Map val = (Map) map.get(id);
									if (StringUtils.isNotEmpty(value) && val != null) {
										val.put(entry_yem_bookmark, value);
									}
								} else {
									Map val = new HashMap();
									val.put(entry_yem_bookmark, value);
									map.put(id, val);
								}
							}
							oneLeveLEntityValMap.put(entityKey, map);
						}
					}
				} else {
					String entry_yem_entity = entryentity.getString(this.yem_entity);// 分录标识
					if (entityKeyMap.containsKey(entry_yem_entity) && entityVal.equals(entry_yem_entity)) {
						String parentKey1 = entityKeyMap.get(entry_yem_entity).toString();
						if (entry_yem_bill.equals(parentKey1)) {
							DynamicObjectCollection objs = dynamicObject.getDynamicObjectCollection(entry_yem_entity);
							Map map = new HashMap();
							if (!oneLeveLEntityValMap.containsKey(entityKey)) {
								for (int j = 0; j < objs.size(); j++) {
									if (map.size() != objs.size()) {
										String id = objs.get(j).getString("id");
										if(null == yem_arrorderentry_id) {
											Map val = new HashMap();
											map.put(id, val);
										}else if(null != yem_arrorderentry_id
												&& (yem_arrorderentry_id.contains(id) || yem_goodentry_map.containsKey(id))) {
											Map val = new HashMap();
											map.put(id, val);
										}
									}
								}
								oneLeveLEntityValMap.put(entityKey, map);
							}
						}
					}
				}
			}
		}
		return oneLeveLEntityValMap;
	}

	/**
	 * 获取非动态数据源二级单据体书签对应值
	 *
	 * @param entityBookMarkMap
	 * @param size
	 * @param entryentitys
	 * @param dynamicObject
	 * @return
	 */
	private Map getTwoLeveLEntityValMap(Map entityKeyMap, Map entityBookMarkMap, int size,
										DynamicObjectCollection entryentitys, DynamicObject dynamicObject) {
		Map twoLeveLEntityValMap = new HashMap();
		Iterator iterator = entityBookMarkMap.keySet().iterator();
		while (iterator.hasNext()) {
			String entityKey = iterator.next().toString();// 分录书签
			String entityVal = entityBookMarkMap.get(entityKey).toString();// 分录标识
			// 实体数据源
			for (int i = 0; i < size; i++) {
				DynamicObject entryentity = entryentitys.get(i);
				String entry_yem_bill = entryentity.getString(this.yem_bill);// 单据标识
				boolean entry_yem_isentity = entryentity.getBoolean(this.yem_isentity);// 是否分录标识
				String entry_yem_field = entryentity.getString(this.yem_field);// 字段标识
				String entry_yem_bookmark = entryentity.getString(this.yem_bookmark);// 书签标识
				if (!entry_yem_isentity) {
					String entry_yem_fieldtype = entryentity.getString(this.yem_fieldtype);// 数据源类型
					String entry_yem_parententry = entryentity.getString(this.yem_parententry);// 关联分录书签标识
					String yem_datatype = entryentity.getString(this.yem_datatype);// 数据类型
					int yem_precision = entryentity.getInt(this.yem_precision);// 精度
					if ("A".equals(entry_yem_fieldtype) && entry_yem_parententry != null
							&& entry_yem_parententry.equals(entityKey)) {
						String[] field = entry_yem_field.split("\\.");
						String firstKey = field[0];
						if (entityKeyMap.containsKey(firstKey)) {
							String parentKey1 = entityKeyMap.get(firstKey).toString();
							if (entityKeyMap.containsKey(parentKey1)) {
								String parentKey2 = entityKeyMap.get(parentKey1).toString();
								if (entry_yem_bill.equals(parentKey2)) {
									Map entityVal2Map2 = new HashMap();
									if (twoLeveLEntityValMap.containsKey(entityKey)) {
										entityVal2Map2 = (Map) twoLeveLEntityValMap.get(entityKey);
									}
									DynamicObjectCollection level1Objs = dynamicObject
											.getDynamicObjectCollection(parentKey1);
									for (int j = 0; j < level1Objs.size(); j++) {
										String id = level1Objs.get(j).getString("id");
										List list = new ArrayList();
										if (entityVal2Map2.containsKey(id)) {
											list = (List) entityVal2Map2.get(id);
										}
										DynamicObjectCollection objs = level1Objs.get(j)
												.getDynamicObjectCollection(firstKey);
										for (int k = 0; k < objs.size(); k++) {
											DynamicObject obj = objs.get(k);
											String id2 = obj.getString("id");
											for (int n = 1; n < field.length - 1; n++) {
												if (obj != null) {
													obj = obj.getDynamicObject(field[n]);
												}
											}
											String value = "";
											if ("G".equals(yem_datatype)) {
												value = obj == null ? ""
														: WebOfficeComUtils.objecttransformation(
														obj.get(field[field.length - 1] + "_tag"), yem_datatype,
														yem_precision);
											} else {
												value = obj == null ? ""
														: WebOfficeComUtils.objecttransformation(
														obj.get(field[field.length - 1]), yem_datatype,
														yem_precision);
											}
											if (list.size() == objs.size()) {
												Map val = (Map) list.get(k);
												val.put(entry_yem_bookmark, value);
											} else {
												Map val = new HashMap();
												val.put("FID", id2);
												val.put(entry_yem_bookmark, value);
												list.add(val);
											}
										}
										entityVal2Map2.put(id, list);
									}
									twoLeveLEntityValMap.put(entityKey, entityVal2Map2);
								}
							}
						}
					}
				}
			}
		}
		return twoLeveLEntityValMap;
	}

	/**
	 * 获取非动态数据源一级单据体书签对应值
	 *
	 * @param entityBookMarkMap
	 * @param size
	 * @param entryentitys
	 * @param dyLevelMap
	 * @param dyValsMap
	 * @param oneLeveLEntityValMap
	 */
	private Map getDyOneLeveLEntityValMap(Map entityBookMarkMap, int size, DynamicObjectCollection entryentitys,
										  Map dyLevelMap, Map dyValsMap, Map oneLeveLEntityValMap) {
		Iterator iterator = entityBookMarkMap.keySet().iterator();
		while (iterator.hasNext()) {
			String entityKey = iterator.next().toString();// 分录书签
			String entityVal = entityBookMarkMap.get(entityKey).toString();// 分录标识
			for (int i = 0; i < size; i++) {
				DynamicObject entryentity = entryentitys.get(i);
				String entry_yem_fieldtype = entryentity.getString(this.yem_fieldtype);// 数据源类型
				String entry_yem_dyqueryfield = entryentity.getString(this.yem_dyqueryfield);// 动态数据源字段
				boolean entry_yem_isentity = entryentity.getBoolean(this.yem_isentity);// 是否分录标识
				String entry_yem_bookmark = entryentity.getString(this.yem_bookmark);// 书签标识
				String entry_yem_parententry = entryentity.getString(this.yem_parententry);// 关联分录书签标识
				String yem_datatype = entryentity.getString(this.yem_datatype);// 数据类型
				int yem_precision = entryentity.getInt(this.yem_precision);// 精度
				if (!entry_yem_isentity && "B".equals(entry_yem_fieldtype) && entry_yem_parententry != null
						&& entry_yem_parententry.equals(entityKey)) {
					DynamicObject entry_yem_dyquery = entryentity.getDynamicObject(this.yem_dyquery);// 动态数据源选择
					String dyqueryId = entry_yem_dyquery.getString("id");
					int level = new Integer(dyLevelMap.get(dyqueryId).toString()).intValue();
					if (level == 2) {
						Iterator entityVal1It = oneLeveLEntityValMap.keySet().iterator();
						while (entityVal1It.hasNext()) {
							String entryBookmark = entityVal1It.next().toString();
							Map val1Map = (Map) oneLeveLEntityValMap.get(entryBookmark);
							Iterator val1It = val1Map.keySet().iterator();
							while (val1It.hasNext()) {
								String id = val1It.next().toString();
								Map valMap = (Map) val1Map.get(id);
								if (dyValsMap.containsKey(dyqueryId)) {
									List dyValList = (List) dyValsMap.get(dyqueryId);
									for (int j = 0; j < dyValList.size(); j++) {
										Map dyValMap = (Map) dyValList.get(j);
										String FENTRYID = dyValMap.get("FENTRYID").toString();
										if (id.equals(FENTRYID)) {
											String val = dyValMap.get(entry_yem_dyqueryfield) == null ? ""
													: WebOfficeComUtils.objecttransformation(
													dyValMap.get(entry_yem_dyqueryfield), yem_datatype,
													yem_precision);
											valMap.put(entry_yem_bookmark, val);
										}
									}
								}
							}
						}
					}
				}
			}
		}
		return oneLeveLEntityValMap;
	}

	/**
	 * 获取非动态数据源二级单据体书签对应值
	 *
	 * @param entityBookMarkMap
	 * @param size
	 * @param entryentitys
	 * @param dyLevelMap
	 * @param dyValsMap
	 * @param twoLeveLEntityValMap
	 */
	private Map getDyTwoLeveLEntityValMap(Map entityBookMarkMap, int size, DynamicObjectCollection entryentitys,
										  Map dyLevelMap, Map dyValsMap, Map twoLeveLEntityValMap) {
		Iterator iterator = entityBookMarkMap.keySet().iterator();
		while (iterator.hasNext()) {
			String entityKey = iterator.next().toString();// 分录书签
			String entityVal = entityBookMarkMap.get(entityKey).toString();// 分录标识
			for (int i = 0; i < size; i++) {
				DynamicObject entryentity = entryentitys.get(i);
				String entry_yem_fieldtype = entryentity.getString(this.yem_fieldtype);// 数据源类型
				String entry_yem_dyqueryfield = entryentity.getString(this.yem_dyqueryfield);// 动态数据源字段
				boolean entry_yem_isentity = entryentity.getBoolean(this.yem_isentity);// 是否分录标识
				String entry_yem_bookmark = entryentity.getString(this.yem_bookmark);// 书签标识
				String entry_yem_parententry = entryentity.getString(this.yem_parententry);// 关联分录书签标识
				String yem_datatype = entryentity.getString(this.yem_datatype);// 数据类型
				int yem_precision = entryentity.getInt(this.yem_precision);// 精度
				if (!entry_yem_isentity && "B".equals(entry_yem_fieldtype) && entry_yem_parententry != null
						&& entry_yem_parententry.equals(entityKey)) {
					DynamicObject entry_yem_dyquery = entryentity.getDynamicObject(this.yem_dyquery);// 动态数据源选择
					String dyqueryId = entry_yem_dyquery.getString("id");
					int level = new Integer(dyLevelMap.get(dyqueryId).toString()).intValue();
					if (level == 3) {
						Map entityVal2 = new HashMap();
						if (twoLeveLEntityValMap.containsKey(entityKey)) {
							entityVal2 = (Map) twoLeveLEntityValMap.get(entityKey);
						}
						if (dyValsMap.containsKey(dyqueryId)) {
							List dyValsList = (List) dyValsMap.get(dyqueryId);
							for (int j = 0; j < dyValsList.size(); j++) {
								Map dyValMap = (Map) dyValsList.get(j);
								String FENTRYID = dyValMap.get("FENTRYID").toString();
								String FDETAILID = dyValMap.get("FDETAILID").toString();
								String val = dyValMap.get(entry_yem_dyqueryfield) == null ? ""
										: WebOfficeComUtils.objecttransformation(dyValMap.get(entry_yem_dyqueryfield),
										yem_datatype, yem_precision);
								if (entityVal2.containsKey(FENTRYID)) {
									List val2List = (List) entityVal2.get(FENTRYID);
									boolean has = false;
									for (int k = 0; k < val2List.size(); k++) {
										Map val2 = (Map) val2List.get(k);
										String FID = val2.get("FID").toString();
										if (FID.equals(FDETAILID)) {
											val2.put(entry_yem_bookmark, val);
											has = true;
										}
									}
									if (!has) {
										Map val2Map = new HashMap();
										val2Map.put("FID", FDETAILID);
										val2Map.put(entry_yem_bookmark, val);
										val2List.add(val2Map);
										entityVal2.put(FENTRYID, val2List);
									}
								} else {
									List val2List = new ArrayList();
									Map val2Map = new HashMap();
									val2Map.put("FID", FDETAILID);
									val2Map.put(entry_yem_bookmark, val);
									val2List.add(val2Map);
									entityVal2.put(FENTRYID, val2List);
								}
							}
						}
						twoLeveLEntityValMap.put(entityKey, entityVal2);
					}
				}
			}
		}
		return twoLeveLEntityValMap;
	}

	/**
	 * 书签赋值
	 *
	 * @param billMarks
	 * @param dyBillMarks
	 * @param entityBookMarkMap
	 * @param entityFieldBookMarkSortMap
	 */
	private void setData(List billMarks, List dyBillMarks, Map oneLeveLEntityValMap, Map twoLeveLEntityValMap,
						 Map entityBookMarkMap, Map entityFieldBookMarkSortMap) {
		WebOffice webOffice = getControl(this.yem_webofficeap);
		if (billMarks.size() > 0) {
			webOffice.modifyMarks(billMarks);
		}
		if (dyBillMarks.size() > 0) {
			webOffice.modifyMarks(dyBillMarks);
		}

		Iterator iterator = entityBookMarkMap.keySet().iterator();
		while (iterator.hasNext()) {
			String entityKey = iterator.next().toString();// 分录书签
			String entityVal = entityBookMarkMap.get(entityKey).toString();// 分录标识
			if (!entityFieldBookMarkSortMap.containsKey(entityKey)) {
				continue;
			}
			Map bookmarks = (Map) entityFieldBookMarkSortMap.get(entityKey);
			int bookmarksSize = bookmarks.size();
			if (bookmarksSize == 0) {
				continue;
			}
			int rowCount = 0;
			if (twoLeveLEntityValMap.containsKey(entityKey)) {
				Map vals = (Map) twoLeveLEntityValMap.get(entityKey);
				Iterator valsIterator = vals.keySet().iterator();
				while (valsIterator.hasNext()) {
					String valsKey = valsIterator.next().toString();
					List list = (List) vals.get(valsKey);
					rowCount = rowCount + list.size();
				}
			} else {
				Map val = (Map) oneLeveLEntityValMap.get(entityKey);
				rowCount = val.size();
			}
			// 构建分录数组
			String[][] tableDatas = new String[rowCount][bookmarksSize];
			// 构建分录数组数据
			if (twoLeveLEntityValMap.containsKey(entityKey)) {
				int index = 0;
				boolean hasSeq = false;
				String seqKey = "";
				Map oneVals = oneLeveLEntityValMap.containsKey(entityKey)
						? (HashMap) oneLeveLEntityValMap.get(entityKey)
						: new HashMap();
				Map twoVals = (Map) twoLeveLEntityValMap.get(entityKey);
				Iterator twoValsIterator = twoVals.keySet().iterator();
				while (twoValsIterator.hasNext()) {
					String twoValsKey = twoValsIterator.next().toString();
					Map oneVal = oneVals.containsKey(twoValsKey) ? (HashMap) oneVals.get(twoValsKey) : new HashMap();
					List list = (List) twoVals.get(twoValsKey);
					for (int i = 0; i < list.size(); i++) {
						Map twoVal = (Map) list.get(i);
						Iterator bookmarksIterator = bookmarks.keySet().iterator();
						while (bookmarksIterator.hasNext()) {
							String bookmarkKey = bookmarksIterator.next().toString();
							int bookmarkIndex = new Integer(bookmarks.get(bookmarkKey).toString()).intValue();
							String val = "";
							if (oneVal.containsKey(bookmarkKey)) {
								val = oneVal.get(bookmarkKey).toString();
							} else if (twoVal.containsKey(bookmarkKey)) {
								val = twoVal.get(bookmarkKey).toString();
							}
							tableDatas[index][bookmarkIndex] = val;
						}
						index++;
					}
				}
			} else if (oneLeveLEntityValMap.containsKey(entityKey)) {
				boolean hasSeq = false;
				String seqKey = "";
				Map oneVals = (Map) oneLeveLEntityValMap.get(entityKey);
				// 判断是否存在序号列
				Iterator seqBookmarksIterator = bookmarks.keySet().iterator();
				while (seqBookmarksIterator.hasNext()) {
					String bookmarkKey = seqBookmarksIterator.next().toString();
					if (bookmarkKey.indexOf("序号") != -1) {
						seqKey = bookmarkKey;
						hasSeq = true;
					}
				}
				if (hasSeq) {
					//获取最小序号
					int minSeq = 0;
					for (int i = 0; i < rowCount; i++) {
						Iterator oneValsIterator = oneVals.keySet().iterator();
						while (oneValsIterator.hasNext()) {
							String oneValKey = oneValsIterator.next().toString();
							Map oneVal = (Map) oneVals.get(oneValKey);
							int seq = new Integer(oneVal.get(seqKey).toString()).intValue();
							if(minSeq == 0 || seq < minSeq) {
								minSeq = seq;
							}
						}
					}
					int index = 0;
					for (int i = minSeq; i < (minSeq+rowCount); i++) {
						Iterator oneValsIterator = oneVals.keySet().iterator();
						while (oneValsIterator.hasNext()) {
							String oneValKey = oneValsIterator.next().toString();
							Map oneVal = (Map) oneVals.get(oneValKey);
							int seq = new Integer(oneVal.get(seqKey).toString()).intValue();
							if ((seq == i) || (seq > 1 && rowCount == 1)) {
								Iterator bookmarksIterator = bookmarks.keySet().iterator();
								while (bookmarksIterator.hasNext()) {
									String bookmarkKey = bookmarksIterator.next().toString();
									int bookmarkIndex = new Integer(bookmarks.get(bookmarkKey).toString()).intValue();
									String val = "";
									if (oneVal.containsKey(bookmarkKey)) {
										val = oneVal.get(bookmarkKey).toString();
									}
									tableDatas[index][bookmarkIndex] = val;
								}
								index++;
							}
						}
					}
				} else {
					int index = 0;
					Iterator oneValsIterator = oneVals.keySet().iterator();
					while (oneValsIterator.hasNext()) {
						String oneValKey = oneValsIterator.next().toString();
						Map oneVal = (Map) oneVals.get(oneValKey);
						Iterator bookmarksIterator = bookmarks.keySet().iterator();
						while (bookmarksIterator.hasNext()) {
							String bookmarkKey = bookmarksIterator.next().toString();
							int bookmarkIndex = new Integer(bookmarks.get(bookmarkKey).toString()).intValue();
							String val = "";
							if (oneVal.containsKey(bookmarkKey)) {
								val = oneVal.get(bookmarkKey).toString();
							}
							tableDatas[index][bookmarkIndex] = val;
						}
						index++;
					}
				}
			}
			List<WebOfficeMark> marks2 = new ArrayList<>();// 分录第一行书签标识替换值
			Iterator bookmarksIterator = bookmarks.keySet().iterator();
			while (bookmarksIterator.hasNext()) {
				String valkey = bookmarksIterator.next().toString();
				int valIndex = new Integer(bookmarks.get(valkey).toString()).intValue();
				if (tableDatas[0][valIndex] != null) {
					marks2.add(new WebOfficeMark(valkey, tableDatas[0][valIndex]));
				}
			}
			if (marks2.size() > 0) {
				webOffice.modifyMarks(marks2);
				if (tableDatas.length > 1) {
					String[][] tableDatas2 = new String[rowCount - 1][bookmarksSize];
					for (int i = 0; i < rowCount - 1; i++) {
						for (int j = 0; j < bookmarksSize; j++) {
							tableDatas2[i][j] = tableDatas[i + 1][j];
						}
					}
					webOffice.addTableRows(entityKey, tableDatas2);
				}
			}
		}
	}

	/**
	 * 保存关联明细
	 *
	 * @param model
	 * @param yem_modelid
	 */
	public void saveBookmarkRelation(IDataModel model, String yem_modelid) {
		DynamicObject webofficemodel = BusinessDataServiceHelper.loadSingle(yem_modelid, "yem_webofficemodel");
		QFilter qFilter1 = new QFilter("yem_webofficemodel", QCP.equals, BigDecimalUtils.toBigDecimal(yem_modelid).longValue());
		DynamicObject[] bookmarkrelations = BusinessDataServiceHelper.load("yem_bookmarkrelation", "id",
				new QFilter[] { qFilter1 });
		DynamicObject bookmarkrelation = null;
		if (bookmarkrelations != null && bookmarkrelations.length > 0) {
			bookmarkrelation = bookmarkrelations[0];
			bookmarkrelation = BusinessDataServiceHelper.loadSingle(bookmarkrelation.getPkValue(),
					"yem_bookmarkrelation");
		} else {
			bookmarkrelation = BusinessDataServiceHelper.newDynamicObject("yem_bookmarkrelation");
		}
		long orgId = 1026161043572809728L;
		bookmarkrelation.set("org", orgId);
		bookmarkrelation.set("createorg", orgId);
		bookmarkrelation.set("useorg", orgId);
		bookmarkrelation.set("name", webofficemodel.get("name") + "书签关联");
		bookmarkrelation.set("number", webofficemodel.get("number") + "_001");
		bookmarkrelation.set("status", "A");
		bookmarkrelation.set("enable", "1");
		bookmarkrelation.set("ctrlstrategy", "5");
		bookmarkrelation.set("creator",
				BusinessDataServiceHelper.loadSingle(Long.valueOf(UserServiceHelper.getCurrentUserId()), "bos_user"));
		bookmarkrelation.set("createtime", new Date(System.currentTimeMillis()));
		bookmarkrelation.set("modifier",
				BusinessDataServiceHelper.loadSingle(Long.valueOf(UserServiceHelper.getCurrentUserId()), "bos_user"));
		bookmarkrelation.set("modifytime", new Date(System.currentTimeMillis()));
		bookmarkrelation.set("yem_webofficemodel", yem_modelid);
		DynamicObjectCollection entryentitys = bookmarkrelation.getDynamicObjectCollection(this.yem_entryentity);
		entryentitys.clear();
		DynamicObjectCollection rows = model.getEntryEntity(this.yem_entryentity);
		for (int i = 0; i < rows.size(); i++) {
			DynamicObject row = rows.get(i);
			DynamicObject entryentity = entryentitys.addNew();
			entryentity.set("seq", i);
			entryentity.set(this.yem_fieldtype, row.get(this.yem_fieldtype));
			entryentity.set(this.yem_dyquery, row.get(this.yem_dyquery));
			entryentity.set(this.yem_dyqueryfieldcn, row.get(this.yem_dyqueryfieldcn));
			entryentity.set(this.yem_dyqueryfield, row.get(this.yem_dyqueryfield));
			entryentity.set(this.yem_fieldchoice, row.get(this.yem_fieldchoice));
			entryentity.set(this.yem_bill, row.get(this.yem_bill));
			entryentity.set(this.yem_billname, row.get(this.yem_billname));
			entryentity.set(this.yem_isentity, row.get(this.yem_isentity));
			entryentity.set(this.yem_entity, row.get(this.yem_entity));
			entryentity.set(this.yem_entityname, row.get(this.yem_entityname));
			entryentity.set(this.yem_field, row.get(this.yem_field));
			entryentity.set(this.yem_fieldname, row.get(this.yem_fieldname));
			entryentity.set(this.yem_bookmark, row.get(this.yem_bookmark));
			entryentity.set(this.yem_parententry, row.get(this.yem_parententry));
			entryentity.set(this.yem_colindex, row.get(this.yem_colindex));
			entryentity.set(this.yem_datatype, row.get(this.yem_datatype));
			entryentity.set(this.yem_precision, row.get(this.yem_precision));
			entryentity.set(this.yem_invoiceseq, row.get(this.yem_invoiceseq));
		}
		Object[] objs = SaveServiceHelper.save(new DynamicObject[] { bookmarkrelation });
		if (objs != null && objs.length > 0) {
			DynamicObject obj = (DynamicObject) objs[0];
			if (obj.getLong("id") != 0) {
				// 修改 主数据内码
				bookmarkrelation = BusinessDataServiceHelper.loadSingle(obj.getLong("id"), "yem_bookmarkrelation");
				bookmarkrelation.set("masterid", obj.getLong("id"));
				SaveServiceHelper.save(new DynamicObject[] { bookmarkrelation });
				this.getView().showSuccessNotification("保存成功！");
			} else {
				this.getView().showErrorNotification("保存失败！");
			}
		} else {
			this.getView().showErrorNotification("保存失败！");
		}
	}

	@Override
	public void propertyChanged(PropertyChangedArgs e) {
		super.propertyChanged(e);
		IDataModel model = this.getModel();
		String name = e.getProperty().getName();
		ChangeData changeData = e.getChangeSet()[0];
		Object newValue = changeData.getNewValue();
		switch (name) {
			case "yem_fieldchoice2":
				model.setValue(this.yem_bill2, "");
				model.setValue(this.yem_billname2, "");
				model.setValue(this.yem_isentity2, false);
				model.setValue(this.yem_entity2, "");
				model.setValue(this.yem_entityname2, "");
				model.setValue(this.yem_field2, "");
				model.setValue(this.yem_fieldname2, "");
				if (newValue != null && !"".equals(newValue)) {
					FormShowParameter showParameter = this.getView().getFormShowParameter();
					Map<String, Object> cusMap = showParameter.getCustomParams();
					String yem_bill = cusMap.get("yem_bill").toString();
					MainEntityType entityType = EntityMetadataCache.getDataEntityType(yem_bill);
					model.setValue(this.yem_bill2, yem_bill);
					model.setValue(this.yem_billname2, entityType.getDisplayName());
					String fieldchoice = newValue.toString();
					if (fieldchoice.indexOf(".") > 0) {
						String yem_fieldname = "";
						EntityTypeUtil helper = new EntityTypeUtil();
						List cols = helper.getFilterColumns(entityType, false);
						Iterator iterator = cols.iterator();
						while (iterator.hasNext() && "".equals(yem_fieldname)) {
							Map indexs = (Map) iterator.next();
							String entry = (String) indexs.get("entryEntity");
							String fieldName = (String) indexs.get("fieldName");
							String fieldCaption = (String) indexs.get("fieldCaption");

							String value = fieldName;
							if (!StringUtils.isEmpty(entry)) {
								value = entry + "." + fieldName;
							}
							if (value.equals(fieldchoice)) {
								yem_fieldname = fieldCaption;
							}
						}
						model.setValue(this.yem_field2, fieldchoice);
						model.setValue(this.yem_fieldname2, yem_fieldname);
					} else {
						String yem_entityname = "";
						EntityTypeUtil helper = new EntityTypeUtil();
						List entityCols = helper.getEntryEntities(entityType);
						Iterator entityModel = entityCols.iterator();
						while (entityModel.hasNext() && "".equals(yem_entityname)) {
							EntryEntityDto indexs = (EntryEntityDto) entityModel.next();
							String key2 = indexs.getKey();
							String fieldName = indexs.getName().toString();
							if (key2.equals(fieldchoice)) {
								yem_entityname = fieldName;
							}
						}
						model.setValue(this.yem_isentity2, true);
						model.setValue(this.yem_entity2, fieldchoice);
						model.setValue(this.yem_entityname2, yem_entityname);
					}
				}
				break;
			case "yem_fieldtype2":
				if (newValue == null || "".equals(newValue.toString())) {
					this.getModel().setValue(this.yem_dyquery2, null);
					this.getModel().setValue(this.yem_dyqueryfieldcn2, null);
					this.getModel().setValue(this.yem_dyqueryfield2, null);
					this.getModel().setValue(this.yem_fieldchoice2, null);
					this.getModel().setValue(this.yem_field2, null);
					this.getModel().setValue(this.yem_fieldname2, null);
					this.getModel().setValue(this.yem_entity2, null);
					this.getModel().setValue(this.yem_entityname2, null);
					this.getModel().setValue(this.yem_bill2, null);
					this.getModel().setValue(this.yem_billname2, null);
					this.getModel().setValue(this.yem_isentity2, null);
					this.getModel().setValue(this.yem_parententry2, null);
					this.getModel().setValue(this.yem_colindex2, null);
					this.getModel().setValue(this.yem_precision2, null);
					this.getView().setVisible(false, this.yem_dyquery2);
					this.getView().setVisible(false, this.yem_dyqueryfieldcn2);
					this.getView().setVisible(false, this.yem_fieldchoice2);
					this.getView().setVisible(false, this.yem_parententry2);
					this.getView().setVisible(false, this.yem_colindex2);
					this.getView().setVisible(false, this.yem_datatype2);
					this.getView().setVisible(false, this.yem_precision2);
				} else if ("A".equals(newValue.toString())) {
					this.getModel().setValue(this.yem_dyquery2, null);
					this.getModel().setValue(this.yem_dyqueryfieldcn2, null);
					this.getModel().setValue(this.yem_dyqueryfield2, null);
					this.getModel().setValue(this.yem_fieldchoice2, null);
					this.getModel().setValue(this.yem_colindex2, null);
					this.getModel().setValue(this.yem_precision2, null);
					this.getView().setVisible(false, this.yem_dyquery2);
					this.getView().setVisible(false, this.yem_dyqueryfieldcn2);
					this.getView().setVisible(true, this.yem_fieldchoice2);
					this.getView().setVisible(true, this.yem_parententry2);
					this.getView().setVisible(true, this.yem_colindex2);
					this.getView().setVisible(true, this.yem_datatype2);
					this.getView().setVisible(true, this.yem_precision2);
				} else if ("B".equals(newValue.toString())) {
					this.getModel().setValue(this.yem_field2, null);
					this.getModel().setValue(this.yem_fieldname2, null);
					this.getModel().setValue(this.yem_entity2, null);
					this.getModel().setValue(this.yem_entityname2, null);
					this.getModel().setValue(this.yem_bill2, null);
					this.getModel().setValue(this.yem_billname2, null);
					this.getModel().setValue(this.yem_isentity2, null);
					this.getModel().setValue(this.yem_parententry2, null);
					this.getModel().setValue(this.yem_colindex2, null);
					this.getModel().setValue(this.yem_precision2, null);
					this.getView().setVisible(true, this.yem_dyquery2);
					this.getView().setVisible(true, this.yem_dyqueryfieldcn2);
					this.getView().setVisible(false, this.yem_fieldchoice2);
					this.getView().setVisible(true, this.yem_parententry2);
					this.getView().setVisible(true, this.yem_colindex2);
					this.getView().setVisible(true, this.yem_datatype2);
					this.getView().setVisible(true, this.yem_precision2);
				}
				break;
			default:
				break;
		}
	}

	@Override
	public void afterBindData(EventObject e) {
		super.afterBindData(e);
		FormShowParameter showParameter = this.getView().getFormShowParameter();
		Map<String, Object> cusMap = showParameter.getCustomParams();
		if (cusMap.containsKey("yem_modelid")) {
			long yem_modelid = BigDecimalUtils.toBigDecimal(cusMap.get("yem_modelid").toString()).longValue();
			DynamicObject webofficeassembly = BusinessDataServiceHelper.loadSingle(yem_modelid,"yem_webofficemodel");
			List<Map<String,Object>> list = AttachmentServiceHelper.getAttachments("yem_webofficemodel",webofficeassembly.getLong("id"),"yem_attachmentpanelap");
			String urlcode = null;
			String name = "";
			if(null != list && list.size()>0) {
				Map map = list.get(0);
				urlcode = map.get("url").toString();
				name = map.get("name").toString();
			}else {
				this.getView().showMessage("未维护webOffice模板，请联系管理员！");
				return;
			}

//			String urlcode = UrlService.getAttachmentFullUrl(cusMap.get("fileUrl").toString());
//			String name = urlcode.substring(urlcode.lastIndexOf("/"));
//			String[] files = name.split("\\.");
//			String tfilename = files.length == 2 ? files[0] : UUID.randomUUID().toString();
//			try {
//				tfilename = URLEncoder.encode(tfilename, "utf-8");
//			} catch (Exception e2) {
//				e2.printStackTrace();
//			}
//			String tExtend = files.length == 2 ? files[1] : "docx";
//			name = tfilename + "." + tExtend;
//			urlcode = urlcode.substring(0, urlcode.lastIndexOf("/"));
//			urlcode = urlcode + name;

			WebOffice webOffice = getControl(this.yem_webofficeap);
			urlcode = urlcode.replaceAll("cmnmovevpn.minmetals.com.cn/ierp", "cmnerp.minmetals.com.cn");
			webOffice.openAll(urlcode, name);

			String yem_bill = cusMap.get("yem_bill").toString();
			String yem_billId = cusMap.get("yem_billId").toString();
			boolean isEdit = false;
			if (cusMap.containsKey("isEdit") && cusMap.get("isEdit") != null) {
				isEdit = (Boolean) cusMap.get("isEdit");
			}

			// 初始化分录
			QFilter qFilter1 = new QFilter("yem_webofficemodel", QCP.equals, yem_modelid);
			DynamicObject[] bookmarkrelations = BusinessDataServiceHelper.load("yem_bookmarkrelation", "id",
					new QFilter[] { qFilter1 });
			if (bookmarkrelations != null && bookmarkrelations.length > 0) {
				IDataModel model = this.getModel();
				DynamicObject bookmarkrelation = bookmarkrelations[0];
				bookmarkrelation = BusinessDataServiceHelper.loadSingle(bookmarkrelation.getPkValue(),
						"yem_bookmarkrelation");
				DynamicObjectCollection entryentitys = bookmarkrelation.getDynamicObjectCollection(this.yem_entryentity);

				int rowCount = model.getEntryRowCount(this.yem_entryentity);
				for (int i = rowCount - 1; i >= 0; i--) {
					model.deleteEntryRow(this.yem_entryentity, i);
				}
				for (int i = 0; i < entryentitys.size(); i++) {
					DynamicObject entryentity = entryentitys.get(i);
					int index = model.createNewEntryRow(this.yem_entryentity);
					model.setValue(this.yem_fieldtype, entryentity.get(this.yem_fieldtype), index);
					model.setValue(this.yem_dyquery, entryentity.get(this.yem_dyquery), index);
					model.setValue(this.yem_dyqueryfieldcn, entryentity.get(this.yem_dyqueryfieldcn), index);
					model.setValue(this.yem_dyqueryfield, entryentity.get(this.yem_dyqueryfield), index);
					model.setValue(this.yem_fieldchoice, entryentity.get(this.yem_fieldchoice), index);
					model.setValue(this.yem_bill, entryentity.get(this.yem_bill), index);
					model.setValue(this.yem_billname, entryentity.get(this.yem_billname), index);
					model.setValue(this.yem_isentity, entryentity.get(this.yem_isentity), index);
					model.setValue(this.yem_entity, entryentity.get(this.yem_entity), index);
					model.setValue(this.yem_entityname, entryentity.get(this.yem_entityname), index);
					model.setValue(this.yem_field, entryentity.get(this.yem_field), index);
					model.setValue(this.yem_fieldname, entryentity.get(this.yem_fieldname), index);
					model.setValue(this.yem_bookmark, entryentity.get(this.yem_bookmark), index);
					model.setValue(this.yem_parententry, entryentity.get(this.yem_parententry), index);
					model.setValue(this.yem_colindex, entryentity.get(this.yem_colindex), index);
					model.setValue(this.yem_datatype, entryentity.get(this.yem_datatype), index);
					model.setValue(this.yem_precision, entryentity.get(this.yem_precision), index);
					model.setValue(this.yem_invoiceseq, entryentity.get(this.yem_invoiceseq), index);
				}
				this.getView().updateView(this.yem_entryentity);
			}
			if (isEdit) {
				// 初始化实体字段选择下拉框
				ComboEdit comboEdit = this.getControl(this.yem_fieldchoice2);
				List<ComboItem> data = new ArrayList<>();
				MainEntityType entityType = EntityMetadataCache.getDataEntityType(yem_bill);
				EntityTypeUtil helper = new EntityTypeUtil();
				// 分录标识
				List entityCols = helper.getEntryEntities(entityType);
				Iterator entityModel = entityCols.iterator();
				while (entityModel.hasNext()) {
					EntryEntityDto indexs = (EntryEntityDto) entityModel.next();
					String key = indexs.getKey();
					String fieldName = indexs.getName().toString();
					String parentKey = indexs.getParentKey();
					if (parentKey == null || parentKey.trim().equals("") || !parentKey.equals(yem_bill)) {
						continue;
					}
					data.add(new ComboItem(new LocaleString(fieldName + "分录"), "" + key + ""));
				}
				// 字段标识
				List cols = helper.getFilterColumns(entityType, false);
				Iterator colsIterator = cols.iterator();
				while (colsIterator.hasNext()) {
					Map indexs = (Map) colsIterator.next();
					String entry = (String) indexs.get("entryEntity");
					String fieldName = (String) indexs.get("fieldName");
					String fieldCaption = (String) indexs.get("fieldCaption");

					String value = fieldName;
					if (!StringUtils.isEmpty(entry)) {
						value = entry + "." + fieldName;
					}
					data.add(new ComboItem(new LocaleString("" + fieldCaption + ""), "" + value + ""));
				}
				comboEdit.setComboItems(data);

				init_yem_parententry2();
			} else {
				this.getView().setVisible(false, this.yem_buttonap);
				this.getView().setVisible(false, this.yem_buttonap1);
				this.getView().setVisible(false, this.yem_buttonap2);
				this.getView().setVisible(false, this.yem_entryentity);
				this.getView().setVisible(false, "yem_flexpanelap2");
				SplitContainer yem_splitcontainerap = this.getControl("yem_splitcontainerap");
				yem_splitcontainerap.changeFlexStatus(SplitDirection.left, true);
				// 赋值
				IDataModel model = this.getModel();
				boolean isSuccess = verification(model);
				if (isSuccess) {
					setBookmarkData(model, yem_billId, yem_bill);
				}
			}
		}
	}

	/**
	 * 校验
	 *
	 * @param model
	 * @return
	 */
	private boolean verification(IDataModel model) {
		boolean isSuccess = true;
		DynamicObjectCollection entryentitys = model.getEntryEntity(this.yem_entryentity);
		if (entryentitys != null && entryentitys.size() > 0) {
			int size = entryentitys.size();
			for (int i = 0; i < size; i++) {
				if (isSuccess) {
					DynamicObject entryentity = entryentitys.get(i);
					String yem_fieldtype = entryentity.getString(this.yem_fieldtype);// 数据源类型
					String yem_dyqueryfield = entryentity.getString(this.yem_dyqueryfield);// 动态数据源字段
					String entry_yem_fieldchoice = entryentity.getString(this.yem_fieldchoice);// 实体字段选择
					String entry_yem_bill = entryentity.getString(this.yem_bill);// 单据标识
					boolean entry_yem_isentity = entryentity.getBoolean(this.yem_isentity);// 是否分录标识
					String entry_yem_entity = entryentity.getString(this.yem_entity);// 分录标识
					String entry_yem_field = entryentity.getString(this.yem_field);// 字段标识
					String entry_yem_bookmark = entryentity.getString(this.yem_bookmark);// 书签标识
					String entry_yem_parententry = entryentity.getString(this.yem_parententry);// 关联分录书签标识
					if (yem_fieldtype == null || "".equals(yem_fieldtype)) {
						this.getView().showErrorNotification("第" + (i + 1) + "行数据源类型不能为空！");
						isSuccess = false;
					}
					if ("A".equals(yem_fieldtype)) {// 实体数据源
						// 检查分录实体字段选择是否为空
						if (entry_yem_fieldchoice == null || "".equals(entry_yem_fieldchoice)) {
							this.getView().showErrorNotification("第" + (i + 1) + "行实体字段选择不能为空！");
							isSuccess = false;
						}
					} else {// 动态数据源
						// 检查分录实体字段选择是否为空
						if (yem_dyqueryfield == null || "".equals(yem_dyqueryfield)) {
							this.getView().showErrorNotification("第" + (i + 1) + "行动态数据源字段不能为空！");
							isSuccess = false;
						}
					}
				}
			}
		}
		return isSuccess;
	}

	@Override
	public void onGetAllBookmarks(WebOfficeBookmarkEvent arg0) {
		List bookmarks = arg0.getBookmarks();
		IDataModel model = this.getModel();
		int rowCount = model.getEntryRowCount(this.yem_entryentity);
		for (int i = rowCount - 1; i >= 0; i--) {
			model.deleteEntryRow(this.yem_entryentity, i);
		}
		if (bookmarks != null && bookmarks.size() > 0) {
			// 获取已保存关联明细
			Map relationMap = new HashMap();
			FormShowParameter showParameter = this.getView().getFormShowParameter();
			Map<String, Object> cusMap = showParameter.getCustomParams();
			long yem_modelid = BigDecimalUtils.toBigDecimal(cusMap.get("yem_modelid")).longValue();
			QFilter qFilter1 = new QFilter("yem_webofficemodel", "=", yem_modelid);
			DynamicObject[] bookmarkrelations = BusinessDataServiceHelper.load("yem_bookmarkrelation", "id",
					new QFilter[] { qFilter1 });
			if (bookmarkrelations != null && bookmarkrelations.length > 0) {
				DynamicObject bookmarkrelation = bookmarkrelations[0];
				bookmarkrelation = BusinessDataServiceHelper.loadSingle(bookmarkrelation.getPkValue(),
						"yem_bookmarkrelation");
				DynamicObjectCollection entryentitys = bookmarkrelation.getDynamicObjectCollection(this.yem_entryentity);
				if (entryentitys != null && entryentitys.size() > 0) {
					for (int i = 0; i < entryentitys.size(); i++) {
						DynamicObject entryentity = entryentitys.get(i);
						relationMap.put(entryentity.getString(this.yem_bookmark), entryentity);
					}
				}
			}
			//
			int size = bookmarks.size();
			for (int i = 0; i < size; i++) {
				int index = model.createNewEntryRow(this.yem_entryentity);
				String bookmark = bookmarks.get(i).toString();
				model.setValue(this.yem_bookmark, bookmark, index);
				if (relationMap.containsKey(bookmark)) {
					DynamicObject entryentity = (DynamicObject) relationMap.get(bookmark);
					model.setValue(this.yem_fieldtype, entryentity.get(this.yem_fieldtype), index);
					model.setValue(this.yem_dyquery, entryentity.get(this.yem_dyquery), index);
					model.setValue(this.yem_dyqueryfieldcn, entryentity.get(this.yem_dyqueryfieldcn), index);
					model.setValue(this.yem_dyqueryfield, entryentity.get(this.yem_dyqueryfield), index);
					model.setValue(this.yem_fieldchoice, entryentity.get(this.yem_fieldchoice), index);
					model.setValue(this.yem_bill, entryentity.get(this.yem_bill), index);
					model.setValue(this.yem_billname, entryentity.get(this.yem_billname), index);
					model.setValue(this.yem_isentity, entryentity.get(this.yem_isentity), index);
					model.setValue(this.yem_parententry, entryentity.get(this.yem_parententry), index);
					model.setValue(this.yem_entity, entryentity.get(this.yem_entity), index);
					model.setValue(this.yem_entityname, entryentity.get(this.yem_entityname), index);
					model.setValue(this.yem_field, entryentity.get(this.yem_field), index);
					model.setValue(this.yem_fieldname, entryentity.get(this.yem_fieldname), index);
					model.setValue(this.yem_colindex, entryentity.get(this.yem_colindex), index);
					model.setValue(this.yem_datatype, entryentity.get(this.yem_datatype), index);
					model.setValue(this.yem_precision, entryentity.get(this.yem_precision), index);
					model.setValue(this.yem_invoiceseq, entryentity.get(this.yem_invoiceseq), index);
				}
			}
			init_yem_parententry2();
		}
	}

	/**
	 * 初始化关联分录书签标识
	 */
	private void init_yem_parententry2() {
		ComboEdit comboEdit = this.getControl(this.yem_parententry2);
		List<ComboItem> data = new ArrayList<>();
		int rowCount = this.getModel().getEntryRowCount(this.yem_entryentity);
		for (int i = 0; i < rowCount; i++) {
			String yem_bookmark = this.getModel().getEntryRowEntity(this.yem_entryentity, i).getString(this.yem_bookmark);
			data.add(new ComboItem(new LocaleString(yem_bookmark), yem_bookmark));
		}
		comboEdit.setComboItems(data);
	}

	@Override
	public void closedCallBack(ClosedCallBackEvent e) {
		super.closedCallBack(e);
		String actionId = e.getActionId();
		Object returnData = e.getReturnData();
		if (StringUtils.isNotEmpty(actionId)) {
			if ("dyquerydatasouchoice".equals(actionId)) {
				Map<String, Object> values = (Map<String, Object>) returnData;
				String yem_textfield = "";
				String yem_textfieldcn = "";
				if (values != null) {
					yem_textfield = values.get("yem_textfield").toString();
					yem_textfieldcn = values.get("yem_textfieldcn").toString();
				}
				this.getModel().setValue(this.yem_dyqueryfieldcn2, yem_textfieldcn);
				this.getModel().setValue(this.yem_dyqueryfield2, yem_textfield);
			}
		}
	}

	@Override
	public void entryRowClick(RowClickEvent evt) {
		int rowIndex = evt.getRow();
		DynamicObject yem_entryentity = this.getModel().getEntryRowEntity(this.yem_entryentity, rowIndex);
		String yem_fieldtype = yem_entryentity.getString(this.yem_fieldtype);// 数据源类型
		DynamicObject yem_dyquery = yem_entryentity.getDynamicObject(this.yem_dyquery);// 动态数据源选择
		String yem_dyqueryfieldcn = yem_entryentity.getString(this.yem_dyqueryfieldcn);// 动态数据源字段
		String yem_dyqueryfield = yem_entryentity.getString(this.yem_dyqueryfield);// 动态数据源字段
		String yem_fieldchoice = yem_entryentity.getString(this.yem_fieldchoice);// 实体数据源字段
		String yem_field = yem_entryentity.getString(this.yem_field);// 字段标识
		String yem_fieldname = yem_entryentity.getString(this.yem_fieldname);// 字段名称
		String yem_entity = yem_entryentity.getString(this.yem_entity);// 分录标识
		String yem_entityname = yem_entryentity.getString(this.yem_entityname);// 分录名称
		String yem_bill = yem_entryentity.getString(this.yem_bill);// 单据标识
		String yem_billname = yem_entryentity.getString(this.yem_billname);// 单据名称
		boolean yem_isentity = yem_entryentity.getBoolean(this.yem_isentity);// 是否分录标识
		String yem_parententry = yem_entryentity.getString(this.yem_parententry);// 关联分录书签标识
		int yem_colindex = yem_entryentity.getInt(this.yem_colindex);// 分录列序号
		String yem_datatype = yem_entryentity.getString(this.yem_datatype);// 数据类型
		int yem_precision = yem_entryentity.getInt(this.yem_precision);// 精度

		this.getModel().setValue(this.yem_fieldtype2, yem_fieldtype);
		this.getModel().setValue(this.yem_dyquery2, yem_dyquery);
		this.getModel().setValue(this.yem_dyqueryfieldcn2, yem_dyqueryfieldcn);
		this.getModel().setValue(this.yem_dyqueryfield2, yem_dyqueryfield);
		this.getModel().setValue(this.yem_fieldchoice2, yem_fieldchoice);
		this.getModel().setValue(this.yem_field2, yem_field);
		this.getModel().setValue(this.yem_fieldname2, yem_fieldname);
		this.getModel().setValue(this.yem_entity2, yem_entity);
		this.getModel().setValue(this.yem_entityname2, yem_entityname);
		this.getModel().setValue(this.yem_bill2, yem_bill);
		this.getModel().setValue(this.yem_billname2, yem_billname);
		this.getModel().setValue(this.yem_isentity2, yem_isentity);
		this.getModel().setValue(this.yem_parententry2, yem_parententry);
		this.getModel().setValue(this.yem_colindex2, yem_colindex);
		this.getModel().setValue(this.yem_datatype2, yem_datatype);
		this.getModel().setValue(this.yem_precision2, yem_precision);

		if ("".equals(yem_fieldtype)) {
			this.getView().setVisible(false, this.yem_dyquery2);
			this.getView().setVisible(false, this.yem_dyqueryfieldcn2);
			this.getView().setVisible(false, this.yem_fieldchoice2);
			this.getView().setVisible(false, this.yem_parententry2);
			this.getView().setVisible(false, this.yem_colindex2);
			this.getView().setVisible(false, this.yem_datatype2);
			this.getView().setVisible(false, this.yem_precision2);
		} else if ("A".equals(yem_fieldtype)) {
			this.getView().setVisible(false, this.yem_dyquery2);
			this.getView().setVisible(false, this.yem_dyqueryfieldcn2);
			this.getView().setVisible(true, this.yem_fieldchoice2);
			this.getView().setVisible(true, this.yem_parententry2);
			this.getView().setVisible(true, this.yem_colindex2);
			this.getView().setVisible(true, this.yem_datatype2);
			this.getView().setVisible(true, this.yem_precision2);
		} else if ("B".equals(yem_fieldtype)) {
			this.getView().setVisible(true, this.yem_dyquery2);
			this.getView().setVisible(true, this.yem_dyqueryfieldcn2);
			this.getView().setVisible(false, this.yem_fieldchoice2);
			this.getView().setVisible(true, this.yem_parententry2);
			this.getView().setVisible(true, this.yem_colindex2);
			this.getView().setVisible(true, this.yem_datatype2);
			this.getView().setVisible(true, this.yem_precision2);
		}
	}

	@Override
	public void onGetAllCheckBoxes(WebOfficeCheckboxEvent arg0) {

	}

	@Override
	public void onGetBookmarkCheckResult(WebOfficeBookmarkCheckEvent arg0) {

	}

	@Override
	public void onGetDocChangeCheckResult(WebOfficeDocChangeEvent arg0) {

	}

	@Override
	public void onGetTextByBookmark(WebOfficeBookmarkValueEvent arg0) {

	}

	@Override
	public void onIsFieldRevise(WebOfficeIsFieldReviseEvent arg0) {

	}

	@Override
	public void onSendOpenStatus(WebOfficeOpenStatusEvent arg0) {

	}

	@Override
	public void beforeF7Select(BeforeF7SelectEvent arg0) {
	}
}
