shinnosuke
2026-09-30 60ebfde91e9c975f59df4d60a6cb5a7861958bd2
ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/TErpGoodsShopController.java
@@ -1,15 +1,13 @@
package com.ruoyi.web.controller.api;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.basic.PageInfo;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.dto.ShoppingCartItem;
import com.ruoyi.system.model.TCrmSupplier;
import com.ruoyi.system.model.TErpGoods;
import com.ruoyi.system.model.TErpGoodsType;
import com.ruoyi.system.model.TErpGoodsUnit;
import com.ruoyi.system.model.*;
import com.ruoyi.system.query.TErpGoodsShopQuery;
import com.ruoyi.system.service.*;
import com.ruoyi.system.vo.ShopCarVO;
@@ -18,13 +16,12 @@
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.*;
import java.util.stream.Collectors;
/**
@@ -52,6 +49,7 @@
    private final TCrmClinicService crmClinicService;
    private final TCrmSupplierService crmSupplierService;
    private final ISysUserService sysUserService;
    private final TErpProcurementGoodsService erpProcurementGoodsService;
    @Autowired
    private RedisCache redisCache;
@@ -60,7 +58,7 @@
    @Autowired
    public TErpGoodsShopController(TErpGoodsService erpGoodsService, TokenService tokenService, TErpGoodsTypeService erpGoodsTypeService, TErpGoodsUnitService erpGoodsUnitService,
                                   TErpSupplierWarehousingBatchService erpSupplierWarehousingBatchService, TErpSupplierWarehousingService erpSupplierWarehousingService,
                                   TErpSupplierOutboundGoodsService erpSupplierOutboundGoodsService, TErpMaintenanceReminderService erpMaintenanceReminderService, TCrmClinicService crmClinicService, TCrmSupplierService crmSupplierService, ISysUserService sysUserService) {
                                   TErpSupplierOutboundGoodsService erpSupplierOutboundGoodsService, TErpMaintenanceReminderService erpMaintenanceReminderService, TCrmClinicService crmClinicService, TCrmSupplierService crmSupplierService, ISysUserService sysUserService, TErpProcurementGoodsService erpProcurementGoodsService) {
        this.erpGoodsService = erpGoodsService;
        this.tokenService = tokenService;
        this.erpGoodsTypeService = erpGoodsTypeService;
@@ -72,6 +70,7 @@
        this.crmClinicService = crmClinicService;
        this.crmSupplierService = crmSupplierService;
        this.sysUserService = sysUserService;
        this.erpProcurementGoodsService = erpProcurementGoodsService;
    }
    /**
@@ -100,17 +99,47 @@
            TErpGoodsUnit goodsUnit = erpGoodsUnitService.getById(goods.getPackingUnitId());
            if (goodsUnit != null) res.setPackingUnitName(goodsUnit.getUnitName());
        }
        List<TErpProcurementGoods> tErpProcurementGoods = erpProcurementGoodsService.list(new LambdaQueryWrapper<TErpProcurementGoods>()
                        .in(TErpProcurementGoods::getGoodsId, goods.getId()));
        if(!CollectionUtils.isEmpty(tErpProcurementGoods)){
            // 计算采购数量
            int sum = tErpProcurementGoods.stream().filter(e -> e.getPurchaseCount() != null).mapToInt(TErpProcurementGoods::getPurchaseCount).sum();
            res.setSalesCount(sum);
        }else {
            res.setSalesCount(0);
        }
        return R.ok(res);
    }
    @ApiOperation(value = "海诊商城-清空购物车")
    @GetMapping(value = "/clear")
    public void clearAll() {
    public R clearAll() {
        Long userId = tokenService.getLoginUser().getUserId();
        redisCache.deleteObject("shopCart:"+userId);
        return R.ok();
    }
    @ApiOperation(value = "海诊商城-购买后删除购物车商品")
    @GetMapping(value = "/clearByIds")
    public R<String> clearByIds(String ids) {
        List<String> list = Arrays.asList(ids.split(","));
        Long userId = tokenService.getLoginUser().getUserId();
        List<ShoppingCartItem> cacheList = redisCache.getCacheList("shopCart:"+userId);
        List<ShoppingCartItem> res = new ArrayList<>();
        redisCache.deleteObject("shopCart:"+userId);
        for (ShoppingCartItem shoppingCartItem : cacheList) {
            if (!list.contains(shoppingCartItem.getId())){
                res.add(shoppingCartItem);
            }
        }
        if (!res.isEmpty()){
            redisCache.setCacheList("shopCart:"+userId, res);
        }
        return R.ok();
    }
    @ApiOperation(value = "海诊商城-单删购物车")
    @GetMapping(value = "/clearById")
    public void clear(String id) {
    public R clear(String id) {
        Long userId = tokenService.getLoginUser().getUserId();
        List<ShoppingCartItem> cacheList = redisCache.getCacheList("shopCart:"+userId);
        List<ShoppingCartItem> res = new ArrayList<>();
@@ -123,7 +152,7 @@
        if (!res.isEmpty()){
            redisCache.setCacheList("shopCart:"+userId, res);
        }
        return R.ok();
    }
    /**
     * 海诊商城-加入购物车
@@ -184,21 +213,21 @@
        if (goodsIds.isEmpty()){
            ShopCarVO shopCarVO = new ShopCarVO();
            shopCarVO.setTotalPrice(BigDecimal.ZERO);
            shopCarVO.setList(new PageInfo<>());
            shopCarVO.setList(new ArrayList<>());
            return R.ok(shopCarVO);
        }
        TErpGoodsShopQuery tErpGoodsShopQuery = new TErpGoodsShopQuery();
        tErpGoodsShopQuery.setGoodsIds(goodsIds);
        PageInfo<TErpGoodsVO> tErpGoodsVOPageInfo = erpGoodsService.pageShopList(tErpGoodsShopQuery);
        List<TErpGoodsVO> records = tErpGoodsVOPageInfo.getRecords();
        for (TErpGoodsVO record : records) {
            for (ShoppingCartItem cartItem : cacheList) {
                if (cartItem.getId().equals(record.getId())) {
                    record.setNumber(cartItem.getNum());
                    record.setPrice(record.getSalesAmount().multiply(new BigDecimal(record.getNumber())).setScale(2, RoundingMode.HALF_UP));
                }
            }
        }
//        PageInfo<TErpGoodsVO> tErpGoodsVOPageInfo = erpGoodsService.pageShopList(tErpGoodsShopQuery);
//        List<TErpGoodsVO> records = tErpGoodsVOPageInfo.getRecords();
//        for (TErpGoodsVO record : records) {
//            for (ShoppingCartItem cartItem : cacheList) {
//                if (cartItem.getId().equals(record.getId())) {
//                    record.setNumber(cartItem.getNum());
//                    record.setPrice(record.getSalesAmount().multiply(new BigDecimal(record.getNumber())).setScale(2, RoundingMode.HALF_UP));
//                }
//            }
//        }
        List<TErpGoodsVO> listAll = erpGoodsService.pageShopListAll(tErpGoodsShopQuery);
        for (TErpGoodsVO record : listAll) {
            for (ShoppingCartItem cartItem : cacheList) {
@@ -208,8 +237,8 @@
                }
            }
        }
        BigDecimal totalPrice = records.stream().map(TErpGoodsVO::getPrice).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
        res.setList(tErpGoodsVOPageInfo);
        BigDecimal totalPrice = listAll.stream().map(TErpGoodsVO::getPrice).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
        res.setList(listAll);
        res.setTotalPrice(totalPrice);
        return R.ok(res);
    }