<%#
 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.
-%>
<%# 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;

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 (restProperties.some(property => property.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.Getter;
import lombok.Setter;
<%_ } _%>
import cn.afterturn.easypoi.excel.annotation.Excel;
<%_ 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 %>
@Setter
@Getter
<%_ } _%>
@SuppressWarnings("common-java:DuplicatedBlocks")
public class <%- dtoClass %><% if (extendAbstractAuditingEntity) { %> extends AbstractAuditingEntity<<%= primaryKey.type %>, <%= dtoClass %>><% } else { %> implements Serializable<% } %> {

<%_ for (property of restProperties.filter(dto => dto.relationshipName || !publicDtoFile || dto.fieldName && publicDto.split('-').includes(dto.fieldName))) {
  if (typeof property.propertyJavadoc) {
_%>
<%- property.propertyJavadoc %>
  <%_ }
  const field = property.fieldName ? property : undefined;
  const relationship = property.relationshipName ? property : undefined;
  const required = field && field.fieldValidate && field.fieldValidationRequired;
  _%>
  <%_ if (field) { _%>
    <%_ if (property.fieldValidate ) { _%>
    <%- include('/_global_partials_entity_/field_validators', {field, reactive}); -%>
    <%_ } _%>
  <%_ } _%>
  <%_ if (relationship && relationship.relationshipRequired) { _%>
    @NotNull
  <%_ } _%>
  <%_ if (property.propertyApiDescription) { _%>
    @Schema(description = "<%- property.propertyApiDescription %>"<% if (required) { %>, requiredMode = Schema.RequiredMode.REQUIRED<% } %>)
    @Excel(name = "<%- property.propertyApiDescription %>")
  <%_ } _%>
  <%_ if (property && property.fieldTypeBytes && databaseTypeSql && ormTool === 'hibernate') { _%>
    @Lob
  <%_ } _%>
    <%_ if (property.collection && relationship && relationship.source?.annotationOnSource?.includes('countByPrimaryKey') && relationship.source?.annotationOnSource?.includes('unidirectional')) { %>
        private Long <%= property.propertyName %>Count;
    <%_ } else if (property.collection && relationship) { _%>
        <%_
        if (((applicationTypeMonolith || applicationTypeMicroservice) && multiModule && moduleName && moduleName !== relationship?.otherEntity?.moduleName) ||
                (applicationTypeMicroservice && !(relationship?.otherEntity?.applications?.includes('*') || relationship?.otherEntity?.applications?.includes(baseName)))
        ) {
        _%>
    private <%= manyListName %><<%= relationship.otherEntity.primaryKey.type %>> <%- property.propertyName %> = new <%= manyListType%><>();
        <%_ } else { _%>
    private <%- property.propertyDtoJavaType %> <%= property.propertyName %> = new <%= manyListType %><>();
        <%_ } _%>
    <%_ } else if (relationship) { _%>
      <%_ if (((applicationTypeMonolith || applicationTypeMicroservice) && multiModule && moduleName && moduleName !== relationship?.otherEntity?.moduleName) ||
          (applicationTypeMicroservice && !(relationship.otherEntity?.applications?.includes('*') || relationship.otherEntity?.applications?.includes(baseName)))
        ) { _%>
    private String <%= relationship.relationshipName + relationship.otherEntityFieldCapitalized %>;
      <%_ } else { _%>
    private <%- property.propertyDtoJavaType %> <%= property.propertyName %>;
      <%_ } _%>
      <%_ if (relationship.relationshipManyToOne || (relationship.relationshipOneToOne && relationship.ownerSide)) { _%>

    private <%= relationship.otherEntity.primaryKey.type %> <%= relationship.relationshipName + relationship.otherEntity.primaryKey.nameCapitalized %>;
      <%_ } _%>
    <%_ } else { _%>
      <%_ if (ormTool === 'hibernate') { _%>
    private <%- property.propertyDtoJavaType %> <%- property.propertyName %><% if (property.collection && relationship) { %> = new <%= manyListType %><>()<% } %>;
      <%_ } else { _%>
        <%_ if (property.id && property.type === 'UUID') { _%>
    private String <%- property.propertyName %>;
        <%_ } else { _%>
    private <%- property.propertyDtoJavaType %> <%- property.propertyName %><% if (property.collection && relationship) { %> = new <%= manyListType %><>()<% } %>;
        <%_ } _%>
      <%_ } _%>
    <%_ } _%>
  <%_ if (property.fieldWithContentType) { _%>
    private String <%- property.propertyName %>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;
    }
<%_ } _%>
<%_ if (!useLombok) { _%>

<%_ for (property of restProperties.filter(dto => dto.relationshipName || !publicDtoFile || dto.fieldName && publicDto.split('-').includes(dto.fieldName))) { _%>
    public <%- property.propertyDtoJavaType %> get<%- property.propertyJavaBeanName %>() {
        return <%- property.propertyName %>;
    }

    public void set<%- property.propertyJavaBeanName %>(<%- property.propertyDtoJavaType %> <%- property.propertyName %>) %> {
        this.<%- property.propertyName %> = <%- property.propertyName %>;
    }
  <%_ if (property.fieldWithContentType) { _%>

    public String get<%- property.propertyJavaBeanName %>ContentType() {
        return <%- property.propertyName %>ContentType;
    }

    public void set<%- property.propertyJavaBeanName %>ContentType(String <%- property.propertyName %>ContentType) {
        this.<%- property.propertyName %>ContentType = <%- property.propertyName %>ContentType;
    }

    public <%= dtoClass %> <%= property.propertyName %>ContentType(String <%= property.propertyName %>ContentType) {<%# add by begcode star %>
        this.<%= property.propertyName %>ContentType = <%= property.propertyName %>ContentType;
        return this;
    }<%# add by begcode end %>
  <%_ } _%>
<%_ } _%>
<%_ } _%>

<%_ for (property of restProperties.filter(dto => dto.relationshipName || !publicDtoFile || dto.fieldName && publicDto.split('-').includes(dto.fieldName))) {
    const relationship = property.relationshipName ? property : undefined;
_%>
  <%_ if (property.collection && relationship && relationship.source?.annotationOnSource?.includes('countByPrimaryKey') && relationship.source?.annotationOnSource?.includes('unidirectional')) { %>
    public <%= dtoClass %> <%- property.propertyName %>Count(Long <%= property.propertyName %>Count) %> {
        this.<%- property.propertyName %>Count = <%- property.propertyName %>Count;
        return this;
    }
  <%_ } else { _%>
    public <%= dtoClass %> <%- property.propertyName %>(<%- property.propertyDtoJavaType %> <%- property.propertyName %>) %> {
        this.<%- property.propertyName %> = <%- property.propertyName %>;
        return this;
    }
    <%_ if (property.relationshipName && property.otherEntity && (property.relationshipManyToOne || (property.relationshipOneToOne && property.ownerSide))) { _%>
    public <%= dtoClass %> <%= property.relationshipName + property.otherEntity.primaryKey.nameCapitalized %>(<%- property.otherEntity.primaryKey.type %> <%- property.relationshipName + property.otherEntity.primaryKey.nameCapitalized %>) {
        this.<%- property.relationshipName + property.otherEntity.primaryKey.nameCapitalized %> = <%- property.relationshipName + property.otherEntity.primaryKey.nameCapitalized %>;
        return this;
    }
    <%_ } _%>
  <%_ } _%>
<%_ } _%>
<%_ 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 %>
<%_ const idNames = primaryKey ? [...primaryKey.fields.map(f => f.propertyName)] : [] _%>
    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
<%_ if (!embedded) { _%>

        if (!(o instanceof <%- dtoClass %><%= javaVersion !== '8' ? ' ' + dtoInstance : ''%>)) {
            return false;
        }

        <%_ if (javaVersion === '8') { _%>
        <%- 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 { _%>

        if (!(o instanceof <%- dtoClass %>)) {
            return false;
        }

        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, property] of restProperties.filter(dto => !(dto.options && dto.options.skipDto)).filter(dto => !publicDtoFile || publicDto.split('-').includes(dto.fieldName)).entries()) {
  const isQuoted = property.fieldName && !property.fieldTypeNumeric _%>
    <%_
    if (property.relationshipName && (((applicationTypeMonolith || applicationTypeMicroservice) && multiModule && moduleName && moduleName !== property.otherEntity?.moduleName) ||
            (applicationTypeMicroservice && !(property.otherEntity?.applications?.includes('*') || property.otherEntity?.applications?.includes(baseName)))
            )) {
    _%>
            "<%- idx === 0 ? '' : ', ' %><%= property.relationshipName + property.otherEntityFieldCapitalized %>=<% if (isQuoted) { %>'<% } %>" + <%= this.buildJavaGet({name: property.relationshipName + property.otherEntityFieldCapitalized}) %> <% if (isQuoted) { %>+ "'" <% } %>+
    <%_ } else if (property.relationshipName && property.source?.annotationOnSource?.includes('unidirectional') && property.source?.annotationOnSource?.includes('countByPrimaryKey')) { _%>
            "<%- idx === 0 ? '' : ', ' %><%- property.propertyName %>Count=" + <%= this.buildJavaGet({...property, name: property.relationshipName + 'Count'}) %> +
    <%_ } else { _%>
            "<%- idx === 0 ? '' : ', ' %><%- property.propertyName %>=<% if (isQuoted) { %>'<% } %>" + get<%- property.propertyJavaBeanName %>() <% if (isQuoted) { %>+ "'" <% } %>+
    <%_ } _%>
<%_ } _%>
            "}";
    }
}
