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.form.events.SetFilterEvent;
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.user.UserServiceHelper;

/**
 * 合同条款库列表根据当前人所在的部门进行过滤
 * @author zhangh
 *
 */
public class ContractTermsListPlugin extends AbstractListPlugin{

	@Override
	public void setFilter(SetFilterEvent e) {
		deptFilterF7(e);
	}

	/**
	 * 根据当前人进行过滤
	 * @param e
	 */
	private void deptFilterF7(SetFilterEvent e) {
		long currentUserId = UserServiceHelper.getCurrentUserId();
		DynamicObject bos_adminorg = BusinessDataServiceHelper.loadSingle("bos_adminorg", "id", new QFilter[] {new QFilter("number", QCP.equals, "XYHG")});
		DynamicObject adminorg = BusinessDataServiceHelper.loadSingle("bos_adminorg", "id", new QFilter[] {new QFilter("number", QCP.equals, "Gent")});
		DynamicObject IT = BusinessDataServiceHelper.loadSingle("bos_adminorg", "id", new QFilter[] {new QFilter("name", QCP.equals, "IT")});
		DynamicObject bos_org = BusinessDataServiceHelper.loadSingle("bos_org", "id", new QFilter[] {new QFilter("number", QCP.equals, "ZJQYSY")});
		DynamicObject bos_user = BusinessDataServiceHelper.loadSingleFromCache(currentUserId, "bos_user");
		if (bos_user != null) {
			List<Long> dept = new ArrayList<>();
			DynamicObjectCollection entryentity = bos_user.getDynamicObjectCollection("entryentity");
			for (DynamicObject row : entryentity) {
				dept.add(row.getDynamicObject("dpt").getLong("id"));
			}
			if (dept != null && IT != null) {
				if (dept.size() > 0 && !dept.contains(IT.getLong("id"))) {
					if (dept.size() > 0 && !dept.contains(bos_adminorg.getLong("id")) && !dept.contains(adminorg.getLong("id"))) {
						List<QFilter> qFilters = e.getQFilters();
						QFilter qFilter = new QFilter("yem_dept", QCP.in, dept).or(new QFilter("yem_sharedscope.fbasedataid", QCP.in, dept));
						qFilters.add(qFilter);
						e.setCustomQFilters(qFilters);
					} else if (dept.contains(bos_adminorg.getLong("id"))) {//信用合规部不能看到青云的数据
						List<QFilter> qFilters = e.getQFilters();
						QFilter qFilter = new QFilter("org", QCP.not_equals, bos_org.getLong("id"));
						qFilters.add(qFilter);
						e.setCustomQFilters(qFilters);
					}
				}
			}
		}
	}
}
