package com.yem.tws.common1;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;

import static com.yem.tws.common1.InvoicingRemarksUtils.getRemarks;

/**
 * 公共类
 *
 */
public class CommonUtils {

	/**
	 * 获取当前登录用户主键
	 *
	 */
	public static Long getCurrentUserId() {
		return RequestContext.get().getCurrUserId();
	}

	/**
	 * 获取当前登录用户对象
	 *
	 */
	public static DynamicObject getCurrentUser() {
		final long currentUserId = RequestContext.get().getCurrUserId();
		return BusinessDataServiceHelper.loadSingleFromCache((Object) currentUserId, "bos_user");
	}

	/**
	 * 判断对象是否为空
	 *
	 * @param o
	 * @return
	 */
	public static boolean isNull(final Object o) {
		return o == null || (o instanceof String && ((String) o).length() == 0)
				|| (o instanceof StringBuffer && ((StringBuffer) o).length() == 0)
				|| (o instanceof List && ((List) o).size() == 0)
				|| (o instanceof Collection && ((Collection) o).size() == 0)
				|| (o instanceof Object[] && ((Object[]) o).length == 0) || (o instanceof Map && ((Map) o).size() == 0);
	}

	/**
	 * 开票备注
	 *
	 * @return
	 */
	public static DynamicObject billingnote(DynamicObject yem_ca_billlogon) {
		DynamicObject yem_transmodbase = yem_ca_billlogon.getDynamicObject("yem_transmodbase");
		if (yem_transmodbase != null) {
			QFilter qFilter = new QFilter("yem_isdefault", QCP.equals, true);
			qFilter.and(new QFilter("yem_transmodbase.name", QCP.equals, yem_transmodbase.getString("name")));
			DynamicObject[] invoicingremarks = BusinessDataServiceHelper.load("yem_invoicingremarks",
					"id,name,number",
					new QFilter[]{qFilter}, null);
			if (invoicingremarks.length > 0) {
				long InvoicingRemarksId = invoicingremarks[0].getLong("id");
				String yem_billingnote = getRemarks(InvoicingRemarksId, yem_ca_billlogon.getLong("id"), "yem_ca_cusdec");
				yem_ca_billlogon.set("yem_invoicingremarks", invoicingremarks[0]);
				yem_ca_billlogon.set("yem_billingnote", yem_billingnote.trim());

			}
		}
		return yem_ca_billlogon;
	}
}
