xuhy
2026-01-20 78a7c810e5f2e30ee596401febc733148830467f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
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.*;
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.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
 
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * <p>
 * erp商品 前端控制器
 * </p>
 *
 * @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;
    private final TErpProcurementGoodsService erpProcurementGoodsService;
 
    @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, TErpProcurementGoodsService erpProcurementGoodsService) {
        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;
        this.erpProcurementGoodsService = erpProcurementGoodsService;
    }
 
    /**
     * 海诊商城分页列表
     */
    @ApiOperation(value = "海诊商城分页列表")
    @PostMapping(value = "/pageShopList")
    public R<PageInfo<TErpGoodsVO>> pageShopList(@RequestBody TErpGoodsShopQuery query) {
        return R.ok(erpGoodsService.pageShopList(query));
    }
 
    /**
     * 海诊商城-商品详情
     */
    @ApiOperation(value = "海诊商城-商品详情")
    @GetMapping(value = "/shopDetail")
    public R<TErpGoodsVO> 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());
        }
 
        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 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 R clear(String id) {
        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 (!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<ShoppingCartItem> cacheList = redisCache.getCacheList("shopCart:"+userId);
        if (cacheList==null){
            cacheList = new ArrayList<>();
        }
        redisCache.deleteObject("shopCart:"+userId);
        if (num==0){
            // 移除
            List<ShoppingCartItem> 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<ShopCarVO> queryShopCar() {
        ShopCarVO res = new ShopCarVO();
        Long userId = tokenService.getLoginUser().getUserId();
        List<ShoppingCartItem> cacheList = redisCache.getCacheList("shopCart:"+userId);
        List<String> 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<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) {
                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);
    }
 
 
}