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

<%_
const serviceClassName = entityClass + 'QueryService';
const instanceType = restClass;
const instanceName = restInstance;
const mapper = entityInstance  + 'Mapper';
const dtoToEntity = mapper + '.'+ 'toEntity';
const entityToDto = mapper + '.'+ 'toDto';
const entityListToDto = mapper + '.' + 'toDto';
const entityToDtoReference = mapper + '::'+ 'toDto';
const repository = entityInstance  + 'Repository';
const criteria = entityClass + 'Criteria';
    const pageName = ormTool !== 'mybatis' ? 'Page' : 'IPage';
    const pageType = ormTool !== 'mybatis' ? 'Pageable' : `Page<${entityClass}>`;
    const specificationType = ormTool !== 'mybatis' ? 'Specification' : 'QueryWrapper';
    const specificationInstance = ormTool !== 'mybatis' ? 'specification' : 'queryWrapper';
    const beans = [];
_%>
<%_
let filterFields = fields.filter(field => !field.options || !field.options.skipCriteria);
let uniqueFieldTypes = Array.from(new Set(filterFields.filter(field => field.filterableField).map(field => field.fieldType)));
if (primaryKey.type === 'String') {
    if (!uniqueFieldTypes.includes('String')) {
        uniqueFieldTypes.push('String');
    }
} else {
    if (!uniqueFieldTypes.includes('Long')) {
        uniqueFieldTypes.push('Long');
    }
}
uniqueFieldTypes = uniqueFieldTypes.filter(type => ['Integer', 'Long', 'Float', 'Double', 'BigDecimal', 'LocalDate', 'ZonedDateTime', 'Instant', 'Duration', 'String'].includes(type));
_%>
<%_ if (entityClass === 'CommonBigDecimal' || uniqueFieldTypes.includes('BigDecimal')) { _%>
import java.math.BigDecimal;
<%_ } _%>
import java.util.*;
<%_ if (ormTool === 'hibernate') { _%>
import javax.persistence.criteria.*;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
<%_ } else { _%>
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
<%_ } _%>

import tech.jhipster.service.QueryService;

import <%= entityAbsolutePackage %>.domain.<%= persistClass %>;
<%_ if (searchEngineElasticsearch) { _%>
import <%= entityAbsolutePackage %>.repository.search.<%= entityClass %>SearchRepository;
<%_ } _%>
import <%= entityAbsolutePackage %>.service.criteria.<%= entityClass %>Criteria;
<%_ if (dtoMapstruct) { _%>
import <%= entityAbsolutePackage %>.service.dto.<%= dtoClass %>;
<%_ }  else { _%>
import <%= entityAbsolutePackage %>.domain.*; // for static metamodels
<%_ } _%>
<%_ if (addCustomMethod.indexOf('queryService') !== -1) { _%>
<%- include(`../../_entityCustom_/${entityClass}.queryService.ejs`, {position: 'importInterface'}); -%>
<%_ } _%>

/**
<%_ if ((enableTranslation && nativeLanguage === 'zh-cn') || !enableTranslation ) { _%>
 * 用于对数据库中的{@link <%= persistClass %>}实体执行复杂查询的Service。
 * 主要输入是一个{@link <%= entityClass %>Criteria}，它被转换为{@link <%= specificationType%>}，
 * 所有字段过滤器都将应用到表达式中。
 * 它返回满足条件的{@link <%= instanceType %>}列表{@link List} 或 {@link <%= instanceType %>} 的分页列表 {@link Page}。
<%_ } else { _%>
 * Service for executing complex queries for {@link <%= persistClass %>} entities in the database.
 * The main input is a {@link <%= entityClass %>Criteria} which gets converted to {@link Specification},
 * in a way that all the filters must apply.
 * It returns a {@link List} of {@link <%= instanceType %>} or a {@link Page} of {@link <%= instanceType %>} which fulfills the criteria.
<%_ } _%>
 */
public abstract class <%= serviceClassName %> extends QueryService<<%= persistClass %>> {

    /**
     * Return a {@link List} of {@link <%= instanceType %>} which matches the criteria from the database.
     * @param criteria The object which holds all the filters, which the entities should match.
     * @return the matching entities.
     */
    public abstract List<<%= instanceType %>> findByCriteria(<%= criteria %> criteria);

    /**
     * Return a {@link <%= pageName%>} of {@link <%= instanceType %>} which matches the criteria from the database.
     * @param <%= specificationInstance%> The object which holds all the filters, which the entities should match.
     * @param page The page, which should be returned.
     * @return the matching entities.
     */
    public abstract <%= pageName%><<%= instanceType %>> findBy<%= specificationType%>(<%= specificationType%><<%= persistClass %>> <%= specificationInstance%>, <%- pageType%> page);

    /**
     * Return a {@link <%= pageName%>} of {@link <%= instanceType %>} which matches the criteria from the database.
     * @param criteria The object which holds all the filters, which the entities should match.
     * @param page The page, which should be returned.
     * @return the matching entities.
     */
    public abstract Page<<%= instanceType %>> findByCriteria(<%= criteria %> criteria, Pageable page);
<%_ 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(<%= criteria %> criteria, <% if (!paginationNo) { %><%- pageType%> pageable<% } %>);
<%_ } _%>

    /**
     * Return the number of matching entities in the database.
     * @param criteria The object which holds all the filters, which the entities should match.
     * @return the number of matching entities.
     */
    public abstract long countByCriteria(<%= criteria %> criteria);

    /**
     * Return a {@link <%= instanceType %>} which matches the criteria from the database.
     * @param criteria The object which holds all the filters, which the entities should match.
     * @return the matching entity.
     */
    public abstract Optional<<%= instanceType %>> getOneByCriteria(<%= criteria %> criteria);

    /**
     * Return the number of matching entities in the database.
     * @param <%= specificationInstance%> The object which holds all the filters, which the entities should match.
     * @return the number of matching entities.
     */
    public abstract long countBy<%= specificationType%>(<%= specificationType%><<%= persistClass%>> <%= specificationInstance%>);

    public abstract long countByFieldNameAndCriteria(String fieldName, Boolean distinct, <%= persistClass%>Criteria criteria);

    public abstract <T> List<T> getFieldByCriteria(Class<T> clazz,String fieldName, Boolean distinct, <%= persistClass%>Criteria criteria);

    public abstract boolean updateBySpecifield(String fieldName, Object value, <%= criteria %> criteria);
    <%_ if (useCommonTable) { _%>

    /**
     * 直接转换为dto。maytoone的，直接查询结果。one-to-many和many-to-many后续加载
     * @param entityName 模型名称
     * @param criteria 条件表达式
     * @param predicate 条件
     * @param pageable 分页
     * @return Page<<%= instanceType %>>
     */
    public abstract <%= pageName%><<%= instanceType %>> selectByCustomEntity(String entityName, <%= criteria %> criteria, Predicate predicate, <%= specificationType%> <%= specificationInstance%>, <%- pageType%> pageable);
    <%_ } _%>

    public abstract List<Map<String, Object>> findAllByJpql(String jqpl);

    public abstract Map<String, Object> getByJpql(String jqpl);

<%_ if (addCustomMethod.indexOf('queryService') !== -1) { _%>
    <%- include(`../../_entityCustom_/${entityClass}.queryService.ejs`, {position: 'importMethod'}); -%>
<%_ } _%>
}
