From 1151fa405da847e0afe526b3ed28c4a1fb3c5a38 Mon Sep 17 00:00:00 2001
From: liujie <1793218484@qq.com>
Date: 星期一, 13 十月 2025 17:00:54 +0800
Subject: [PATCH] bug修改

---
 ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/TSysGoodsController.java |  169 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 167 insertions(+), 2 deletions(-)

diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/TSysGoodsController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/TSysGoodsController.java
index 01e6c0f..612a601 100644
--- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/TSysGoodsController.java
+++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/TSysGoodsController.java
@@ -1,8 +1,31 @@
 package com.ruoyi.web.controller.api;
 
 
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.ruoyi.common.basic.PageInfo;
+import com.ruoyi.common.core.domain.BaseModel;
+import com.ruoyi.common.core.domain.R;
+import com.ruoyi.common.core.domain.entity.SysUser;
+import com.ruoyi.framework.web.service.TokenService;
+import com.ruoyi.system.dto.TErpGoodsUpdateStatusDto;
+import com.ruoyi.system.dto.UpdateStatusProcurementDto;
+import com.ruoyi.system.model.*;
+import com.ruoyi.system.query.TSysGoodsQuery;
+import com.ruoyi.system.query.TSysOrderQuery;
+import com.ruoyi.system.service.*;
+import com.ruoyi.system.vo.TErpGoodsVO;
+import com.ruoyi.system.vo.TSysOrderPageVo;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.security.core.parameters.P;
+import org.springframework.web.bind.annotation.*;
+
+import javax.validation.Valid;
+import java.util.List;
+import java.util.stream.Collectors;
 
 /**
  * <p>
@@ -14,7 +37,149 @@
  */
 @RestController
 @RequestMapping("/t-sys-goods")
+@Api(tags = "积分商品管理")
 public class TSysGoodsController {
 
+    private final TSysGoodsService sysGoodsService;
+    private final TokenService tokenService;
+    private final TCrmSupplierService crmSupplierService;
+    private final TSysGoodsExchangeService sysGoodsExchangeService;
+
+
+    @Autowired
+    public TSysGoodsController(TokenService tokenService, TSysGoodsService sysGoodsService, TCrmSupplierService crmSupplierService, TSysGoodsExchangeService sysGoodsExchangeService) {
+        this.sysGoodsService = sysGoodsService;
+        this.tokenService = tokenService;
+        this.crmSupplierService = crmSupplierService;
+        this.sysGoodsExchangeService = sysGoodsExchangeService;
+
+    }
+
+    /**
+     * 获取积分商品列表
+     */
+    @ApiOperation(value = "获取积分商品列表")
+    @PostMapping(value = "/pageList")
+    public R<PageInfo<TSysGoods>> pageList(@RequestBody TSysGoodsQuery query) {
+
+        SysUser user = tokenService.getLoginUser().getUser();
+        Integer roleType = user.getRoleType();
+        PageInfo<TSysGoods> pageInfo = new PageInfo<>(query.getPageNum(), query.getPageSize());
+        if (roleType == 4) {
+            // 供应商
+            TCrmSupplier crmSupplier = crmSupplierService.getOne(Wrappers.lambdaQuery(TCrmSupplier.class)
+                    .eq(TCrmSupplier::getUserId, user.getUserId())
+                    .last("LIMIT 1"));
+            LambdaQueryWrapper<TSysGoods> tSysGoodsLambdaQueryWrapper = new LambdaQueryWrapper<>();
+            tSysGoodsLambdaQueryWrapper.eq(TSysGoods::getSupplierId, crmSupplier.getId());
+            if (query.getGoodsName() != null && !query.getGoodsName().isEmpty()) {
+                tSysGoodsLambdaQueryWrapper.like(TSysGoods::getGoodsName, query.getGoodsName());
+            }
+            if (query.getStatus() != null) {
+                tSysGoodsLambdaQueryWrapper.eq(TSysGoods::getStatus, query.getStatus());
+            }
+            tSysGoodsLambdaQueryWrapper.orderByDesc(BaseModel::getCreateTime);
+
+            Page<TSysGoods> page = sysGoodsService.page(pageInfo, tSysGoodsLambdaQueryWrapper);
+            List<TSysGoods> records = page.getRecords();
+            if (!records.isEmpty()) {
+                List<TSysGoodsExchange> list = sysGoodsExchangeService.list(new LambdaQueryWrapper<TSysGoodsExchange>().in(TSysGoodsExchange::getGoodsId, records.stream().map(TSysGoods::getId).collect(Collectors.toList())));
+                records.forEach(item -> {
+                    item.setExchangeQuantity(list.stream().filter(item1 -> item1.getGoodsId().equals(item.getId())).mapToInt(TSysGoodsExchange::getGoodsCount).sum());
+                    item.setSurplusQuantity(item.getGoodsTotal() - item.getExchangeQuantity());
+                });
+            }
+            pageInfo.setRecords(records);
+            return R.ok(pageInfo);
+        }
+        return R.ok(pageInfo);
+    }
+
+
+
+    @ApiOperation(value = "添加积分商品")
+    @PostMapping(value = "/add")
+    public R<String> add(@RequestBody TSysGoods goods) {
+        SysUser user = tokenService.getLoginUser().getUser();
+        Integer roleType = user.getRoleType();
+        if (roleType == 4) {
+            // 供应商
+            TCrmSupplier crmSupplier = crmSupplierService.getOne(Wrappers.lambdaQuery(TCrmSupplier.class)
+                    .eq(TCrmSupplier::getUserId, user.getUserId())
+                    .last("LIMIT 1"));
+            goods.setSupplierId(crmSupplier.getId());
+            sysGoodsService.save(goods);
+            return R.ok(goods.getId());
+        }
+        return R.ok();
+    }
+
+    @ApiOperation(value = "编辑积分商品")
+    @PostMapping(value = "/update")
+    public R<String> update(@RequestBody TSysGoods goods) {
+        SysUser user = tokenService.getLoginUser().getUser();
+        Integer roleType = user.getRoleType();
+
+        List<TSysGoodsExchange> list = sysGoodsExchangeService.list(new LambdaQueryWrapper<TSysGoodsExchange>().eq(TSysGoodsExchange::getGoodsId, goods.getId()));
+        int sum = list.stream().mapToInt(TSysGoodsExchange::getGoodsCount).sum();
+        if(goods.getGoodsTotal()< sum){
+            return R.fail("商品总数不能小于已兑换数量");
+        }
+        if (roleType == 4) {
+            // 供应商
+            TCrmSupplier crmSupplier = crmSupplierService.getOne(Wrappers.lambdaQuery(TCrmSupplier.class)
+                    .eq(TCrmSupplier::getUserId, user.getUserId())
+                    .last("LIMIT 1"));
+            goods.setSupplierId(crmSupplier.getId());
+            sysGoodsService.updateById(goods);
+            return R.ok(goods.getId());
+        }
+        return R.ok();
+    }
+
+
+
+    @ApiOperation(value = "删除积分商品")
+    @DeleteMapping(value = "/delete/{id}")
+    public R<String> delete(@PathVariable String id) {
+        SysUser user = tokenService.getLoginUser().getUser();
+        Integer roleType = user.getRoleType();
+        if (roleType == 4) {
+            // 供应商
+            TCrmSupplier crmSupplier = crmSupplierService.getOne(Wrappers.lambdaQuery(TCrmSupplier.class)
+                    .eq(TCrmSupplier::getUserId, user.getUserId())
+                    .last("LIMIT 1"));
+            TSysGoods goods = sysGoodsService.getById(id);
+            if(goods.getSupplierId().equals(crmSupplier.getId())){
+                sysGoodsService.removeById(id);
+            }else {
+                return R.fail("没有权限");
+            }
+        }
+        return R.ok();
+    }
+
+
+    @ApiOperation(value = "积分商品 上下架")
+    @PostMapping(value = "/updateStatus")
+    public R<String> updateStatus(@RequestBody @Valid TErpGoodsUpdateStatusDto dto) {
+        SysUser user = tokenService.getLoginUser().getUser();
+        Integer roleType = user.getRoleType();
+        if (roleType == 4) {
+            // 供应商
+            TCrmSupplier crmSupplier = crmSupplierService.getOne(Wrappers.lambdaQuery(TCrmSupplier.class)
+                    .eq(TCrmSupplier::getUserId, user.getUserId())
+                    .last("LIMIT 1"));
+            TSysGoods goods = sysGoodsService.getById(dto.getId());
+            if(goods.getSupplierId().equals(crmSupplier.getId())){
+                goods.setStatus(dto.getState());
+                sysGoodsService.updateById(goods);
+            }else {
+                return R.fail("没有权限");
+            }
+        }
+        return R.ok();
+    }
+
 }
 

--
Gitblit v1.7.1