export default class Utils{
    static toDepthObject(obj)
    {
        var target = {};
        for(var key in obj)
        {
            var keyArr = key.split("_"); //以下划线或分割
            var tempobj = target;
            for(var i=0;i<keyArr.length - 1;i++)
            {
                if(tempobj[keyArr[i]])
                    tempobj = tempobj[keyArr[i]];
                else
                {
                    tempobj[keyArr[i]] = {};
                    tempobj = tempobj[keyArr[i]];
                }	
            }
            //给叶子元素赋值
            tempobj[keyArr[keyArr.length - 1]]=obj[key];
        }
        return target;
    }
    static flatObject(obj,target,pkey)
	{
		target = target||{};
		for(var key in obj)
		{
			var nkey = pkey?pkey+"_"+key:key;
			if(!obj[key])
				target[nkey] = obj[key];
			else if(obj[key] instanceof Array)
                target[nkey] = obj[key];
            else if(obj[key] instanceof Date)
                target[nkey] = obj[key];
			else if(typeof obj[key] == "object")
                Utils.flatObject(obj[key],target,nkey); //递归赋值
			else
				target[nkey] = obj[key];
		}
		return target;
    }
    static copyValueFromVO(model,vo)
    {
        var target = model;
        for(var key in vo)
        {
            var keyArr = key.split("_"); //以下划线或分割
            var tempobj = target;
            for(var i=0;i<keyArr.length - 1;i++)
            {
                if(tempobj[keyArr[i]])
                    tempobj = tempobj[keyArr[i]];
                else
                {
                    tempobj = null;
                    break;
                } 
            }
            //给叶子元素赋值
            if(tempobj )
            {
                let leafProp = keyArr[keyArr.length - 1];
                if(tempobj.hasOwnProperty(leafProp))
                    tempobj[leafProp]=vo[key];
            }
        }
        return target;
    }
}