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

<%_ } _%>
<%_ if (position === 'method') { _%>

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