From efdb99f8cecc4afb592afad79c761081d5d5cf22 Mon Sep 17 00:00:00 2001 From: lee <4766465@qq.com> Date: Wed, 18 Dec 2024 13:27:00 +0800 Subject: [PATCH] init --- yami-shop-api/src/main/java/com/yami/shop/api/controller/UserController.java | 275 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 275 insertions(+), 0 deletions(-) diff --git a/yami-shop-api/src/main/java/com/yami/shop/api/controller/UserController.java b/yami-shop-api/src/main/java/com/yami/shop/api/controller/UserController.java new file mode 100644 index 0000000..7d1cd71 --- /dev/null +++ b/yami-shop-api/src/main/java/com/yami/shop/api/controller/UserController.java @@ -0,0 +1,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); + } + } +} -- Gitblit v1.9.3