feat: 宠物及预约查询服务优化
This commit is contained in:
parent
6d154f1f9a
commit
cd1555ef0a
@ -41,4 +41,8 @@ public interface AppointmentMapper extends JpaRepository<Appointment, Long> {
|
||||
@Param("storeId") Long storeId,
|
||||
@Param("start") LocalDateTime start,
|
||||
@Param("end") LocalDateTime end);
|
||||
|
||||
/** 本店是否存在关联该宠物的预约(与 listByStoreServed 口径一致) */
|
||||
@Query("SELECT COUNT(a) > 0 FROM Appointment a WHERE a.petId = :petId AND a.storeId = :storeId AND a.deleted = false")
|
||||
boolean existsByPetIdAndStoreIdAndDeletedFalse(@Param("petId") Long petId, @Param("storeId") Long storeId);
|
||||
}
|
||||
|
||||
@ -1,7 +1,9 @@
|
||||
package com.petstore.service;
|
||||
|
||||
import com.petstore.entity.Pet;
|
||||
import com.petstore.mapper.AppointmentMapper;
|
||||
import com.petstore.mapper.PetMapper;
|
||||
import com.petstore.mapper.UserMapper;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@ -14,6 +16,8 @@ import java.util.Optional;
|
||||
@RequiredArgsConstructor
|
||||
public class PetService {
|
||||
private final PetMapper petMapper;
|
||||
private final UserMapper userMapper;
|
||||
private final AppointmentMapper appointmentMapper;
|
||||
|
||||
/** 客户:仅自己的宠物 */
|
||||
public List<Pet> listByOwner(Long ownerUserId) {
|
||||
@ -61,8 +65,20 @@ public class PetService {
|
||||
if (!pet.getOwnerUserId().equals(operatorUserId)) {
|
||||
return Map.of("code", 403, "message", "无权修改该宠物");
|
||||
}
|
||||
} else if ("boss".equals(role) || "staff".equals(role)) {
|
||||
var uOpt = userMapper.findByIdAndDeletedFalse(operatorUserId);
|
||||
if (uOpt.isEmpty()) {
|
||||
return Map.of("code", 403, "message", "无权修改该宠物");
|
||||
}
|
||||
Long storeId = uOpt.get().getStoreId();
|
||||
if (storeId == null) {
|
||||
return Map.of("code", 403, "message", "无权修改该宠物");
|
||||
}
|
||||
if (!appointmentMapper.existsByPetIdAndStoreIdAndDeletedFalse(pet.getId(), storeId)) {
|
||||
return Map.of("code", 403, "message", "仅可修改本店预约关联过的宠物");
|
||||
}
|
||||
} else {
|
||||
return Map.of("code", 403, "message", "仅客户可编辑自己的宠物档案");
|
||||
return Map.of("code", 403, "message", "无权修改该宠物");
|
||||
}
|
||||
if (input.getName() != null && !input.getName().isBlank()) {
|
||||
pet.setName(input.getName());
|
||||
|
||||
Loading…
Reference in New Issue
Block a user