xuhy
2024-12-20 cf7740957f2b859e541116f2f4603beff8a0c832
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
package com.ruoyi.web.controller.api;
 
 
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.basic.PageInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.CodeGenerateUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.query.TGoodsQuery;
import com.ruoyi.system.service.*;
import com.ruoyi.system.vo.TGoodsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
import java.util.List;
import java.util.Objects;
 
/**
 * <p>
 * 商品管理 前端控制器
 * </p>
 *
 * @author xiaochen
 * @since 2024-08-14
 */
@Api(tags = "商品管理")
@RestController
@RequestMapping("/t-goods")
public class TGoodsController {
 
    private final TGoodsService goodsService;
    private final TGoodsTypeService goodsTypeService;
    private final TokenService tokenService;
    private final TOrderStockGoodsService orderStockGoodsService;
    private final TOrderSaleGoodsService orderSaleGoodsService;
    private final TOrderMealGoodsService orderMealGoodsService;
 
    @Autowired
    public TGoodsController(TGoodsService goodsService, TGoodsTypeService goodsTypeService, TokenService tokenService, TOrderStockGoodsService orderStockGoodsService, TOrderSaleGoodsService orderSaleGoodsService, TOrderMealGoodsService orderMealGoodsService) {
        this.goodsService = goodsService;
        this.goodsTypeService = goodsTypeService;
        this.tokenService = tokenService;
        this.orderStockGoodsService = orderStockGoodsService;
        this.orderSaleGoodsService = orderSaleGoodsService;
        this.orderMealGoodsService = orderMealGoodsService;
    }
 
    /**
     * 查询商品管理分页列表
     */
    @ApiOperation( value = "查询商品管理分页列表")
    @PostMapping(value = "/pageList")
    public AjaxResult<PageInfo<TGoodsVO>> pageList(@RequestBody TGoodsQuery query) {
        Integer roleType = tokenService.getLoginUser().getRoleType();
        if(roleType != 1){
            query.setShopId(tokenService.getLoginUser().getObjectId());
        }
        return AjaxResult.success(goodsService.pageList(query));
    }
 
    /**
     * 添加商品管理管理
     */
    @ApiOperation( value = "添加商品管理")
    @PostMapping(value = "/add")
    public AjaxResult<Boolean> add(@RequestBody TGoods dto) {
        dto.setShopId(tokenService.getLoginUser().getObjectId());
        String num = CodeGenerateUtils.generateVolumeSn();
        long count = goodsService.count(Wrappers.lambdaQuery(TGoods.class)
                .eq(TGoods::getGoodsNum, num));
        if(count>0){
            num = CodeGenerateUtils.generateVolumeSn();
        }
        dto.setGoodsNum(num);
        return AjaxResult.success(goodsService.save(dto));
    }
 
    /**
     * 修改商品管理
     */
    @ApiOperation( value = "修改商品管理")
    @PostMapping(value = "/update")
    public AjaxResult<Boolean> update(@RequestBody TGoods dto) {
        // 判断是否修改了名称
        TGoods goods = goodsService.getById(dto.getId());
        if(!goods.getGoodsName().equals(dto.getGoodsName())){
            List<TOrderStockGoods> orderStockGoods = orderStockGoodsService.list(Wrappers.lambdaQuery(TOrderStockGoods.class)
                    .eq(TOrderStockGoods::getGoodsNum, goods.getGoodsNum()));
            orderStockGoods.stream().forEach(tOrderStockGoods -> tOrderStockGoods.setGoodsName(dto.getGoodsName()));
            orderStockGoodsService.updateBatchById(orderStockGoods);
 
            List<TOrderMealGoods> orderMealGoods = orderMealGoodsService.list(Wrappers.lambdaQuery(TOrderMealGoods.class)
                    .eq(TOrderMealGoods::getGoodsNum, goods.getGoodsNum()));
            orderMealGoods.stream().forEach(tOrderMealGoods -> tOrderMealGoods.setGoodsName(dto.getGoodsName()));
            orderMealGoodsService.updateBatchById(orderMealGoods);
 
            List<TOrderSaleGoods> list = orderSaleGoodsService.list(Wrappers.lambdaQuery(TOrderSaleGoods.class)
                    .eq(TOrderSaleGoods::getGoodsNum, goods.getGoodsNum()));
            list.stream().forEach(tOrderSaleGoods -> tOrderSaleGoods.setGoodsName(dto.getGoodsName()));
            orderSaleGoodsService.updateBatchById(list);
        }
        return AjaxResult.success(goodsService.updateById(dto));
    }
 
    /**
     * 查看商品管理详情
     */
    @ApiOperation( value = "查看商品管理详情")
    @GetMapping(value = "/getDetailById")
    public AjaxResult<TGoodsVO> getDetailById(@RequestParam("id") Long id) {
        TGoods goods = goodsService.getById(id);
        TGoodsVO tGoodsVO = new TGoodsVO();
        BeanUtils.copyProperties(goods, tGoodsVO);
        TGoodsType goodsType = goodsTypeService.getById(goods.getTypeId());
        if(Objects.nonNull(goodsType)){
            tGoodsVO.setTypeName(goodsType.getTypeName());
        }
        return AjaxResult.success(tGoodsVO);
    }
 
    /**
     * 删除商品管理
     */
    @ApiOperation( value = "删除商品管理")
    @DeleteMapping(value = "/deleteById")
    public AjaxResult<Boolean> deleteById(@RequestParam("id") Long id) {
        TGoods goods = goodsService.getById(id);
        long count = goodsService.count(Wrappers.lambdaQuery(TGoods.class)
                .eq(TGoods::getTypeId, goods.getTypeId())
                .ne(TGoods::getId,id));
        if(count == 0){
            return AjaxResult.error("当前菜品所属分类下至少保留一种菜品");
        }
        return AjaxResult.success(goodsService.removeById(id));
    }
 
    /**
     * 批量删除商品管理
     */
    @ApiOperation( value = "批量删除商品管理")
    @DeleteMapping(value = "/deleteByIds")
    public AjaxResult<Boolean> deleteByIds(@RequestBody List<Long> ids) {
        return AjaxResult.success(goodsService.removeByIds(ids));
    }
    
}