<%_ if (position === 'import') { _%>
import <%= packageName %>.domain.CommonTableField;
import <%= packageName %>.domain.CommonTableRelationship;
import <%= packageName %>.web.rest.errors.BadRequestAlertException;
import <%= packageName %>.repository.CommonTableFieldRepository;
import <%= packageName %>.repository.CommonTableRelationshipRepository;
import <%= packageName %>.security.SecurityUtils;
    <%_ if (databaseTypeSql && ormTool === 'mybatis') { _%>
import cn.hutool.core.util.StrUtil;
import <%= packageName %>.domain.enumeration.RelationshipType;
    <%_ } _%>
<%_ } _%>
<%_ if (position === 'extendsImport') { _%>
import <%= packageName %>.repository.CommonTableFieldRepository;
import <%= packageName %>.repository.CommonTableRelationshipRepository;
<%_ } _%>
<%_ if (position === 'methodInterface') { _%>

    Optional<CommonTableDTO> copyFromId(<%= databaseTypeSql ? 'Long' : 'String' %> commonTableId);

    Optional<CommonTableDTO> findOneByEntityName(String entityName);
<%_ } _%>
<%_ if (position === 'method') { _%>

    <%_ if (databaseTypeSql && ormTool === 'mybatis') { _%>
    @Transactional
    <%_ } _%>
    public Optional<CommonTableDTO> copyFromId(<%= databaseTypeSql ? 'Long' : 'String' %> commonTableId) {
        Optional<CommonTable> byId = commonTableRepository.findById(commonTableId);
        if (byId.isPresent()) {
            CommonTable commonTable = byId.get();
            CommonTable result = new CommonTable();
            BeanUtil.copyProperties(commonTable, result, "creator", "relationships", "commonTableFields");
            result.setName("copy" + result.getName());
            result.setEntityName("copy" + result.getEntityName());
            result.setId(null);
            result.setBaseTableId(commonTableId);
            result.setSystem(false);
            <%_ if (ormTool !== 'mybatis') { _%>
            result = commonTableRepository.save(result);
            CommonTable finalResult = result;
            <%_ } else { _%>
            commonTableRepository.save(result);
            CommonTable finalResult = commonTableRepository.selectById(result.getId());
            <%_ } _%>
            commonTable.getCommonTableFields().forEach(commonTableField -> {
                CommonTableField newField = new CommonTableField();
                BeanUtil.copyProperties(commonTableField, newField, "commonTable");
                newField.setCommonTable(finalResult);
                newField.setId(null);
                commonTableFieldRepository.save(newField);
            });
            commonTable.getRelationships().forEach(commonTableRelationship -> {
                CommonTableRelationship newRelationship = new CommonTableRelationship();
                BeanUtil.copyProperties(commonTableRelationship, newRelationship, "commonTable");
                newRelationship.setCommonTable(finalResult);
                newRelationship.setId(null);
                commonTableRelationshipRepository.save(newRelationship);
            });
            return findOne(result.getId());
        } else {
            throw new BadRequestAlertException("Invalid commonTableId", "CommonTable", "IdNotFound");
        }
    }

    public Optional<CommonTableDTO> findOneByEntityName(String entityName) {
        return commonTableRepository.findOneByEntityName(entityName)
            <%_ if (ormTool === 'mybatis' && databaseTypeSql) { _%>
            .map(commonTable -> {
                Binder.bindRelations(commonTable<%- skipRelationships%>);
                return commonTable;
            })
            <%_ } _%>
            .map(commonTableMapper::toDto);
    }
<%_ } _%>
