package com.ruoyi.web.controller.api; 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.query.TErpGoodsShopQuery; import com.ruoyi.system.service.*; import com.ruoyi.system.vo.ShopCarVO; import com.ruoyi.system.vo.TErpGoodsVO; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.math.BigDecimal; import java.math.RoundingMode; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Objects; import java.util.stream.Collectors; /** *

* erp商品 前端控制器 *

* * @author xiaochen * @since 2025-08-20 */ @RestController @RequestMapping("/t-erp-goods") @Api(tags = "2.0新增海诊商城") public class TErpGoodsShopController { private final TErpGoodsService erpGoodsService; private final TokenService tokenService; private final TErpGoodsTypeService erpGoodsTypeService; private final TErpGoodsUnitService erpGoodsUnitService; private final TErpSupplierWarehousingBatchService erpSupplierWarehousingBatchService; private final TErpSupplierWarehousingService erpSupplierWarehousingService; private final TErpSupplierOutboundGoodsService erpSupplierOutboundGoodsService; private final TErpMaintenanceReminderService erpMaintenanceReminderService; private final TCrmClinicService crmClinicService; private final TCrmSupplierService crmSupplierService; private final ISysUserService sysUserService; @Autowired private RedisCache redisCache; @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) { this.erpGoodsService = erpGoodsService; this.tokenService = tokenService; this.erpGoodsTypeService = erpGoodsTypeService; this.erpGoodsUnitService = erpGoodsUnitService; this.erpSupplierWarehousingBatchService = erpSupplierWarehousingBatchService; this.erpSupplierWarehousingService = erpSupplierWarehousingService; this.erpSupplierOutboundGoodsService = erpSupplierOutboundGoodsService; this.erpMaintenanceReminderService = erpMaintenanceReminderService; this.crmClinicService = crmClinicService; this.crmSupplierService = crmSupplierService; this.sysUserService = sysUserService; } /** * 海诊商城分页列表 */ @ApiOperation(value = "海诊商城分页列表") @PostMapping(value = "/pageShopList") public R> pageShopList(@RequestBody TErpGoodsShopQuery query) { return R.ok(erpGoodsService.pageShopList(query)); } /** * 海诊商城-商品详情 */ @ApiOperation(value = "海诊商城-商品详情") @GetMapping(value = "/shopDetail") public R pageShopList(String id) { TErpGoods goods = erpGoodsService.getById(id); TErpGoodsVO res = new TErpGoodsVO(); BeanUtils.copyProperties(goods, res); TCrmSupplier supplier = crmSupplierService.getById(goods.getSupplierClinicId()); if (supplier != null) res.setSupplierName(supplier.getSupplierName()); TErpGoodsType erpGoodsType = erpGoodsTypeService.getById(goods.getTypeId()); if (erpGoodsType != null) res.setTypeName(erpGoodsType.getTypeName()); if (goods.getPackingUnitId() != null) { TErpGoodsUnit goodsUnit = erpGoodsUnitService.getById(goods.getPackingUnitId()); if (goodsUnit != null) res.setPackingUnitName(goodsUnit.getUnitName()); } return R.ok(res); } @ApiOperation(value = "海诊商城-清空购物车") @GetMapping(value = "/clear") public R clearAll() { Long userId = tokenService.getLoginUser().getUserId(); redisCache.deleteObject("shopCart:"+userId); return R.ok(); } @ApiOperation(value = "海诊商城-购买后删除购物车商品") @GetMapping(value = "/clearByIds") public void clearByIds(String ids) { List list = Arrays.asList(ids.split(",")); Long userId = tokenService.getLoginUser().getUserId(); List cacheList = redisCache.getCacheList("shopCart:"+userId); List 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); } } @ApiOperation(value = "海诊商城-单删购物车") @GetMapping(value = "/clearById") public R clear(String id) { Long userId = tokenService.getLoginUser().getUserId(); List cacheList = redisCache.getCacheList("shopCart:"+userId); List res = new ArrayList<>(); redisCache.deleteObject("shopCart:"+userId); for (ShoppingCartItem shoppingCartItem : cacheList) { if (!shoppingCartItem.getId().equals(id)){ res.add(shoppingCartItem); } } if (!res.isEmpty()){ redisCache.setCacheList("shopCart:"+userId, res); } return R.ok(); } /** * 海诊商城-加入购物车 */ @ApiOperation(value = "海诊商城-加入购物车") @GetMapping(value = "/addShopCar") public R addShopCar(String id, Integer num) { ShoppingCartItem shoppingCartItem = new ShoppingCartItem(); shoppingCartItem.setId(id); shoppingCartItem.setNum(num); Long userId = tokenService.getLoginUser().getUserId(); List cacheList = redisCache.getCacheList("shopCart:"+userId); if (cacheList==null){ cacheList = new ArrayList<>(); } redisCache.deleteObject("shopCart:"+userId); if (num==0){ // 移除 List res = new ArrayList<>(); for (ShoppingCartItem cartItem : cacheList) { if (!cartItem.getId().equals( id)){ res.add(cartItem); } } redisCache.setCacheList("shopCart:"+userId, res); }else{ ShoppingCartItem temp = cacheList.stream().filter(e -> e.getId().equals(id)).findFirst().orElse(null); if (temp != null){ // 修改数量 for (ShoppingCartItem cartItem : cacheList) { if (cartItem.getId().equals(id)) { cartItem.setNum(num); break; } } redisCache.setCacheList("shopCart:"+userId, cacheList); }else{ // 新增 cacheList.add(shoppingCartItem); redisCache.setCacheList("shopCart:"+userId, cacheList); } } return R.ok(); } /** * 海诊商城-查询购物车 */ @ApiOperation(value = "海诊商城-查询购物车") @GetMapping(value = "/queryShopCar") public R queryShopCar() { ShopCarVO res = new ShopCarVO(); Long userId = tokenService.getLoginUser().getUserId(); List cacheList = redisCache.getCacheList("shopCart:"+userId); List goodsIds = cacheList.stream().map(ShoppingCartItem::getId).collect(Collectors.toList()); if (goodsIds.isEmpty()){ ShopCarVO shopCarVO = new ShopCarVO(); shopCarVO.setTotalPrice(BigDecimal.ZERO); shopCarVO.setList(new ArrayList<>()); return R.ok(shopCarVO); } TErpGoodsShopQuery tErpGoodsShopQuery = new TErpGoodsShopQuery(); tErpGoodsShopQuery.setGoodsIds(goodsIds); // PageInfo tErpGoodsVOPageInfo = erpGoodsService.pageShopList(tErpGoodsShopQuery); // List 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 listAll = erpGoodsService.pageShopListAll(tErpGoodsShopQuery); for (TErpGoodsVO record : listAll) { 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)); } } } 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); } }