From 5d7b65670282a4fad015e37d567cfa171b162052 Mon Sep 17 00:00:00 2001
From: huliguo <2023611923@qq.com>
Date: 星期二, 20 五月 2025 12:25:19 +0800
Subject: [PATCH] 基础代码

---
 pt-errand/src/main/java/com/ruoyi/errand/service/impl/AddressBookServiceImpl.java |  130 +++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 130 insertions(+), 0 deletions(-)

diff --git a/pt-errand/src/main/java/com/ruoyi/errand/service/impl/AddressBookServiceImpl.java b/pt-errand/src/main/java/com/ruoyi/errand/service/impl/AddressBookServiceImpl.java
new file mode 100644
index 0000000..8e730c5
--- /dev/null
+++ b/pt-errand/src/main/java/com/ruoyi/errand/service/impl/AddressBookServiceImpl.java
@@ -0,0 +1,130 @@
+package com.ruoyi.errand.service.impl;
+
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.ruoyi.common.exception.ServiceException;
+import com.ruoyi.common.utils.bean.BeanUtils;
+import com.ruoyi.errand.constant.DelFlagConstant;
+import com.ruoyi.errand.constant.IsDefaultConstant;
+import com.ruoyi.errand.domain.AddressBook;
+import com.ruoyi.errand.domain.AppUser;
+import com.ruoyi.errand.domain.Community;
+import com.ruoyi.errand.mapper.AddressBookMapper;
+import com.ruoyi.errand.mapper.CommunityMapper;
+import com.ruoyi.errand.object.dto.app.AddAddressBookDTO;
+import com.ruoyi.errand.object.dto.app.UpdateAddressBookDTO;
+import com.ruoyi.errand.object.vo.app.AddressBookByCommunityIdVO;
+import com.ruoyi.errand.object.vo.app.AddressBookListVO;
+import com.ruoyi.errand.service.AddressBookService;
+import org.springframework.security.core.context.SecurityContextHolder;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.Resource;
+import java.time.LocalDateTime;
+import java.util.List;
+
+@Service
+public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper, AddressBook> implements AddressBookService {
+    @Resource
+    private CommunityMapper communityMapper;
+
+    @Override
+    public List<AddressBookByCommunityIdVO> addressBookByCommunityId(Integer communityId) {
+        //校验一下communityId
+        Community community = communityMapper.selectById(communityId);
+        if (community == null|| community.getDelFlag().equals(DelFlagConstant.DELETE)) {
+            throw new ServiceException("小区不存在");
+        }
+        AppUser appuser = (AppUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
+        return  this.getBaseMapper().addressBookByCommunityId(communityId,appuser.getId());
+    }
+
+    @Override
+    public List<AddressBookListVO> addressBookList() {
+        AppUser appuser = (AppUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
+        return this.getBaseMapper().addressBookList(appuser.getId());
+    }
+
+    @Override
+    public void setDefaultAddress(Integer id) {
+        AppUser appuser = (AppUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
+        //查询这个默认地址
+        AddressBook addressBook = this.getById(id);
+        if (null==addressBook||addressBook.getDel_flag().equals(DelFlagConstant.DELETE)) {
+            throw new ServiceException("该地址不存在");
+        }
+        //查看原本小区内是否有默认地址
+        List<AddressBook> addressBooks = this.getBaseMapper().selectList(new LambdaUpdateWrapper<AddressBook>()
+                .eq(AddressBook::getCommunityId, addressBook.getCommunityId())
+                .eq(AddressBook::getApp_user_id, appuser.getId())
+                .eq(AddressBook::getDel_flag, DelFlagConstant.UNDELETE)
+                .eq(AddressBook::getIs_default, IsDefaultConstant.TRUE)
+        );
+        //将原本地址修改掉
+        addressBooks.forEach(x->{
+            x.setIs_default(IsDefaultConstant.FALSE);
+            x.setUpdate_time(LocalDateTime.now());
+            this.getBaseMapper().updateById(x);}
+        );
+        //将新地址设置为默认地址
+        addressBook.setIs_default(IsDefaultConstant.TRUE);
+        addressBook.setUpdate_time(LocalDateTime.now());
+        this.getBaseMapper().updateById(addressBook);
+    }
+
+    @Override
+    public void add(AddAddressBookDTO addAddressBookDTO) {
+        AppUser appuser = (AppUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
+        //判断小区是否存在
+        Community community = communityMapper.selectById(addAddressBookDTO.getCommunityId());
+        if (null==community|| community.getDelFlag().equals(DelFlagConstant.DELETE)) {
+            throw new ServiceException("小区不存在");
+        }
+        AddressBook addressBook = new AddressBook();
+        BeanUtils.copyProperties(addAddressBookDTO,addressBook);
+        addressBook.setIs_default(IsDefaultConstant.FALSE);
+        addressBook.setCreate_time(LocalDateTime.now());
+        addressBook.setApp_user_id(appuser.getId());
+        this.save(addressBook);
+    }
+
+    @Override
+    public void set(UpdateAddressBookDTO updateAddressBookDTO) {
+        AppUser appuser = (AppUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
+        //判断小区是否存在
+        Community community = communityMapper.selectById(updateAddressBookDTO.getCommunityId());
+        if (null==community|| community.getDelFlag().equals(DelFlagConstant.DELETE)) {
+            throw new ServiceException("小区不存在");
+        }
+        //判断地址是否存在
+        AddressBook addressBook = this.getById(updateAddressBookDTO.getId());
+        if (null==addressBook||addressBook.getDel_flag().equals(DelFlagConstant.DELETE)) {
+            throw new ServiceException("地址不存在");
+        }
+        //判断用户是否同一个
+        if (!appuser.getId().equals(addressBook.getApp_user_id())) {
+            throw new ServiceException("该地址不属于您");
+        }
+        BeanUtils.copyProperties(updateAddressBookDTO,addressBook);
+        addressBook.setUpdate_time(LocalDateTime.now());
+        this.updateById(addressBook);
+
+    }
+
+    @Override
+    public void delete(Integer id) {
+        AppUser appuser = (AppUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
+        //查看该地址是否存在
+        AddressBook addressBook = this.getById(id);
+        if (null==addressBook||addressBook.getDel_flag().equals(DelFlagConstant.DELETE)) {
+            throw new ServiceException("该地址不存在");
+        }
+        //判断用户是否同一个
+        if (!appuser.getId().equals(addressBook.getApp_user_id())) {
+            throw new ServiceException("该地址不属于您");
+        }
+
+        addressBook.setDel_flag(DelFlagConstant.DELETE);
+        this.updateById(addressBook);
+    }
+}
\ No newline at end of file

--
Gitblit v1.7.1