package space.anyi.petServiceManagementSystem.server.service.impl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import space.anyi.petServiceManagementSystem.server.common.BusinessException; import space.anyi.petServiceManagementSystem.server.dto.pet.DewormingDto; import space.anyi.petServiceManagementSystem.server.dto.pet.PetDto; import space.anyi.petServiceManagementSystem.server.dto.pet.VaccinationDto; import space.anyi.petServiceManagementSystem.server.entity.DewormingRecord; import space.anyi.petServiceManagementSystem.server.entity.Pet; import space.anyi.petServiceManagementSystem.server.entity.VaccinationRecord; import space.anyi.petServiceManagementSystem.server.mapper.DewormingRecordMapper; import space.anyi.petServiceManagementSystem.server.mapper.PetMapper; import space.anyi.petServiceManagementSystem.server.mapper.VaccinationRecordMapper; import space.anyi.petServiceManagementSystem.server.service.PetService; import space.anyi.petServiceManagementSystem.server.vo.pet.DewormingVo; import space.anyi.petServiceManagementSystem.server.vo.pet.PetVo; import space.anyi.petServiceManagementSystem.server.vo.pet.VaccinationVo; import java.util.List; import java.util.stream.Collectors; @Service public class PetServiceImpl implements PetService { @Autowired private PetMapper petMapper; @Autowired private VaccinationRecordMapper vaccinationRecordMapper; @Autowired private DewormingRecordMapper dewormingRecordMapper; @Override public List list(Long userId) { return petMapper.selectByUserId(userId).stream() .map(this::toPetVo).collect(Collectors.toList()); } @Override @Transactional(rollbackFor = Exception.class) public PetVo add(PetDto dto, Long userId) { Pet pet = new Pet(); pet.setUserId(userId); pet.setName(dto.getName()); pet.setSpecies(dto.getSpecies()); pet.setBreed(dto.getBreed()); pet.setGender(dto.getGender()); pet.setBirthDate(dto.getBirthDate()); pet.setWeight(dto.getWeight()); pet.setAvatar(dto.getAvatar()); pet.setRemark(dto.getRemark()); petMapper.insert(pet); return toPetVo(pet); } @Override public PetVo getById(Long id, Long userId,String role) { Pet pet = petMapper.selectById(id); if (pet == null || !pet.getUserId().equals(userId) && !"admin".equals(role)) { throw new BusinessException("宠物不存在或无权访问"); } return toPetVo(pet); } @Override @Transactional(rollbackFor = Exception.class) public Boolean update(Long id, PetDto dto, Long userId) { Pet pet = petMapper.selectById(id); if (pet == null || !pet.getUserId().equals(userId)) { throw new BusinessException("宠物不存在或无权访问"); } pet.setName(dto.getName()); pet.setSpecies(dto.getSpecies()); pet.setBreed(dto.getBreed()); pet.setGender(dto.getGender()); pet.setBirthDate(dto.getBirthDate()); pet.setWeight(dto.getWeight()); pet.setAvatar(dto.getAvatar()); pet.setRemark(dto.getRemark()); return petMapper.update(pet) > 0; } @Override @Transactional(rollbackFor = Exception.class) public Boolean delete(Long id, Long userId) { Pet pet = petMapper.selectById(id); if (pet == null || !pet.getUserId().equals(userId)) { throw new BusinessException("宠物不存在或无权访问"); } vaccinationRecordMapper.deleteByPetId(id); dewormingRecordMapper.deleteByPetId(id); return petMapper.deleteById(id) > 0; } @Override public List getVaccinations(Long petId, Long userId) { checkPetOwnership(petId, userId); return vaccinationRecordMapper.selectByPetId(petId).stream() .map(this::toVaccinationVo).collect(Collectors.toList()); } @Override @Transactional(rollbackFor = Exception.class) public VaccinationVo addVaccination(Long petId, VaccinationDto dto, Long userId) { checkPetOwnership(petId, userId); VaccinationRecord record = new VaccinationRecord(); record.setPetId(petId); record.setVaccineName(dto.getVaccineName()); record.setVaccineDate(dto.getVaccineDate()); record.setHospital(dto.getHospital()); record.setDoctor(dto.getDoctor()); record.setRemark(dto.getRemark()); vaccinationRecordMapper.insert(record); return toVaccinationVo(record); } @Override public List getDeworming(Long petId, Long userId) { checkPetOwnership(petId, userId); return dewormingRecordMapper.selectByPetId(petId).stream() .map(this::toDewormingVo).collect(Collectors.toList()); } @Override @Transactional(rollbackFor = Exception.class) public DewormingVo addDeworming(Long petId, DewormingDto dto, Long userId) { checkPetOwnership(petId, userId); DewormingRecord record = new DewormingRecord(); record.setPetId(petId); record.setDewormingName(dto.getDewormingName()); record.setDewormingDate(dto.getDewormingDate()); record.setHospital(dto.getHospital()); record.setDoctor(dto.getDoctor()); record.setRemark(dto.getRemark()); dewormingRecordMapper.insert(record); return toDewormingVo(record); } private void checkPetOwnership(Long petId, Long userId) { Pet pet = petMapper.selectById(petId); if (pet == null || !pet.getUserId().equals(userId)) { throw new BusinessException("宠物不存在或无权访问"); } } private PetVo toPetVo(Pet pet) { PetVo vo = new PetVo(); vo.setId(pet.getId()); vo.setUserId(pet.getUserId()); vo.setName(pet.getName()); vo.setSpecies(pet.getSpecies()); vo.setBreed(pet.getBreed()); vo.setGender(pet.getGender()); vo.setBirthDate(pet.getBirthDate()); vo.setWeight(pet.getWeight()); vo.setAvatar(pet.getAvatar()); vo.setRemark(pet.getRemark()); vo.setCreatedAt(pet.getCreatedAt()); return vo; } private VaccinationVo toVaccinationVo(VaccinationRecord r) { VaccinationVo vo = new VaccinationVo(); vo.setId(r.getId()); vo.setPetId(r.getPetId()); vo.setVaccineName(r.getVaccineName()); vo.setVaccineDate(r.getVaccineDate()); vo.setHospital(r.getHospital()); vo.setDoctor(r.getDoctor()); vo.setRemark(r.getRemark()); vo.setCreatedAt(r.getCreatedAt()); return vo; } private DewormingVo toDewormingVo(DewormingRecord r) { DewormingVo vo = new DewormingVo(); vo.setId(r.getId()); vo.setPetId(r.getPetId()); vo.setDewormingName(r.getDewormingName()); vo.setDewormingDate(r.getDewormingDate()); vo.setHospital(r.getHospital()); vo.setDoctor(r.getDoctor()); vo.setRemark(r.getRemark()); vo.setCreatedAt(r.getCreatedAt()); return vo; } }