package com.yem.ensign.weboffice.form;

import java.util.EventObject;
import java.util.HashMap;
import java.util.Map;

import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.form.events.AfterDoOperationEventArgs;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.DeleteServiceHelper;

/**
 * webOffice模板表单插件
 *@author hdd
 *@date 2020-08-14 下午5:23:11
 */
public class WebOfficeModelEdit extends AbstractBillPlugIn{

	@Override
	public void afterCreateNewData(EventObject e) {
		super.afterCreateNewData(e);
		String yem_bill = this.getView().getFormShowParameter().getCustomParam("yem_bill");
		this.getModel().setValue("yem_bill", yem_bill);
		IDataModel model = this.getModel();
    	DynamicObject createorg =(DynamicObject)  model.getValue("createorg");
    	model.setValue("useorg", createorg);
    	model.setValue("org", createorg);
	}
	
	@Override
	public void afterBindData(EventObject e) {
		super.afterBindData(e);
	}

	@Override
	public void afterDoOperation(AfterDoOperationEventArgs e) {
		super.afterDoOperation(e);
		boolean isSuccess = false;
		if (e.getOperationResult() != null) {
			isSuccess = e.getOperationResult().isSuccess();
		}
		String operateKey = e.getOperateKey();
		if ("deletenew".equals(operateKey)) {
			doDelete();
		}
	}
	
	private void doDelete() {
		long yem_billid = (long) this.getModel().getValue("id");
		DynamicObject webofficemodel = BusinessDataServiceHelper.loadSingle(yem_billid,"yem_webofficemodel");
		String status = webofficemodel.getString("status");
		if("B".equals(status)) {
            this.getView().showErrorNotification("单据已提交，不允许删除！");
            return;
		}else if("C".equals(status)) {
            this.getView().showErrorNotification("单据已审核，不允许删除！");
            return;
		}
		QFilter qFilter1 = new QFilter("id", "=", webofficemodel.getPkValue());
		DeleteServiceHelper.delete("yem_webofficemodel", new QFilter[] { qFilter1 });
		Map map = new HashMap();
		this.getView().returnDataToParent(map);
		this.getView().showMessage("删除成功!");
		this.getView().close();
	}	
}
