huliguo
2 小时以前 6acf6357094588946b5528f1ef1ed84a0f1037fd
ruoyi-service/ruoyi-other/src/main/java/com/ruoyi/other/service/impl/GoodsServiceImpl.java
@@ -1,15 +1,12 @@
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;
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;
@@ -21,26 +18,31 @@
import com.ruoyi.order.feignClient.RemoteOrderGoodsClient;
import com.ruoyi.order.vo.Price;
import com.ruoyi.other.api.domain.*;
import com.ruoyi.other.api.vo.GetGoodsBargainPrice;
import com.ruoyi.other.api.vo.GetSeckillActivityInfo;
import com.ruoyi.other.dto.AddGoodsDTO;
import com.ruoyi.other.enums.GoodsStatus;
import com.ruoyi.other.mapper.GoodsEvaluateMapper;
import com.ruoyi.other.mapper.GoodsMapper;
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.GoodsEvaluateVO;
import com.ruoyi.other.vo.GoodsVO;
import com.ruoyi.other.vo.NearbyShopVO;
import com.ruoyi.system.api.domain.SysConfig;
import com.ruoyi.system.api.feignClient.SysConfigClient;
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;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
 * <p>
@@ -72,41 +74,54 @@
    private GoodsSeckillService goodsSeckillService;
    @Resource
    private OrderClient orderClient;
    @Resource
    private SysConfigClient  sysConfigClient;
    @Resource
    private ShopService shopService;
    @Resource
    private GoodsEvaluateMapper goodsEvaluateMapper;
    /**
     * 热门商品列表
     * @param search
     * @return
     */
    @Override
    public PageInfo<GoodsVO> goodsList(Goods search) {
        Integer vipId = 0;
        String provinceCode = "0";
        String cityCode = "0";
        String districtCode = "0";
        String token = SecurityUtils.getToken(ServletUtils.getRequest());
        if(StringUtils.isNotEmpty(token)){
            Long userid = tokenService.getLoginUserApplet().getUserid();
            AppUser appUser = appUserClient.getAppUserById(userid);
            vipId = appUser.getVipId();
            provinceCode = appUser.getProvinceCode();
            cityCode = appUser.getCityCode();
            districtCode = appUser.getDistrictCode();
        List<Long> shopIds=null;
        if (null!=search.getLongitude()&&null!=search.getLatitude()){
            //获取最近十家店的id
            List<NearbyShopVO> nearbyShopVOS = shopService.nearbyShopList(search.getLongitude(), search.getLatitude(), new Shop());
            int endIndex = Math.min(10, nearbyShopVOS.size());
            shopIds = nearbyShopVOS.subList(0, endIndex).stream().map(NearbyShopVO::getId).collect(Collectors.toList());
        }
        List<GoodsVO> list = this.baseMapper.goodsList(search.getGoodsCategoryId(), search.getName(), vipId);
        //查找满足条件的商品  分类、名称、附近十家店
        List<GoodsVO> list = this.baseMapper.goodsList(search.getGoodsCategoryId(), search.getName(),shopIds);
        //去重
        Map<Integer, GoodsVO> uniqueGoodsMap = new LinkedHashMap<>();
        for (GoodsVO goods : list) {
            Price price = getPrice(vipId, goods.getGoodsId(), null, 1, provinceCode, cityCode, districtCode);
            uniqueGoodsMap.putIfAbsent(goods.getGoodsId(), goods);
        }
        list = new ArrayList<>(uniqueGoodsMap.values());
        for (GoodsVO goods : list) {
            //计算所需价格和积分
            Price price = getPrice( goods.getGoodsId());
            if(null != price){
                goods.setCashPayment(price.getCashPayment() ? 1 : 0);
                goods.setPointPayment(price.getPointPayment() ? 1 : 0);
                //秒杀活动
                goods.setSellingPrice(price.getCash());
                goods.setIntegral(price.getPoint());
                goods.setStartTime(price.getStartTime());
                goods.setEndTime(price.getEndTime());
                goods.setPurchaseLimit(price.getPurchaseLimit());
            }
            Integer data = orderClient.getGoodsSaleNum(goods.getGoodsId(), 1).getData();
            goods.setSaleNum(data);
        }
        //手动排序
        if(StringUtils.isNotEmpty(search.getOrderByColumn())){
            if("tgs.selling_price".equals(search.getOrderByColumn())){
            if("tgs.selling_price".equals(search.getOrderByColumn())){//价格排序
                list.sort(new Comparator<GoodsVO>() {
                    @Override
                    public int compare(GoodsVO o1, GoodsVO o2) {
@@ -123,7 +138,7 @@
                    }
                });
            }
            if("integral".equals(search.getOrderByColumn())){
            if("integral".equals(search.getOrderByColumn())){//积分排序
                list.sort(new Comparator<GoodsVO>() {
                    @Override
                    public int compare(GoodsVO o1, GoodsVO o2) {
@@ -140,7 +155,7 @@
                    }
                });
            }
            if("sale_num".equals(search.getOrderByColumn())){
            if("sale_num".equals(search.getOrderByColumn())){//销量
                list.sort(new Comparator<GoodsVO>() {
                    @Override
                    public int compare(GoodsVO o1, GoodsVO o2) {
@@ -179,143 +194,114 @@
    }
    @Override
    public GoodsVO goodsDetail(Long goodsId, Integer shopId, String longitude, String latitude) {
    public GoodsVO goodsDetail(Long goodsId) {
        if (goodsId == null || goodsId <= 0) {
            throw new NullPointerException("商品ID不能为空");
        }
        Integer vipId = 0;
        String provinceCode = null;
        String cityCode = null;
        String districtCode = null;
        String token = SecurityUtils.getToken(ServletUtils.getRequest());
        if(StringUtils.isNotEmpty(token)){
            Long userid = tokenService.getLoginUserApplet().getUserid();
            AppUser appUser = appUserClient.getAppUserById(userid);
            vipId = appUser.getVipId();
            provinceCode = appUser.getProvinceCode();
            cityCode = appUser.getCityCode();
            districtCode = appUser.getDistrictCode();
        }
        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(), shopId, 1, provinceCode, cityCode, districtCode);
        //计算所需价格和积分
        Price price = getPrice( goods.getId());
        if(null != price){
            goodsVO.setPointPayment(price.getPointPayment() ? 1 : 0);
            goodsVO.setCashPayment(price.getCashPayment() ? 1 : 0);
            //秒杀活动
            goodsVO.setSellingPrice(price.getCash());
            goodsVO.setIntegral(price.getPoint());
            goodsVO.setStartTime(price.getStartTime());
            goodsVO.setEndTime(price.getEndTime());
            goodsVO.setPurchaseLimit(price.getPurchaseLimit());
        }
        //已售数量
        Integer data = orderClient.getGoodsSaleNum(goods.getId(), 1).getData();
        goodsVO.setSaleNum(data);
        //一个商品对应一个门店
        //查找门店
        GoodsShop goodsShop = goodsShopMapper.selectOne(new LambdaQueryWrapper<GoodsShop>()
                .eq(GoodsShop::getGoodsId, goodsId));
        Shop shop1 = shopMapper.selectById(goodsShop.getShopId());
        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::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);
        }
        goodsVO.setShop(shop1);
        //已售数量
        Integer integer = orderClient.getGoodsSaleNum(goods.getId(), 1).getData();
        goodsVO.setSaleNum(integer);
        //商品评价
        GoodsEvaluate goodsEvaluate=goodsEvaluateMapper.getGoodsEvaluateOne(goods.getId());
        if (null != goodsEvaluate){
            AppUser appUserById = appUserClient.getAppUserById(goodsEvaluate.getAppUserId());
            goodsEvaluate.setAvatar(appUserById.getAvatar());
            goodsEvaluate.setUserName(appUserById.getName());
            goodsVO.setGoodsEvaluate(goodsEvaluate);
        }
        return goodsVO;
    }
    @Override
    public List<Goods> getGoodsListByShopId(PageInfo<Goods> pageInfo, Integer shopId) {
        Integer vipId = 0;
        String provinceCode = "0";
        String cityCode = "0";
        String districtCode = "0";
        String token = SecurityUtils.getToken(ServletUtils.getRequest());
        if(StringUtils.isNotEmpty(token)){
            Long userid = tokenService.getLoginUserApplet().getUserid();
            AppUser appUser = appUserClient.getAppUserById(userid);
            vipId = appUser.getVipId();
            provinceCode = appUser.getProvinceCode();
            cityCode = appUser.getCityCode();
            districtCode = appUser.getDistrictCode();
    public List<GoodsVO> getGoodsListByShopId(PageInfo<GoodsVO> pageInfo, Integer shopId) {
        //查询该门店商品
        List<GoodsVO> goods = goodsMapper.selectListByShopId(pageInfo, shopId);
        if (goods == null) {
            return null;
        }
        List<Goods> goods = goodsMapper.selectListByShopId(pageInfo, shopId, vipId);
        for (Goods good : goods) {
            Price price = getPrice(vipId, good.getId(), shopId, 1, provinceCode, cityCode, districtCode);
        for (GoodsVO good : goods) {
            //价格
            Price price = getPrice( good.getGoodsId());
            if(null != price){
                good.setCashPayment(price.getCashPayment() ? 1 : 0);
                good.setPointPayment(price.getPointPayment() ? 1 : 0);
                //秒杀活动
                good.setSellingPrice(price.getCash());
                good.setIntegral(price.getPoint());
                good.setPurchaseLimit(price.getPurchaseLimit());
                good.setStartTime(price.getStartTime());
                good.setEndTime(price.getEndTime());
            }
            Integer data = orderClient.getGoodsSaleNum(good.getId(), 1).getData();
            Integer data = orderClient.getGoodsSaleNum(good.getGoodsId(), 1).getData();
            good.setSaleNum(data);
        }
        return goods;
    }
    @Override
    public IPage<Goods> getManageGoodsList(Page<Goods> page, Goods 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;
    public R getManageGoodsList(Integer pageNum,Integer pageSize,  Goods goods) {
        PageInfo<Goods> pageInfo = new PageInfo<>(pageNum,pageSize);
        List<Goods> list = goodsMapper.selectManageGoodsList(pageInfo, goods);
        pageInfo.setRecords(list);
        return R.ok(pageInfo);
    }
    @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 (null != appointStore && appointStore == 1){
            List<GoodsShop> goodsShopList = goods.getGoodsShopList();
            if (CollectionUtils.isEmpty(goodsShopList)){
                throw new NullPointerException("请选择指定门店");
            }
            saveGoodsShopList(goodsShopList, goods.getId());
        }
        //保存商品门店关系
        GoodsShop goodsShop = new GoodsShop();
        goodsShop.setGoodsId(goods.getId());
        goodsShop.setShopId(goods.getShopId());
        Shop shop = shopMapper.selectById(goods.getShopId());
        goodsShop.setShopName(shop.getName());
        goodsShop.setOwnerName(shop.getShopManager());
        goodsShop.setPhone(shop.getPhone());
        goodsShop.setAddress(shop.getAddress());
        goodsShopService.save(goodsShop);
        //判断是否参加秒杀活动
    }
    @Override
@@ -324,9 +310,8 @@
        //修改个别字段
        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());
        // 保存门店
        saveGoodsShop(goods.getShopId(),goods.getId());
    }
@@ -336,16 +321,16 @@
    private void saveGoodsShopList(List<GoodsShop> goodsShopList, Integer goodsId) {
    private void saveGoodsShop(Integer shopId, 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);
        GoodsShop goodsShop = new GoodsShop();
        goodsShop.setGoodsId(goodsId);
        goodsShop.setShopId(shopId);
        goodsShop.setId(null);
        goodsShopService.save(goodsShop);
    }
    @Override
@@ -354,65 +339,84 @@
        if (goods == null){
            return null;
        }
        // 指定门店
        List<GoodsShop> goodsShops = goodsShopService.list(new LambdaQueryWrapper<GoodsShop>()
        // 获取指定门店
        GoodsShop goodsShop = goodsShopService.getOne(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.setShopId(goodsShop.getShopId());
        Shop shop = shopService.getById(goodsShop.getShopId());
        if(null != shop){
            goodsShop.setShopName(shop.getName());
            goodsShop.setPhone(shop.getPhone());
            goodsShop.setAddress(shop.getAddress());
            goodsShop.setOwnerName(shop.getShopManager());
        }
        goods.setGoodsShopList(goodsShops);
        goods.setGoodsShop(goodsShop);
        return goods;
    }
    /**
     * 根据商品的价格配置体系获取商品当前的价格
     * @param vip
     * @param goodsId
     * @param type          1普通商品,2秒杀商品
     * @param shopId
     * @param provinceCode
     * @param cityCode
     * @param districtCode
     * @return
     * 获取商品当前的价格,就是判断是否在秒杀活动中
     */
    public Price getPrice(Integer vip, Integer goodsId, Integer shopId, Integer type, String provinceCode, String cityCode, String districtCode){
        //获取支付价格
        //秒杀活动>门店特价>地区价格>会员价格
        //判断是否有秒杀活动
    public Price getPrice( Integer goodsId ){
        //判断是否有在秒杀活动时间中
        Price price = new Price();
        SeckillActivityInfo one = seckillActivityInfoService.getOne(new LambdaQueryWrapper<SeckillActivityInfo>().eq(SeckillActivityInfo::getGoodId, goodsId)
                .eq(SeckillActivityInfo::getIsShelves, 1).eq(SeckillActivityInfo::getDelFlag, 0)
                .last(" and now() between start_time and end_time and FIND_IN_SET(" + vip + ", vip_ids) order by create_time desc limit 0, 1"));
        GoodsSeckill goodsSeckill = null;
                .last(" and now() between start_time and end_time order by create_time desc limit 0, 1"));
        GoodsSeckill goodsSeckill = null;//秒杀配置
        if(null != one){
            goodsSeckill = goodsSeckillService.getOne(new LambdaQueryWrapper<GoodsSeckill>().eq(GoodsSeckill::getSeckillActivityInfoId, one.getId()).eq(GoodsSeckill::getVip, vip));
            //有秒杀活动,查看秒杀价格
            goodsSeckill = goodsSeckillService.getOne(new LambdaQueryWrapper<GoodsSeckill>().eq(GoodsSeckill::getSeckillActivityInfoId, one.getId()));
        }
        //没有秒杀活动或者添加的普通商品则不使用秒杀活动价格
        if(null != goodsSeckill && type == 1){
            //构建价格数据
            if(goodsSeckill.getCashPayment() == 1 && goodsSeckill.getPointPayment() == 1){
                price.setCash(goodsSeckill.getSellingPrice());
                price.setPoint(goodsSeckill.getIntegral());
            }
            if(goodsSeckill.getCashPayment() == 1 && goodsSeckill.getPointPayment() == 0){
                price.setCash(goodsSeckill.getSellingPrice());
            }
            if(goodsSeckill.getCashPayment() == 0 && goodsSeckill.getPointPayment() == 1){
                price.setPoint(goodsSeckill.getIntegral());
            }
            price.setCashPayment(goodsSeckill.getCashPayment() == 1);
            price.setPointPayment(goodsSeckill.getPointPayment() == 1);
        //没有秒杀活动或者添加的普通商品则使用秒杀活动价格
        if(null == goodsSeckill ){
           return null;
        }
        //秒杀活动价格
        price.setCash(goodsSeckill.getSellingPrice());
        //计算对应积分
        price.setPoint(getPoint(price.getCash()));
        price.setStartTime(one.getStartTime());
        price.setEndTime(one.getEndTime());
        price.setPurchaseLimit(one.getMaxNum());
        return price;
    }
    /**
     * 获取现金对应积分
     */
    public Integer getPoint(BigDecimal cash){
        if (cash == null || cash.compareTo(BigDecimal.ZERO) < 0) {
            throw new IllegalArgumentException("金额不能为null或负数");
        }
        // 获取积分兑换比例配置
        R<SysConfig> info = sysConfigClient.getInfo(6L);
        if (info == null || info.getData() == null) {
            throw new RuntimeException("获取积分兑换比例配置失败");
        }
        String configValue = info.getData().getConfigValue();
        if (StringUtils.isBlank(configValue)) {
            throw new RuntimeException("积分兑换比例配置值为空");
        }
        try {
            // 使用BigDecimal处理比例,避免精度问题
            BigDecimal ratio = new BigDecimal(configValue.trim());
            if (ratio.compareTo(BigDecimal.ZERO) <= 0) {
                throw new RuntimeException("积分兑换比例必须大于0");
            }
            // 计算积分并四舍五入取整
            return cash.multiply(ratio).intValue();
        } catch (NumberFormatException e) {
            throw new RuntimeException("积分兑换比例配置值格式错误", e);
        } catch (ArithmeticException e) {
            throw new RuntimeException("积分计算结果溢出", e);
        }
    }
}