package yem.wm.cm.ht.list;

import java.util.ArrayList;
import java.util.List;



import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.entity.LocaleString;
import kd.bos.dataentity.utils.StringUtils;
import kd.bos.entity.datamodel.ListSelectedRowCollection;
import kd.bos.form.CloseCallBack;
import kd.bos.form.ShowFormHelper;
import kd.bos.form.events.AfterDoOperationEventArgs;
import kd.bos.form.events.ClosedCallBackEvent;
import kd.bos.list.IListView;
import kd.bos.list.ListShowParameter;
import kd.bos.list.plugin.AbstractListPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import yem.base.common.utils.YEM;

/**
 * 我方合同模板---跳转合同条款库
 * @author zhangh
 *
 */
public class OurTemplateListPlugin extends AbstractListPlugin {

	@Override
	public void afterDoOperation(AfterDoOperationEventArgs e) {
		String key = e.getOperateKey();
		switch (key) {
		case "batchadjust":
			showBillForm((IListView) this.getView());
			break;

		default:
			break;
		}
	}

	public void showBillForm(IListView view) {
		ListShowParameter showParameter = ShowFormHelper.createShowListForm("yem_cm_contractterms", false);
		showParameter.setCloseCallBack(new CloseCallBack(this, "yem_cm_contractterms"));
		view.showForm(showParameter);
	}

	@Override
	public void closedCallBack(ClosedCallBackEvent e) {
		String actionId = e.getActionId();
		Object returnData = e.getReturnData();
		switch (actionId) {
		case "yem_cm_contractterms":
			updateData(returnData);
			break;

		default:
			break;
		}
	}

	/**
	 * 修改合同条款库数据
	 * @param returnData
	 */
	public void updateData(Object returnData) {
		if (returnData != null) {
			ListSelectedRowCollection  listSelect = (ListSelectedRowCollection) returnData;
			Object listSelectedRow = listSelect.get(0).getPrimaryKeyValue();
			DynamicObject data = BusinessDataServiceHelper.loadSingleFromCache(listSelectedRow, "yem_cm_contractterms");
			List<QFilter> qFilters = new ArrayList<>();
			qFilters.add(new QFilter("yem_contractterms.yem_termname", QCP.equals, data.getLong("id")));
			DynamicObject[] cm_contracttemplate = BusinessDataServiceHelper.load("yem_cm_contracttemplate", "id",
					qFilters.toArray(new QFilter[] {}));
			for (DynamicObject contracttemplate : cm_contracttemplate) {
				contracttemplate = BusinessDataServiceHelper.loadSingle(contracttemplate.getLong("id"),
						contracttemplate.getDataEntityType().getName());
				DynamicObjectCollection yem_contractterms = contracttemplate.getDynamicObjectCollection("yem_contractterms");
				for (DynamicObject contractterms : yem_contractterms) {
					DynamicObject yem_termname = contractterms.getDynamicObject("yem_termname");
					if (yem_termname != null && yem_termname.getLong("id") == data.getLong("id")) {
						String yem_tradetype = contracttemplate.getString("yem_tradetype");
						String yem_clauseschina = "";
						if (StringUtils.isNotEmpty(yem_tradetype)) {
							if (yem_tradetype.equals("A")) {
								yem_clauseschina = data.getString("yem_clausecontentchina") + "\n" + yem_termname.getString("yem_clausecontentenglish");
								
							} else {
								yem_clauseschina = data.getString("yem_clausecontentchina");
							}
						}
						DynamicObject yem_clausetype = data.getDynamicObject("yem_clausetype");
						contractterms.set("yem_clausetype", yem_clausetype);
						contractterms.set("yem_clausescontent", yem_clauseschina);
						contractterms.set("yem_clausescontent_tag", yem_clauseschina);
						contractterms.set("yem_contentbefore", yem_clauseschina);
						contractterms.set("yem_contentbefore_tag", yem_clauseschina);
						
						contractterms.set("yem_describe", data.getString("yem_textfield"));
						LocaleString locStr = new LocaleString();
						locStr.setLocaleValue_zh_CN(data.getString("yem_clausechina"));
						locStr.setLocaleValue_en(data.getString("yem_clauseenglish"));
						contractterms.set("yem_clausename", locStr);
						
						String yem_termbillno = contractterms.getString("yem_termbillno");
						if (YEM.isNotEmpty(yem_termbillno)) {
							String termbillno = yem_termbillno.split("、")[0];
							contractterms.set("yem_termbillno", termbillno + "、" + yem_clausetype.getString("yem_clausename"));
						}
					}
				}
				SaveServiceHelper.save(new DynamicObject[] {contracttemplate});
			}
		}
	}
}
