xin
2025-07-16 f2fdc4a1e311bafd1dadf45f043bc9c5c77e0a23
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
package com.oying.modules.sh.service.impl;
 
import com.oying.exception.BadRequestException;
import com.oying.modules.sh.domain.UserAddress;
import com.oying.utils.FileUtil;
import com.oying.utils.SecurityUtils;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.oying.modules.sh.service.UserAddressService;
import com.oying.modules.sh.domain.dto.UserAddressQueryCriteria;
import com.oying.modules.sh.mapper.UserAddressMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.oying.utils.PageUtil;
 
import java.util.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
 
import com.oying.utils.PageResult;
 
/**
 * @author lixin
 * @description 服务实现
 * @date 2025-06-11
 **/
@Service
@RequiredArgsConstructor
public class UserAddressServiceImpl extends ServiceImpl<UserAddressMapper, UserAddress> implements UserAddressService {
 
    private final UserAddressMapper userAddressMapper;
 
    @Override
    public PageResult<UserAddress> queryAll(UserAddressQueryCriteria criteria, Page<Object> page) {
        return PageUtil.toPage(userAddressMapper.findAll(criteria, page));
    }
 
    @Override
    public List<UserAddress> queryAll(UserAddressQueryCriteria criteria) {
        return userAddressMapper.findAll(criteria);
    }
 
    @Override
    public List<UserAddress> queryUserAddress(double longitude, double latitude) {
        UserAddressQueryCriteria criteria = new UserAddressQueryCriteria();
        criteria.setUserId(SecurityUtils.getCurrentUserId());
        criteria.setLongitude(longitude);
        criteria.setLatitude(latitude);
        return queryAll(criteria);
    }
 
    @Override
    public UserAddress getById(Long addressId, Double longitude, Double latitude) {
        return userAddressMapper.getById(addressId, SecurityUtils.getCurrentUserId(), longitude, latitude);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(UserAddress resources) {
        if (resources.getIsDefault()) {
            userAddressMapper.isDefault(resources.getUserId());
        }
        userAddressMapper.insert(resources);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(UserAddress resources) {
        UserAddress userAddress = getById(resources.getAddressId());
        if (!userAddress.getUserId().equals(SecurityUtils.getCurrentUserId())) {
            throw new BadRequestException("不能修改他人资料");
        }
        if (resources.getIsDefault()) {
            userAddressMapper.isDefault(resources.getUserId());
        }
        userAddress.copy(resources);
        userAddressMapper.updateById(userAddress);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Long> ids) {
        for (Long id : ids) {
            UserAddress userAddress = getById(id);
            if (!userAddress.getUserId().equals(SecurityUtils.getCurrentUserId())) {
                throw new BadRequestException("不能删除他人资料");
            }
        }
        userAddressMapper.deleteBatchIds(ids);
    }
 
    @Override
    public void download(List<UserAddress> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (UserAddress userAddress : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("收货人", userAddress.getReceiverName());
            map.put("收货人电话", userAddress.getReceiverPhone());
            map.put("省份", userAddress.getProvince());
            map.put("城市", userAddress.getCity());
            map.put("区县", userAddress.getDistrict());
            map.put("街道", userAddress.getStreet());
            map.put("短地址", userAddress.getShortAddress());
            map.put("详细地址", userAddress.getDetail());
            map.put("经度", userAddress.getLongitude());
            map.put("纬度", userAddress.getLatitude());
            map.put("是否默认", userAddress.getIsDefault() ? "是" : "否");
            map.put("标签", userAddress.getTag());
            map.put("创建人", userAddress.getCreateBy());
            map.put("创建时间", userAddress.getCreateTime());
            map.put("修改者", userAddress.getUpdateBy());
            map.put("修改时间", userAddress.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}