From d53c16a12e612a95dc8f6fb248e3669cbc441eb4 Mon Sep 17 00:00:00 2001
From: Pu Zhibing <393733352@qq.com>
Date: 星期二, 17 十二月 2024 16:48:41 +0800
Subject: [PATCH] 新增加商品特价接口

---
 ruoyi-service/ruoyi-other/src/main/java/com/ruoyi/other/service/impl/GoodsServiceImpl.java |  280 +++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 258 insertions(+), 22 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 d5f42ee..0a91761 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,27 +1,34 @@
 package com.ruoyi.other.service.impl;
 
-import cn.hutool.json.JSONArray;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+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.domain.R;
 import com.ruoyi.common.core.utils.StringUtils;
 import com.ruoyi.common.core.utils.bean.BeanUtils;
 import com.ruoyi.common.security.service.TokenService;
-import com.ruoyi.other.api.domain.Goods;
-import com.ruoyi.other.api.domain.VipSetting;
+import com.ruoyi.order.feignClient.RemoteOrderGoodsClient;
+import com.ruoyi.order.vo.Price;
+import com.ruoyi.other.api.domain.*;
 import com.ruoyi.other.enums.GoodsStatus;
 import com.ruoyi.other.mapper.GoodsAreaMapper;
 import com.ruoyi.other.mapper.GoodsMapper;
-import com.ruoyi.other.service.GoodsService;
-import com.ruoyi.other.service.GoodsVipService;
-import com.ruoyi.other.service.VipSettingService;
+import com.ruoyi.other.mapper.GoodsShopMapper;
+import com.ruoyi.other.mapper.ShopMapper;
+import com.ruoyi.other.service.*;
 import com.ruoyi.other.vo.GoodsVO;
 import com.ruoyi.system.api.model.LoginUser;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+import org.springframework.util.CollectionUtils;
 
 import javax.annotation.Resource;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Objects;
+import java.math.BigDecimal;
+import java.util.*;
+import java.util.stream.Collectors;
 
 /**
  * <p>
@@ -43,18 +50,41 @@
     private GoodsAreaMapper goodsAreaMapper;
     @Resource
     private GoodsVipService goodsVipService;
+    @Resource
+    private AppUserClient appUserClient;
+    @Resource
+    private GoodsShopMapper goodsShopMapper;
+    @Resource
+    private GoodsShopService goodsShopService;
+    @Resource
+    private GoodsAppUserService goodsAppUserService;
+    @Resource
+    private ShopMapper shopMapper;
+    @Resource
+    private GoodsAreaService goodsAreaService;
+    @Resource
+    private RemoteOrderGoodsClient remoteOrderGoodsClient;
 
     @Override
     public List<GoodsVO> goodsList(Goods search) {
         List<Goods> goodsList = this.list(new LambdaQueryWrapper<Goods>()
-                .eq(Goods::getStatus, GoodsStatus.UP)
-                .eq(Objects.nonNull(search.getGoodsCategoryId()) ,Goods::getGoodsCategoryId, search.getGoodsCategoryId())
+                .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);
         }
         return result;
@@ -62,27 +92,233 @@
 
     @Override
     public GoodsVO goodsDetail(Long goodsId) {
-        // TODO 根据会员等级计算价格、积分
+        if (goodsId == null || goodsId <= 0) {
+            throw new NullPointerException("商品ID不能为空");
+        }
+
         LoginUser loginUserApplet = tokenService.getLoginUserApplet();
-        VipSetting vipSetting = vipSettingService.getVipSettingByUserId(loginUserApplet.getUserid());
-        // ...
+        AppUser appUser = appUserClient.getAppUserById(loginUserApplet.getUserid());
+        BigDecimal sellingPrice;
+        Integer integral;
+
+        GoodsArea goodsArea = goodsAreaMapper.selectOne(new LambdaQueryWrapper<GoodsArea>()
+                .eq(GoodsArea::getGoodsId, goodsId)
+                .eq(GoodsArea::getProvinceCode, appUser.getProvinceCode())
+                .eq(StringUtils.isNotEmpty(appUser.getCityCode()), GoodsArea::getCityCode, appUser.getCityCode())
+                .eq(StringUtils.isNotEmpty(appUser.getDistrictCode()), GoodsArea::getDistrictsCode, appUser.getDistrictCode()));
+
+        if (Objects.nonNull(goodsArea)){
+            sellingPrice = goodsArea.getSellingPrice();
+            integral = goodsArea.getIntegral();
+        }else {
+            VipSetting vipSetting = vipSettingService.getVipSettingByUserId(loginUserApplet.getUserid());
+            GoodsVip goodsVip = goodsVipService.getOne(new LambdaQueryWrapper<GoodsVip>()
+                    .eq(GoodsVip::getVip, vipSetting.getId())
+                    .eq(GoodsVip::getGoodsId, goodsId));
+
+            sellingPrice = goodsVip.getSellingPrice();
+            integral = goodsVip.getIntegral();
+        }
 
         Goods goods = this.getById(goodsId);
-        if (Objects.nonNull(goods)){
-            GoodsVO goodsVO = new GoodsVO();
-            BeanUtils.copyBeanProp(goodsVO, goods);
-            return goodsVO;
+        GoodsVO goodsVO = new GoodsVO();
+        BeanUtils.copyBeanProp(goodsVO, goods);
+        goodsVO.setGoodsId(goods.getId());
+        goodsVO.setGoodsName(goods.getName());
+        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());
+            List<Shop> shopList = shopMapper.selectList(new LambdaQueryWrapper<Shop>()
+                    .in(Shop::getId, shopIds));
+
+            goodsVO.setShopList(shopList);
         }
-        return new GoodsVO();
+        return goodsVO;
     }
+
 
     @Override
     public List<Goods> getGoodsListByShopId(Integer shopId) {
         LoginUser loginUserApplet = tokenService.getLoginUserApplet();
         VipSetting vipSetting = vipSettingService.getVipSettingByUserId(loginUserApplet.getUserid());
-        JSONArray array = new JSONArray();
-        array.add(vipSetting.getId());
-        return goodsMapper.selectListByShopId(shopId,array.toString());
+        return goodsMapper.selectListByShopId(shopId, vipSetting.getId());
     }
 
+    @Override
+    public IPage<Goods> getManageGoodsList(Page<Goods> page, Goods goods) {
+        return goodsMapper.selectManageGoodsList(page, goods);
+    }
+
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public void addGoods(Goods goods) {
+        goods.setSaleNum(0);
+        goods.setStatus(GoodsStatus.DOWN.getCode());
+        goodsMapper.insert(goods);
+
+        // 指定门店
+        Integer appointStore = goods.getAppointStore();
+        if (appointStore == 1){
+            List<GoodsShop> goodsShopList = goods.getGoodsShopList();
+            if (CollectionUtils.isEmpty(goodsShopList)){
+                throw new NullPointerException("请选择指定门店");
+            }
+            for (GoodsShop goodsShop : goodsShopList) {
+                goodsShop.setGoodsId(goods.getId());
+                goodsShop.setId(null);
+            }
+            goodsShopService.saveBatch(goodsShopList);
+        }
+
+        // 指定用户
+        List<GoodsAppUser> goodsAppUserList = goods.getGoodsAppUserList();
+        if (!CollectionUtils.isEmpty(goodsAppUserList)){
+            for (GoodsAppUser goodsAppUser : goodsAppUserList) {
+                goodsAppUser.setId(null );
+                goodsAppUser.setGoodsId(goods.getId());
+            }
+            goodsAppUserService.saveBatch(goodsAppUserList);
+        }
+
+        // 会员价格配置
+        List<GoodsVip> goodsVipList = goods.getGoodsVipList();
+        for (GoodsVip goodsVip : goodsVipList) {
+            goodsVip.setId(null);
+            goodsVip.setGoodsId(goods.getId());
+        }
+        goodsVipService.saveBatch(goodsVipList);
+
+        // 特殊地区售价设置
+        List<GoodsArea> goodsAreaList = goods.getGoodsAreaList();
+        if (!CollectionUtils.isEmpty(goodsAreaList)){
+            for (GoodsArea goodsArea : goodsAreaList) {
+                List<GoodsArea> goodsAreaList1 = goodsArea.getGoodsAreaList();
+                for (GoodsArea area : goodsAreaList1) {
+                    area.setGoodsId(goods.getId());
+                    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.saveBatch(goodsAreaList1);
+            }
+        }
+
+    }
+
+    @Override
+    public void updateManageGoods(Goods goods) {
+        goodsMapper.updateById(goods);
+        // 指定门店
+        List<GoodsShop> goodsShopList = goods.getGoodsShopList();
+        goodsShopService.updateBatchById(goodsShopList);
+
+        // 指定用户
+        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.setGoodsId(goods.getId());
+                    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);
+            }
+        }
+    }
+
+    @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());
+            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));
+        goods.setGoodsVipList(goodsVipList);
+
+        // 特殊地区售价设置
+        List<GoodsArea> goodsAreaList = goodsAreaService.list(new LambdaQueryWrapper<GoodsArea>()
+                .eq(GoodsArea::getGoodsId, goodsId));
+
+        Map<String, List<GoodsArea>> listMap = goodsAreaList.stream()
+                .collect(Collectors.groupingBy(goodsArea ->
+                        goodsArea.getProvinceCode() + goodsArea.getCityCode() + goodsArea.getDistrictsCode()));
+
+        List<GoodsArea> goodsAreaList1 = new ArrayList<>();
+        listMap.forEach((s, goodsAreas) -> {
+
+            List<Integer> vipIds = goodsAreas.stream().map(GoodsArea::getVip).collect(Collectors.toList());
+            List<VipSetting> vipSettings = vipSettingService.listByIds(vipIds);
+            goodsAreas.forEach(goodsArea -> vipSettings.stream()
+                    .filter(vipSetting -> vipSetting.getId().equals(goodsArea.getVip()))
+                    .findFirst().ifPresent( vipSetting -> goodsArea.setVipName(vipSetting.getVipName())));
+
+            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);
+            goodsAreaList1.add(goodsArea1);
+        });
+
+        goods.setGoodsAreaList(goodsAreaList1);
+        return goods;
+    }
 }

--
Gitblit v1.7.1