huliguo
2025-04-21 17abf0608f62cdd318dba3e7b12a32ea486cb482
ruoyi-service/ruoyi-account/src/main/java/com/ruoyi/account/controller/AppUserController.java
@@ -8,6 +8,8 @@
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.account.api.model.*;
import com.ruoyi.account.dto.ApplyForAdmissionDTO;
import com.ruoyi.account.dto.ChangeAppUserInfo;
import com.ruoyi.account.mapper.AppUserMapper;
import com.ruoyi.account.service.*;
import com.ruoyi.account.util.ObsUploadUtil;
@@ -18,6 +20,7 @@
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.page.PageInfo;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.order.feignClient.OrderClient;
@@ -25,11 +28,14 @@
import com.ruoyi.order.model.Order;
import com.ruoyi.other.api.domain.Shop;
import com.ruoyi.other.api.feignClient.ShopClient;
import com.ruoyi.system.api.domain.SysConfig;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.feignClient.SysConfigClient;
import com.ruoyi.system.api.feignClient.SysUserClient;
import com.ruoyi.system.api.model.LoginUser;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
@@ -52,7 +58,7 @@
 * @author luodangjia
 * @since 2024-11-21
 */
@Api(tags = {"登录注册-小程序"})
@Api(tags = {"用户端-小程序"})
@RestController
@RequestMapping("/app-user")
@Slf4j
@@ -72,6 +78,10 @@
    private SysUserClient sysUserClient;
    @Resource
    private OrderClient orderClient;
    @Resource
    private SysConfigClient sysConfigClient;
    @Resource
    private AppUserShopService appUserShopService;
    
    @Resource
    private UserCancellationLogService userCancellationLogService;
@@ -90,6 +100,15 @@
    @ApiOperation(value = "手机号登录")
    public R<LoginVo> mobileLogin(@RequestBody MobileLogin mobileLogin) {
        return appUserService.mobileLogin(mobileLogin);
    }
    @GetMapping("/logout")
    @ApiOperation(value = "登出")
    public R logout() {
        Long userId = tokenService.getLoginUserApplet().getUserid();
        loginout(userId);
        return R.ok();
    }
@@ -140,44 +159,6 @@
    }
  /*  @ResponseBody
    @PostMapping("/registerAccount")
    @ApiOperation(value = "注册新账号")
    public R<LoginVo> registerAccount(@RequestBody RegisterAccount registerAccount) {
        return appUserService.registerAccount(registerAccount);
    }*/
   /* @ResponseBody
    @GetMapping("/getReferrer/{id}")
    @ApiOperation(value = "获取推荐人信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "推荐人id", required = true, dataType = "long")
    })
    public R<String> getReferrer(@PathVariable("id") Long id) {
        AppUser appUser = appUserService.getById(id);
        String phone = appUser.getPhone();
        phone = phone.substring(0, 3) + "****" + phone.substring(7);
        return R.ok(appUser.getName() + "-" + phone);
    }
*/
    @ResponseBody
    @PostMapping("/info")
    @ApiOperation(value = "我的资料", tags = {"小程序-个人中心首页-我的资料"})
    public R<AppUser> info() {
        Long userId = tokenService.getLoginUserApplet().getUserid();
        AppUser user = appUserService.getById(userId);
        if(StringUtils.isEmpty(user.getQrCode())){
            //获取微信推广二维码
            String fileName = UUID.randomUUID() + ".jpg";
            String getwxacodeunlimit = weChatUtil.getwxacodeunlimit("pages/start/start", "id=" + user.getId(), EnvVersion.RELEASE, filePath + fileName);
            user.setQrCode(getwxacodeunlimit);
            appUserService.updateById(user);
        }
        return R.ok(user);
    }
@@ -187,133 +168,66 @@
    public R unregis() {
        Long userId = tokenService.getLoginUserApplet().getUserid();
        AppUser user = appUserService.getById(userId);
        //添加注销记录
        UserCancellationLog userCancellationLog = new UserCancellationLog();
        userCancellationLog.setAppUserId(user.getId());
        userCancellationLog.setVipId(user.getVipId());
        userCancellationLogService.save(userCancellationLog);
        user.setStatus(3);
        appUserService.updateById(user);
        List<AppUser> subUserList = appUserService.list(new LambdaQueryWrapper<AppUser>()
                .eq(AppUser::getInviteUserId, userId));
        for (AppUser appUser : subUserList) {
            Long inviteUserId = user.getInviteUserId();
            appUser.setInviteUserId(inviteUserId);
        }
        appUserService.updateBatchById(subUserList);
        return R.ok();
    }
    @ResponseBody
    @GetMapping("/recommend")
    @ApiOperation(value = "推广中心", tags = {"小程序-推广中心"})
    public R<AppUser> recommend() {
        Long userId = tokenService.getLoginUserApplet().getUserid();
        //获取绑定门店
        AppUser user = appUserService.getById(userId);
        return R.ok(user);
    @GetMapping("/getServicePhone")
    @ApiOperation(value = "获取客服电话", tags = {"小程序-个人中心首页-客服"})
    public R getServicePhone() {
        SysConfig data = sysConfigClient.getInfo(7L).getData();
        List<String> phoneList = null;
        if (data != null) {
            phoneList = Arrays.stream(data.getConfigValue().split(";"))
                    .map(String::trim)  // 去除每个号码前后的空格
                    .filter(s -> !s.isEmpty())  // 过滤空字符串
                    .collect(Collectors.toList());
        }
        return R.ok(phoneList);
    }
    @GetMapping("/index")
    @ApiOperation(value = "个人中心首页", tags = {"小程序-个人中心首页"})
    @ApiOperation(value = "个人信息", tags = {"小程序-个人中心首页"})
    public R<AppUser> index() {
        Long userId = tokenService.getLoginUserApplet().getUserid();
        //当前用户信息
        AppUser user = appUserService.getById(userId);
        if(StringUtils.isEmpty(user.getQrCode())){
            //获取微信推广二维码
            String fileName = UUID.randomUUID() + ".jpg";
            String getwxacodeunlimit = weChatUtil.getwxacodeunlimit("pages/start/start", "id=" + user.getId(), EnvVersion.RELEASE, filePath + fileName);
            user.setQrCode(getwxacodeunlimit);
            appUserService.updateById(user);
        }
        return R.ok(user);
        return R.ok(appUserService.index());
    }
    /**
     * 获取门店作为服务商的所有用户
     * @param shopId
     * @return
     */
    public Set<Long> getShopServerUser(Integer shopId){
        Shop shop = shopClient.getShopById(shopId).getData();
        List<Shop> shopList = shopClient.getAllShop().getData();
        List<Long> appUserIds = shopList.stream().map(Shop::getAppUserId).collect(Collectors.toList());
        Long appUserId = shop.getAppUserId();
        //所有未开店的用户
        List<AppUser> list = appUserService.list(new LambdaQueryWrapper<AppUser>().eq(AppUser::getDelFlag, 0)
                .ne(AppUser::getStatus, 3).notIn(AppUser::getId, appUserIds));
        Set<Long> ids = new HashSet<>();
        getShopServerUser(appUserId, list, ids);
        return ids;
    }
    
    
    public void getShopServerUser(Long appUserId, List<AppUser> appUserList, Set<Long> ids){
        List<AppUser> collect = appUserList.stream().filter(s->s.getInviteUserId().equals(appUserId)).collect(Collectors.toList());
        if(collect.size() == 0){
            return;
        }
        Set<Long> appUserSet = collect.stream().map(AppUser::getId).collect(Collectors.toSet());
        if(ids.containsAll(appUserSet)){
            return;
        }
        ids.addAll(appUserSet);
        for (AppUser appUser : collect) {
            getShopServerUser(appUser.getId(), appUserList, ids);
        }
    }
    @GetMapping("/index/change")
    @PostMapping("/index/change")
    @ApiOperation(value = "修改个人资料", tags = {"小程序-个人中心首页"})
    public R<AppUser> indexchange(String avatar, String name) {
    public R<AppUser> indexchange(@RequestBody ChangeAppUserInfo changeAppInfo) {
        Long userId = tokenService.getLoginUserApplet().getUserid();
        //当前用户信息
        AppUser user = appUserService.getById(userId);
        user.setName(name);
        user.setAvatar(avatar);
        if (null != changeAppInfo.getName()){
            user.setName(changeAppInfo.getName());
        }
        if (null != changeAppInfo.getAvatar()){
            user.setAvatar(changeAppInfo.getAvatar());
        }
        appUserService.updateById(user);
        return R.ok();
    }
    /**
     * 获取用户的祖籍列表
     */
    @GetMapping("/getUserAncestorList")
    public R<List<AppUser>> getUserAncestorList(Long id) {
        List<AppUser> list = appUserService.getUserAncestorList(id, null);
        return R.ok(list);
    }
    /**
     * 获取用户的直帮上级用户
     */
    @ResponseBody
    @PostMapping("/getSuperiorLeader")
    @ApiOperation(value = "获取用户的直帮上级用户")
    public R<AppUser> getSuperiorLeader(@RequestParam("id") Long id) {
        AppUser superiorLeader = appUserService.getSuperiorLeader(id);
        return R.ok(superiorLeader);
    }
    @ResponseBody
    @PostMapping("/onlineRecord")
    @ApiOperation(value = "10分钟定时任务调用,记录用户在线时长", tags = {"小程序-个人中心首页"})
    public R onlineRecord() {
        appUserService.onlineRecord();
        return R.ok();
    }
    /**
@@ -377,8 +291,8 @@
            List<Order> orders = orderClient.getRedeemedOrdersByShop(shopId).getData();
            List<Long> userIds = orders.stream().map(Order::getAppUserId).collect(Collectors.toList());
            //门店作为服务商的用户
            Set<Long> shopServerUser = getShopServerUser(shopId);
            userIds.addAll(shopServerUser);
           /* Set<Long> shopServerUser = getShopServerUser(shopId);
            userIds.addAll(shopServerUser);*/
            queryWrapper.in(!CollectionUtils.isEmpty(userIds), "id", userIds)
                    .like(StringUtils.isNotEmpty( appUser.getName()),"name", appUser.getName());
        }
@@ -399,31 +313,10 @@
    public R<IPage<AppUser>> getAppuserPage(@ApiParam("页码") @RequestParam Integer pageNum,
                                               @ApiParam("每一页数据大小") Integer pageSize,
                                               AppUser appUser){
        Long userid = tokenService.getLoginUser().getUserid();
        SysUser sysUser = sysUserClient.getSysUser(userid).getData();
        Integer shopId = null;
        Set<Long> userId = null;
        if(sysUser.getRoleType() == 2){
            shopId = sysUser.getObjectId();
            userId = orderClient.getAppUserByShoppingShop(shopId).getData();
        }
        appUser.setExcludeStatus(3);
        IPage<AppUser> appuserPage = appUserService.getAppuserPage(pageNum, pageSize, appUser, shopId, userId);
        for (AppUser record : appuserPage.getRecords()) {
            if (record.getInviteUserId() != null) {
                AppUser byId1 = appUserService.getById(record.getInviteUserId());
                if (byId1!=null) {
                    record.setInviteUserName(byId1.getName());
                }
            }
            Shop shop1 = shopClient.getServiceProvider(record.getId()).getData();
            if(null != shop1){
                record.setShopName(shop1.getName());
                record.setShopId(shop1.getId());
            }
            record.setTotalPoint(record.getUserTotalPoint());
        }
        return R.ok(appuserPage);
    }
@@ -445,26 +338,26 @@
        }
        IPage<AppUser> appuserPage = appUserService.getAppuserPage(pageNum, pageSize, appUser, shopId, userId);
        for (AppUser record : appuserPage.getRecords()) {
            if (record.getInviteUserId() != null) {
           /* if (record.getInviteUserId() != null) {
                AppUser byId1 = appUserService.getById(record.getInviteUserId());
                if (byId1!=null) {
                    record.setInviteUserName(byId1.getName());
                }
            }
            }*/
            if (record.getShopId()!=null){
                R<Shop> shopById = shopClient.getShopById(record.getShopId());
                if (shopById.getData()!=null){
                    record.setShopName(shopById.getData().getName());
                }
            }
            record.setTotalPoint(record.getUserTotalPoint());
        }
        return R.ok(appuserPage);
    }
    @GetMapping("/shop/getAppuserPage")
    @ApiOperation(value = "用户列表", tags = {"门店后台"})
    @ApiOperation(value = "用户列表-在门店下过单的", tags = {"门店后台"})
    public R<IPage<AppUser>> shopGetAppuserPage(@ApiParam("页码") @RequestParam Integer pageCurr,
                                                @ApiParam("每一页数据大小") Integer pageSize,
                                                AppUser appUser) {
@@ -481,8 +374,8 @@
            for (Order datum : listR.getData()) {
                userIds.add(datum.getAppUserId());
            }
            Set<Long> shopServerUser = getShopServerUser(objectId);
            userIds.addAll(shopServerUser);
           /* Set<Long> shopServerUser = getShopServerUser(objectId);
            userIds.addAll(shopServerUser);*/
        }
        IPage<AppUser> appuserPage = appUserService.getAppuserPage1(pageCurr, pageSize, appUser, userIds);
        return R.ok(appuserPage);
@@ -529,6 +422,7 @@
/*
    @GetMapping("/detail")
    @ApiOperation(value = "用户列表-详情", tags = {"管理后台"})
@@ -549,20 +443,13 @@
            }
            byId.setShopNames(shopName);
        }
        //推广人
        if (byId.getInviteUserId() != null) {
            AppUser appUser = appUserService.getById(byId.getInviteUserId());
            if(null != appUser){
                byId.setInviteUserName(appUser.getName());
            }
        }
        //最后下单时间
        R<Order> lastOrder = remoteOrderGoodsClient.getLastOrder(id);
        if (lastOrder.getData() != null) {
            byId.setLastOrderTime(lastOrder.getData().getCreateTime());
        }
        List<AppUser> list = appUserService.lambdaQuery().eq(AppUser::getInviteUserId, id).list();
        byId.setBottomUsers(list);
        //消费总金额
        if(null == shopId || 1 == sysUser.getRoleType()){
            shopId = -1;
@@ -580,32 +467,9 @@
        }
        return R.ok(byId);
    }
    /**
     * 获取指定用户的下级用户
     */
    @GetMapping("/bottom/list")
    @ApiOperation(value = "用户列表-下级用户", tags = {"管理后台"})
    public R<Page<AppUser>> bottom(Integer pageNum, Integer pageSize, Long userId){
        Page<AppUser> page = appUserService.page(Page.of(pageNum, pageSize), new LambdaQueryWrapper<AppUser>()
                .eq(AppUser::getInviteUserId, userId));
        return R.ok(page);
    }
*/
    @GetMapping("/bottom")
    @ApiOperation(value = "用户列表-绑定下级列表", tags = {"管理后台"})
    public R<Page<AppUser>> bottom(Long id, Integer pageNum, Integer pageSize) {
        //绑定下级
        Page<AppUser> page = appUserService.lambdaQuery()
                .eq(AppUser::getInviteUserId, id)
                .eq(AppUser::getDelFlag, 0)
                .eq(AppUser::getStatus, 1)
                .page(Page.of(pageNum, pageSize));
        return R.ok(page);
    }
    @GetMapping("/orders")
    @ApiOperation(value = "用户列表-订单列表", tags = {"管理后台"})
@@ -659,63 +523,78 @@
    }
    /**
     * 设置指定用户的下级用户绑定门店
     */
    @PostMapping("/setLowerUserShop")
    public R<Void> setLowerUserShop(@RequestParam("userId") Long userId,@RequestParam("shopId") Integer shopId) {
        appUserService.update(new LambdaUpdateWrapper<AppUser>()
                .set(AppUser::getShopId,shopId)
                .eq(AppUser::getInviteUserId, userId));
        return R.ok();
    }
    /**
     * 用户统计
     * 工作台-顶部
     */
    @GetMapping("/statistics")
    @ApiOperation(value = "用户统计", tags = {"管理后台-首页统计-用户统计"})
    @GetMapping("/homeStatistics/statistics")
    @ApiOperation(value = "统计", tags = {"后台-工作台-顶部"})
    public R<UserStatistics> statistics() {
        Long userId = tokenService.getLoginUser().getUserid();
        SysUser data = sysUserClient.getSysUser(userId).getData();
        QueryWrapper<AppUser> queryWrapper = new QueryWrapper<>();
        if (data.getRoleType() == 2) {
            Integer shopId = data.getObjectId();
            List<Order> orders = orderClient.getRedeemedOrdersByShop(shopId).getData();
            List<Long> userIds = orders.stream().map(Order::getAppUserId).collect(Collectors.toList());
            queryWrapper.in(!CollectionUtils.isEmpty(userIds), "id", userIds); // userIds 不为空时,查询 id 在 userIds 中
        }
        queryWrapper.eq("del_flag", 0);
        queryWrapper.ne("status", 3);
        List<AppUser> appUserList = appUserService.list(queryWrapper);
        queryWrapper.ne("status", 3);//没有注销的
        Map<Integer, Long> vipIdCountMap = appUserList.stream()
                .collect(Collectors.groupingBy(AppUser::getVipId, Collectors.counting()));
        List<AppUser> appUserList = appUserService.list(queryWrapper);
        long consumerUser = appUserList.stream()
                .filter(user -> user.getLastShopTime() != null)
                .count();//消费者总数
        UserStatistics userStatistics = new UserStatistics();
        userStatistics.setTotalUser(appUserList.size());
        userStatistics.setConsumerUser(vipIdCountMap.getOrDefault(0, 0L).intValue() +
                vipIdCountMap.getOrDefault(1, 0L).intValue() +
                vipIdCountMap.getOrDefault(2, 0L).intValue() +
                vipIdCountMap.getOrDefault(3, 0L).intValue());
        userStatistics.setCommonUser(vipIdCountMap.getOrDefault(1, 0L).intValue());
        userStatistics.setGoldUser(vipIdCountMap.getOrDefault(2, 0L).intValue());
        userStatistics.setDiamondUser(vipIdCountMap.getOrDefault(3, 0L).intValue());
        userStatistics.setEntrepreneurUser(vipIdCountMap.getOrDefault(4, 0L).intValue() +
                vipIdCountMap.getOrDefault(5, 0L).intValue() +
                vipIdCountMap.getOrDefault(6, 0L).intValue() +
                vipIdCountMap.getOrDefault(7, 0L).intValue());
        userStatistics.setProxyUser(vipIdCountMap.getOrDefault(4, 0L).intValue());
        userStatistics.setAgentUser(vipIdCountMap.getOrDefault(5, 0L).intValue());
        userStatistics.setTotalAgentUser(vipIdCountMap.getOrDefault(6, 0L).intValue());
        userStatistics.setPartnerUser(vipIdCountMap.getOrDefault(7, 0L).intValue());
        userStatistics.setTotalUser(appUserList.size());//总用户数
        userStatistics.setConsumerUser((int) consumerUser);//消费过的用户
        //用户类型统计门店
        long shopUserCount = appUserList.stream()
                .filter(appUser -> appUser.getUserType() == 2)
                .count();
        userStatistics.setShopUser((int) shopUserCount);
        //门店总数
        long shopNum = shopClient.getAllShop().getData().size();
        userStatistics.setShopNum((int) shopNum);
        return R.ok(userStatistics);
    }
    /**
     * 工作台-用户分析
     */
    @GetMapping("/homeStatistics/userAnalysis")
    @ApiOperation(value = "用户分析", tags = {"后台-工作台-用户分析"})
    public R<UserStatisticsDetail> userAnalysis(@ApiParam(value = "用户id") @RequestParam(value = "userId",required = false) Long userId) {
        UserStatisticsDetail userStatistics = new UserStatisticsDetail();
        QueryWrapper<AppUser> queryWrapper = new QueryWrapper<>();
        // 条件构造  统计充值积分
        queryWrapper.select("SUM(recharge_point) as total_points");
        queryWrapper.eq("del_flag", 0);//未删除的
        queryWrapper.ne("status", 3);//未注销的
        if (userId != null) {
            queryWrapper.eq("user_id", userId);
        }
        //统计充值积分
        Map<String, Object> result = appUserService.getMap(queryWrapper);
        if (result == null || result.get("total_points") == null) {
            userStatistics.setTotalScore(0L);
        }else {
            userStatistics.setTotalScore((Long) result.get("total_points"));
        }
        //统计消费积分和现金支付数
        Map<String, Object> data = orderClient.getConsumeScoreAndPayAmount(userId).getData();
        if (data == null || data.get("total_points") == null) {
            userStatistics.setConsumeScore(0L);
        }else {
            userStatistics.setConsumeScore(Long.valueOf(data.get("total_points").toString()) );
        }
        if (data == null || data.get("payment_amounts") == null) {
            userStatistics.setShopAmount(BigDecimal.ZERO);
        }else {
            userStatistics.setShopAmount(new BigDecimal(data.get("payment_amounts").toString()));
        }
        return R.ok(userStatistics);
    }
@@ -738,8 +617,8 @@
            shopId = data.getObjectId();
            if(null == userId){
                userIds = orderClient.getAppUserByShoppingShop(shopId).getData();
                Set<Long> shopServerUser = getShopServerUser(shopId);
                userIds.addAll(shopServerUser);
              /*  Set<Long> shopServerUser = getShopServerUser(shopId);
                userIds.addAll(shopServerUser);*/
            }else{
                userIds = new HashSet<>();
                userIds.add(userId);
@@ -758,14 +637,14 @@
     * @return
     */
    @PostMapping("/upload")
    public String upload(MultipartFile file){
    public R upload(MultipartFile file){
        String s = null;
        try {
            s = ObsUploadUtil.obsUpload(file);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return s;
        return R.ok(s);
    }
    
    
@@ -782,5 +661,32 @@
        return R.ok();
    }
    /**
     * 获取所有用户积分列表
     */
    @ResponseBody
    @GetMapping("/getUserPointPageList")
    @ApiOperation(value = "所有用户积分列表", tags = "后台-财务统计-用户积分统计")
    public R<PageInfo<UserPointStatisticsVO>> getUserPointPageList(@RequestParam(value = "name",required = false) String name,
                                                                   @ApiParam("当前页")@RequestParam("pageCurr") Integer pageCurr,
                                                                   @ApiParam("分页大小")@RequestParam("pageSize") Integer pageSize) {
        PageInfo<UserPointStatisticsVO> pageInfo=appUserService.getUserPointPageList(name,pageCurr,pageSize);
        return R.ok(pageInfo);
    }
    @PostMapping("/saveOrUpdateAppUser")
    Long saveOrUpdateAppUser(@RequestBody AppUser appuser){
        if (appuser.getId() == null) {
            //保存用户
            appUserService.save(appuser);
        }else {
            appUserService.updateById(appuser);
        }
        return appuser.getId();
    }
}