From 8494e398ecdf718aae1f001629d61d9ed25db310 Mon Sep 17 00:00:00 2001 From: lihongjie0209 Date: Thu, 18 Apr 2024 15:06:22 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96AcAppointment?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../acAppointment/dto/AcAppointmentDto.java | 28 ++--- .../entity/AcAppointmentEntity.java | 41 +------ .../entity/AcAppointmentViewEntity.java | 114 ------------------ .../mapper/AcAppointmentMapper.java | 2 - .../AcAppointmentViewRepository.java | 9 -- .../service/AcAppointmentService.java | 65 ++++++++-- .../cn/lihongjie/coal/common/JpaUtils.java | 31 ++++- 7 files changed, 94 insertions(+), 196 deletions(-) delete mode 100644 src/main/java/cn/lihongjie/coal/acAppointment/entity/AcAppointmentViewEntity.java delete mode 100644 src/main/java/cn/lihongjie/coal/acAppointment/repository/AcAppointmentViewRepository.java diff --git a/src/main/java/cn/lihongjie/coal/acAppointment/dto/AcAppointmentDto.java b/src/main/java/cn/lihongjie/coal/acAppointment/dto/AcAppointmentDto.java index ad511221..b06a3c2c 100644 --- a/src/main/java/cn/lihongjie/coal/acAppointment/dto/AcAppointmentDto.java +++ b/src/main/java/cn/lihongjie/coal/acAppointment/dto/AcAppointmentDto.java @@ -2,6 +2,8 @@ package cn.lihongjie.coal.acAppointment.dto; import cn.lihongjie.coal.acDevice.dto.AcDeviceDto; import cn.lihongjie.coal.base.dto.OrgCommonDto; +import cn.lihongjie.coal.common.DictCode; +import cn.lihongjie.coal.pojoProcessor.DictTranslate; import io.hypersistence.utils.hibernate.type.array.ListArrayType; @@ -51,13 +53,7 @@ public class AcAppointmentDto extends OrgCommonDto { @Comment("下发状态") private String sendStatus; - @Formula( - "(select i.name\n" - + "from t_dictionary d,\n" - + " t_dictionary_item i\n" - + "where d.id = i.dictionary_id\n" - + " and d.code = 'common.step.status'\n" - + " and i.code = send_status)") + @DictTranslate(dictKey = DictCode.COMMON_STEP_STATUS) private String sendStatusName; @Comment("下发完成时间") @@ -73,13 +69,7 @@ public class AcAppointmentDto extends OrgCommonDto { @Comment("下发状态") private String deleteStatus; - @Formula( - "(select i.name\n" - + "from t_dictionary d,\n" - + " t_dictionary_item i\n" - + "where d.id = i.dictionary_id\n" - + " and d.code = 'common.step.status'\n" - + " and i.code = delete_status)") + @DictTranslate(dictKey = DictCode.COMMON_STEP_STATUS) private String deleteStatusName; @Comment("删除完成时间") @@ -91,12 +81,8 @@ public class AcAppointmentDto extends OrgCommonDto { @Comment("预约状态") private String appointmentStatus; - @Formula( - "(select i.name\n" - + "from t_dictionary d,\n" - + " t_dictionary_item i\n" - + "where d.id = i.dictionary_id\n" - + " and d.code = 'appointment.status'\n" - + " and i.code = appointment_status)") + + + @DictTranslate(dictKey = DictCode.APPOINTMENT_STATUS) private String appointmentStatusName; } diff --git a/src/main/java/cn/lihongjie/coal/acAppointment/entity/AcAppointmentEntity.java b/src/main/java/cn/lihongjie/coal/acAppointment/entity/AcAppointmentEntity.java index ede474e2..c3c2525c 100644 --- a/src/main/java/cn/lihongjie/coal/acAppointment/entity/AcAppointmentEntity.java +++ b/src/main/java/cn/lihongjie/coal/acAppointment/entity/AcAppointmentEntity.java @@ -10,7 +10,6 @@ import lombok.Data; import org.hibernate.annotations.ColumnDefault; import org.hibernate.annotations.Comment; -import org.hibernate.annotations.Formula; @Data @Entity @@ -45,23 +44,12 @@ public class AcAppointmentEntity extends OrgCommonEntity { @Comment("下发状态") private String sendStatus; - @Formula( - "(select i.name\n" - + "from t_dictionary d,\n" - + " t_dictionary_item i\n" - + "where d.id = i.dictionary_id\n" - + " and d.code = 'common.step.status'\n" - + " and i.code = send_status)") - private String sendStatusName; + @Comment("下发完成时间") private java.time.LocalDateTime sendFinishTime; -// -// @Formula("(select array_agg(d.id) from t_ac_device_data d where d.plate_no = plate_no and d.pass_time between start_time and end_time)") -// @Type(ListArrayType.class) -// private List acDeviceDataIds; @@ -72,14 +60,7 @@ public class AcAppointmentEntity extends OrgCommonEntity { @Comment("下发状态") private String deleteStatus; - @Formula( - "(select i.name\n" - + "from t_dictionary d,\n" - + " t_dictionary_item i\n" - + "where d.id = i.dictionary_id\n" - + " and d.code = 'common.step.status'\n" - + " and i.code = delete_status)") - private String deleteStatusName; + @Comment("删除完成时间") private java.time.LocalDateTime deleteFinishTime; @@ -87,25 +68,11 @@ public class AcAppointmentEntity extends OrgCommonEntity { @Comment("预约状态") private String appointmentStatus; - @Formula( - "(select i.name\n" - + "from t_dictionary d,\n" - + " t_dictionary_item i\n" - + "where d.id = i.dictionary_id\n" - + " and d.code = 'appointment.status'\n" - + " and i.code = appointment_status)") - private String appointmentStatusName; + @Comment("归档状态") @ColumnDefault("'0'") private String archiveStatus = "0"; - @Formula( - "(select i.name\n" - + "from t_dictionary d,\n" - + " t_dictionary_item i\n" - + "where d.id = i.dictionary_id\n" - + " and d.code = 'archiveStatus'\n" - + " and i.code = archive_status)") - private String archiveStatusName; + } diff --git a/src/main/java/cn/lihongjie/coal/acAppointment/entity/AcAppointmentViewEntity.java b/src/main/java/cn/lihongjie/coal/acAppointment/entity/AcAppointmentViewEntity.java deleted file mode 100644 index 9b0c9cc8..00000000 --- a/src/main/java/cn/lihongjie/coal/acAppointment/entity/AcAppointmentViewEntity.java +++ /dev/null @@ -1,114 +0,0 @@ -package cn.lihongjie.coal.acAppointment.entity; - -import cn.lihongjie.coal.acDevice.entity.AcDeviceEntity; -import cn.lihongjie.coal.base.entity.OrgCommonEntity; - -import io.hypersistence.utils.hibernate.type.array.ListArrayType; - -import jakarta.persistence.Entity; -import jakarta.persistence.ManyToOne; - -import lombok.Data; - -import org.hibernate.annotations.*; - -import java.util.List; - -@Data -@Entity -@Subselect("select * from t_ac_appointment") -public class AcAppointmentViewEntity extends OrgCommonEntity { - - @ManyToOne - private AcDeviceEntity device; - - @Comment("预约开始时间") - private java.time.LocalDateTime startTime; - - @Comment("预约结束时间") - private java.time.LocalDateTime endTime; - - @Comment("车牌号") - private String plateNo; - - @Comment("姓名") - private String driverName; - - @Comment("联系电话") - private String driverPhone; - - @Comment("事由") - private String reason; - - - - @Comment("下发时间") - private java.time.LocalDateTime sendTime; - - @Comment("下发状态") - private String sendStatus; - - @Formula( - "(select i.name\n" - + "from t_dictionary d,\n" - + " t_dictionary_item i\n" - + "where d.id = i.dictionary_id\n" - + " and d.code = 'common.step.status'\n" - + " and i.code = send_status)") - private String sendStatusName; - - @Comment("下发完成时间") - private java.time.LocalDateTime sendFinishTime; - - - - @Formula("(select array_agg(d.id) from t_ac_device_data d where d.plate_no = plate_no and d.pass_time between start_time and end_time and d.device_id = device_id)") - @Type(ListArrayType.class) - private List acDeviceDataIds; - - - - @Comment("删除时间") - private java.time.LocalDateTime deleteTime; - - - @Comment("下发状态") - private String deleteStatus; - - @Formula( - "(select i.name\n" - + "from t_dictionary d,\n" - + " t_dictionary_item i\n" - + "where d.id = i.dictionary_id\n" - + " and d.code = 'common.step.status'\n" - + " and i.code = delete_status)") - private String deleteStatusName; - - @Comment("删除完成时间") - private java.time.LocalDateTime deleteFinishTime; - - @Comment("预约状态") - private String appointmentStatus; - - @Formula( - "(select i.name\n" - + "from t_dictionary d,\n" - + " t_dictionary_item i\n" - + "where d.id = i.dictionary_id\n" - + " and d.code = 'appointment.status'\n" - + " and i.code = appointment_status)") - private String appointmentStatusName; - - @Comment("归档状态") - @ColumnDefault("'0'") - private String archiveStatus = "0"; - - @Formula( - "(select i.name\n" - + "from t_dictionary d,\n" - + " t_dictionary_item i\n" - + "where d.id = i.dictionary_id\n" - + " and d.code = 'archiveStatus'\n" - + " and i.code = archive_status)") - private String archiveStatusName; -} diff --git a/src/main/java/cn/lihongjie/coal/acAppointment/mapper/AcAppointmentMapper.java b/src/main/java/cn/lihongjie/coal/acAppointment/mapper/AcAppointmentMapper.java index 44c606aa..99236b15 100644 --- a/src/main/java/cn/lihongjie/coal/acAppointment/mapper/AcAppointmentMapper.java +++ b/src/main/java/cn/lihongjie/coal/acAppointment/mapper/AcAppointmentMapper.java @@ -4,7 +4,6 @@ import cn.lihongjie.coal.acAppointment.dto.AcAppointmentDto; import cn.lihongjie.coal.acAppointment.dto.CreateAcAppointmentDto; import cn.lihongjie.coal.acAppointment.dto.UpdateAcAppointmentDto; import cn.lihongjie.coal.acAppointment.entity.AcAppointmentEntity; -import cn.lihongjie.coal.acAppointment.entity.AcAppointmentViewEntity; import cn.lihongjie.coal.base.mapper.BaseMapper; import cn.lihongjie.coal.base.mapper.CommonEntityMapper; import cn.lihongjie.coal.base.mapper.CommonMapper; @@ -23,6 +22,5 @@ public interface AcAppointmentMapper CreateAcAppointmentDto, UpdateAcAppointmentDto> { - AcAppointmentDto toDto(AcAppointmentViewEntity entity); } diff --git a/src/main/java/cn/lihongjie/coal/acAppointment/repository/AcAppointmentViewRepository.java b/src/main/java/cn/lihongjie/coal/acAppointment/repository/AcAppointmentViewRepository.java deleted file mode 100644 index b2f990f2..00000000 --- a/src/main/java/cn/lihongjie/coal/acAppointment/repository/AcAppointmentViewRepository.java +++ /dev/null @@ -1,9 +0,0 @@ -package cn.lihongjie.coal.acAppointment.repository; - -import cn.lihongjie.coal.acAppointment.entity.AcAppointmentViewEntity; -import cn.lihongjie.coal.base.dao.BaseRepository; - -import org.springframework.stereotype.Repository; - -@Repository -public interface AcAppointmentViewRepository extends BaseRepository {} diff --git a/src/main/java/cn/lihongjie/coal/acAppointment/service/AcAppointmentService.java b/src/main/java/cn/lihongjie/coal/acAppointment/service/AcAppointmentService.java index 79bdbac9..ea772bb4 100644 --- a/src/main/java/cn/lihongjie/coal/acAppointment/service/AcAppointmentService.java +++ b/src/main/java/cn/lihongjie/coal/acAppointment/service/AcAppointmentService.java @@ -5,18 +5,21 @@ import cn.lihongjie.coal.acAppointment.dto.BatchCreateAcAppointmentDto; import cn.lihongjie.coal.acAppointment.dto.CreateAcAppointmentDto; import cn.lihongjie.coal.acAppointment.dto.UpdateAcAppointmentDto; import cn.lihongjie.coal.acAppointment.entity.AcAppointmentEntity; -import cn.lihongjie.coal.acAppointment.entity.AcAppointmentViewEntity; import cn.lihongjie.coal.acAppointment.mapper.AcAppointmentMapper; import cn.lihongjie.coal.acAppointment.repository.AcAppointmentRepository; -import cn.lihongjie.coal.acAppointment.repository.AcAppointmentViewRepository; import cn.lihongjie.coal.base.dto.CommonQuery; import cn.lihongjie.coal.base.dto.IdRequest; import cn.lihongjie.coal.base.service.BaseService; import cn.lihongjie.coal.common.ArchiveUtils; +import cn.lihongjie.coal.common.JpaUtils; import cn.lihongjie.coal.dbFunctions.DbFunctionService; import cn.lihongjie.coal.exception.BizException; import cn.lihongjie.coal.rabbitmq.RabbitMQService; +import jakarta.persistence.EntityManager; +import jakarta.persistence.PersistenceContext; +import jakarta.persistence.Query; +import jakarta.persistence.Tuple; import jakarta.persistence.criteria.CriteriaBuilder; import jakarta.persistence.criteria.CriteriaQuery; import jakarta.persistence.criteria.Predicate; @@ -24,6 +27,7 @@ import jakarta.persistence.criteria.Root; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.collections4.CollectionUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.convert.ConversionService; import org.springframework.data.domain.Page; @@ -35,6 +39,7 @@ import org.springframework.transaction.annotation.Transactional; import java.time.LocalDateTime; import java.util.*; +import java.util.function.Function; import java.util.stream.Collectors; @Service @@ -48,9 +53,6 @@ public class AcAppointmentService @Autowired private ConversionService conversionService; @Autowired private DbFunctionService dbFunctionService; - @Autowired - private AcAppointmentViewRepository viewRepository; - public AcAppointmentDto create(CreateAcAppointmentDto request) { AcAppointmentEntity entity = mapper.toEntity(request); @@ -214,16 +216,63 @@ public class AcAppointmentService return mapper.toDto(entity); } + @PersistenceContext EntityManager em; + public Page list(CommonQuery query) { - Page page = - viewRepository.findAll( + Page page = + repository.findAll( query.specification(conversionService), PageRequest.of( query.getPageNo(), query.getPageSize(), Sort.by(query.getOrders()))); - return page.map(this.mapper::toDto); + Page pages = page.map(this.mapper::toDto); + + List ids = + page.stream().map(AcAppointmentEntity::getId).collect(Collectors.toList()); + + if (CollectionUtils.isNotEmpty(ids)) { + + Query nativeQuery = + em.createNativeQuery( + """ + + select ap.id, array_agg(d.id) as data_ids + from t_ac_appointment ap + left join t_ac_device_data d on ap.plate_no = d.plate_no and d.pass_time between ap.start_time and ap.end_time + + where ap.id in :ids + + group by ap.id + + """, + Tuple.class); + + + nativeQuery.setParameter("ids", ids); + + List resultList = nativeQuery.getResultList(); + List> maps = JpaUtils.convertTuplesToMap(resultList, Function.identity(), JpaUtils.handleArray); + + pages = + pages.map( + x -> { + Optional> first = + maps.stream() + .filter(y -> x.getId().equals(y.get("id"))) + .findFirst(); + + if (first.isPresent()) { + x.setAcDeviceDataIds( + (List) first.get().get("data_ids")); + } + + return x; + }); + } + + return pages; } public void archive(IdRequest dto) { diff --git a/src/main/java/cn/lihongjie/coal/common/JpaUtils.java b/src/main/java/cn/lihongjie/coal/common/JpaUtils.java index 636e4e92..5e1505e4 100644 --- a/src/main/java/cn/lihongjie/coal/common/JpaUtils.java +++ b/src/main/java/cn/lihongjie/coal/common/JpaUtils.java @@ -17,11 +17,35 @@ public class JpaUtils { return convertTuplesToMap(tuples, Function.identity(), Function.identity()); } - public static List> convertTuplesToMap(List tuples, Function keyMapper) { + public static Function handleArray = + (Object value) -> { + if (value == null) { + return null; + } + if (value.getClass().isArray()) { + + + Object[] objects = (Object[]) value; + + if (objects.length == 0 || (objects.length == 1 && objects[0] == null) ) { + return new ArrayList<>(); + } + + return new ArrayList<>(Arrays.asList(objects)); + } else { + return value; + } + }; + + public static List> convertTuplesToMap( + List tuples, Function keyMapper) { return convertTuplesToMap(tuples, keyMapper, Function.identity()); } - public static List> convertTuplesToMap(List tuples, Function keyMapper, Function valueMapper) { + public static List> convertTuplesToMap( + List tuples, + Function keyMapper, + Function valueMapper) { List> result = new ArrayList<>(); for (Tuple single : tuples) { Map tempMap = new HashMap<>(); @@ -32,7 +56,4 @@ public class JpaUtils { } return result; } - - - }