package yem.wm.cm.ht.op;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.args.AfterOperationArgs;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

/**
 * 1、合同条款库版本号修复
 * 2、修复新加字段原条款类型、原条款名称、原条款内容
 * @author zhangh
 *
 */
public class ClauseVersionUpdateOp extends AbstractOperationServicePlugIn{

	@Override
	public void afterExecuteOperationTransaction(AfterOperationArgs e) {
		String key = e.getOperationKey();
		for (DynamicObject dataEntity : e.getDataEntities()) {
			dataEntity = BusinessDataServiceHelper.loadSingleFromCache(dataEntity.getLong("id"), dataEntity.getDataEntityType().getName());
			switch (key) {
			case "version":
				update(dataEntity);
				updateInsertField(dataEntity);
				break;

			default:
				break;
			}
		}
		
	}
	
	/**
	 * 修复新加字段原条款类型、原条款名称、原条款内容
	 * @param dataEntity
	 */
	private void updateInsertField(DynamicObject dataEntity) {
		dataEntity.set("yem_changeclause", dataEntity.getDynamicObject("yem_clausetype"));
		dataEntity.set("yem_changenamechina", dataEntity.getString("yem_clausechina"));
		dataEntity.set("yem_changeclauseeng", dataEntity.getString("yem_clauseenglish"));
		dataEntity.set("yem_changechina", dataEntity.getString("yem_clausecontentchina"));
		dataEntity.set("yem_changechina_tag", dataEntity.getString("yem_clausecontentchina_tag"));
		dataEntity.set("yem_changeenglish", dataEntity.getString("yem_clausecontentenglish"));
		dataEntity.set("yem_changeenglish_tag", dataEntity.getString("yem_clausecontentenglish_tag"));
		SaveServiceHelper.save(new DynamicObject[] {dataEntity});
	}

	/**
	 * 修复版本号
	 * @param dataEntity
	 */
	public void update(DynamicObject dataEntity) {
		dataEntity.set("yem_version", 1);
		dataEntity.set("yem_changeman", null);
		dataEntity.set("yem_changedate", null);
		dataEntity.set("yem_effector", null);
		dataEntity.set("yem_effectdate", null);
		dataEntity.set("yem_masterid", null);
		dataEntity.set("yem_effective", false);
		SaveServiceHelper.save(new DynamicObject[] {dataEntity});
	}
}
