huliguo
2 天以前 5d7b65670282a4fad015e37d567cfa171b162052
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
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);
    }
}