PetServiceImpl.java 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. package space.anyi.petServiceManagementSystem.server.service.impl;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.stereotype.Service;
  4. import org.springframework.transaction.annotation.Transactional;
  5. import space.anyi.petServiceManagementSystem.server.common.BusinessException;
  6. import space.anyi.petServiceManagementSystem.server.dto.pet.DewormingDto;
  7. import space.anyi.petServiceManagementSystem.server.dto.pet.PetDto;
  8. import space.anyi.petServiceManagementSystem.server.dto.pet.VaccinationDto;
  9. import space.anyi.petServiceManagementSystem.server.entity.DewormingRecord;
  10. import space.anyi.petServiceManagementSystem.server.entity.Pet;
  11. import space.anyi.petServiceManagementSystem.server.entity.VaccinationRecord;
  12. import space.anyi.petServiceManagementSystem.server.mapper.DewormingRecordMapper;
  13. import space.anyi.petServiceManagementSystem.server.mapper.PetMapper;
  14. import space.anyi.petServiceManagementSystem.server.mapper.VaccinationRecordMapper;
  15. import space.anyi.petServiceManagementSystem.server.service.PetService;
  16. import space.anyi.petServiceManagementSystem.server.vo.pet.DewormingVo;
  17. import space.anyi.petServiceManagementSystem.server.vo.pet.PetVo;
  18. import space.anyi.petServiceManagementSystem.server.vo.pet.VaccinationVo;
  19. import java.util.List;
  20. import java.util.stream.Collectors;
  21. @Service
  22. public class PetServiceImpl implements PetService {
  23. @Autowired
  24. private PetMapper petMapper;
  25. @Autowired
  26. private VaccinationRecordMapper vaccinationRecordMapper;
  27. @Autowired
  28. private DewormingRecordMapper dewormingRecordMapper;
  29. @Override
  30. public List<PetVo> list(Long userId) {
  31. return petMapper.selectByUserId(userId).stream()
  32. .map(this::toPetVo).collect(Collectors.toList());
  33. }
  34. @Override
  35. @Transactional(rollbackFor = Exception.class)
  36. public PetVo add(PetDto dto, Long userId) {
  37. Pet pet = new Pet();
  38. pet.setUserId(userId);
  39. pet.setName(dto.getName());
  40. pet.setSpecies(dto.getSpecies());
  41. pet.setBreed(dto.getBreed());
  42. pet.setGender(dto.getGender());
  43. pet.setBirthDate(dto.getBirthDate());
  44. pet.setWeight(dto.getWeight());
  45. pet.setAvatar(dto.getAvatar());
  46. pet.setRemark(dto.getRemark());
  47. petMapper.insert(pet);
  48. return toPetVo(pet);
  49. }
  50. @Override
  51. public PetVo getById(Long id, Long userId,String role) {
  52. Pet pet = petMapper.selectById(id);
  53. if (pet == null || !pet.getUserId().equals(userId) && !"admin".equals(role)) {
  54. throw new BusinessException("宠物不存在或无权访问");
  55. }
  56. return toPetVo(pet);
  57. }
  58. @Override
  59. @Transactional(rollbackFor = Exception.class)
  60. public Boolean update(Long id, PetDto dto, Long userId) {
  61. Pet pet = petMapper.selectById(id);
  62. if (pet == null || !pet.getUserId().equals(userId)) {
  63. throw new BusinessException("宠物不存在或无权访问");
  64. }
  65. pet.setName(dto.getName());
  66. pet.setSpecies(dto.getSpecies());
  67. pet.setBreed(dto.getBreed());
  68. pet.setGender(dto.getGender());
  69. pet.setBirthDate(dto.getBirthDate());
  70. pet.setWeight(dto.getWeight());
  71. pet.setAvatar(dto.getAvatar());
  72. pet.setRemark(dto.getRemark());
  73. return petMapper.update(pet) > 0;
  74. }
  75. @Override
  76. @Transactional(rollbackFor = Exception.class)
  77. public Boolean delete(Long id, Long userId) {
  78. Pet pet = petMapper.selectById(id);
  79. if (pet == null || !pet.getUserId().equals(userId)) {
  80. throw new BusinessException("宠物不存在或无权访问");
  81. }
  82. vaccinationRecordMapper.deleteByPetId(id);
  83. dewormingRecordMapper.deleteByPetId(id);
  84. return petMapper.deleteById(id) > 0;
  85. }
  86. @Override
  87. public List<VaccinationVo> getVaccinations(Long petId, Long userId) {
  88. checkPetOwnership(petId, userId);
  89. return vaccinationRecordMapper.selectByPetId(petId).stream()
  90. .map(this::toVaccinationVo).collect(Collectors.toList());
  91. }
  92. @Override
  93. @Transactional(rollbackFor = Exception.class)
  94. public VaccinationVo addVaccination(Long petId, VaccinationDto dto, Long userId) {
  95. checkPetOwnership(petId, userId);
  96. VaccinationRecord record = new VaccinationRecord();
  97. record.setPetId(petId);
  98. record.setVaccineName(dto.getVaccineName());
  99. record.setVaccineDate(dto.getVaccineDate());
  100. record.setHospital(dto.getHospital());
  101. record.setDoctor(dto.getDoctor());
  102. record.setRemark(dto.getRemark());
  103. vaccinationRecordMapper.insert(record);
  104. return toVaccinationVo(record);
  105. }
  106. @Override
  107. public List<DewormingVo> getDeworming(Long petId, Long userId) {
  108. checkPetOwnership(petId, userId);
  109. return dewormingRecordMapper.selectByPetId(petId).stream()
  110. .map(this::toDewormingVo).collect(Collectors.toList());
  111. }
  112. @Override
  113. @Transactional(rollbackFor = Exception.class)
  114. public DewormingVo addDeworming(Long petId, DewormingDto dto, Long userId) {
  115. checkPetOwnership(petId, userId);
  116. DewormingRecord record = new DewormingRecord();
  117. record.setPetId(petId);
  118. record.setDewormingName(dto.getDewormingName());
  119. record.setDewormingDate(dto.getDewormingDate());
  120. record.setHospital(dto.getHospital());
  121. record.setDoctor(dto.getDoctor());
  122. record.setRemark(dto.getRemark());
  123. dewormingRecordMapper.insert(record);
  124. return toDewormingVo(record);
  125. }
  126. private void checkPetOwnership(Long petId, Long userId) {
  127. Pet pet = petMapper.selectById(petId);
  128. if (pet == null || !pet.getUserId().equals(userId)) {
  129. throw new BusinessException("宠物不存在或无权访问");
  130. }
  131. }
  132. private PetVo toPetVo(Pet pet) {
  133. PetVo vo = new PetVo();
  134. vo.setId(pet.getId());
  135. vo.setUserId(pet.getUserId());
  136. vo.setName(pet.getName());
  137. vo.setSpecies(pet.getSpecies());
  138. vo.setBreed(pet.getBreed());
  139. vo.setGender(pet.getGender());
  140. vo.setBirthDate(pet.getBirthDate());
  141. vo.setWeight(pet.getWeight());
  142. vo.setAvatar(pet.getAvatar());
  143. vo.setRemark(pet.getRemark());
  144. vo.setCreatedAt(pet.getCreatedAt());
  145. return vo;
  146. }
  147. private VaccinationVo toVaccinationVo(VaccinationRecord r) {
  148. VaccinationVo vo = new VaccinationVo();
  149. vo.setId(r.getId());
  150. vo.setPetId(r.getPetId());
  151. vo.setVaccineName(r.getVaccineName());
  152. vo.setVaccineDate(r.getVaccineDate());
  153. vo.setHospital(r.getHospital());
  154. vo.setDoctor(r.getDoctor());
  155. vo.setRemark(r.getRemark());
  156. vo.setCreatedAt(r.getCreatedAt());
  157. return vo;
  158. }
  159. private DewormingVo toDewormingVo(DewormingRecord r) {
  160. DewormingVo vo = new DewormingVo();
  161. vo.setId(r.getId());
  162. vo.setPetId(r.getPetId());
  163. vo.setDewormingName(r.getDewormingName());
  164. vo.setDewormingDate(r.getDewormingDate());
  165. vo.setHospital(r.getHospital());
  166. vo.setDoctor(r.getDoctor());
  167. vo.setRemark(r.getRemark());
  168. vo.setCreatedAt(r.getCreatedAt());
  169. return vo;
  170. }
  171. }