luofl
2025-02-20 e4eaabefaaa2ca95f809c47a11712c3bee56e1b5
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
package com.ruoyi.other.controller;
 
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.account.api.feignClient.AppUserClient;
import com.ruoyi.account.api.model.AppUser;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.page.PageInfo;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.order.feignClient.OrderClient;
import com.ruoyi.order.model.Order;
import com.ruoyi.other.api.domain.Goods;
import com.ruoyi.other.api.domain.GoodsEvaluate;
import com.ruoyi.other.service.GoodsEvaluateService;
import com.ruoyi.other.service.GoodsService;
import com.ruoyi.other.vo.GoodsEvaluateVO;
import com.ruoyi.system.api.model.LoginUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import org.springframework.web.bind.annotation.*;
 
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author luodangjia
 * @since 2024-11-20
 */
@RestController
@RequestMapping("/goods-evaluate")
@Api("评论")
public class GoodsEvaluateController {
    @Resource
    private GoodsEvaluateService goodsEvaluateService;
    @Resource
    private AppUserClient appUserClient;
    @Resource
    private GoodsService goodsService;
    @Resource
    private TokenService tokenService;
    @Resource
    private OrderClient orderClient;
 
 
 
 
    @GetMapping("/goodsList")
    @ApiOperation(value = "获取商品评价", tags = {"小程序-获取商品评价"})
    public R<PageInfo<GoodsEvaluate>> goodsList(@ApiParam("商品id") Integer goodsId, Integer pageNum, Integer pageSize){
        PageInfo<GoodsEvaluate> pageInfo = new PageInfo<>(pageNum, pageSize);
        PageInfo<GoodsEvaluate> page = goodsEvaluateService.lambdaQuery().eq(GoodsEvaluate::getGoodsId, goodsId).eq(GoodsEvaluate::getDelFlag, 0)
                .eq(GoodsEvaluate::getStatus, 2).isNotNull(GoodsEvaluate::getComment).ne(GoodsEvaluate::getComment, "")
                .orderByDesc(GoodsEvaluate::getCreateTime).page(pageInfo);
        for (GoodsEvaluate goodsEvaluate : page.getRecords()) {
            AppUser appUserById = appUserClient.getAppUserById(goodsEvaluate.getAppUserId());
            if(null != appUserById){
                goodsEvaluate.setUserName(appUserById.getName());
                goodsEvaluate.setAvatar(appUserById.getAvatar());
                goodsEvaluate.setIdStr(String.valueOf(goodsEvaluate.getId()));
            }
        }
        return R.ok(page);
    }
 
    /**
     * 发布商品评价
     */
    @PostMapping("/addGoodsEvaluate")
    @ApiOperation(value = "发布商品评价", tags = {"小程序-个人中心-我的订单"})
    public R<Void> addGoodsEvaluate(@RequestBody GoodsEvaluateVO goodsEvaluateVO){
        goodsEvaluateService.addGoodsEvaluate(goodsEvaluateVO);
        return R.ok();
    }
 
    /**
     * 评论详情
     */
    @GetMapping("/detail/{orderId}")
    @ApiOperation(value = "评论详情", tags = {"小程序-评论详情"})
    public R<List<GoodsEvaluate>> detail(@PathVariable("orderId") Long orderId){
        LoginUser loginUserApplet = tokenService.getLoginUserApplet();
        List<GoodsEvaluate> list = goodsEvaluateService.list(new LambdaQueryWrapper<GoodsEvaluate>()
                .eq(GoodsEvaluate::getStatus, 2)
                .eq(GoodsEvaluate::getAppUserId,loginUserApplet.getUserid())
                .eq(GoodsEvaluate::getDelFlag, 0)
                .eq(GoodsEvaluate::getOrderId, orderId));
        for (GoodsEvaluate goodsEvaluate : list) {
            Goods goods = goodsService.getById(goodsEvaluate.getGoodsId());
            goodsEvaluate.setGoodsName(goods.getName());
            goodsEvaluate.setGoodsImg(goods.getHomePagePicture());
        }
        return R.ok(list);
    }
 
 
    /**
     * 删除评论
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除评论", tags = {"管理后台-商品管理-评价管理"})
    public R<Void> delete(@PathVariable("id") Long id){
        GoodsEvaluate goodsEvaluate = goodsEvaluateService.getById(id);
        goodsEvaluate.setDelFlag(1);
        goodsEvaluateService.updateById(goodsEvaluate);
        //修改订单为待评价
        Order data = orderClient.getOrderById(goodsEvaluate.getOrderId()).getData();
        data.setOrderStatus(data.getOldOrderStatus());
        orderClient.editOrder(data);
        return R.ok();
    }
 
 
 
    /**
     * 评论列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "评论列表", tags = {"管理后台-商品管理-评价管理"})
    public R<Page<GoodsEvaluate>> list(@ApiParam("页码") @RequestParam Integer pageNum,
                                       @ApiParam("每一页数据大小") Integer pageSize,
                                       GoodsEvaluate goodsEvaluate){
        List<Integer> goodsIds = new ArrayList<>();
        List<Long> usersNameList = new ArrayList<>();
        List<Long> usersPhoneList = new ArrayList<>();
 
        if (StringUtils.isNotEmpty(goodsEvaluate.getGoodsName())) {
            goodsIds = goodsService.lambdaQuery()
                    .like(Goods::getName, goodsEvaluate.getGoodsName())
                    .list().stream().map(Goods::getId).collect(Collectors.toList());
            if (goodsIds.isEmpty()) {
                return R.ok(new Page<>());
            }
        }
        if (StringUtils.isNotEmpty(goodsEvaluate.getUserName())) {
            usersNameList = appUserClient.getAppUserByName(goodsEvaluate.getUserName()).getData().stream().map(AppUser::getId).collect(Collectors.toList());
            if (usersNameList.isEmpty()) {
                return R.ok(new Page<>());
            }
 
        }
        if (StringUtils.isNotEmpty(goodsEvaluate.getPhone())) {
            usersPhoneList = appUserClient.getAppUserByPhoneNoFilter(goodsEvaluate.getPhone()).getData().stream().map(AppUser::getId).collect(Collectors.toList());
            if (usersPhoneList.isEmpty()) {
                return R.ok(new Page<>());
            }
        }
        if (StringUtils.isNotEmpty(goodsEvaluate.getPhone())&&StringUtils.isNotEmpty(goodsEvaluate.getUserName())){
            // 取交集
            usersNameList.retainAll(usersPhoneList);
            if (usersNameList.isEmpty())usersNameList.add(-1L);
        }else{
            usersNameList.addAll(usersPhoneList);
        }
        Page<GoodsEvaluate> page = goodsEvaluateService.page(Page.of(pageNum, pageSize), new LambdaQueryWrapper<GoodsEvaluate>()
                .in(!goodsIds.isEmpty(), GoodsEvaluate::getGoodsId, goodsIds)
                .in(!usersNameList.isEmpty(), GoodsEvaluate::getAppUserId, usersNameList)
                .eq(goodsEvaluate.getStatus() != null && !goodsEvaluate.getStatus().equals(0), GoodsEvaluate::getStatus, goodsEvaluate.getStatus())
                .eq(GoodsEvaluate::getDelFlag, 0)
                .orderByDesc(GoodsEvaluate::getCreateTime));
 
 
        page.getRecords().forEach(this::buildDetail);
 
        return R.ok(page);
    }
 
    private void buildDetail(GoodsEvaluate item) {
        AppUser appUser = appUserClient.getAppUserById(item.getAppUserId());
        if (appUser != null){
            item.setPhone(appUser.getPhone());
            item.setUserName(appUser.getName());
        }
        Goods goods = goodsService.getById(item.getGoodsId());
        if(null != goods){
            item.setGoodsName(goods.getName());
        }
    }
 
    /**
     * 评论详情
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "评论详情", tags = {"管理后台-商品管理-评价管理"})
    public R<GoodsEvaluate> getDetail(@PathVariable("id") Long id){
        GoodsEvaluate goodsEvaluate = goodsEvaluateService.getById(id);
        buildDetail(goodsEvaluate);
        return R.ok(goodsEvaluate);
    }
 
    /**
     * 评论上/下架
     */
    @PutMapping("/changeStatus")
    @ApiOperation(value = "上/下架", tags = {"管理后台-商品管理-评价管理"})
    public R<Void> changeStatus(@RequestBody GoodsEvaluate goodsEvaluate){
        goodsEvaluateService.update(new LambdaUpdateWrapper<GoodsEvaluate>()
                .eq(GoodsEvaluate::getId, goodsEvaluate.getId())
                .set(GoodsEvaluate::getStatus, goodsEvaluate.getStatus()));
        return R.ok();
    }
 
}