huliguo
10 天以前 e3a2245265516fef78b4737d6fffc939e7c5e0af
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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.domain.Region;
import com.ruoyi.errand.mapper.AddressBookMapper;
import com.ruoyi.errand.mapper.CommunityMapper;
import com.ruoyi.errand.mapper.RegionMapper;
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;
    @Resource
    private RegionMapper regionMapper;
 
    @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();
        List<AddressBookListVO> voList = this.getBaseMapper().addressBookList(appuser.getId());
        voList.forEach(vo->{
            String regionFullName = getRegionFullName(vo.getRegionId());
            vo.setRegionName(regionFullName);
        });
        return voList;
    }
 
    /**
     * 根据区ID查询完整的省市区名称
     */
    public String getRegionFullName(Integer districtId) {
        Region district = regionMapper.selectById(districtId);
        if (district == null) {
            return "";
        }
 
        Region city = regionMapper.selectById(district.getParentId());
        if (city == null) {
            return district.getName();
        }
 
        Region province = regionMapper.selectById(city.getParentId());
        if (province == null) {
            return city.getName()  + district.getName();
        }
 
        return province.getName()  + city.getName()  + district.getName();
    }
    @Override
    public void setDefaultAddress(Integer id) {
        AppUser appuser = (AppUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        //查询这个默认地址
        AddressBook addressBook = this.getById(id);
        if (null==addressBook||addressBook.getDelFlag().equals(DelFlagConstant.DELETE)) {
            throw new ServiceException("该地址不存在");
        }
        //查看原本小区内是否有默认地址
        List<AddressBook> addressBooks = this.getBaseMapper().selectList(new LambdaUpdateWrapper<AddressBook>()
                .eq(AddressBook::getCommunityId, addressBook.getCommunityId())
                .eq(AddressBook::getAppUserId, appuser.getId())
                .eq(AddressBook::getDelFlag, DelFlagConstant.UNDELETE)
                .eq(AddressBook::getIsDefault, IsDefaultConstant.TRUE)
        );
        //将原本地址修改掉
        addressBooks.forEach(x->{
            x.setIsDefault(IsDefaultConstant.FALSE);
            x.setUpdateTime(LocalDateTime.now());
            this.getBaseMapper().updateById(x);}
        );
        //将新地址设置为默认地址
        addressBook.setIsDefault(IsDefaultConstant.TRUE);
        addressBook.setUpdateTime(LocalDateTime.now());
        this.getBaseMapper().updateById(addressBook);
    }
 
    @Override
    public Integer 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.setIsDefault(IsDefaultConstant.FALSE);
        addressBook.setCreateTime(LocalDateTime.now());
        addressBook.setAppUserId(appuser.getId());
        this.save(addressBook);
        return addressBook.getId();
    }
 
    @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.getDelFlag().equals(DelFlagConstant.DELETE)) {
            throw new ServiceException("地址不存在");
        }
        //判断用户是否同一个
        if (!appuser.getId().equals(addressBook.getAppUserId())) {
            throw new ServiceException("该地址不属于您");
        }
        BeanUtils.copyProperties(updateAddressBookDTO,addressBook);
        addressBook.setUpdateTime(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.getDelFlag().equals(DelFlagConstant.DELETE)) {
            throw new ServiceException("该地址不存在");
        }
        //判断用户是否同一个
        if (!appuser.getId().equals(addressBook.getAppUserId())) {
            throw new ServiceException("该地址不属于您");
        }
 
        addressBook.setDelFlag(DelFlagConstant.DELETE);
        this.updateById(addressBook);
    }
}