1.保存宠物信息代码
@Override
public ResponseDTO<Boolean> savePet(PetDTO petDTO) {
CodeMsg validate = ValidateEntityUtil.validate(petDTO);
if (!validate.getCode().equals(CodeMsg.SUCCESS.getCode())) {
return ResponseDTO.errorByMsg(validate);
}
Pet pet = CopyUtil.copy(petDTO, Pet.class);
if(CommonUtil.isEmpty(pet.getId())) {
pet.setId(UuidUtil.getShortUuid());
pet.setCreateTime(new Date());
pet.setState(PetStateEnum.WAIT.getCode());
batchInsertPicture(petDTO.getPhotoList(), pet.getId());
if(petMapper.insertSelective(pet) == 0) {
return ResponseDTO.errorByMsg(CodeMsg.PET_ADD_ERROR);
}
} else {
pet.setState(Optional.ofNullable(pet.getState()).orElse(PetStateEnum.WAIT.getCode()));
PictureExample pictureExample = new PictureExample();
pictureExample.createCriteria().andRefIdEqualTo(pet.getId());
pictureMapper.deleteByExample(pictureExample);
batchInsertPicture(petDTO.getPhotoList(), pet.getId());
if(petMapper.updateByPrimaryKeySelective(pet) == 0) {
return ResponseDTO.errorByMsg(CodeMsg.PET_EDIT_ERROR);
}
}
return ResponseDTO.successByMsg(true, "保存成功!");
}
2.提交订单信息代码
@Override
public ResponseDTO<Boolean> submitOrder(OrderDTO orderDTO) {
CodeMsg validate = ValidateEntityUtil.validate(orderDTO);
if (!validate.getCode().equals(CodeMsg.SUCCESS.getCode())) {
return ResponseDTO.errorByMsg(validate);
}
Order order = CopyUtil.copy(orderDTO, Order.class);
Pet pet = petMapper.selectByPrimaryKey(order.getPetId());
if(pet.getUserId().equals(order.getUserId())) {
return ResponseDTO.errorByMsg(CodeMsg.ORDER_REPEAT_ERROR);
}
if(!PetStateEnum.SUCCESS.getCode().equals(pet.getState())) {
return ResponseDTO.errorByMsg(CodeMsg.ORDER_PET_STATE_ERROR);
}
Category category = categoryMapper.selectByPrimaryKey(pet.getCategoryId());
order.setCategoryName(Optional.ofNullable(category.getName()).orElse(""));
Plate plate = plateMapper.selectByPrimaryKey(pet.getPlateId());
order.setPlateName(Optional.ofNullable(plate.getName()).orElse(""));
order.setId(UuidUtil.getShortUuid());
order.setTotalPrice(pet.getPrice());
order.setPetName(pet.getName());
order.setPetInfo(pet.getInfo());
PictureExample pictureExample = new PictureExample();
pictureExample.createCriteria().andTypeEqualTo(PictureTypeEnum.PET.getCode()).andRefIdEqualTo(pet.getId());
pictureExample.setOrderByClause("sort asc");
List<Picture> pictureList = pictureMapper.selectByExample(pictureExample);
if(pictureList.size() > 0) {
order.setPetPhoto(pictureList.get(0).getPhoto());
}
order.setSellerId(pet.getUserId());
order.setCreateTime(new Date());
if(orderMapper.insertSelective(order) == 0) {
return ResponseDTO.errorByMsg(CodeMsg.ORDER_ADD_ERROR);
}
pet.setState(PetStateEnum.SELL.getCode());
petMapper.updateByPrimaryKeySelective(pet);
return ResponseDTO.successByMsg(true, "下单成功!");
}
3.查询评论信息代码
@Override
public ResponseDTO<List<CommentDTO>> getCommentList(CommentDTO commentDTO) {
CommentExample commentExample = new CommentExample();
CommentExample.Criteria criteria = commentExample.createCriteria();
int total = 0;
if(!CommonUtil.isEmpty(commentDTO.getPostId())) {
criteria.andPostIdEqualTo(commentDTO.getPostId());
total = commentMapper.countByExample(commentExample);
}
criteria.andParentIdEqualTo("");
commentExample.setOrderByClause("create_time desc");
List<Comment> commentList = commentMapper.selectByExample(commentExample);
List<CommentDTO> commentDTOList = CopyUtil.copyList(commentList, CommentDTO.class);
for(CommentDTO comment : commentDTOList) {
User user = userMapper.selectByPrimaryKey(comment.getUserId());
comment.setUserDTO(CopyUtil.copy(user, UserDTO.class));
CommentExample childCommentExample = new CommentExample();
childCommentExample.createCriteria().andParentIdEqualTo(comment.getId());
childCommentExample.setOrderByClause("create_time desc");
List<Comment> childCommentList = commentMapper.selectByExample(childCommentExample);
List<CommentDTO> childCommentDTOList = CopyUtil.copyList(childCommentList, CommentDTO.class);
for(CommentDTO childComment : childCommentDTOList) {
childComment.setUserDTO(CopyUtil.copy(userMapper.selectByPrimaryKey(childComment.getUserId()), UserDTO.class));
childComment.setReplyUserDTO(CopyUtil.copy(userMapper.selectByPrimaryKey(childComment.getReplyId()), UserDTO.class));
}
comment.setChildCommentDTOList(childCommentDTOList);
}
return ResponseDTO.successByMsg(commentDTOList, String.valueOf(total));
}