<%_ if (position === 'import') { _%>
    <%_ if (databaseTypeSql && ormTool === 'mybatis') { _%>
import <%= packageName %>.util.mybatis.handler.HandlerTableChange;
import cn.hutool.core.util.StrUtil;
import <%= packageName %>.domain.CommonTableField;
import <%= packageName %>.domain.enumeration.RelationshipType;
    <%_ } _%>
<%_ } _%>
<%_ if (position === 'method') { _%>

    /**
     * Save a commonTableRelationship.
     *
     * @param commonTableRelationshipDTO the entity to save.
     * @return the persisted entity.
     */
    <%_ if (databaseTypeSql && ormTool === 'mybatis') { _%>
    @Transactional
    <%_ } _%>
    public CommonTableRelationshipDTO save(CommonTableRelationshipDTO commonTableRelationshipDTO) {
        log.debug("Request to save CommonTableRelationship : {}", commonTableRelationshipDTO);
        // 获得未修改数据
        Optional<CommonTableRelationshipDTO> old = this.findOne(commonTableRelationshipDTO.getId());
        // 提取扩展数据
        Map<String, Object> extData = commonTableRelationshipDTO.getExtData();
        CommonTableRelationship commonTableRelationship = commonTableRelationshipMapper.toEntity(commonTableRelationshipDTO);
        <%_ if (ormTool === 'mybatis' && databaseTypeSql) { _%>
        this.saveOrUpdate(commonTableRelationship);
        // 保存拓展数据
        if (!extData.isEmpty()) {
            this.saveExtData(commonTableRelationship, extData);
        }
        // 获得更新后数据，但不含拓展数据
        CommonTableRelationship tableRelationship = this.getById(commonTableRelationship.getId());
        // 绑定关联数据，但不含拓展数据
        Binder.bindRelations(tableRelationship, new String[]{"extData"});
        CommonTableRelationshipDTO result = commonTableRelationshipMapper.toDto(tableRelationship);
        // 涉及修改表结构，导致无法直接加载新结构时的extData数据。
        // result.setExtData(this.getExtData(result.getId()));
        <%_ if (searchEngine === 'elasticsearch') { _%>
        commonTableRelationshipSearchRepository.save(commonTableRelationship);
        <%_ } _%>
        // 修改相关的表结构
        if (!commonTableRelationshipDTO.getSystem()) {
            if (commonTableRelationshipDTO.getId() == null) {
                HandlerTableChange.compareCommonTableRelationship(result, null);
            } else {
                HandlerTableChange.compareCommonTableRelationship(result, old.get());
            }
        }
        return result;
        <%_ } else { _%>
        this.commonTableRelationshipRepository.save(commonTableRelationship);
        this.commonExtDataService.saveExtData(commonTableRelationship);
        <%_ if (searchEngine === 'elasticsearch') { _%>
        commonTableRelationshipSearchRepository.save(commonTableRelationship);
        <%_ } _%>
        return commonTableRelationshipMapper.toDto(commonTableRelationship);
        <%_ } _%>
    }
<%_ } _%>
