<%#
 Copyright 2013-2024 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.
-%>
<%_
  // const beans = []; // comment by begcode
  const superCall = []; // add by begcode
  // 通过Service一般controller中使用
  if (viaService) {
    beans.push({class: `${entityClass}Service`, instance: `${entityInstance}Service`});
    if (!readOnly) {
      beans.push({class: `${entityClass}Repository`, instance: `${entityInstance}Repository`});
    }
    if (entityClass !== 'CommonCondition' && databaseTypeSql) { // add by begcode
      // beans.push({class: `CommonConditionQueryService`, instance: `commonConditionQueryService`});
    }
    if (queryService && !reactive) {
      beans.push({class: `${entityClass}QueryService`, instance: `${entityInstance}QueryService`});
    }
    if (isUsingMapsId && !isController) {
      const mapsIdEntity = mapsIdAssoc.otherEntityNameCapitalized;
      const mapsIdEntityInstance = mapsIdEntity.charAt(0).toLowerCase() + mapsIdEntity.slice(1);
      const mapsIdRepoInstance = `${mapsIdEntityInstance}Repository`;
      beans.push({class: `${mapsIdEntity}Repository`, instance: mapsIdRepoInstance});
    }
  } else {
    beans.push({class: `${entityClass}Repository`, instance: `${entityInstance}Repository`});
    // add by begcode start
    if (constructorName.endsWith('QueryService') || constructorName.endsWith('QueryServiceImpl')) {
      if (ormTool === 'hibernate') {
          beans.push({class: 'EntityManager', instance: 'em'});
      }
      if (useCommonTable) {
          beans.push({class: 'CommonTableService', instance: 'commonTableService'});
      }
    } else {
      if (!constructorName.endsWith('QueryService') && !isController && !reactive) {
          beans.push({class: 'CacheManager', instance: 'cacheManager'})
      }
      if (extendData && ormTool === 'mybatis') {
          beans.push({class: 'ExtDataRepository', instance: 'extDataRepository'});
          if (entityClass !== 'CommonTable') {
              if (!beans.some(bean => bean.class === 'CommonTableRepository')) {
                  beans.push({class: 'CommonTableRepository', instance: 'commonTableRepository'});
              }
          }
      }
    }
    // add by begcode end
    if (dtoMapstruct) {
      beans.push({class: `${entityClass}Mapper`, instance: `${entityInstance}Mapper`});
    }
    if (searchEngineElasticsearch) {
      beans.push({class: `${entityClass}SearchRepository`, instance: `${entityInstance}SearchRepository`});
    }
    if (isUsingMapsId) {
      const mapsIdEntity = mapsIdAssoc.otherEntityNameCapitalized;
      const mapsIdEntityInstance = mapsIdEntity.charAt(0).toLowerCase() + mapsIdEntity.slice(1);
      const mapsIdRepoInstance = `${mapsIdEntityInstance}Repository`;
      beans.push({class: `${mapsIdEntity}Repository`, instance: mapsIdRepoInstance});
    }
    // add by begcode start
    if (!isController) {
      if (!(constructorName.endsWith('QueryService') || constructorName.endsWith('QueryServiceImpl')) ) {
          // if (entityClass === 'UploadFile' || entityClass === 'UploadImage') {
          //     beans.push({class: 'OssProperties', instance: 'ossProperties'});
          // }
          if (extendData && ormTool !== 'mybatis' && !reactive) {
              beans.push({class: 'CommonExtDataService', instance: 'commonExtDataService'});
          }
          if (entityClass === 'ApiPermission') {
              beans.push({class: 'RequestMappingHandlerMapping', instance: 'requestMappingHandlerMapping'});
          }
      }
    }
    // add by begcode end
  }
  if (saveUserSnapshot && (viaService || constructorName.endsWith('Resource'))) {
    if (!beans.find(bean => (bean.class === 'UserRepository' && bean.instance === 'userRepository'))) { // add by begcode
      if (!constructorName.endsWith('QueryService')) { // add by wangxin
    beans.push({class: 'UserRepository', instance: 'userRepository'});
      }
    }
  }
_%>

<%_ for (const bean of beans) { _%>
    protected final <%= bean.class %> <%= bean.instance %>;<%# modify by begcode %>

<%_ } _%>
    public <%= constructorName %>(
<%_ for (const bean of beans) { _%>
        <%= bean.class %> <%= bean.instance %><% if (bean !== beans[beans.length -1]) { %>,<% } %>
<%_ } _%>
    ) {
    <%_ if (constructorName.endsWith('QueryService') && ormTool === 'mybatis') { _%><%# add by begcode %>
        <%= superCall.join('\n        ') %>
    <%_ } _%>
<%_ for (const bean of beans) { _%>
        this.<%= bean.instance %> = <%= bean.instance %>;
<%_ } _%>
    }
