package com.yem.wm.bc.common;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.entity.OrmLocaleValue;
import kd.bos.entity.datamodel.events.ChangeData;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.user.UserServiceHelper;

import java.math.BigDecimal;
import java.util.*;

/**
 * @author SC
 * @ClassName CommonUtils
 * @description 外贸公共类
 * @date 2023年06月15日
 * @version: 1.0
 */
public class CommonUtils {

	/**
	 * @MethodName	getUSerID
	 * @author  SC
	 * @Description 获取当前用户ID
	 * @Date 14:42 2023/6/20
	 * @Param   默认当前登录用户
	 * @return  用户ID
	 **/
	public static long getUserID() {
		return UserServiceHelper.getCurrentUserId();
	}

	/**
	 * @return long
	 * @MethodName getUSerDy
	 * @Author SC
	 * @Description 获取当前用户对象；默认显示 为名称Name
	 * @Date 15:02 2023/6/20
	 * @Param 获取当前用户对象
	 **/
	public static DynamicObject getUserDy() {
		return UserServiceHelper.getCurrentUser("name");
	}

	/**
	 * @MethodName getOperator
	 * @Author  SC
	 * @Description 获取用户对应业务员
	 * @Date 17:24 2023/6/20
	 * @Param [USerID, opergrptype] [long, java.lang.String] 人员ID; 业务组类型
	 * @return kd.bos.dataentity.entity.DynamicObject 业务员
	 **/
	public static DynamicObject getOperator(long USerID ,String opergrptype) {
		QFilter qFilter = new QFilter("invalid", QCP.equals, false);
		QFilter qFilter1 = new QFilter("operatorid", QCP.equals, USerID);
		QFilter qFilter2 = new QFilter("opergrptype", QCP.equals, opergrptype);
		DynamicObject[] puroperators = BusinessDataServiceHelper.load("bd_operator", "id,operatornumber,operatorname,opergrptype",
				new QFilter[] { qFilter,qFilter1,qFilter2});
		if (puroperators != null && puroperators.length > 0) {
			return puroperators[0];
		}
		return null;
	}

	/**
	 * @MethodName getOperatorGroup
	 * @Author  SC
	 * @Description 获取当前用户对应业务组
	 * @Date 17:47 2023/6/20
	 * @Param [USerID, opergrptype] [long, java.lang.String] 人员ID; 业务组类型
	 * @return kd.bos.dataentity.entity.DynamicObject 业务组
	 **/

	public static DynamicObject getOperatorGroup(long USerID , String opergrptype) {
		QFilter qFilter = new QFilter("enable", QCP.equals, "1");
		QFilter qFilter1 = new QFilter("operatorgrouptype", QCP.equals, opergrptype);
		QFilter qFilter2 = new QFilter("entryentity.operator", QCP.equals, USerID);
		QFilter qFilter3 = new QFilter("entryentity.invalid", QCP.equals, false);
		DynamicObject[] puroperators = BusinessDataServiceHelper.load("bd_operatorgroup", "id,number,name,operatorgrouptype,entryentity.operator,entryentity.invalid",
				new QFilter[] { qFilter,qFilter1,qFilter2,qFilter3});
		if (puroperators != null && puroperators.length > 0) {
			return puroperators[0];
		}
		return null;
	}

	/**
	 * @MethodName getMainUserdep
	 * @Author  SC
	 * @Description 获取当前用户对应主部门
	 * @Date 18:08 2023/6/20
	 * @Param [USerID] [long] 用户ID
	 * @return kd.bos.dataentity.entity.DynamicObject 部门
	 **/
	public static DynamicObject getMainUserdep(long USerID) {
		DynamicObject  user= BusinessDataServiceHelper.loadSingle(USerID,"bos_user");
		DynamicObjectCollection depentrycol = user.getDynamicObjectCollection("entryentity");
		for(DynamicObject depentry:depentrycol){
			if(!depentry.getBoolean("ispartjob")){
				return depentry.getDynamicObject("dpt");
			}

		}
		return null;
	}

	/**
	 * 获取当前用户、当前组织对应部门
	 * @param orgID 组织ID
	 * @param userID 用户ID
	 * @return
	 */
	public static DynamicObject getOrgUserDep(long orgID,long userID) {
		String longnumber = "";
		QFilter qFilter = new QFilter("org", QCP.equals, orgID);
		qFilter.and(new QFilter("view.number", QCP.equals, "15"));
		DynamicObject[] bos_org_s = BusinessDataServiceHelper.load("bos_org_structure",
				"id,name,number,longnumber", new QFilter[] { qFilter }, null);
		if(null != bos_org_s && bos_org_s.length>0){
			longnumber = bos_org_s[0].getString("longnumber");
		}

		Map org_map = new HashMap();
		qFilter = new QFilter("longnumber", QCP.like, longnumber+"%");
		bos_org_s = BusinessDataServiceHelper.load("bos_org_structure","id,name,number,org", new QFilter[] { qFilter }, null);
		if(null != bos_org_s && bos_org_s.length>0){
			for(DynamicObject bos_org:bos_org_s){
				org_map.put(bos_org.getDynamicObject("org").getLong("id"),bos_org.getDynamicObject("org"));
			}
		}
		DynamicObject user = BusinessDataServiceHelper.loadSingle(userID,"bos_user");
		DynamicObjectCollection depentrycol = user.getDynamicObjectCollection("entryentity");
		for(DynamicObject depentry:depentrycol){
			boolean isPartJob = depentry.getBoolean("ispartjob");
			DynamicObject dpt = depentry.getDynamicObject("dpt");
			if(null != dpt){
//				long dpt_id = dpt.getLong("id");
//				if(org_map.containsKey(dpt_id)){
//					return dpt;
//				}
				if(!isPartJob){
					return dpt;
				}
			}
		}
		return null;
	}

	/**
	 * 获取当前用户、当前组织对应部门
	 * @param orgID 组织ID
	 * @param userID 用户ID
	 * @return
	 */
	public static List getOrgUserDeps(long orgID,long userID) {
		List list = new ArrayList();
		String longnumber = "";
		QFilter qFilter = new QFilter("org", QCP.equals, orgID);
		qFilter.and(new QFilter("view.number", QCP.equals, "15"));
		DynamicObject[] bos_org_s = BusinessDataServiceHelper.load("bos_org_structure",
				"id,name,number,longnumber", new QFilter[] { qFilter }, null);
		if(null != bos_org_s && bos_org_s.length>0){
			longnumber = bos_org_s[0].getString("longnumber");
		}

		Map org_map = new HashMap();
		qFilter = new QFilter("longnumber", QCP.like, longnumber+"%");
		bos_org_s = BusinessDataServiceHelper.load("bos_org_structure","id,name,number,org", new QFilter[] { qFilter }, null);
		if(null != bos_org_s && bos_org_s.length>0){
			for(DynamicObject bos_org:bos_org_s){
				org_map.put(bos_org.getDynamicObject("org").getLong("id"),bos_org.getDynamicObject("org"));
			}
		}
		DynamicObject user = BusinessDataServiceHelper.loadSingle(userID,"bos_user");
		DynamicObjectCollection depentrycol = user.getDynamicObjectCollection("entryentity");
		for(DynamicObject depentry:depentrycol){
			DynamicObject dpt = depentry.getDynamicObject("dpt");
			if(null != dpt){
				long dpt_id = dpt.getLong("id");
				if(org_map.containsKey(dpt_id)){
					list.add(dpt_id);
				}
			}
		}
		return list;
	}

	/**
	 * @MethodName getAllUserdep
	 * @Author  SC
	 * @Description 获取当前用户对应部门
	 * @Date 18:08 2023/6/20
	 * @Param [USerID] [long] 用户ID
	 * @return java.util.List<java.lang.Long> 	部门ID集合
	 **/
	public static List<Long> getAllUserdep(long USerID) {
		List<Long> depIdList =  UserServiceHelper.getOrgsUserJoin(USerID);
		return depIdList;
	}

	/**
	 * @MethodName getdepUser
	 * @Author  SC
	 * @Description 获取部门对应所有用户
	 * @Date 14:35 2023/6/21
	 * @Param [ordid, isMain] [long, boolean] 组织ID ； 是否主要部门
	 * @return java.util.List<java.lang.Long> 用户集合
	 **/
	public static List<Long> getdepUser(long ordid, boolean isMain) {
		List<Long> UserIdList = new ArrayList<>();

		QFilter qFilter = new QFilter("entryentity.dpt.id", QCP.equals, ordid);
		if(isMain){
			QFilter qFilter1 = new QFilter("entryentity.ispartjob", QCP.equals, false);
			qFilter.and(qFilter1);
		}
		DynamicObject[] userCol = BusinessDataServiceHelper.load("bos_user", "id,number,name,entryentity.dpt,entryentity.ispartjob",
				new QFilter[] { qFilter});
		for(DynamicObject user:userCol){
			UserIdList.add(user.getLong("id"));
		}

		return UserIdList;
	}


	public static boolean isNull(Object o) {
		if (o == null) {
			return true;
		} else if (o instanceof String && ((String)o).length() == 0) {
			return true;
		} else if (o instanceof StringBuffer && ((StringBuffer)o).length() == 0) {
			return true;
		} else if (o instanceof List && ((List<?>)o).isEmpty()) {
			return true;
		} else if (o instanceof Collection && ((Collection<?>)o).isEmpty()) {
			return true;
		} else if (o instanceof Object[] && ((Object[])((Object[])o)).length == 0) {
			return true;
		} else {
			return o instanceof Map && ((Map<?, ?>)o).size() == 0;
		}
	}

	public static boolean isRealChanged(ChangeData changeSet) {
		if (changeSet == null) {
			return false;
		} else {
			Object oldObject = changeSet.getOldValue();
			Object newObject = changeSet.getNewValue();
			return isRealChanged(oldObject, newObject);
		}
	}

	public static boolean isRealChanged(Object oldObject, Object newObject) {
		if (oldObject == null && newObject == null) {
			return false;
		} else if (oldObject == null) {
			return true;
		} else if (newObject == null) {
			return true;
		} else {
			if (oldObject instanceof BigDecimal) {
				if (newObject instanceof BigDecimal && ((BigDecimal)oldObject).compareTo((BigDecimal)newObject) != 0) {
					return true;
				}

				if (newObject instanceof String && ((BigDecimal)oldObject).compareTo(new BigDecimal(newObject.toString())) != 0) {
					return true;
				}
			} else if (oldObject.getClass().isAssignableFrom(newObject.getClass())) {
				if (newObject instanceof DynamicObject) {
					if (!((DynamicObject)newObject).getPkValue().equals(((DynamicObject)oldObject).getPkValue())) {
						return true;
					}
				} else {
					if (!(newObject instanceof OrmLocaleValue) || !(oldObject instanceof OrmLocaleValue)) {
						return !oldObject.equals(newObject);
					}

					if (((OrmLocaleValue)newObject).getLocaleValue() == null) {
						return true;
					}

					if (((OrmLocaleValue)oldObject).getLocaleValue() == null) {
						return true;
					}

					if (!((OrmLocaleValue)oldObject).getLocaleValue().equals(((OrmLocaleValue)newObject).getLocaleValue())) {
						return true;
					}
				}
			}

			return false;
		}
	}


	/**
	 * 获取组织综合本位币
	 * @param orgId
	 * @return
	 */
	public static DynamicObject getBaseCurrrency(long orgId){
		DynamicObject basecurrrency = null;
		QFilter qFilter = new QFilter("baseacctorg", QCP.equals, orgId);
		DynamicObject[] bd_accountingsys_base_s = BusinessDataServiceHelper.load("bd_accountingsys_base",
				"id,basecurrrency,exratetable", new QFilter[] { qFilter }, null);
		if(null != bd_accountingsys_base_s && bd_accountingsys_base_s.length>0){
			DynamicObject bd_accountingsys_base = bd_accountingsys_base_s[0];
			basecurrrency = bd_accountingsys_base.getDynamicObject("basecurrrency");
		}
		return basecurrrency;
	}

	/**
	 * 获取组织综合本位币
	 * @param orgId
	 * @return
	 */
	public static DynamicObject getExratetable(long orgId){
		DynamicObject exratetable = null;
		QFilter qFilter = new QFilter("baseacctorg", QCP.equals, orgId);
		DynamicObject[] bd_accountingsys_base_s = BusinessDataServiceHelper.load("bd_accountingsys_base",
				"id,basecurrrency,exratetable", new QFilter[] { qFilter }, null);
		if(null != bd_accountingsys_base_s && bd_accountingsys_base_s.length>0){
			DynamicObject bd_accountingsys_base = bd_accountingsys_base_s[0];
			exratetable = bd_accountingsys_base.getDynamicObject("exratetable");
		}
		return exratetable;
	}
}
