基于mall4j产品的二开项目后端
lee
2024-12-18 921461a3f906d74403aeb6a27051deb77eca10fc
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
/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许,不可做商业用途!
 *
 * 版权所有,侵权必究!
 */
package com.yami.shop.api.controller;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yami.shop.bean.app.dto.UserAddrDto;
import com.yami.shop.bean.app.param.AddrParam;
import com.yami.shop.bean.model.UserAddr;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.i18n.I18nMessage;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.security.api.util.SecurityUtils;
import com.yami.shop.service.UserAddrService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
import javax.validation.Valid;
import java.util.Date;
import java.util.List;
 
/**
 * @author LGH
 */
@RestController
@RequestMapping("/p/address")
@Tag(name = "地址接口")
public class AddrController {
 
    @Autowired
    private MapperFacade mapperFacade;
 
    @Autowired
    private UserAddrService userAddrService;
 
    @GetMapping("/list")
    @Operation(summary = "用户地址列表" , description = "获取用户的所有地址信息")
    public ServerResponseEntity<List<UserAddrDto>> dvyList() {
        String userId = SecurityUtils.getUser().getUserId();
        List<UserAddr> userAddrs = userAddrService.list(new LambdaQueryWrapper<UserAddr>().eq(UserAddr::getUserId, userId).orderByDesc(UserAddr::getCommonAddr).orderByDesc(UserAddr::getUpdateTime));
        return ServerResponseEntity.success(mapperFacade.mapAsList(userAddrs, UserAddrDto.class));
    }
 
    @PostMapping("/addAddr")
    @Operation(summary = "新增用户地址" , description = "新增用户地址")
    public ServerResponseEntity<String> addAddr(@Valid @RequestBody AddrParam addrParam) {
        String userId = SecurityUtils.getUser().getUserId();
        if (addrParam.getAddrId() != null && addrParam.getAddrId() != 0) {
            // 该地址已存在
            return ServerResponseEntity.showFailMsg(I18nMessage.getMessage("yami.address.exists"));
        }
        int receiverLengthLimit = 50;
        if (addrParam.getReceiver().length() > receiverLengthLimit) {
            // 收货人名称长度超过50个字符
            return ServerResponseEntity.showFailMsg(I18nMessage.getMessage("yami.address.receiver.length.limit"));
        }
        int addrCount = userAddrService.count(new LambdaQueryWrapper<UserAddr>().eq(UserAddr::getUserId, userId));
        UserAddr userAddr = mapperFacade.map(addrParam, UserAddr.class);
        int maxUserAddr = 10;
        if (addrCount >= maxUserAddr) {
            // 收货地址已达到上限,无法再新增地址
            throw new YamiShopBindException("yami.address.add.limit");
        } else if (addrCount == 0) {
            userAddr.setCommonAddr(1);
        } else {
            userAddr.setCommonAddr(0);
        }
        userAddr.setUserId(userId);
        userAddr.setStatus(1);
        userAddr.setCreateTime(new Date());
        userAddr.setUpdateTime(new Date());
        userAddrService.save(userAddr);
        if (userAddr.getCommonAddr() == 1) {
            // 清除默认地址缓存
            userAddrService.removeUserAddrByUserId(0L, userId);
        }
        // 添加地址成功
        return ServerResponseEntity.success(I18nMessage.getMessage("yami.address.added.successfully"));
    }
 
    @PutMapping("/updateAddr")
    @Operation(summary = "修改用户地址" , description = "修改用户地址")
    public ServerResponseEntity<String> updateAddr(@Valid @RequestBody AddrParam addrParam) {
        String userId = SecurityUtils.getUser().getUserId();
        UserAddr dbUserAddr = userAddrService.getUserAddrByUserId(addrParam.getAddrId(), userId);
        if (dbUserAddr == null) {
            // 该地址已被删除
            return ServerResponseEntity.showFailMsg(I18nMessage.getMessage("yami.user.address.delete"));
        }
        int receiverLengthLimit = 50;
        if (addrParam.getReceiver().length() > receiverLengthLimit) {
            // 收货人名称长度超过50个字符
            return ServerResponseEntity.showFailMsg(I18nMessage.getMessage("yami.address.receiver.length.limit"));
        }
        UserAddr userAddr = mapperFacade.map(addrParam, UserAddr.class);
        userAddr.setUserId(userId);
        userAddr.setUpdateTime(new Date());
        userAddrService.updateById(userAddr);
        // 清除当前地址缓存
        userAddrService.removeUserAddrByUserId(addrParam.getAddrId(), userId);
        // 清除默认地址缓存
        userAddrService.removeUserAddrByUserId(0L, userId);
        // 修改地址成功
        return ServerResponseEntity.success(I18nMessage.getMessage("yami.address.modified.successfully"));
    }
 
    @DeleteMapping("/deleteAddr/{addrId}")
    @Operation(summary = "删除订单用户地址" , description = "根据地址id,删除用户地址")
    @Parameter(name = "addrId", description = "地址ID" , required = true)
    public ServerResponseEntity<String> deleteDvy(@PathVariable("addrId") Long addrId) {
        String userId = SecurityUtils.getUser().getUserId();
        UserAddr userAddr = userAddrService.getUserAddrByUserId(addrId, userId);
        if (userAddr == null) {
            // 该地址已被删除
            return ServerResponseEntity.showFailMsg(I18nMessage.getMessage("yami.user.address.delete"));
        }
        if (userAddr.getCommonAddr() == 1) {
            // 默认地址无法删除
            return ServerResponseEntity.showFailMsg(I18nMessage.getMessage("yami.address.cannot.delete"));
        }
        userAddrService.removeById(addrId);
        userAddrService.removeUserAddrByUserId(addrId, userId);
        // 删除地址成功
        return ServerResponseEntity.success(I18nMessage.getMessage("yami.deleted.address.successfully"));
    }
 
    @PutMapping("/defaultAddr/{addrId}")
    @Operation(summary = "设置默认地址" , description = "根据地址id,设置默认地址")
    @Parameter(name = "addrId", description = "地址ID" , required = true)
    public ServerResponseEntity<String> defaultAddr(@PathVariable("addrId") Long addrId) {
        String userId = SecurityUtils.getUser().getUserId();
        userAddrService.updateDefaultUserAddr(addrId, userId);
        userAddrService.removeUserAddrByUserId(0L, userId);
        // 清楚掉该用户下所有使用地址使用的缓存,否则无法删除上一次的地址
        List<UserAddr> list = userAddrService.list(new LambdaQueryWrapper<UserAddr>().eq(UserAddr::getUserId, userId));
        for (UserAddr userAddr : list) {
            userAddrService.removeUserAddrByUserId(userAddr.getAddrId(), userId);
        }
        // 修改地址成功
        return ServerResponseEntity.success(I18nMessage.getMessage("yami.address.modified.successfully"));
    }
 
    @GetMapping("/addrInfo/{addrId}")
    @Operation(summary = "获取地址信息" , description = "根据地址id,获取订单配送地址信息")
    @Parameter(name = "addrId", description = "地址ID" , required = true)
    public ServerResponseEntity<UserAddrDto> addrInfo(@PathVariable("addrId") Long addrId) {
        String userId = SecurityUtils.getUser().getUserId();
        UserAddr userAddr = userAddrService.getUserAddrByUserId(addrId, userId);
        if (userAddr == null) {
            // 该地址已被删除
            throw new YamiShopBindException("yami.user.address.delete");
        }
        return ServerResponseEntity.success(mapperFacade.map(userAddr, UserAddrDto.class));
    }
}