优化AcAppointment

This commit is contained in:
2024-04-18 15:06:22 +08:00
parent 326ba6a930
commit 8494e398ec
7 changed files with 94 additions and 196 deletions

View File

@@ -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;
}

View File

@@ -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<String> 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;
}

View File

@@ -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<String> 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;
}

View File

@@ -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);
}

View File

@@ -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<AcAppointmentViewEntity> {}

View File

@@ -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<AcAppointmentDto> list(CommonQuery query) {
Page<AcAppointmentViewEntity> page =
viewRepository.findAll(
Page<AcAppointmentEntity> page =
repository.findAll(
query.specification(conversionService),
PageRequest.of(
query.getPageNo(),
query.getPageSize(),
Sort.by(query.getOrders())));
return page.map(this.mapper::toDto);
Page<AcAppointmentDto> pages = page.map(this.mapper::toDto);
List<String> 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<Tuple> resultList = nativeQuery.getResultList();
List<Map<String, Object>> maps = JpaUtils.convertTuplesToMap(resultList, Function.identity(), JpaUtils.handleArray);
pages =
pages.map(
x -> {
Optional<Map<String, Object>> first =
maps.stream()
.filter(y -> x.getId().equals(y.get("id")))
.findFirst();
if (first.isPresent()) {
x.setAcDeviceDataIds(
(List<String>) first.get().get("data_ids"));
}
return x;
});
}
return pages;
}
public void archive(IdRequest dto) {

View File

@@ -17,11 +17,35 @@ public class JpaUtils {
return convertTuplesToMap(tuples, Function.identity(), Function.identity());
}
public static List<Map<String, Object>> convertTuplesToMap(List<Tuple> tuples, Function<String, String> keyMapper) {
public static Function<Object, Object> 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<Map<String, Object>> convertTuplesToMap(
List<Tuple> tuples, Function<String, String> keyMapper) {
return convertTuplesToMap(tuples, keyMapper, Function.identity());
}
public static List<Map<String, Object>> convertTuplesToMap(List<Tuple> tuples, Function<String, String> keyMapper, Function<Object, Object> valueMapper) {
public static List<Map<String, Object>> convertTuplesToMap(
List<Tuple> tuples,
Function<String, String> keyMapper,
Function<Object, Object> valueMapper) {
List<Map<String, Object>> result = new ArrayList<>();
for (Tuple single : tuples) {
Map<String, Object> tempMap = new HashMap<>();
@@ -32,7 +56,4 @@ public class JpaUtils {
}
return result;
}
}