<%#
 Copyright 2013-2023 the original author or authors from the JHipster project.

 This file is part of the JHipster project, see https://www.jhipster.tech/
 for more information.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      https://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-%>
<%_ if (reactive) { _%>
        return <%= entityInstance %><%= viaService ? 'Service.delete' : 'Repository.deleteById' %>(id)
  <%_ if (!viaService && searchEngineElasticsearch) { %>
            .then(<%= entityInstance %>SearchRepository.deleteById(id))
  <%_ } -%>
  <%_ if (!fromResource) { -%>
        ;
  <%_ } _%>
<%_ } else { _%>
    <%# add by begcode start %>
    <%_ if (fieldsContainParentAndChildren && !viaService) { _%>
      <%_ if (ormTool === 'mybatis' && databaseTypeSql) { _%>
        <%= entityClass %> <%= entityInstance %> = <%= entityInstance %>Repository.selectById(id);
      <%_ } else { _%>
        <%_ if (databaseTypeSql) { _%>
        <%= entityClass %> <%= entityInstance %> = <%= entityInstance %>Repository.getOne(id);
        <%_ } else if (databaseTypeMongodb) { _%>
        <%= entityClass %> <%= entityInstance %> = <%= entityInstance %>Repository.findById(id).get();
        <%_ } else { _%>
        <%= entityClass %> <%= entityInstance %> = <%= entityInstance %>Repository.findById(id).get();
        <%_ }  _%>
      <%_ }  _%>
      <%_ if (ormTool === 'hibernate') { _%>
        if (<%= entityInstance %>.getParent() != null) {
            <%= entityInstance %>.getParent().removeChildren(<%= entityInstance %>);
        }
      <%_ } _%>
        if ( <%= entityInstance %>.getChildren() != null) {
      <%_ if (ormTool === 'hibernate') { _%>
            <%= entityInstance %>.getChildren().forEach(sub<%= entityClass %> ->sub<%= entityClass %>.setParent(null));
      <%_ } else { _%>
            <%= entityInstance %>.getChildren().forEach(sub<%= entityClass %> ->sub<%= entityClass %>.setParent(null));
            // todo 可能涉及到级联删除，需要手动处理，上述代码无效。
      <%_ } _%>
        }
  <%_ }  _%>
<%# 如果存在单身一对多和单向多对多应该在这里处理 %>
  <%_ if (ormTool === 'mybatis' && !viaService) { _%>
    <%_ const { showInOneConfig, n2nConfig } = (bcEntityData || { formFieldLines: [], showInOneRelationships: {}, showInOneConfig: {} }); _%>
    <%_ if (showInOneConfig && Object.keys(showInOneConfig).length > 0) { _%>
        <%_ if (Object.keys(showInOneConfig).length === 1)  {
            const showInOneConfigRelationship = showInOneConfig[Object.keys(showInOneConfig)[0]].relationship;
            let relationshipNameCapitalized = showInOneConfigRelationship.otherRelationship.relationshipNameCapitalized;
            if (showInOneConfigRelationship.otherRelationship.relationshipRightSide && showInOneConfigRelationship.otherRelationship.bcConfig.leftRelateByIdEntity) {
                relationshipNameCapitalized = 'OwnerEntity';
            }
        _%>
          <%_ if (relationshipNameCapitalized === 'OwnerEntity') { _%>
        this.deleteEntityAndRelatedEntities(id, <%= showInOneConfigRelationship.otherEntity.persistClass %>.class, <%= showInOneConfigRelationship.otherEntityNameCapitalized %>::set<%= relationshipNameCapitalized %>Id, <%= showInOneConfigRelationship.otherEntityNameCapitalized %>::set<%= relationshipNameCapitalized %>Name, "<%= persistClass %>");
          <%_ } else { _%>
        this.deleteEntityAndRelatedEntities(id, <%= showInOneConfigRelationship.otherEntity.persistClass %>.class, <%= showInOneConfigRelationship.otherEntityNameCapitalized %>::set<%= relationshipNameCapitalized %>Id);
          <%_ } _%>
        <%_ } else { _%>
        // 关联属性设置为空集合
        <%= persistInstance %><%- Object.keys(showInOneConfig).map(key => `.${key}(new ArrayList<>())`).join('') %>;
        this.createOrUpdateN2NRelations(<%= persistInstance %>, Arrays.asList("<%- Object.keys(showInOneConfig).join('", "') %>"));
        <%= entityInstance %><%= viaService ? 'Service.delete' : 'Repository.deleteById' %>(id);
        <%_ } _%>
      <%_ } else if (n2nConfig && Object.keys(n2nConfig).length > 0) { _%>
        // 关联属性设置为空集合
        <%= persistInstance %><%- Object.keys(n2nConfig).map(key => `.${key}(new ArrayList<>())`).join('') %>;
        this.createOrUpdateN2NRelations(<%= persistInstance %>, Arrays.asList("<%- Object.keys(n2nConfig).join('", "') %>"));
        <%= entityInstance %><%= viaService ? 'Service.delete' : 'Repository.deleteById' %>(id);
      <%_ } else { _%>
        <%= entityInstance %><%= viaService ? 'Service.delete' : 'Repository.deleteById' %>(id);
      <%_ } _%>
  <%_ } else { _%>
        <%= entityInstance %><%= viaService ? 'Service.delete' : 'Repository.deleteById' %>(id);
  <%_ } _%>
  <%_ if (!viaService && searchEngineElasticsearch) { _%>
        <%= entityInstance %>SearchRepository.deleteFromIndexById(id);
  <%_ } _%>
<%_ } _%>
