基于mall4j产品的二开项目后端
lee
2024-12-18 efdb99f8cecc4afb592afad79c761081d5d5cf22
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许,不可做商业用途!
 *
 * 版权所有,侵权必究!
 */
package com.yami.shop.api.controller;
 
 
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yami.shop.bean.app.dto.OrderCountData;
import com.yami.shop.bean.app.dto.UserCenterInfoDto;
import com.yami.shop.bean.app.dto.UserDto;
import com.yami.shop.bean.app.dto.UserInfoDto;
import com.yami.shop.bean.app.param.UserInfoParam;
import com.yami.shop.bean.event.UpdateDistributionUserEvent;
import com.yami.shop.bean.model.ShopAuditing;
import com.yami.shop.bean.model.ShopDetail;
import com.yami.shop.bean.model.User;
import com.yami.shop.bean.model.UserExtension;
import com.yami.shop.bean.param.UserParam;
import com.yami.shop.bean.param.im.ImConfigParam;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.i18n.I18nMessage;
import com.yami.shop.common.i18n.LanguageEnum;
import com.yami.shop.common.response.ResponseEnum;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.config.WxConfig;
import com.yami.shop.distribution.common.dto.AchievementDataDto;
import com.yami.shop.distribution.common.dto.DistributionUserWalletDto;
import com.yami.shop.distribution.common.model.DistributionUser;
import com.yami.shop.distribution.common.service.DistributionUserService;
import com.yami.shop.security.api.util.SecurityUtils;
import com.yami.shop.security.common.service.ImTokenService;
import com.yami.shop.security.common.vo.ImTokenInfo;
import com.yami.shop.service.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import ma.glasnost.orika.MapperFacade;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.*;
 
import java.text.DecimalFormat;
import java.util.Objects;
 
/**
 * 用户信息
 *
 * @author LGH
 */
@RestController
@RequestMapping("/p/user")
@Tag(name = "用户接口")
@AllArgsConstructor
public class UserController {
 
    private final UserService userService;
 
    private final ApplicationContext applicationContext;
 
    private final MapperFacade mapperFacade;
 
    private final OrderService orderService;
 
    private final ShopDetailService shopDetailService;
 
    private final ShopAuditingService shopAuditingService;
 
    private final WxConfig wxConfig;
 
    private final UserExtensionService userExtensionService;
 
    private DistributionUserService distributionUserService;
 
    private final ImTokenService imTokenService;
 
    private final SysConfigService sysConfigService;
 
    @GetMapping("/userInfo")
    @Operation(summary = "查看用户信息" , description = "根据用户ID(userId)获取用户信息")
    public ServerResponseEntity<UserDto> userInfo() {
        String userId = SecurityUtils.getUser().getUserId();
        User user = userService.getUserByUserId(userId);
        if (Objects.isNull(user)) {
            return ServerResponseEntity.success();
        }
        //获取用户等级积分详细表
        UserExtension extension = userExtensionService.getOne(
                new LambdaQueryWrapper<UserExtension>().eq(UserExtension::getUserId, SecurityUtils.getUser().getUserId()));
        UserDto userDto = mapperFacade.map(user, UserDto.class);
        userDto.setLevel(extension.getLevel());
        userDto.setUsername(user.getUserName());
        userDto.setGrowth(extension.getGrowth());
        userDto.setScore(extension.getScore());
        userDto.setLevelType(extension.getLevelType());
        userDto.setBalance(extension.getBalance());
        if (userDto.getUserMobile() != null) {
            userDto.setMobile(userDto.getUserMobile().replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2"));
        }
        return ServerResponseEntity.success(userDto);
    }
 
    @GetMapping("/getUserByUserId")
    @Operation(summary = "查看用户信息" , description = "根据用户ID(userId)获取用户信息")
    public ServerResponseEntity<User> getUserByUserId() {
        String userId = SecurityUtils.getUser().getUserId();
        User user = userService.getUserByUserId(userId);
        if (null!= user) {
            return ServerResponseEntity.success(user);
        }
        return ServerResponseEntity.showFailMsg("用户信息不存在");
    }
 
    @PutMapping("/setUserInfo")
    @Operation(summary = "设置用户信息" , description = "设置用户信息")
    public ServerResponseEntity<Void> setUserInfo(@RequestBody UserInfoParam userInfoParam) {
        String userId = SecurityUtils.getUser().getUserId();
        User user = new User();
        user.setUserId(userId);
        user.setPic(StrUtil.isBlank(userInfoParam.getAvatarUrl()) ? user.getPic() : userInfoParam.getAvatarUrl());
        user.setSex(userInfoParam.getSex() == null ? user.getSex() : userInfoParam.getSex());
        user.setUserMobile(userInfoParam.getUserMobile() == null ? user.getUserMobile() : userInfoParam.getUserMobile());
        user.setBirthDate(userInfoParam.getBirthDate() == null ? user.getBirthDate() : userInfoParam.getBirthDate());
        user.setUserMail(StrUtil.isNotBlank(userInfoParam.getUserMail()) ? userInfoParam.getUserMail() : user.getUserMail());
        if (StrUtil.isNotBlank(userInfoParam.getNickName())) {
            user.setNickName(userInfoParam.getNickName());
        }
        // 如果有一项不为空,则发送事件,修改分销员信息(如果是分销员)
        if (StrUtil.isNotBlank(userInfoParam.getAvatarUrl()) || StrUtil.isNotBlank(userInfoParam.getNickName())) {
            applicationContext.publishEvent(new UpdateDistributionUserEvent(userInfoParam, user.getUserId()));
        }
        userService.updateById(user);
 
        User userInfo = userService.getById(userId);
        userInfo.setPayPassword(null);
 
        try {
            imSetUserInfo(userInfo);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return ServerResponseEntity.success();
    }
 
    @Async
    private void imSetUserInfo(User userInfo) {
        ImConfigParam imConfig = sysConfigService.getSysConfigObject("IM_CONFIG", ImConfigParam.class);
        imTokenService.updateImUserInfo(userInfo, imConfig);
    }
 
    @GetMapping("/centerInfo")
    @Operation(summary = "个人中心信息" , description = "获取用户个人中心信息")
    public ServerResponseEntity<UserCenterInfoDto> centerInfo() {
        String userId = SecurityUtils.getUser().getUserId();
        UserCenterInfoDto userCenterInfoDto = new UserCenterInfoDto();
        userCenterInfoDto.setOrderCountData(orderService.getOrderCount(userId));
        ShopAuditing shopAuditing = shopAuditingService.getShopAuditingByUserId(userId);
        userCenterInfoDto.setShopAuditStatus(shopAuditing == null ? null : shopAuditing.getStatus());
        ShopDetail shopDetail = shopDetailService.getShopDetailByUserId(userId);
        userCenterInfoDto.setIsSetPassword(shopDetail != null && StrUtil.isNotBlank(shopDetail.getPassword()));
        userCenterInfoDto.setShopId(shopDetail == null ? null : shopDetail.getShopId());
        userCenterInfoDto.setShopStatus(shopDetail == null ? null : shopDetail.getShopStatus());
        return ServerResponseEntity.success(userCenterInfoDto);
    }
 
 
    @GetMapping("/getUserScore")
    @Operation(summary = "获取用户积分" , description = "返回用户的积分信息")
    public ServerResponseEntity<UserParam> getUserScore() {
        String userId = SecurityUtils.getUser().getUserId();
        User user = userService.getById(userId);
        UserParam userParam = mapperFacade.map(user, UserParam.class);
        System.out.println();
        return ServerResponseEntity.success(userParam);
    }
 
    @GetMapping("/getUserInfo")
    @Operation(summary = "获取用户信息" , description = "返回用户的信息")
    public ServerResponseEntity<UserInfoDto> getUserInfo() {
        String userId = SecurityUtils.getUser().getUserId();
        UserInfoDto userInfo = userExtensionService.getUserInfo(userId);
        return ServerResponseEntity.success(userInfo);
    }
 
    @Operation(summary = "注销账户" , description = "注销账户")
    @GetMapping("/destroy")
    @Parameter(name = "forceDestroy", description = "强制注销" )
    public ServerResponseEntity<Void> destroy(@RequestParam(value = "forceDestroy", required = false, defaultValue = "false") Boolean forceDestroy) {
        String userId = SecurityUtils.getUser().getUserId();
        // 检查此账户是否有未完成的订单
        OrderCountData orderCount = orderService.getOrderCount(userId);
 
        if (orderCount.getConsignment() + orderCount.getPayed() + orderCount.getRefund() + orderCount.getGrouping() > 0) {
            // 存在未完成订单不能注销
            throw new YamiShopBindException("yami.order.unfinished.cannot.destroy.account");
        }
 
        if (!forceDestroy) {
            //用户余额
            Double balance = 0.00;
            //分销可提现余额
            Double settledAmount = 0.00;
            // 检查余额
            UserInfoDto userInfo = userExtensionService.getUserInfo(userId);
            if (userInfo.getTotalBalance() > 0) {
                balance = userInfo.getTotalBalance();
            }
            // 检查分销账户余额
            DistributionUser distributionUser = distributionUserService.getByUserIdAndShopId(userId, Constant.PLATFORM_SHOP_ID);
            if (Objects.nonNull(distributionUser)) {
                AchievementDataDto achievementDataDto = distributionUserService.getAchievementDataDtoById(distributionUser.getDistributionUserId());
                DistributionUserWalletDto distributionUserWallet = achievementDataDto.getDistributionUserWallet();
                settledAmount=distributionUserWallet.getSettledAmount();
            }
            String warnMsg = "";
            DecimalFormat df = new DecimalFormat("#0.00");
            String formatBanlance = df.format(balance);
            String formatSettledAmount = df.format(settledAmount);
            if(balance>0&&settledAmount>0){
                if (Objects.equals(I18nMessage.getDbLang(), LanguageEnum.LANGUAGE_ZH_CN.getLang())) {
                    warnMsg = "您的账户当前仍有余额" + formatBanlance + "元及分销余额"+formatSettledAmount +"元,注销账户后所有余额将清零,请考虑清楚后再注销";
                }else {
                    warnMsg = "You still have" + formatBanlance +" dollars and settledAmount "+formatSettledAmount+" dollars in your account, all balance will be cleared after you destroy the account. Please consider carefully before operation";
                }
                return ServerResponseEntity.showFailMsg(warnMsg);
            }else if(balance>0){
                if (Objects.equals(I18nMessage.getDbLang(), LanguageEnum.LANGUAGE_ZH_CN.getLang())) {
                    warnMsg = "您的账户当前仍有余额" +formatBanlance+ "元,注销账户后所有余额将清零,请考虑清楚后再注销";
                }else {
                    warnMsg = "You still have" + formatBanlance +" dollars in your account, all balance will be cleared after you destroy the account. Please consider carefully before operation";
                }
                return ServerResponseEntity.showFailMsg(warnMsg);
            }else if(settledAmount>0){
                if (Objects.equals(I18nMessage.getDbLang(), LanguageEnum.LANGUAGE_ZH_CN.getLang())) {
                    warnMsg = "您的账户当前仍有分销余额" + formatSettledAmount + "元,注销账户后所有余额将清零,请考虑清楚后再注销";
                }else {
                    warnMsg = "You still have settledAmount " + formatSettledAmount +" dollars in your account, all balance will be cleared after you destroy the account. Please consider carefully before operation";
                }
                return ServerResponseEntity.showFailMsg(warnMsg);
            }
        }
        userService.destroyUser(userId);
        return ServerResponseEntity.success();
    }
 
    @PostMapping("/updateUser")
    @Operation(summary = "修改用户信息")
    public ServerResponseEntity<String> updateUser(@RequestBody User user) {
        Boolean flag = userService.updateById(user);
        if(flag){
            return ServerResponseEntity.success(ResponseEnum.OK.getMsg());
        }else{
            return ServerResponseEntity.showFailMsg(ResponseEnum.ERROR.getMsg());
        }
    }
    @PostMapping("/activeUserAdver")
    @Operation(summary = "用户激活广告信息")
    public ServerResponseEntity<String> activeUserAdver() {
        String userId = SecurityUtils.getUser().getUserId();
        String msg = userService.activeUserAdver(userId);
        if(ResponseEnum.OK.getMsg().equals(msg)){
            return ServerResponseEntity.success(ResponseEnum.OK.getMsg());
        }else{
            return ServerResponseEntity.showFailMsg(msg);
        }
    }
}