huliguo
2025-04-21 17abf0608f62cdd318dba3e7b12a32ea486cb482
ruoyi-service/ruoyi-other/src/main/java/com/ruoyi/other/controller/GoodsController.java
@@ -6,16 +6,23 @@
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.annotation.DistributedLock;
import com.ruoyi.other.api.domain.Goods;
import com.ruoyi.other.dto.AddGoodsDTO;
import com.ruoyi.other.enums.GoodsStatus;
import com.ruoyi.other.service.GoodsService;
import com.ruoyi.other.vo.GoodsVO;
import com.ruoyi.system.api.domain.SysConfig;
import com.ruoyi.system.api.feignClient.SysConfigClient;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
/**
@@ -32,6 +39,12 @@
public class GoodsController extends BaseController {
    @Resource
    private GoodsService goodsService;
    @Resource
    private SysConfigClient sysConfigClient;
    /**
     * 添加商品
@@ -39,11 +52,50 @@
    @PostMapping("/addGoods")
    @ApiOperation(value = "发布商品", tags = {"管理后台-商品管理"})
    public R<Void> addGoods(@RequestBody Goods goods) {
        if(goods.getPurchaseLimit()==null){
            goods.setPurchaseLimit(-1);
        }
        goods.setSaleNum(0);
        goods.setStatus(GoodsStatus.DOWN.getCode());
        goods.setIntegral(getPoint(goods.getSellingPrice()));
        goods.setDelFlag(0);
        goods.setCreateTime(LocalDateTime.now());
        goodsService.addGoods(goods);
        return R.ok();
    }
    /**
     * 获取现金对应积分
     */
    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);
        }
    }
    /**
@@ -88,17 +140,12 @@
        if(goods.getPurchaseLimit()==null){
            goods.setPurchaseLimit(-1);
        }
        goods.setSaleNum(0);
        goods.setStatus(GoodsStatus.DOWN.getCode());
        goods.setIntegral(getPoint(goods.getSellingPrice()));
        goods.setDelFlag(0);
        goods.setCreateTime(LocalDateTime.now());
        goodsService.updateManageGoods(goods);
        if (goods.getType()==2){
            goods.setAppointStore(2);
            goodsService.updateById(goods);
        }
        if (goods.getType()==1){
            LambdaUpdateWrapper<Goods> goodsLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            goodsLambdaUpdateWrapper.set(Goods::getDistributionMode,null);
            goodsLambdaUpdateWrapper.eq(Goods::getId, goods.getId());
            goodsService.update(goodsLambdaUpdateWrapper);
        }
        return R.ok();
    }
@@ -180,14 +227,13 @@
    /**
     * 根据类型(1=服务商品,2=单品商品)获取商品数据
     * @param type
     * 获取商品数据
     * @return
     */
    @ResponseBody
    @PostMapping("/getGoodsByType")
    public R<List<Goods>> getGoodsByType(@RequestParam("type") Integer type){
        List<Goods> list = goodsService.list(new LambdaQueryWrapper<Goods>().eq(Goods::getType, type).eq(Goods::getDelFlag, 0).eq(Goods::getStatus, 2));
    public R<List<Goods>> getGoodsByType(){
        List<Goods> list = goodsService.list(new LambdaQueryWrapper<Goods>().eq(Goods::getDelFlag, 0).eq(Goods::getStatus, 2));
        return R.ok(list);
    }