Pu Zhibing
2025-01-24 fd45fcbd496bbeaf9ee18edd8f2f858cf9cebaf3
ruoyi-service/ruoyi-other/src/main/java/com/ruoyi/other/service/impl/GoodsServiceImpl.java
@@ -1,5 +1,6 @@
package com.ruoyi.other.service.impl;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
@@ -16,6 +17,7 @@
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.OrderClient;
import com.ruoyi.order.feignClient.RemoteOrderGoodsClient;
import com.ruoyi.order.vo.Price;
import com.ruoyi.other.api.domain.*;
@@ -27,6 +29,7 @@
import com.ruoyi.other.mapper.GoodsShopMapper;
import com.ruoyi.other.mapper.ShopMapper;
import com.ruoyi.other.service.*;
import com.ruoyi.other.util.GeodesyUtil;
import com.ruoyi.other.vo.GoodsVO;
import com.ruoyi.system.api.model.LoginUser;
import org.jetbrains.annotations.NotNull;
@@ -82,16 +85,21 @@
    private GoodsBargainPriceService goodsBargainPriceService;
    @Resource
    private GoodsBargainPriceDetailService goodsBargainPriceDetailService;
    @Resource
    private OrderClient orderClient;
    @Resource
    private GoodsEvaluateService goodsEvaluateService;
    @Override
    public PageInfo<GoodsVO> goodsList(Goods search) {
        PageInfo<GoodsVO> pageInfo = new PageInfo(search.getPageCurr(), search.getPageSize());
        Integer vipId = 0;
        String provinceCode = null;
        String cityCode = null;
        String districtCode = null;
        String provinceCode = "0";
        String cityCode = "0";
        String districtCode = "0";
        String token = SecurityUtils.getToken(ServletUtils.getRequest());
        if(StringUtils.isNotEmpty(token)){
            Long userid = tokenService.getLoginUserApplet().getUserid();
@@ -101,19 +109,95 @@
            cityCode = appUser.getCityCode();
            districtCode = appUser.getDistrictCode();
        }
        List<GoodsVO> list = this.baseMapper.goodsList(pageInfo, search.getGoodsCategoryId(), search.getName(), vipId);
        List<GoodsVO> list = this.baseMapper.goodsList(search.getGoodsCategoryId(), search.getName(), vipId);
        for (GoodsVO goods : list) {
            Price price = getPrice(vipId, goods.getGoodsId(), 1, null, provinceCode, cityCode, districtCode);
            Price price = getPrice(vipId, goods.getGoodsId(), null, 1, provinceCode, cityCode, districtCode);
            if(null != price){
                goods.setCashPayment(price.getCashPayment() ? 1 : 0);
                goods.setPointPayment(price.getPointPayment() ? 1 : 0);
                goods.setSellingPrice(price.getCash());
                goods.setIntegral(price.getPoint());
            }
            Integer data = orderClient.getGoodsSaleNum(goods.getGoodsId(), 1).getData();
            goods.setSaleNum(data);
        }
        return pageInfo.setRecords(list);
        //手动排序
        if(StringUtils.isNotEmpty(search.getOrderByColumn())){
            if("selling_price".equals(search.getOrderByColumn())){
                list.sort(new Comparator<GoodsVO>() {
                    @Override
                    public int compare(GoodsVO o1, GoodsVO o2) {
                        if(null != o1.getSellingPrice() && null != o2.getSellingPrice()){
                            return o1.getSellingPrice().compareTo(o2.getSellingPrice()) * ("desc".equals(search.getIsAsc()) ? -1 : 1);
                        }
                        if(null == o1.getSellingPrice() && null != o2.getSellingPrice()){
                            return BigDecimal.ZERO.compareTo(o2.getSellingPrice()) * ("desc".equals(search.getIsAsc()) ? -1 : 1);
                        }
                        if(null != o1.getSellingPrice() && null == o2.getSellingPrice()){
                            return o1.getSellingPrice().compareTo(BigDecimal.ZERO) * ("desc".equals(search.getIsAsc()) ? -1 : 1);
                        }
                        return 0;
                    }
                });
            }
            if("integral".equals(search.getOrderByColumn())){
                list.sort(new Comparator<GoodsVO>() {
                    @Override
                    public int compare(GoodsVO o1, GoodsVO o2) {
                        if(null != o1.getIntegral() && null != o2.getIntegral()){
                            return o1.getIntegral().compareTo(o2.getIntegral()) * ("desc".equals(search.getIsAsc()) ? -1 : 1);
                        }
                        if(null == o1.getIntegral() && null != o2.getIntegral()){
                            return Integer.valueOf(0).compareTo(o2.getIntegral()) * ("desc".equals(search.getIsAsc()) ? -1 : 1);
                        }
                        if(null != o1.getIntegral() && null == o2.getIntegral()){
                            return o1.getIntegral().compareTo(Integer.valueOf(0)) * ("desc".equals(search.getIsAsc()) ? -1 : 1);
                        }
                        return 0;
                    }
                });
            }
            if("sale_num".equals(search.getOrderByColumn())){
                list.sort(new Comparator<GoodsVO>() {
                    @Override
                    public int compare(GoodsVO o1, GoodsVO o2) {
                        return o1.getSaleNum().compareTo(o2.getSaleNum()) * ("desc".equals(search.getIsAsc()) ? -1 : 1);
                    }
                });
            }
        }
        // 手动处理分页
        PageInfo<GoodsVO> pageInfo = new PageInfo<>();
        pageInfo.setSize(search.getPageSize());
        pageInfo.setTotal(list.size());
        int pageSize = search.getPageSize();
        int pageCurr = search.getPageCurr();
        // 计算起始索引
        int pageNum = (pageCurr - 1) * pageSize;
        // 检查 pageNum 是否合法
        if (pageNum < 0 || pageNum >= list.size()) {
            // 如果 pageNum 不合法,返回空列表
            pageInfo.setRecords(new ArrayList<>());
            return pageInfo;
        }
        // 计算结束索引
        int endIndex = Math.min(pageNum + pageSize, list.size());
        // 获取分页数据
        List<GoodsVO> pageList = list.subList(pageNum, endIndex);
        // 设置分页结果
        pageInfo.setRecords(pageList);
        return pageInfo;
    }
    @Override
    public GoodsVO goodsDetail(Long goodsId) {
    public GoodsVO goodsDetail(Long goodsId, Integer shopId, String longitude, String latitude) {
        if (goodsId == null || goodsId <= 0) {
            throw new NullPointerException("商品ID不能为空");
        }
@@ -133,12 +217,23 @@
        }
        Goods goods = this.getById(goodsId);
        if(null == goods || goods.getDelFlag() == 1){
            throw new RuntimeException("商品不存在");
        }
        if(goods.getStatus() == 1){
            throw new RuntimeException("商品已被下架");
        }
        if(!goods.getCommodityAuthority().contains("-1") && !goods.getCommodityAuthority().contains(vipId.toString())){
            throw new RuntimeException("权限不足");
        }
        GoodsVO goodsVO = new GoodsVO();
        BeanUtils.copyBeanProp(goodsVO, goods);
        goodsVO.setGoodsId(goods.getId());
        goodsVO.setGoodsName(goods.getName());
        Price price = getPrice(vipId, goods.getId(), 1, null, provinceCode, cityCode, districtCode);
        Price price = getPrice(vipId, goods.getId(), shopId, 1, provinceCode, cityCode, districtCode);
        if(null != price){
            goodsVO.setPointPayment(price.getPointPayment() ? 1 : 0);
            goodsVO.setCashPayment(price.getCashPayment() ? 1 : 0);
            goodsVO.setSellingPrice(price.getCash());
            goodsVO.setIntegral(price.getPoint());
        }
@@ -163,6 +258,34 @@
                    .in(Shop::getDelFlag, 0));
            goodsVO.setShopList(shopList);
        }
        if(StringUtils.isNotEmpty(longitude) && StringUtils.isNotEmpty(latitude)){
            List<Shop> shopList = goodsVO.getShopList();
            for (Shop shop : shopList) {
                Double wgs84 = GeodesyUtil.getDistance(longitude + "," + latitude, shop.getLongitude() + "," + shop.getLatitude()).get("WGS84");
                shop.setDistance(wgs84);
            }
            shopList.sort(new Comparator<Shop>() {
                @Override
                public int compare(Shop o1, Shop o2) {
                    return o1.getDistance().compareTo(o2.getDistance());
                }
            });
            goodsVO.setShopList(shopList);
        }
        Integer integer = orderClient.getGoodsSaleNum(goods.getId(), 1).getData();
        goodsVO.setSaleNum(integer);
        GoodsEvaluate goodsEvaluate = goodsEvaluateService.lambdaQuery().eq(GoodsEvaluate::getGoodsId, goodsId).ne(GoodsEvaluate::getComment, "")
                .orderByDesc(GoodsEvaluate::getGrade).eq(GoodsEvaluate::getStatus, 2).isNotNull(GoodsEvaluate::getComment)
                .eq(GoodsEvaluate::getDelFlag, 0).last(" limit 0, 1").one();
        if (null != goodsEvaluate) {
            AppUser appUserById = appUserClient.getAppUserById(goodsEvaluate.getAppUserId());
            if(null != appUserById){
                goodsEvaluate.setUserName(appUserById.getName());
                goodsEvaluate.setAvatar(appUserById.getAvatar());
                goodsEvaluate.setIdStr(String.valueOf(goodsEvaluate.getId()));
            }
        }
        goodsVO.setGoodsEvaluate(goodsEvaluate);
        return goodsVO;
    }
@@ -170,9 +293,9 @@
    @Override
    public List<Goods> getGoodsListByShopId(PageInfo<Goods> pageInfo, Integer shopId) {
        Integer vipId = 0;
        String provinceCode = null;
        String cityCode = null;
        String districtCode = null;
        String provinceCode = "0";
        String cityCode = "0";
        String districtCode = "0";
        String token = SecurityUtils.getToken(ServletUtils.getRequest());
        if(StringUtils.isNotEmpty(token)){
            Long userid = tokenService.getLoginUserApplet().getUserid();
@@ -184,18 +307,27 @@
        }
        List<Goods> goods = goodsMapper.selectListByShopId(pageInfo, shopId, vipId);
        for (Goods good : goods) {
            Price price = getPrice(vipId, good.getId(), 1, shopId, provinceCode, cityCode, districtCode);
            Price price = getPrice(vipId, good.getId(), shopId, 1, provinceCode, cityCode, districtCode);
            if(null != price){
                good.setCashPayment(price.getCashPayment() ? 1 : 0);
                good.setPointPayment(price.getPointPayment() ? 1 : 0);
                good.setSellingPrice(price.getCash());
                good.setIntegral(price.getPoint());
            }
            Integer data = orderClient.getGoodsSaleNum(good.getId(), 1).getData();
            good.setSaleNum(data);
        }
        return goods;
    }
    @Override
    public IPage<Goods> getManageGoodsList(Page<Goods> page, Goods goods) {
        return goodsMapper.selectManageGoodsList(page, goods);
        IPage<Goods> goodsIPage = goodsMapper.selectManageGoodsList(page, goods);
        goodsIPage.getRecords().forEach(goods1 -> {
            Integer data = orderClient.getGoodsSaleNum(goods1.getId(), 1).getData();
            goods1.setSaleNum(data);
        });
        return goodsIPage;
    }
    @Override
@@ -235,7 +367,10 @@
    @Override
    public void updateManageGoods(Goods goods) {
        goodsMapper.updateById(goods);
        this.updateById(goods);
        //修改个别字段
        this.update(new LambdaUpdateWrapper<Goods>().eq(Goods::getId, goods.getId()).set(Goods::getSellingPrice, goods.getSellingPrice())
                .set(Goods::getIntegral, goods.getIntegral()));
        // 指定门店
        List<GoodsShop> goodsShopList = goods.getGoodsShopList();
        saveGoodsShopList(goodsShopList, goods.getId());
@@ -253,12 +388,17 @@
        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();
                List<GoodsArea> goodsAreaList2 = new ArrayList<>();
                for (GoodsArea area : goodsAreaList1) {
                    area.setId(null);
                    area.setGoodsId(id);
@@ -268,8 +408,12 @@
                    area.setProvinceCode(goodsArea.getProvinceCode());
                    area.setCityCode(goodsArea.getCityCode());
                    area.setDistrictsCode(goodsArea.getDistrictsCode());
//                    if(null != area.getSellingPrice() || null != area.getIntegral()){
//                        goodsAreaList2.add(area);
//                    }
                    goodsAreaList2.add(area);
                }
                goodsAreaService.saveBatch(goodsAreaList1);
                goodsAreaService.saveBatch(goodsAreaList2);
            }
        }
    }
@@ -289,7 +433,7 @@
                .eq(GoodsAppUser::getGoodsId, id));
        if (!CollectionUtils.isEmpty(goodsAppUserList)){
            for (GoodsAppUser goodsAppUser : goodsAppUserList) {
                goodsAppUser.setId(null );
                goodsAppUser.setId(null);
                goodsAppUser.setGoodsId(id);
            }
        }
@@ -336,6 +480,7 @@
        List<AppUser> appUsers = appUserClient.listByIds(userIds);
        for (GoodsAppUser goodsAppUser : goodsAppUserList) {
            appUsers.stream().filter(u -> u.getId().equals(goodsAppUser.getAppUserId())).findFirst().ifPresent( u -> {
                goodsAppUser.setAppUserIdStr(goodsAppUser.getAppUserId().toString());
                goodsAppUser.setUserName(u.getName());
                goodsAppUser.setPhone(u.getPhone());
                VipSetting vipSetting = vipSettingService.getById(u.getVipId());
@@ -442,20 +587,37 @@
            if(null == bargainPriceDetail){
                //没有门店特价,判断地区价格配置
                LambdaQueryWrapper<GoodsArea> queryWrapper = new LambdaQueryWrapper<GoodsArea>().eq(GoodsArea::getGoodsId, goodsId).eq(GoodsArea::getVip, vip);
                if(StringUtils.isNotEmpty(districtCode)){
                    queryWrapper.eq(GoodsArea::getDistrictsCode, districtCode);
                if(StringUtils.isNotEmpty(provinceCode)){
                    queryWrapper.eq(GoodsArea::getProvinceCode, provinceCode);
                }
                if(StringUtils.isNotEmpty(cityCode)){
                    queryWrapper.eq(GoodsArea::getCityCode, cityCode);
                }
                if(StringUtils.isNotEmpty(provinceCode)){
                    queryWrapper.eq(GoodsArea::getProvinceCode, provinceCode);
                if(StringUtils.isNotEmpty(districtCode)){
                    queryWrapper.eq(GoodsArea::getDistrictsCode, districtCode);
                }
                GoodsArea goodsArea = goodsAreaService.getOne(queryWrapper);
                if(null == goodsArea){
                if(goodsArea == null){
                    queryWrapper = new LambdaQueryWrapper<GoodsArea>().eq(GoodsArea::getGoodsId, goodsId).eq(GoodsArea::getVip, vip);
                    if(StringUtils.isNotEmpty(provinceCode)){
                        queryWrapper.eq(GoodsArea::getProvinceCode, provinceCode);
                    }
                    if(StringUtils.isNotEmpty(cityCode)){
                        queryWrapper.eq(GoodsArea::getCityCode, cityCode);
                    }
                    goodsArea = goodsAreaService.getOne(queryWrapper);
                }
                if(goodsArea == null){
                    queryWrapper = new LambdaQueryWrapper<GoodsArea>().eq(GoodsArea::getGoodsId, goodsId).eq(GoodsArea::getVip, vip);
                    if(StringUtils.isNotEmpty(provinceCode)){
                        queryWrapper.eq(GoodsArea::getProvinceCode, provinceCode);
                    }
                    goodsArea = goodsAreaService.getOne(queryWrapper);
                }
                if(null == goodsArea || (null == goodsArea.getCashPayment() && null == goodsArea.getPointPayment())){
                    //没有地区价格,则使用会员价格
                    GoodsVip goodsVip = goodsVipService.getOne(new LambdaQueryWrapper<GoodsVip>().eq(GoodsVip::getGoodsId, goodsId).eq(GoodsVip::getVip, vip));
                    if(null == goodsVip){
                    if(null == goodsVip || (null == goodsVip.getCashPayment() && null == goodsVip.getPointPayment())){
                        //没有配置价格,直接使用原始基础价格
                        return null;
                    }else{
@@ -511,4 +673,5 @@
        }
        return price;
    }
}