无关风月
2026-01-09 f9e857610cd8ec3c5807651ad11fd3034f1e0be7
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
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.List;
import java.util.Objects;
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;
 
    @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<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());
        }
        return R.ok(res);
    }
    @ApiOperation(value = "海诊商城-清空购物车")
    @GetMapping(value = "/clear")
    public void clearAll() {
        Long userId = tokenService.getLoginUser().getUserId();
        redisCache.deleteObject("shopCart:"+userId);
    }
    @ApiOperation(value = "海诊商城-单删购物车")
    @GetMapping(value = "/clearById")
    public void 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);
        }
 
    }
    /**
     * 海诊商城-加入购物车
     */
    @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);
        redisCache.deleteObject("shopCart:"+userId);
        if (cacheList != null) {
            if (!cacheList.isEmpty()) {
                for (ShoppingCartItem cartItem : cacheList) {
                    if (cartItem.getId().equals(id)) {
                        cartItem.setNum( num);
                        redisCache.setCacheList("shopCart:"+userId, cacheList);
                        return R.ok();
                    }else{
                        cacheList.add(cartItem);
                    }
                }
            } else {
                cacheList = new ArrayList<>();
                cacheList.add(shoppingCartItem);
            }
        } else {
            cacheList = new ArrayList<>();
            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 PageInfo<>());
            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 = records.stream().map(TErpGoodsVO::getPrice).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
        res.setList(tErpGoodsVOPageInfo);
        res.setTotalPrice(totalPrice);
        return R.ok(res);
    }
 
 
}