From 210828d1a6947596fe00ab30fcd8725c6714dde4 Mon Sep 17 00:00:00 2001
From: puzhibing <393733352@qq.com>
Date: 星期四, 02 一月 2025 21:04:16 +0800
Subject: [PATCH] 修改bug

---
 ruoyi-service/ruoyi-other/src/main/java/com/ruoyi/other/service/impl/GoodsServiceImpl.java |  248 +++++++++++++++++++++++++++++++++++--------------
 1 files changed, 178 insertions(+), 70 deletions(-)

diff --git a/ruoyi-service/ruoyi-other/src/main/java/com/ruoyi/other/service/impl/GoodsServiceImpl.java b/ruoyi-service/ruoyi-other/src/main/java/com/ruoyi/other/service/impl/GoodsServiceImpl.java
index 211121b..d647b1d 100644
--- a/ruoyi-service/ruoyi-other/src/main/java/com/ruoyi/other/service/impl/GoodsServiceImpl.java
+++ b/ruoyi-service/ruoyi-other/src/main/java/com/ruoyi/other/service/impl/GoodsServiceImpl.java
@@ -1,15 +1,20 @@
 package com.ruoyi.other.service.impl;
 
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.ruoyi.account.api.feignClient.AppUserClient;
 import com.ruoyi.account.api.model.AppUser;
+import com.ruoyi.common.core.constant.TokenConstants;
 import com.ruoyi.common.core.domain.R;
+import com.ruoyi.common.core.utils.ServletUtils;
 import com.ruoyi.common.core.utils.StringUtils;
 import com.ruoyi.common.core.utils.bean.BeanUtils;
+import com.ruoyi.common.core.web.page.PageInfo;
 import com.ruoyi.common.security.service.TokenService;
+import com.ruoyi.common.security.utils.SecurityUtils;
 import com.ruoyi.order.feignClient.RemoteOrderGoodsClient;
 import com.ruoyi.order.vo.Price;
 import com.ruoyi.other.api.domain.*;
@@ -21,6 +26,7 @@
 import com.ruoyi.other.service.*;
 import com.ruoyi.other.vo.GoodsVO;
 import com.ruoyi.system.api.model.LoginUser;
+import org.jetbrains.annotations.NotNull;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.util.CollectionUtils;
@@ -29,6 +35,7 @@
 import java.math.BigDecimal;
 import java.util.*;
 import java.util.stream.Collectors;
+import java.util.stream.Stream;
 
 /**
  * <p>
@@ -66,28 +73,26 @@
     private RemoteOrderGoodsClient remoteOrderGoodsClient;
 
     @Override
-    public List<GoodsVO> goodsList(Goods search) {
-        List<Goods> goodsList = this.list(new LambdaQueryWrapper<Goods>()
-                .eq(Goods::getStatus, GoodsStatus.UP.getCode())
-                .eq(Objects.nonNull(search.getGoodsCategoryId()), Goods::getGoodsCategoryId, search.getGoodsCategoryId())
-                .like(StringUtils.isNotEmpty(search.getName()), Goods::getName, search.getName()));
-
-        List<GoodsVO> result = new ArrayList<>();
-        LoginUser loginUserApplet = tokenService.getLoginUserApplet();
-        for (Goods goods : goodsList) {
-            GoodsVO goodsVO = new GoodsVO();
-            BeanUtils.copyBeanProp(goodsVO, goods);
-            goodsVO.setGoodsId(goods.getId());
-            goodsVO.setGoodsName(goods.getName());
-            R<Price> r = remoteOrderGoodsClient.getGoodsPrice(loginUserApplet.getUserid(), goods.getId(), null);
-            if (R.isSuccess(r)){
-                Price price = r.getData();
-                goodsVO.setSellingPrice(price.getCash());
-                goodsVO.setIntegral(price.getPoint());
-            }
-            result.add(goodsVO);
+    public PageInfo<GoodsVO> goodsList(Goods search) {
+        PageInfo<GoodsVO> pageInfo = new PageInfo(search.getPageCurr(), search.getPageSize());
+        List<GoodsVO> list = this.baseMapper.goodsList(pageInfo, search.getGoodsCategoryId(), search.getName());
+        Long userId = null;
+        String token = SecurityUtils.getToken(ServletUtils.getRequest());
+        if(StringUtils.isNotEmpty(token)){
+            userId = tokenService.getLoginUserApplet().getUserid();
         }
-        return result;
+        for (GoodsVO goods : list) {
+            //游客展示基础售价
+            if(null != userId){
+                R<Price> r = remoteOrderGoodsClient.getGoodsPrice(userId, goods.getGoodsId(), null);
+                if (null != r.getData()){
+                    Price price = r.getData();
+                    goods.setSellingPrice(price.getCash());
+                    goods.setIntegral(price.getPoint());
+                }
+            }
+        }
+        return pageInfo.setRecords(list);
     }
 
     @Override
@@ -98,11 +103,12 @@
 
         LoginUser loginUserApplet = tokenService.getLoginUserApplet();
         AppUser appUser = appUserClient.getAppUserById(loginUserApplet.getUserid());
-        BigDecimal sellingPrice;
-        Integer integral;
+        BigDecimal sellingPrice = BigDecimal.ZERO;
+        Integer integral = 0;
 
         GoodsArea goodsArea = goodsAreaMapper.selectOne(new LambdaQueryWrapper<GoodsArea>()
                 .eq(GoodsArea::getGoodsId, goodsId)
+                .eq(GoodsArea::getVip, appUser.getVipId())
                 .eq(GoodsArea::getProvinceCode, appUser.getProvinceCode())
                 .eq(StringUtils.isNotEmpty(appUser.getCityCode()), GoodsArea::getCityCode, appUser.getCityCode())
                 .eq(StringUtils.isNotEmpty(appUser.getDistrictCode()), GoodsArea::getDistrictsCode, appUser.getDistrictCode()));
@@ -115,9 +121,10 @@
             GoodsVip goodsVip = goodsVipService.getOne(new LambdaQueryWrapper<GoodsVip>()
                     .eq(GoodsVip::getVip, vipSetting.getId())
                     .eq(GoodsVip::getGoodsId, goodsId));
-
-            sellingPrice = goodsVip.getSellingPrice();
-            integral = goodsVip.getIntegral();
+            if(null != goodsVip){
+                sellingPrice = goodsVip.getSellingPrice();
+                integral = goodsVip.getIntegral();
+            }
         }
 
         Goods goods = this.getById(goodsId);
@@ -128,13 +135,24 @@
         goodsVO.setSellingPrice(sellingPrice);
         goodsVO.setIntegral(integral);
 
-        List<GoodsShop> goodsShopList = goodsShopMapper.selectList(new LambdaQueryWrapper<GoodsShop>()
-                .eq(GoodsShop::getGoodsId, goodsId));
-        if (!CollectionUtils.isEmpty(goodsShopList)){
-            List<Integer> shopIds = goodsShopList.stream().map(GoodsShop::getShopId).collect(Collectors.toList());
+        if(goods.getType() == 1){
+            if(goods.getAppointStore() == 2){
+                List<Shop> shopList = shopMapper.selectList(new LambdaQueryWrapper<Shop>()
+                        .in(Shop::getDelFlag, 0));
+                goodsVO.setShopList(shopList);
+            }else{
+                List<GoodsShop> goodsShopList = goodsShopMapper.selectList(new LambdaQueryWrapper<GoodsShop>()
+                        .eq(GoodsShop::getGoodsId, goodsId));
+                if (!CollectionUtils.isEmpty(goodsShopList)){
+                    List<Integer> shopIds = goodsShopList.stream().map(GoodsShop::getShopId).collect(Collectors.toList());
+                    List<Shop> shopList = shopMapper.selectList(new LambdaQueryWrapper<Shop>()
+                            .in(Shop::getId, shopIds));
+                    goodsVO.setShopList(shopList);
+                }
+            }
+        }else{
             List<Shop> shopList = shopMapper.selectList(new LambdaQueryWrapper<Shop>()
-                    .in(Shop::getId, shopIds));
-
+                    .in(Shop::getDelFlag, 0));
             goodsVO.setShopList(shopList);
         }
         return goodsVO;
@@ -142,10 +160,10 @@
 
 
     @Override
-    public List<Goods> getGoodsListByShopId(Integer shopId) {
+    public List<Goods> getGoodsListByShopId(PageInfo<Goods> pageInfo, Integer shopId) {
         LoginUser loginUserApplet = tokenService.getLoginUserApplet();
         VipSetting vipSetting = vipSettingService.getVipSettingByUserId(loginUserApplet.getUserid());
-        return goodsMapper.selectListByShopId(shopId, vipSetting.getId());
+        return goodsMapper.selectListByShopId(pageInfo, shopId, vipSetting.getId());
     }
 
     @Override
@@ -162,30 +180,61 @@
 
         // 指定门店
         Integer appointStore = goods.getAppointStore();
-        if (appointStore == 1){
+        if (null != appointStore && appointStore == 1){
             List<GoodsShop> goodsShopList = goods.getGoodsShopList();
             if (CollectionUtils.isEmpty(goodsShopList)){
                 throw new NullPointerException("请选择指定门店");
             }
-            goodsShopService.saveBatch(goodsShopList);
+            saveGoodsShopList(goodsShopList, goods.getId());
         }
 
         // 指定用户
         List<GoodsAppUser> goodsAppUserList = goods.getGoodsAppUserList();
         if (!CollectionUtils.isEmpty(goodsAppUserList)){
-            goodsAppUserService.saveBatch(goodsAppUserList);
+            saveGoodsAppUserList(goodsAppUserList, goods.getId());
         }
 
         // 会员价格配置
         List<GoodsVip> goodsVipList = goods.getGoodsVipList();
-        goodsVipService.saveBatch(goodsVipList);
+        saveGoodsVipList(goodsVipList, goods.getId());
 
         // 特殊地区售价设置
         List<GoodsArea> goodsAreaList = goods.getGoodsAreaList();
         if (!CollectionUtils.isEmpty(goodsAreaList)){
+            saveGoodsAreaList(goodsAreaList, goods.getId());
+        }
+
+    }
+
+    @Override
+    public void updateManageGoods(Goods goods) {
+        goodsMapper.updateById(goods);
+        // 指定门店
+        List<GoodsShop> goodsShopList = goods.getGoodsShopList();
+        saveGoodsShopList(goodsShopList, goods.getId());
+
+        // 指定用户
+        List<GoodsAppUser> goodsAppUserList = goods.getGoodsAppUserList();
+        saveGoodsAppUserList(goodsAppUserList, goods.getId());
+
+        // 会员价格配置
+        List<GoodsVip> goodsVipList = goods.getGoodsVipList();
+        saveGoodsVipList(goodsVipList, goods.getId());
+
+        // 特殊地区售价设置
+        List<GoodsArea> goodsAreaList = goods.getGoodsAreaList();
+        saveGoodsAreaList(goodsAreaList, goods.getId());
+    }
+
+    private void saveGoodsAreaList(List<GoodsArea> goodsAreaList, Integer id) {
+        goodsAreaService.remove(new LambdaQueryWrapper<GoodsArea>()
+                .eq(GoodsArea::getGoodsId, id));
+        if (!CollectionUtils.isEmpty(goodsAreaList)){
             for (GoodsArea goodsArea : goodsAreaList) {
                 List<GoodsArea> goodsAreaList1 = goodsArea.getGoodsAreaList();
                 for (GoodsArea area : goodsAreaList1) {
+                    area.setId(null);
+                    area.setGoodsId(id);
                     area.setProvince(goodsArea.getProvince());
                     area.setCity(goodsArea.getCity());
                     area.setDistricts(goodsArea.getDistricts());
@@ -196,58 +245,90 @@
                 goodsAreaService.saveBatch(goodsAreaList1);
             }
         }
-
     }
 
-    @Override
-    public void updateManageGoods(Goods goods) {
-        goodsMapper.updateById(goods);
-        // 指定门店
-        List<GoodsShop> goodsShopList = goods.getGoodsShopList();
-        goodsShopService.updateBatchById(goodsShopList);
+    private void saveGoodsVipList(List<GoodsVip> goodsVipList, Integer id) {
+        goodsVipService.remove(new LambdaQueryWrapper<GoodsVip>()
+                .eq(GoodsVip::getGoodsId, id));
+        goodsVipList.forEach(item -> {
+            item.setId(null);
+            item.setGoodsId(id);
+        });
+        goodsVipService.saveBatch(goodsVipList);
+    }
 
-        // 指定用户
-        List<GoodsAppUser> goodsAppUserList = goods.getGoodsAppUserList();
-        goodsAppUserService.updateBatchById(goodsAppUserList);
-
-        // 会员价格配置
-        List<GoodsVip> goodsVipList = goods.getGoodsVipList();
-        goodsVipService.updateBatchById(goodsVipList);
-
-        // 特殊地区售价设置
-        List<GoodsArea> goodsAreaList = goods.getGoodsAreaList();
-        if (!CollectionUtils.isEmpty(goodsAreaList)){
-            for (GoodsArea goodsArea : goodsAreaList) {
-                List<GoodsArea> goodsAreaList1 = goodsArea.getGoodsAreaList();
-                for (GoodsArea area : goodsAreaList1) {
-                    area.setProvince(goodsArea.getProvince());
-                    area.setCity(goodsArea.getCity());
-                    area.setDistricts(goodsArea.getDistricts());
-                    area.setProvinceCode(goodsArea.getProvinceCode());
-                    area.setCityCode(goodsArea.getCityCode());
-                    area.setDistrictsCode(goodsArea.getDistrictsCode());
-                }
-                goodsAreaService.updateBatchById(goodsAreaList1);
+    private void saveGoodsAppUserList(List<GoodsAppUser> goodsAppUserList, Integer id) {
+        goodsAppUserService.remove(new LambdaQueryWrapper<GoodsAppUser>()
+                .eq(GoodsAppUser::getGoodsId, id));
+        if (!CollectionUtils.isEmpty(goodsAppUserList)){
+            for (GoodsAppUser goodsAppUser : goodsAppUserList) {
+                goodsAppUser.setId(null );
+                goodsAppUser.setGoodsId(id);
             }
         }
+        goodsAppUserService.saveBatch(goodsAppUserList);
+    }
+
+    private void saveGoodsShopList(List<GoodsShop> goodsShopList, Integer goodsId) {
+        goodsShopService.remove(new LambdaQueryWrapper<GoodsShop>()
+                .eq(GoodsShop::getGoodsId, goodsId));
+        if (!CollectionUtils.isEmpty(goodsShopList)){
+            for (GoodsShop goodsShop : goodsShopList) {
+                goodsShop.setGoodsId(goodsId);
+                goodsShop.setId(null);
+            }
+        }
+        goodsShopService.saveBatch(goodsShopList);
     }
 
     @Override
     public Goods getManageGoodsDetail(Long goodsId) {
         Goods goods = getById(goodsId);
+        if (goods == null){
+            return null;
+        }
         // 指定门店
         List<GoodsShop> goodsShops = goodsShopService.list(new LambdaQueryWrapper<GoodsShop>()
                 .eq(GoodsShop::getGoodsId, goodsId));
+
+        for (GoodsShop goodsShop : goodsShops) {
+            Shop shop = shopMapper.selectById(goodsShop.getShopId());
+            if(null != shop){
+                goodsShop.setShopName(shop.getName());
+                goodsShop.setOwnerName(shop.getShopManager());
+                goodsShop.setPhone(shop.getPhone());
+                goodsShop.setAddress(shop.getAddress());
+            }
+        }
         goods.setGoodsShopList(goodsShops);
 
         // 指定用户
         List<GoodsAppUser> goodsAppUserList = goodsAppUserService.list(new LambdaQueryWrapper<GoodsAppUser>()
                 .eq(GoodsAppUser::getGoodsId, goodsId));
+        List<Long> userIds = goodsAppUserList.stream().map(GoodsAppUser::getAppUserId).collect(Collectors.toList());
+        List<AppUser> appUsers = appUserClient.listByIds(userIds);
+        for (GoodsAppUser goodsAppUser : goodsAppUserList) {
+            appUsers.stream().filter(u -> u.getId().equals(goodsAppUser.getAppUserId())).findFirst().ifPresent( u -> {
+                goodsAppUser.setUserName(u.getName());
+                goodsAppUser.setPhone(u.getPhone());
+                VipSetting vipSetting = vipSettingService.getById(u.getVipId());
+                if (vipSetting != null){
+                    goodsAppUser.setVipName(vipSetting.getVipName());
+                    goodsAppUser.setVipId(u.getVipId());
+                }
+            });
+        }
+
         goods.setGoodsAppUserList(goodsAppUserList);
 
         // 会员价格
         List<GoodsVip> goodsVipList = goodsVipService.list(new LambdaQueryWrapper<GoodsVip>()
                 .eq(GoodsVip::getGoodsId, goodsId));
+        List<VipSetting> vipSettings = getVipSettings(goodsVipList.stream().map(GoodsVip::getVip));
+        goodsVipList.forEach(goodsArea -> vipSettings.stream()
+                .filter(vipSetting -> vipSetting.getId().equals(goodsArea.getVip()))
+                .findFirst().ifPresent( vipSetting -> goodsArea.setVipName(vipSetting.getVipName())));
+
         goods.setGoodsVipList(goodsVipList);
 
         // 特殊地区售价设置
@@ -260,12 +341,39 @@
 
         List<GoodsArea> goodsAreaList1 = new ArrayList<>();
         listMap.forEach((s, goodsAreas) -> {
-            GoodsArea goodsArea = goodsAreas.get(0);
-            goodsArea.setGoodsAreaList(goodsAreas);
-            goodsAreaList1.add(goodsArea);
+
+            List<VipSetting> vipSettings1 = getVipSettings(goodsAreas.stream().map(GoodsArea::getVip));
+            goodsAreas.forEach(goodsArea -> vipSettings1.stream()
+                    .filter(vipSetting -> vipSetting.getId().equals(goodsArea.getVip()))
+                    .findFirst().ifPresent( vipSetting -> goodsArea.setVipName(vipSetting.getVipName())));
+
+            GoodsArea goodsArea1 = getGoodsArea(goodsAreas);
+            goodsAreaList1.add(goodsArea1);
         });
 
         goods.setGoodsAreaList(goodsAreaList1);
         return goods;
     }
+
+    private List<VipSetting> getVipSettings(Stream<Integer> goodsVipList) {
+        List<Integer> vipIds = goodsVipList.collect(Collectors.toList());
+        if (CollectionUtils.isEmpty(vipIds)){
+            return new ArrayList<>();
+        }
+        return vipSettingService.listByIds(vipIds);
+    }
+
+    @NotNull
+    private static GoodsArea getGoodsArea(List<GoodsArea> goodsAreas) {
+        GoodsArea goodsArea = goodsAreas.get(0);
+        GoodsArea goodsArea1 = new GoodsArea();
+        goodsArea1.setProvinceCode(goodsArea.getProvinceCode());
+        goodsArea1.setCityCode(goodsArea.getCityCode());
+        goodsArea1.setDistrictsCode(goodsArea.getDistrictsCode());
+        goodsArea1.setProvince(goodsArea.getProvince());
+        goodsArea1.setCity(goodsArea.getCity());
+        goodsArea1.setDistricts(goodsArea.getDistricts());
+        goodsArea1.setGoodsAreaList(goodsAreas);
+        return goodsArea1;
+    }
 }

--
Gitblit v1.7.1