add
yj
2024-12-05 b9900893177c78fc559223521fe839aa21000017
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
162
163
164
165
package com.iotechn.unimall.app.api.address;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dobbinsoft.fw.core.exception.AppServiceException;
import com.dobbinsoft.fw.core.exception.ServiceException;
import com.iotechn.unimall.data.domain.AddressDO;
import com.iotechn.unimall.data.exception.ExceptionDefinition;
import com.iotechn.unimall.data.mapper.AddressMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
 
import java.util.Date;
import java.util.List;
 
/*
@author kbq
@date  2019/7/4 - 22:07
*/
@Service
public class AddressServiceImpl implements AddressService {
 
    @Autowired
    private AddressMapper addressMapper;
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String create(String province, String city, String county, String address, Integer defaultAddress, Long userId, String phone, String consignee) throws ServiceException {
        Integer addressCount = addressMapper.selectCount(new QueryWrapper<AddressDO>().eq("user_id", userId));
        AddressDO addressDO = new AddressDO();
        addressDO.setProvince(province);
        addressDO.setCity(city);
        addressDO.setCounty(county);
        addressDO.setAddress(address);
        addressDO.setUserId(userId);
        addressDO.setPhone(phone);
        addressDO.setConsignee(consignee);
        if (addressCount == 0) {
            //  若没有地址,把当前增加的地址设为默认地址
            addressDO.setDefaultAddress(1);
        } else {
            // 若有地址
            if (defaultAddress != 0) {
                // 若这个地址需要是默认地址,则将其他的地址设置为非默认状态
                AddressDO preDefault = new AddressDO();
                preDefault.setDefaultAddress(0);
                addressMapper.update(preDefault, new QueryWrapper<AddressDO>()
                        .eq("user_id", userId)
                        .eq("default_address", 1));
                addressDO.setDefaultAddress(1);
            } else {
                addressDO.setDefaultAddress(0);
            }
        }
        Date now = new Date();
        addressDO.setGmtCreate(now);
        addressDO.setGmtUpdate(now);
        if (addressMapper.insert(addressDO) > 0) {
            return "ok";
        }
        throw new AppServiceException(ExceptionDefinition.ADDRESS_DATABASE_QUERY_FAILED);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String delete(Long addressId, Long userId) throws ServiceException {
        Integer defaultNum = addressMapper.selectCount(new QueryWrapper<AddressDO>()
                .eq("user_id", userId)
                .eq("id", addressId)
                .eq("default_address", 1));
        if (defaultNum == 0) {
            // 若不是默认地址,则直接删除
            if (addressMapper.delete(new QueryWrapper<AddressDO>()
                    .eq("id", addressId)
                    .eq("user_id", userId)) > 0) {
                return "ok";
            }
            throw new AppServiceException(ExceptionDefinition.ADDRESS_DATABASE_QUERY_FAILED);
        } else {
            // 若是默认地址,需要将其他1个设置为默认地址
            if (addressMapper.delete(new QueryWrapper<AddressDO>()
                    .eq("id", addressId)
                    .eq("user_id", userId)) <= 0) {
                throw new AppServiceException(ExceptionDefinition.ADDRESS_DATABASE_QUERY_FAILED);
            } else {
                List<AddressDO> addressDOS = addressMapper.selectList(new QueryWrapper<AddressDO>().eq("user_id", userId));
                if (!CollectionUtils.isEmpty(addressDOS)) {
                    AddressDO addressDO = addressDOS.get(0);
                    addressDO.setDefaultAddress(1);
                    if (addressMapper.updateById(addressDO) > 0) {
                        return "ok";
                    }
                }
                throw new AppServiceException(ExceptionDefinition.ADDRESS_DATABASE_QUERY_FAILED);
            }
        }
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String edit(Long addressId, String province, String city, String county, String address, Integer defaultAddress, Long userId, String phone, String consignee) throws ServiceException {
        AddressDO addressDO = new AddressDO();
        addressDO.setProvince(province);
        addressDO.setCity(city);
        addressDO.setCounty(county);
        addressDO.setAddress(address);
        addressDO.setUserId(userId);
        addressDO.setPhone(phone);
        addressDO.setConsignee(consignee);
        addressDO.setDefaultAddress(defaultAddress);
        Date now = new Date();
        if (defaultAddress != 0) {
            defaultAddress = 1;
            // 将所有地址更新为非默认
            AddressDO updateAddressDO = new AddressDO();
            updateAddressDO.setDefaultAddress(0);
            addressMapper.update(updateAddressDO, new QueryWrapper<AddressDO>().eq("user_id", userId));
        }
        addressDO.setDefaultAddress(defaultAddress);
        addressDO.setGmtUpdate(now);
        if (addressMapper.update(addressDO,
                new QueryWrapper<AddressDO>()
                        .eq("id", addressId)
                        .eq("user_id", userId)) > 0) {
            return "ok";
        }
        throw new AppServiceException(ExceptionDefinition.ADDRESS_DATABASE_QUERY_FAILED);
    }
 
    @Override
    public List<AddressDO> list(Long userId) throws ServiceException {
        return addressMapper.selectList(new QueryWrapper<AddressDO>()
                .eq("user_id", userId));
    }
 
    @Override
    public AddressDO getAddressById(Long userId, Long addressId) throws ServiceException {
        return addressMapper.selectOne(
                new QueryWrapper<AddressDO>()
                        .eq("id", addressId)
                        .eq("user_id", userId));
    }
 
    @Override
    public AddressDO getDefAddress(Long userId) throws ServiceException {
        List<AddressDO> addressDOS = addressMapper.selectList(
                new QueryWrapper<AddressDO>()
                        .eq("user_id", userId)
                        .eq("default_address", 1));
        if (CollectionUtils.isEmpty(addressDOS)) {
            AddressDO addressDO = new AddressDO();
            addressDO.setCity("XXX");
            addressDO.setProvince("XXX");
            addressDO.setCounty("XXX");
            addressDO.setAddress("请点击此处完善收货地址");
            addressDO.setConsignee("匿名");
            addressDO.setPhone("XXXXXXXXXXX");
            return addressDO;
        } else {
            return addressDOS.get(0);
        }
    }
 
}