package cn.ibizlab.core.lite.extensions.domain;

import cn.ibizlab.core.lite.domain.MetaEntity;
import cn.ibizlab.core.lite.domain.MetaField;
import cn.ibizlab.core.lite.domain.MetaRelationship;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import org.springframework.util.DigestUtils;
import org.springframework.util.ObjectUtils;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Data
public class SysModel {

    private String systemid;

    @JsonIgnore
    private String systemname;

    private List<MetaEntity> entities;

    /**
     * 获取实体
     * @return
     */
    public Set<MetaEntity>  getEntity(){
        Set<MetaEntity> metaEntities=new HashSet<>();
        metaEntities.addAll(entities);
        return metaEntities;
    }

    /**
     * 获取实体属性
     * @return
     */
    public Set<MetaField>  getField(){
        Set<MetaField> fields=new HashSet<>();
        this.entities.forEach(entityNode->{
            List<MetaField> deField=entityNode.getFields();
            if(!ObjectUtils.isEmpty(deField)){
                deField.forEach(field->{
                    field.setEntityId(DigestUtils.md5DigestAsHex(String.format("%s||%s" ,systemid,entityNode.getEntityName()).getBytes()));
                });
                fields.addAll(deField);
            }
            }
        );
        return fields;
    }

    /**
     * 获取实体关系
     * @return
     */
    public Set<MetaRelationship>  getRelation(){
        Set<MetaRelationship> relations=new HashSet<>();
        this.entities.forEach(entityNode->{
            List<MetaRelationship> parentRelation= entityNode.getParentEntitys();
            List<MetaRelationship> subRelation= entityNode.getSubEntitys();
            if(!ObjectUtils.isEmpty(parentRelation)){
                addToRelations(relations,parentRelation);
            }
            if(!ObjectUtils.isEmpty(subRelation)){
                addToRelations(relations,subRelation);
            }
        });
        return relations;
    }

    private void addToRelations(Set<MetaRelationship> relations, List<MetaRelationship> entityRelations){
        entityRelations.forEach(entityRelation->{
            entityRelation.setEntityId(DigestUtils.md5DigestAsHex(String.format("%s||%s" ,systemid,entityRelation.getEntityName()).getBytes()));
            entityRelation.setRefEntityId(DigestUtils.md5DigestAsHex(String.format("%s||%s" ,systemid,entityRelation.getRefEntityName()).getBytes()));
        });
        relations.addAll(entityRelations);
    }


//    private List<EntityModel> entities;
//
//    /**
//     * 获取实体
//     * @return
//     */
//    public Set<MetaEntity>  getEntity(){
//        Set<MetaEntity> entities=new HashSet<>();
//        this.entities.forEach(entity->{
//            MetaEntity metaEntity=new MetaEntity();
//            metaEntity.setEntityName(entity.getEntityName());
//            metaEntity.setCodeName(entity.getCodeName());
//            metaEntity.setLogicName(entity.getLogicName());
//            metaEntity.setSystemId(entity.getSystemId());
//            metaEntity.setSystemName(entity.getSystemId());
//            metaEntity.setTableName(entity.getTableName());
//            entities.add(metaEntity);
//            }
//        );
//        return entities;
//    }
//
//    /**
//     * 获取实体属性
//     * @return
//     */
//    public Set<MetaField>  getField(){
//        Set<MetaField> fields=new HashSet<>();
//        this.entities.forEach(entity->{
//            entity.getFields().forEach(fieldModel->{
//                    MetaField metaField=new MetaField();
//                    metaField.setCodeName(fieldModel.getCodeName());
//                    metaField.setFieldName(fieldModel.getColumnName());
//                    metaField.setFieldLogicName(fieldModel.getComment());
//                    metaField.setFieldShowName(fieldModel.getShowName());
//                    metaField.setEntityId(DigestUtils.md5DigestAsHex(String.format("%s||%s" ,systemid,entity.getEntityName()).getBytes()));
//                    fields.add(metaField);
//                }
//            );
//            }
//        );
//        return fields;
//    }
//
//    /**
//     * 获取实体关系
//     * @return
//     */
//    public Set<MetaRelationship>  getRelation(){
//        Set<MetaRelationship> relations=new HashSet<>();
//        this.entities.forEach(entity->{
//                    entity.getNesteds().forEach(relation->{
//                            MetaRelationship metaRelationship=new MetaRelationship();
////                            metaRelationship.setName();
//                            metaRelationship.setCodeName(relation.getCodeName());
//                            metaRelationship.setEntityId(DigestUtils.md5DigestAsHex(String.format("%s||%s" ,systemid,relation.getEntityName()).getBytes()));
//                            metaRelationship.setEntityName(relation.getEntityName());
////                            metaRelationship.setRefEntityName();
//                            metaRelationship.setRefEntityId(DigestUtils.md5DigestAsHex(String.format("%s||%s" ,systemid,relation.getEntityName()).getBytes()));
//                        }
//                    );
//                }
//        );
//        return relations;
//    }

}
