<%#
 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.
-%>
package <%= entityAbsolutePackage %>.service.base;

<%_
const instanceType = restClass;
const instanceName = restInstance;
const optionalOrMono = (reactive) ? 'Mono' : 'Optional';
const listOrFlux = (reactive) ? 'Flux' : 'List';
const pageOrFlux = (reactive) ? 'Flux' : (ormTool === 'hibernate' ? 'Page' : 'IPage');
// add by begcode start
const searchPageOrFlux = reactive ? 'Flux' : (ormTool !== 'mybatis' ? 'Page' : 'org.springframework.data.domain.Page');
const pageType = ormTool !== 'mybatis' ? 'Pageable' : `Page<${entityClass}>`;
// add by begcode end
_%>
<%_ if (dtoMapstruct) { _%>
import <%= entityAbsolutePackage %>.service.dto.<%= dtoClass %>;
<%_ } else { _%>
import <%= entityAbsolutePackage %>.domain.<%= persistClass %>;
<%_ } _%>
<%_ if (jpaMetamodelFiltering && reactive) {  _%>
import <%= entityAbsolutePackage %>.domain.criteria.<%= entityClass %>Criteria;
<%_ }  _%>
<%_ if ((!paginationNo || implementsEagerLoadApis) && ormTool === 'hibernate') { _%>

  <%_ if (!reactive) { _%>
import org.springframework.data.domain.Page;
  <%_ } _%>
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Pageable;
<%_ } _%>
<%_ if (ormTool === 'mybatis') { _%>
import com.diboot.core.service.BaseService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
<%_ } _%>
<%_ if (reactive) { _%>
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
<%_ } _%>

<%_ if (paginationNo || fieldsContainNoOwnerOneToOne) { _%>
import java.util.List;
<%_ } _%>
<%_ if (!reactive) { _%>
import java.util.Optional;
<%_ } _%>
<%_ if (primaryKey.typeUUID) { _%>
import java.util.UUID;
<%_ } _%>
<%_ if (addCustomMethod.indexOf('service') !== -1) { _%>
<%- include(`../../_entityCustom_/${entityClass}.service.ejs`, {position: 'importInterface'}); -%>
<%_ } _%>

/**
 * Service Interface for managing {@link <%= entityAbsoluteClass %>}.
 */
public interface <%= entityClass %>BaseService<% if (ormTool === 'mybatis') { %> extends BaseService<<%= entityClass %>><% } %> {

    /**
     * Save a <%= entityInstance %>.
     *
     * @param <%= instanceName %> the entity to save.
     * @return the persisted entity.
     */
    <%_ if (ormTool === 'hibernate') { _%>
    <% if (reactive) { %>Mono<<% } %><%= instanceType %><% if (reactive) { %>><% } %> save(<%= instanceType %> <%= instanceName %>);
    <%_ } else { _%>
      <%_ if (!dtoMapstruct) { _%>
        <%= optionalOrMono%><<%= instanceType %>> saveAndGet(<%= instanceType %> <%= instanceName %>);
      <%_ } else { _%>
        <%= instanceType %> save(<%= instanceType %> <%= instanceName %>);
      <%_ } _%>
    <%_ } _%>

    /**
     * Updates a <%= entityInstance %>.
     *
     * @param <%= instanceName %> the entity to update.
     * @return the persisted entity.
     */
     <% if (reactive) { %>Mono<<% } %><%= instanceType %><% if (reactive) { %>><% } %> update(<%= instanceType %> <%= instanceName %>);

    /**
     * Partially updates a <%= entityInstance %>.
     *
     * @param <%= instanceName %> the entity to update partially.
     * @return the persisted entity.
     */
    <%_ if (ormTool === 'hibernate')  { _%>
    <% if (reactive) { %>Mono<% } else { %>Optional<% } %><<%= instanceType %>> partialUpdate(<%= instanceType %> <%= instanceName %>);
    <%_ } else { _%>
    <%= optionalOrMono%><<%= instanceType %>> partialUpdate(<%= instanceType %> <%= instanceName %>);
    <%_ } _%>

    /**
     * Get all the <%= entityInstancePlural %>.
     *<% if (!paginationNo) { %>
     * @param pageable the pagination information.<% } %>
     * @return the list of entities.
     */
    <% if (paginationNo) { %><%= listOrFlux %><% } else { %><%= pageOrFlux %><% } %><<%= instanceType %>> findAll(<% if (!paginationNo) { %><%- pageType%> pageable<% } %>);

    <%_ if (jpaMetamodelFiltering && reactive) {  _%>
      /**
      * Find <%= entityInstancePlural %> by criteria.
      *<% if (!paginationNo) { %>
      * @param pageable the pagination information.<% } %>
      * @return the list of entities.
      */
      <% if (paginationNo) { %><%= listOrFlux %><% } else { %><%= pageOrFlux %><% } %><<%= instanceType %>> findByCriteria(<%= entityClass %>Criteria criteria<% if (!paginationNo) { %>, <%- pageType%> pageable<% } %>);

      /**
      * Find the count of <%= entityInstancePlural %> by criteria.
      * @param criteria filtering criteria
      * @return the count of <%= entityInstancePlural %>
      */
      public Mono<Long> countByCriteria(<%= entityClass %>Criteria criteria);
    <%_ } _%>

<%_ for (const relationship of relationships) { _%>
  <%_ if (relationship.relationshipOneToOne && !relationship.ownerSide) { _%>
    /**
     * Get all the <%= restClass %> where <%= relationship.relationshipNameCapitalized %> is {@code null}.
     *
     * @return the {@link <%= listOrFlux %>} of entities.
     */
    <%= listOrFlux %><<%= instanceType %>> findAllWhere<%= relationship.relationshipNameCapitalized %>IsNull();
<%_ } } _%>

<%_ if (implementsEagerLoadApis) { _%>
    /**
     * Get all the <%= entityInstancePlural %> with eager load of many-to-many relationships.
     *
     * @param pageable the pagination information.
     * @return the list of entities.
     */
    <%= pageOrFlux %><<%= instanceType %>> findAllWithEagerRelationships(<%- pageType %> pageable);

<%_ } _%>
<%_ if (reactive) { _%>
    /**
    * Returns the number of <%= entityInstancePlural %> available.
    * @return the number of entities in the database.
    *
    */
    Mono<Long> countAll();
  <%_ if (searchEngineElasticsearch) { _%>

    /**
    * Returns the number of <%= entityInstancePlural %> available in search repository.
    *
    */
    Mono<Long> searchCount();
  <%_  } _%>

<%_  } _%>

    /**
     * Get the "id" <%= entityInstance %>.
     *
     * @param id the id of the entity.
     * @return the entity.
     */
    <%= optionalOrMono %><<%= instanceType %>> findOne(<%= primaryKey.type %> id);

    /**
     * Delete the "id" <%= entityInstance %>.
     *
     * @param id the id of the entity.
<%_ if (reactive) { _%>
     * @return a Mono to signal the deletion
<%_ } _%>
     */
    <%- reactive ? 'Mono<Void>' : 'void' %> delete(<%= primaryKey.type %> id);
<%_ if (searchEngineAny) { _%>

    /**
     * Search for the <%= entityInstance %> corresponding to the query.
     *
     * @param query the query of the search.
  <%_ if (!paginationNo) { _%>
     *
     * @param pageable the pagination information.
  <%_ } _%>
     * @return the list of entities.
     */
    <% if (!paginationNo) { %><%= pageOrFlux %><<%= instanceType %><% } else { %><%= listOrFlux %><<%= instanceType %><% } %>> search(String query<% if (!paginationNo) { %>, Pageable pageable<% } %>);
<%_ } _%>

    /**
    * Update specified field by ids
    */
    void updateBySpecifiedFieldBatch(<%= instanceType %> change<%= instanceType %>, String fieldName, List<<%= primaryKey.type%>> ids);

<%_ if (fieldsContainParentAndChildren) { _%>
    /**
    * Get all the <%= entityInstancePlural %> for parent is null.
    *<% if (!paginationNo) { %>
    * @param pageable the pagination information<% } %>
    * @return the list of entities
    */
    public <% if (!paginationNo) { %><%= pageOrFlux %><<%= instanceType %><% } else { %>List<<%= instanceType %><% } %>> findAllTop(<% if (!paginationNo) { %><%- pageType%> pageable<% } %>);

    /**
    * Get all the <%= entityInstancePlural %> for parent is parentId.
    *<% if (!paginationNo) { %>
    * @param parentId the Id of parent<% } %>
    * @return the list of entities
    */
    public <%= reactive ? 'Flux' : 'List'%><<%= instanceType %>> findChildrenByParentId(<%= primaryKey.type%> parentId);
<%_ } _%>

    /**
    * count all the <%= entityInstancePlural %>.
    * @return the count of entities
    */
    long count();
        <%_ if (!reactive) { _%>

    /**
     * Get one <%= entityInstance %> by example.
     *
     * @param example the example of the entity.
     * @return the entity.
     */
    <%= optionalOrMono %><<%= instanceType %>> findOneByExample(Example<<%= entityClass %>> example);

    /**
    * Get all the <%= entityInstancePlural %> by example.
    *
    * @param example the example of the entity.
    * @return the entities.
    */
    <%= pageOrFlux %><<%= instanceType %>> findAllByExample(Example<<%= entityClass %>> example, Pageable pageable);
        <%_ } _%>

    /**
    * Update ignore specified fields by <%= entityInstance %>
    */
    <% if (!reactive) { %>Optional<<% } %><% if (reactive) { %>Mono<<% } %><%= instanceType %>> updateByIgnoreSpecifiedFields(<%= instanceType %> change<%= instanceType %>, Set<String> unchangedFields);

    /**
    * Update specified fields by <%= entityInstance %>
    */
    <% if (!reactive) { %>Optional<<% } %><% if (reactive) { %>Mono<<% } %><%= instanceType %>> updateBySpecifiedFields(<%= instanceType %> change<%= instanceType %>, Set<String> fieldNames);

    /**
    * Update specified field by <%= entityInstance %>
    */
    <% if (!reactive) { %>Optional<<% } %><% if (reactive) { %>Mono<<% } %><%= instanceType %>> updateBySpecifiedField(<%= instanceType %> change<%= instanceType %>, String fieldName);

<%_ if (addCustomMethod.indexOf('service') !== -1) { _%>

<%- include(`../../_entityCustom_/${entityClass}.service.ejs`, { position: 'methodInterface' }); -%>
<%_ } _%>
}
