package com.yem.wm.bc.change.business;

import kd.bos.dataentity.metadata.IDataEntityProperty;
import kd.bos.dataentity.metadata.IDataEntityType;
import kd.bos.dataentity.metadata.clr.DataEntityPropertyCollection;
import kd.bos.dataentity.metadata.dynamicobject.DynamicPropertyCollection;
import kd.bos.entity.property.EntryProp;

import java.util.Iterator;

/**
 * @author 夜子
 * @ClassName MetaDataHelper
 * @description: 元数据 属性名获取帮助类
 * @date 2023年08月03日
 * @version: 1.0
 */
public class MetaDataHelper {
    public MetaDataHelper() {
    }

    public static String getPropertyAlias(IDataEntityType dt, String propertyName) {
        DynamicPropertyCollection dynPropColl = (DynamicPropertyCollection)dt.getProperties();
        Iterator<IDataEntityProperty> it = dynPropColl.iterator();

        IDataEntityProperty dataEntityProp;
        do {
            if (!it.hasNext()) {
                return null;
            }

            dataEntityProp = (IDataEntityProperty)it.next();
        } while(dataEntityProp instanceof EntryProp || !dataEntityProp.getName().equals(propertyName));

        return dataEntityProp.getDisplayName().toString();
    }

    public static String getEntryPropertyAlias(IDataEntityType dt, String entryName, String propertyName) {
        DynamicPropertyCollection dynPropColl = (DynamicPropertyCollection)dt.getProperties();
        Iterator<IDataEntityProperty> it = dynPropColl.iterator();

        while(true) {
            label54:
            while(it.hasNext()) {
                IDataEntityProperty dataEntityProp = (IDataEntityProperty)it.next();
                DataEntityPropertyCollection entry;
                Iterator itEntry;
                IDataEntityProperty dataEntityPropEntry;
                if (dataEntityProp instanceof EntryProp && dataEntityProp.getName().equals(entryName)) {
                    entry = ((EntryProp)dataEntityProp).getDynamicCollectionItemPropertyType().getProperties();
                    itEntry = entry.iterator();

                    while(itEntry.hasNext()) {
                        dataEntityPropEntry = (IDataEntityProperty)itEntry.next();
                        if (dataEntityPropEntry.getName().equals(propertyName)&&dataEntityPropEntry.getDisplayName()!=null) {
                            return dataEntityPropEntry.getDisplayName().toString();
                        }
                    }
                } else if (dataEntityProp instanceof EntryProp) {
                    entry = ((EntryProp)dataEntityProp).getDynamicCollectionItemPropertyType().getProperties();
                    itEntry = entry.iterator();

                    while(true) {
                        do {
                            do {
                                if (!itEntry.hasNext()) {
                                    continue label54;
                                }

                                dataEntityPropEntry = (IDataEntityProperty)itEntry.next();
                            } while(!(dataEntityPropEntry instanceof EntryProp));
                        } while(!dataEntityPropEntry.getName().equals(entryName));

                        DataEntityPropertyCollection subEntry = ((EntryProp)dataEntityPropEntry).getDynamicCollectionItemPropertyType().getProperties();

                        for (IDataEntityProperty dataEntityPropSubEntry : subEntry) {
                            if (dataEntityPropSubEntry.getName().equals(propertyName) && dataEntityPropSubEntry.getDisplayName() != null) {
                                return dataEntityPropSubEntry.getDisplayName().toString();
                            }
                        }
                    }
                }
            }

            return null;
        }
    }
}