<%#
 Copyright 2013-2021 the original author or authors from the BegCode 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.
-%>
<%_ const instanceType = restClass;
const instanceName = restInstance;
const mapper = entityInstance  + 'Mapper';
const dtoToEntity = mapper + '.' + 'toEntity';
const entityToDto = mapper + '.' + 'toDto';
const entityToDtoReference = mapper + '::' + 'toDto';
const returnPrefix = (returnDirectly && !searchEngineElasticsearch) ? 'return' : instanceType + ' result =';
let resultEntity;
let mapsIdEntityInstance;
let mapsIdRepoInstance;
let otherEntityName;

if (isUsingMapsId) {
    mapsIdEntityInstance = mapsIdAssoc.otherEntityNameCapitalized.charAt(0).toLowerCase() + mapsIdAssoc.otherEntityNameCapitalized.slice(1);
    mapsIdRepoInstance = `${mapsIdEntityInstance}Repository`;
    otherEntityName = mapsIdAssoc.otherEntityName;
  if (isController) { _%>
        if (Objects.isNull(<%= instanceName %>.get<%= mapsIdAssoc.relationshipNameCapitalized %>())) {
            throw new BadRequestAlertException("Invalid association value provided", ENTITY_NAME, "null");
        }
  <%_ } _%>
<%_ } _%>
<%_ if (!viaService) { _%>
  <%_ if (dtoMapstruct) { _%>
    <%_ resultEntity = persistInstance; _%>
    <%_ if (extendData) { _%>
        Map<String, Object> extData = <%= instanceName %>.getExtData();
    <%_ } _%>
        <%= persistClass %> <%= persistInstance %> = <%= dtoToEntity %>(<%= instanceName %>);
    <%_ if (isUsingMapsId) { _%>
        <%= mapsIdAssoc.otherEntity.primaryKey.type %> <%= otherEntityName %>Id = <%= instanceName %>.get<%= mapsIdAssoc.relationshipNameCapitalized %>().get<%= primaryKey.nameCapitalized %>();
        <%= mapsIdRepoInstance %>.findById(<%= otherEntityName %>Id).ifPresent(<%= persistInstance %>::<%_ if (!fluentMethods) { _%>set<%= mapsIdAssoc.relationshipNameCapitalized %> <%_ } else { _%><%= mapsIdAssoc.relationshipName %><%_ } _%>);
    <%_ } _%>
    <%_ if (fieldsContainParentAndChildren) { _%>
        clearChildrenCache();
    <%_ }  _%>
        <%= persistInstance %> = <%= persistInstance %>Repository.save(<%= persistInstance %>);
    <%_ if (extendData) { _%>
        <%_ if (multiModel) { %>
        <%= persistInstance %>.setExtData(extData);
        commonExtDataService.saveExtDataByModelId(<%= persistInstance %>,<%= instanceName %>.get<%= upperFirstCamelCase(multiModel)%>Id());
        <%_ } else { _%>
        <%= persistInstance %>.setExtData(extData);
        commonExtDataService.saveExtData(<%= persistInstance %>);
        <%_ } _%>
    <%_ } _%>
    <%_ if (fieldsContainParentAndChildren) { _%>
        // 更新缓存
        if (<%= persistInstance %>.getParent() != null) {
            <%= persistInstance %>.getParent().addChildren(<%= persistInstance %>);
        }
    <%_ } _%>
        <%= returnPrefix %> <%= entityToDto %>(<%= persistInstance %>);
  <%_ } else {
      resultEntity = 'result'; _%>
    <%_ if (isUsingMapsId) { _%>
        <%= mapsIdAssoc.otherEntity.primaryKey.type %> <%= otherEntityName %>Id = <%= instanceName %>.get<%= mapsIdAssoc.relationshipNameCapitalized %>().get<%= primaryKey.nameCapitalized %>();
        <%= mapsIdRepoInstance %>.findById(<%= otherEntityName %>Id).ifPresent(<%= instanceName %>::<%_ if (!fluentMethods) { _%>set<%= mapsIdAssoc.relationshipNameCapitalized %> <%_ } else { _%><%= otherEntityName %><%_ } _%>);
    <%_ } _%>
        <%= returnPrefix %> <%= entityInstance %>Repository.save(<%= persistInstance %>);
  <%_ } _%>
  <%_ if (searchEngineElasticsearch) { _%>
        <%= entityInstance %>SearchRepository.save(<%= resultEntity %>);
    <%_ if (returnDirectly) { _%>
        return result;
    <%_ } _%>
  <%_ } _%>
<%_ } else { _%>
  <%_ if (isUsingMapsId && !isController) { _%>
        <%= mapsIdAssoc.otherEntity.primaryKey.type %> <%= otherEntityName %>Id = <%= entityInstance %>.get<%= mapsIdAssoc.relationshipNameCapitalized %>().get<%= primaryKey.nameCapitalized %>();
        <%= mapsIdRepoInstance %>.findById(<%= otherEntityName %>Id).ifPresent(<%= entityInstance %>::<%_ if (!fluentMethods) { _%>set<%= mapsIdAssoc.relationshipNameCapitalized %> <%_ } else { _%><%= otherEntityName %><%_ } _%>);
  <%_ } _%>
        <%= returnPrefix %> <%= entityInstance %>Service.save(<%= instanceName %>);
<%_ } _%>
