cloudroam
3 天以前 3a819e4f668c15e8b77b188b322470da12bb7a43
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
package com.mzl.flower.service.customer;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mzl.flower.config.exception.ValidationException;
import com.mzl.flower.config.security.SecurityUtils;
import com.mzl.flower.dto.request.customer.CreateAddressDTO;
import com.mzl.flower.dto.request.customer.UpdateAddressDTO;
import com.mzl.flower.entity.customer.Address;
import com.mzl.flower.mapper.customer.AddressMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.List;
 
@Service
@Transactional
public class AddressService {
 
    private final AddressMapper addressMapper;
 
    public AddressService(AddressMapper addressMapper) {
        this.addressMapper = addressMapper;
    }
 
 
    public void add(CreateAddressDTO dto) {
        Address address  = new Address();
        BeanUtils.copyProperties(dto,address);
        address.create(SecurityUtils.getUserId());
        updateDefault(dto.getIsDefault());
        addressMapper.insert(address);
    }
 
    public void update(UpdateAddressDTO dto) {
        Address address = addressMapper.selectById(dto.getId());
        if (address == null){
            throw new ValidationException("地址不存在");
        }
        if(!address.getCreateBy().equals(SecurityUtils.getUserId())){
            throw new ValidationException("无权限修改");
        }
        BeanUtils.copyProperties(dto,address);
        address.update(SecurityUtils.getUserId());
        updateDefault(dto.getIsDefault());
        addressMapper.updateById(address);
    }
 
    private void updateDefault(Boolean isDefault) {
        if(isDefault!=null && isDefault){
            LambdaQueryWrapper<Address> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Address::getIsDefault,true);
            queryWrapper.eq(Address::getCreateBy,SecurityUtils.getUserId());
            List<Address> list = addressMapper.selectList(queryWrapper);
            if(list!=null && list.size()>0){
                for (Address oldDefault : list) {
                    oldDefault.setIsDefault(false);
                    addressMapper.updateById(oldDefault);
                }
            }
        }
    }
 
    public void delete(Long id) {
        Address address = addressMapper.selectById(id);
        if (address == null){
            throw new ValidationException("地址不存在");
        }
        if(!address.getCreateBy().equals(SecurityUtils.getUserId())){
            throw new ValidationException("无权限删除");
        }
        addressMapper.deleteById(id);
    }
 
    public List<Address> query(String userId,Boolean isDefault) {
        LambdaQueryWrapper<Address> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Address::getCreateBy,userId);
        if(isDefault!=null){
            queryWrapper.eq(Address::getIsDefault,isDefault);
        }
        queryWrapper.orderByDesc(Address::getIsDefault);
        return addressMapper.selectList(queryWrapper);
    }
 
    public Address findDetail(Long id) {
        return addressMapper.selectById(id);
    }
 
    public void setDefault(Long id) {
        Address address = addressMapper.selectById(id);
        if (address == null){
            throw new ValidationException("地址不存在");
        }
        if(!address.getCreateBy().equals(SecurityUtils.getUserId())){
            throw new ValidationException("无权限修改");
        }
        address.setIsDefault(true);
        address.update(SecurityUtils.getUserId());
        updateDefault(true);
        addressMapper.updateById(address);
    }
}