<%#
 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.
-%>
<%# add by begcode start %>
<%_
const manyListName = ormTool !== 'mybatis' ? 'Set' : 'List';
const manyListType = ormTool !== 'mybatis' ? 'LinkedHashSet' : 'ArrayList';
let hasShowInOne = false;
for (idx in relationships) {
    const relationshipType = relationships[idx].relationshipType;
    const otherEntityNameCapitalized = relationships[idx].otherEntityNameCapitalized;
    const bcConfig = relationships[idx].bcConfig; // 自定义的配置
    if (relationshipType === 'one-to-many' && (otherEntityNameCapitalized === entityClass || bcConfig && bcConfig.showInOne )) {
        hasShowInOne = true;
        break;
    }
}
_%>
<%# add by begcode end %>
package <%= entityAbsolutePackage %>.service.dto;

<%_ if (!useLombok) { _%>
import java.util.Objects;
<%_ } _%>
<%_ if (entityApiDescription || importApiModelProperty) { _%>
import io.swagger.v3.oas.annotations.media.Schema;
<%_ } _%>
<%_ if (anyFieldIsInstant) { _%>
import java.time.Instant;
<%_ } _%>
<%_ if (anyFieldIsLocalDate) { _%>
import java.time.LocalDate;
<%_ } _%>
<%_ if (anyFieldIsZonedDateTime) { _%>
import java.time.ZonedDateTime;
<%_ } _%>
<%_ if (anyFieldIsDuration) { _%>
import java.time.Duration;
<%_ } _%>
<%_ if (anyPropertyHasValidation) { _%>
import <%= javaVersion === '17' ? 'jakarta' : 'javax'%>.validation.constraints.*;
<%_ } _%>
<%_ if (extendAbstractAuditingEntity) { _%>
import <%= packageName %>.domain.AbstractAuditingEntity;<%# add by begcode %>
<%_ } else { _%>
import java.io.Serializable;
<%_ } _%>
<%_ if (anyFieldIsBigDecimal) { _%>
import java.math.BigDecimal;
<%_ } _%>
<%_ if (anyFieldIsBlobDerived && databaseTypeCassandra) { _%>
import java.nio.ByteBuffer;
<%_ } _%>
<%_ if (dtoReferences.some(reference => reference.collection) || fieldsContainParentAndChildren || hasShowInOne || extendData) { _%>
  <%_ if (ormTool === 'hibernate') { _%>
import java.util.LinkedHashSet;<%# add by begcode %>
import java.util.HashSet;
import java.util.Set;
  <%_ } else { _%>
import java.util.List;
import java.util.ArrayList;
  <%_ } _%>
<%_ } _%>
<%_ if (anyFieldIsUUID) { _%>
import java.util.UUID;
<%_ } _%>
<%_ if (anyFieldIsBlobDerived && databaseTypeSql && ormTool === 'hibernate') { _%>
import <%= javaVersion === '17' ? 'jakarta' : 'javax'%>.persistence.Lob;
<%_ } _%>
<%_ Object.keys(uniqueEnums).forEach(function(element) { _%>
import <%= packageName %>.domain.enumeration.<%= element %>;
<%_ }); _%>
<%# add by begcode start %>
<%_ if (extendData) { _%>
import java.util.HashMap;
import java.util.Map;
<%_ } _%>
<%_ if (useLombok) { _%>
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
<%_ } _%>
<%_ if (addCustomMethod.indexOf('dto') !== -1) { _%>
<%- include(`../../../_entityCustom_/${entityClass}.dto.ejs`, {position: 'import'}); -%>
<%_ } _%>

// jhipster-needle-add-import - JHipster will add getters and setters here, do not remove
<%# add by begcode end %>

<%_ for (const otherEntity of otherEntities.filter(otherEntity => otherEntity.entityAbsolutePackage !== entityAbsolutePackage)) { _%>
  <%_ if (applicationTypeMonolith || (applicationTypeMicroservice && (otherEntity.applications.includes('*') || otherEntity.applications.includes(baseName)))) { _%><%# add by begcode %>
import <%= `${otherEntity.entityAbsolutePackage}.service.dto.${otherEntity.dtoClass}` %>;
  <%_ } _%>
<%_ } _%>

/**
<% if ((enableTranslation && nativeLanguage === 'zh-cn') || !enableTranslation ) { %>
 * {@link <%= entityAbsolutePackage %>.domain.<%= persistClass %>}的DTO。<%# add by begcode %>
<%_ } else { _%>
 * A DTO for the {@link <%= entityAbsolutePackage %>.domain.<%= persistClass %>} entity.
<%_ } _%>
 */
<%_ if (entityApiDescription) { _%>
@Schema(description = "<%- entityApiDescription %>")
<%_ } _%>
<%_ if (useLombok) { _%><%# add by begcode %>
@Data
@ToString
@EqualsAndHashCode<% if (extendAbstractAuditingEntity) { %>(callSuper = true)<% } %>
<%_ } _%>
@SuppressWarnings("common-java:DuplicatedBlocks")
public class <%= dtoClass %><% if (extendAbstractAuditingEntity) { %> extends AbstractAuditingEntity<<%= primaryKey.type %>, <%= dtoClass %>><% } else { %> implements Serializable<% } %> {

<%_ for (reference of dtoReferences.filter(dto => dto.relationship || !publicDtoFile || publicDto.split('-').includes(dto.name))) {
  if (typeof reference.doc !== 'undefined') {
_%>
<%- reference.propertyJavadoc %>
  <%_ }
  const field = reference.field;
  const required = field && field.fieldValidate && field.fieldValidationRequired;
  if (field) {
    if (field.fieldValidate ) { _%>
    <%- include('/_global_partials_entity_/field_validators', {field, reactive}); -%>
    <%_ }
  } _%>
  <%_ if (reference.doc) { _%>
    @Schema(description = "<%- reference.propertyApiDescription %>"<% if (required) { %>, required = true<% } %>)
  <%_ } _%>
  <%_ if (field && field.fieldTypeBytes && databaseTypeSql && ormTool === 'hibernate') { _%>
    @Lob
  <%_ } _%>
  <%_ if (field && field.blobContentTypeText) { _%>
    private String <%= reference.name %>;
  <%_ } else if (reference.collection && reference.relationship && reference.relationship.source && reference.relationship.source.annotationOnSource && reference.relationship.source.annotationOnSource.includes('countByPrimaryKey') && reference.relationship.source.annotationOnSource.includes('unidirectional')) { _%>
    private Long <%= reference.name %>Count;
  <%_ } else if (reference.collection && reference.relationship) { _%>
        <%_
        if (((applicationTypeMonolith || applicationTypeMicroservice) && multiModule && moduleName && moduleName !== reference.relationship.otherEntity.moduleName) ||
                (applicationTypeMicroservice && !(reference.relationship.otherEntity.applications.includes('*') || reference.relationship.otherEntity.applications.includes(baseName)))
        ) {
        _%>
    private <%= manyListName %><<%= reference.relationship.otherEntity.primaryKey.type %>> <%= reference.name %> = new <%= manyListType%><>();
        <%_ } else { _%>
    private <%= manyListName %><<%= reference.relationship.otherEntity.dtoClass %>> <%= reference.name %> = new <%= manyListType%><>();
        <%_ } _%>
  <%_ } else if (reference.relationship) { _%>
    <%_
    if (((applicationTypeMonolith || applicationTypeMicroservice) && multiModule && moduleName && moduleName !== reference.relationship.otherEntity.moduleName) ||
    (applicationTypeMicroservice && !(reference.relationship.otherEntity.applications.includes('*') || reference.relationship.otherEntity.applications.includes(baseName)))
    ) {
    _%>
    private String <%= reference.relationship.relationshipName + this._.upperFirst(reference.relationship.otherEntityField) %>;
    <%_ } else { _%>
    private <%= reference.relationship.otherEntity.dtoClass %> <%= reference.name %>;
    <%_ } _%>
    <%_ if (reference.relationship.relationshipManyToOne || (reference.relationship.relationshipOneToOne && reference.relationship.ownerSide)) { _%>
    private <%= reference.relationship.otherEntity.primaryKey.type %> <%= reference.relationship.relationshipName + reference.relationship.otherEntity.primaryKey.nameCapitalized %>;
    <%_ } _%>
  <%_ } else { _%>
    <%_ if (ormTool === 'hibernate') { _%>
    private <%= reference.type %> <%= reference.name %>;
    <%_ } else { _%>
      <%_ if (reference.id && reference.type === 'UUID') { _%>
    private String <%= reference.name %>;
      <%_ } else { _%>
    private <%= reference.type %> <%= reference.name %>;
      <%_ } _%>
    <%_ } _%>
  <%_ } _%>

  <%_ if (reference.field && reference.field.fieldWithContentType) { _%>
    private String <%= reference.name %>ContentType;
  <%_ }
}
_%>

<%# add by begcode start %>
<%_ if (addCustomMethod.indexOf('dto') !== -1) { _%>
<%- include(`../../../_entityCustom_/${entityClass}.dto.ejs`, {position: 'field'}); -%>
<%_ } _%>

<%_ if (extendData) { _%>
    private Map<String, Object> extData = new HashMap<>();
<%_ } _%>

    // jhipster-needle-dto-add-field - JHipster will add fields here, do not remove

<%_ if (extendData) { _%>
    <%_ if (!useLombok) { _%>

    public Map<String, Object> getExtData() {
        return extData;
    }

    public void setExtData(Map<String, Object> extData) {
        this.extData = extData;
    }
    <%_ } _%>

    public <%= dtoClass %> extData(Map<String, Object> extData) {
        this.extData = extData;
        return this;
    }
<%_ } _%>
<%# add by begcode end %>

<%_ for (reference of dtoReferences.filter(dto => !(dto.options && dto.options.skipDto)).filter(dto => !publicDtoFile || publicDto.split('-').includes(dto.name))) { _%>
  <%_ if (!useLombok) { _%><%# add by begcode %>
  <%_ if (reference.field && reference.field.blobContentTypeText) { _%>
    public <%= this.buildJavaGetter(reference, 'String') %> {
  <%_ } else if (reference.collection && reference.relationship) { _%><%# modify by begcode %>
    public <%- this.buildJavaGetter(reference, `Set<${ reference.relationship.otherEntity.dtoClass }>` ) %> {
  <%_ } else if (reference.relationship) { _%>
    <%_
    if (((applicationTypeMonolith || applicationTypeMicroservice) && multiModule && moduleName && moduleName !== reference.relationship.otherEntity.moduleName) ||
            (applicationTypeMicroservice && !(reference.relationship.otherEntity.applications.includes('*') || reference.relationship.otherEntity.applications.includes(baseName)))
    ) {
    _%>
    public <%= this.buildJavaGetter({name: reference.relationship.relationshipName + this._.upperFirst(reference.relationship.otherEntityField)}, 'String') %> {
    <%_ } else { _%>
    public <%= this.buildJavaGetter(reference, reference.relationship.otherEntity.dtoClass) %> {
    <%_ } _%>
  <%_ } else { _%>
    public <%= this.buildJavaGetter(reference) %> {
  <%_ } _%>
    <%_ if (!reference.relationship) { _%><%# add by begcode start %>
        return <%= reference.name %>;
    <%_ } else { _%>
    <%_ if (!(reference.relationship.bcConfig && reference.relationship.bcConfig.unidirectionSkip)) { _%>
        <%_
        if (((applicationTypeMonolith || applicationTypeMicroservice) && multiModule && moduleName && moduleName !== reference.relationship.otherEntity.moduleName) ||
                (applicationTypeMicroservice && !(reference.relationship.otherEntity.applications.includes('*') || reference.relationship.otherEntity.applications.includes(baseName)))
        ) {
        _%>
        return <%= reference.relationship.relationshipName + this._.upperFirst(reference.relationship.otherEntityField) %>;
        <%_ } else { _%><%# add by begcode end %>
        return <%= reference.name %>;
        <%_ } _%><%# add by begcode start %>
    <%_ } _%>
    <%_ } _%><%# add by begcode end %>
    }

  <%_ if (reference.field && reference.field.blobContentTypeText) { _%>
    public void <%= this.buildJavaSetter(reference, `String ${reference.name}`) %> {
  <%_ } else if (reference.collection && reference.relationship && (!reference.relationship.bcConfig || !reference.relationship.bcConfig.unidirectionSkip)) { _%><%# modify by begcode %>
    public void <%- this.buildJavaSetter(reference, `Set<${ reference.relationship.otherEntity.dtoClass }> ${reference.name}`) %> {
  <%_ } else if (reference.relationship) { _%>
    <%_ if (!(reference.relationship.bcConfig && reference.relationship.bcConfig.unidirectionSkip)) { _%><%# add by begcode start %>
    <%_
    if (((applicationTypeMonolith || applicationTypeMicroservice) && multiModule && moduleName && moduleName !== reference.relationship.otherEntity.moduleName) ||
            (applicationTypeMicroservice && !(reference.relationship.otherEntity.applications.includes('*') || reference.relationship.otherEntity.applications.includes(baseName)))
    ) {
    _%>
        public void <%= this.buildJavaSetter({name: reference.relationship.relationshipName + this._.upperFirst(reference.relationship.otherEntityField)}, `String ${reference.relationship.relationshipName + _.upperFirst(reference.relationship.otherEntityField)}`) %> {
    <%_ } else { _%><%# add by begcode end %>
    public void <%= this.buildJavaSetter(reference, `${ reference.relationship.otherEntity.dtoClass } ${reference.name}`) %> {
    <%_ } _%>
    <%_ } _%>
  <%_ } else { _%>
    public void <%= this.buildJavaSetter(reference) %> {
  <%_ } _%>
    <%_ if (!reference.relationship) { _%><%# add by begcode %>
        this.<%= reference.name %> = <%= reference.name %>;
    <%_ } else { _%>
      <%_ if (!(reference.relationship.bcConfig && reference.relationship.bcConfig.unidirectionSkip)) { _%>
        <%_
        if (((applicationTypeMonolith || applicationTypeMicroservice) && multiModule && moduleName && moduleName !== reference.relationship.otherEntity.moduleName) ||
                (applicationTypeMicroservice && !(reference.relationship.otherEntity.applications.includes('*') || reference.relationship.otherEntity.applications.includes(baseName)))
        ) {
        _%>
        this.<%= reference.relationship.relationshipName + this._.upperFirst(reference.relationship.otherEntityField) %> = <%= reference.relationship.relationshipName + _.upperFirst(reference.relationship.otherEntityField) %>;
        <%_ } else { _%>
        this.<%= reference.name %> = <%= reference.name %>;
        <%_ } _%>
      <%_ } _%>
    <%_ } _%>
    }
<%_ } _%>
<%# add by begcode start %>
<%# 链式访问 -%>

    <%_ if (reference.field && reference.field.blobContentTypeText) { _%>
    public <%= dtoClass %> <%= this._buildJavaChain(reference, `String ${reference.name}`) %> {
    <%_ } else if (reference.collection && reference.relationship) { _%>
        <%_
        if (((applicationTypeMonolith || applicationTypeMicroservice) && multiModule && moduleName && moduleName !== reference.relationship.otherEntity.moduleName) ||
                (applicationTypeMicroservice && !(reference.relationship.otherEntity.applications.includes('*') || reference.relationship.otherEntity.applications.includes(baseName)))
        ) {
        _%>
    public <%= dtoClass %> <%- this._buildJavaChain(reference, `${manyListName}<${reference.relationship.otherEntity.primaryKey.type}> ${reference.name}`) %> {
        <%_ } else if (reference.relationship.source && reference.relationship.source.annotationOnSource && reference.relationship.source.annotationOnSource.includes('unidirectional') && reference.relationship.source.annotationOnSource.includes('countByPrimaryKey')) { _%>
    public <%= dtoClass %> <%= reference.name %>Count(Long <%= reference.name %>Count) {
        <%_ } else { _%>
    public <%= dtoClass %> <%- this._buildJavaChain(reference, `${manyListName}<${ reference.relationship.otherEntity.dtoClass }> ${reference.name}`) %> {
        <%_ } _%>
    <%_ } else if (reference.relationship) {
        let parentDtoName = reference.relationship.otherEntity.dtoClass
    _%>
        <%_
        if (((applicationTypeMonolith || applicationTypeMicroservice) && multiModule && moduleName && moduleName !== reference.relationship.otherEntity.moduleName) ||
                (applicationTypeMicroservice && !(reference.relationship.otherEntity.applications.includes('*') || reference.relationship.otherEntity.applications.includes(baseName)))
        ) {
        _%>
    public <%= dtoClass %> <%= this._buildJavaChain({name: reference.relationship.relationshipName + this._.upperFirst(reference.relationship.otherEntityField)}, `String ${reference.relationship.relationshipName + _.upperFirst(reference.relationship.otherEntityField)}`) %> {
        <%_ } else { _%>
    public <%= dtoClass %> <%= this._buildJavaChain(reference, `${ parentDtoName } ${reference.name}`) %> {
        <%_ } _%>
    <%_ } else { _%>
    public <%= dtoClass %> <%= this._buildJavaChain(reference) %> {
    <%_ } _%>
    <%_ if (!reference.relationship) { _%>
        this.<%= reference.name %> = <%= reference.name %>;
        return this;
    }
    <%_ } else if (reference.collection) { _%>
        <%_
        if (((applicationTypeMonolith || applicationTypeMicroservice) && multiModule && moduleName && moduleName !== reference.relationship.otherEntity.moduleName) ||
                (applicationTypeMicroservice && !(reference.relationship.otherEntity.applications.includes('*') || reference.relationship.otherEntity.applications.includes(baseName)))
        ) {
        _%>
        this.<%= reference.name %> = <%= reference.name %>;
        return this;
    }
        <%_ } else if (reference.relationship.source && reference.relationship.source.annotationOnSource && reference.relationship.source.annotationOnSource.includes('unidirectional') && reference.relationship.source.annotationOnSource.includes('countByPrimaryKey')) { _%>
        this.<%= reference.name %>Count = <%= reference.name %>Count;
        return this;
    }
        <%_ } else { _%>
        this.<%= reference.name %> = <%= reference.name %>;
        return this;
    }
        <%_ } _%>
    <%_ } else { _%>
        <%_
        if (((applicationTypeMonolith || applicationTypeMicroservice) && multiModule && moduleName && moduleName !== reference.relationship.otherEntity.moduleName) ||
                (applicationTypeMicroservice && !(reference.relationship.otherEntity.applications.includes('*') || reference.relationship.otherEntity.applications.includes(baseName)))
        ) {
        _%>
        this.<%= reference.relationship.relationshipName + this._.upperFirst(reference.relationship.otherEntityField) %> = <%= reference.relationship.relationshipName + _.upperFirst(reference.relationship.otherEntityField) %>;
        return this;
    }
        <%_ } else { _%>
        this.<%= reference.name %> = <%= reference.name %>;
        return this;
    }
        <%_ } _%>
    <%_ } _%>
<%# add by begcode end %>
  <%_ if (reference.field && reference.field.fieldWithContentType) { _%>

    public String get<%= this.javaBeanCase(reference.name) %>ContentType() {
        return <%= reference.name %>ContentType;
    }

    public void set<%= this.javaBeanCase(reference.name) %>ContentType(String <%= reference.name %>ContentType) {
        this.<%= reference.name %>ContentType = <%= reference.name %>ContentType;
    }

    public <%= dtoClass %> <%= reference.name %>ContentType(String <%= reference.name %>ContentType) {<%# add by begcode star %>
        this.<%= reference.name %>ContentType = <%= reference.name %>ContentType;
        return this;
    }<%# add by begcode end %>
  <%_ } _%>
<%_ } _%>
<%_ if (extendAbstractAuditingEntity && primaryKey.name.toLowerCase() !== 'id') { _%>

    public String getId() {
        return <%= primaryKey.name %><%= primaryKey.type !== 'String' ? '.toString()' : ''%>;
    }
<%_ } _%>

<%# add by begcode start %>
<%_ if (addCustomMethod.indexOf('dto') !== -1) { _%>
<%- include(`../../../_entityCustom_/${entityClass}.dto.ejs`, {position: 'method'}); -%>
<%_ } _%>

// jhipster-needle-dto-add-getters-setters - JHipster will add getters and setters here, do not remove
<%# add by begcode end %>
<%_ if (!useLombok) { _%><%# add by begcode %>
<%_ const idNames = primaryKey ? [...primaryKey.fields.map(f => f.fieldName)] : [] _%>
    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof <%= dtoClass %>)) {
            return false;
        }

<%_ if (!embedded) { _%>
        <%= dtoClass %> <%= dtoInstance %> = (<%= dtoClass %>) o;
        if (<%- idNames.map(n => `this.${n} == null`).join(' && ') %>){
            return false;
        }
        return <%- idNames.map(n => `Objects.equals(this.${n}, ${dtoInstance}.${n})`).join(' &&\n            ') %>;
<%_ } else { _%>
        return false;
<%_ } _%>
    }

    @Override
    public int hashCode() {
<%_ if (!embedded) { _%>
        return Objects.hash(<%- idNames.map(n => `this.${n}`).join(', ') %>);
<%_ } else { _%>
        return getClass().hashCode();
<%_ } _%>
    }

    // prettier-ignore
    @Override
    public String toString() {
        return "<%= dtoClass %>{" +
<%_ for ([idx, reference] of dtoReferences.filter(dto => !(dto.options && dto.options.skipDto)).filter(dto => !publicDtoFile || publicDto.split('-').includes(dto.name)).entries()) {
  const isQuoted = reference.field && !reference.field.fieldTypeNumeric _%>
    <%_
    if (((applicationTypeMonolith || applicationTypeMicroservice) && multiModule && moduleName && moduleName !== reference.relationship.otherEntity.moduleName) ||
            (applicationTypeMicroservice && !(reference.relationship.otherEntity.applications.includes('*') || reference.relationship.otherEntity.applications.includes(baseName)))
    ) {
    _%>
            "<%= idx === 0 ? '' : ', ' %><%= reference.relationship.relationshipName + this._.upperFirst(reference.relationship.otherEntityField) %>=<% if (isQuoted) { %>'<% } %>" + <%= buildJavaGet({name: reference.relationship.relationshipName + _.upperFirst(reference.relationship.otherEntityField)}) %> <% if (isQuoted) { %>+ "'" <% } %>+
    <%_ } else if (reference.relationship.source && reference.relationship.source.annotationOnSource && reference.relationship.source.annotationOnSource.includes('unidirectional') && reference.relationship.source.annotationOnSource.includes('countByPrimaryKey')) { _%><%# add by begcode end %>
            "<%= idx === 0 ? '' : ', ' %><%= reference.name %>Count=" + <%= this.buildJavaGet({...reference, name: reference.name + 'Count'}) %> +
    <%_ } else { _%>
            "<%= idx === 0 ? '' : ', ' %><%= reference.name %>=<% if (isQuoted) { %>'<% } %>" + <%= this.buildJavaGet(reference) %> <% if (isQuoted) { %>+ "'" <% } %>+
    <%_ } _%>
<%_ } _%>
            "}";
    }
<%_ } _%><%# add by begcode %>
}
