基于mall4j产品的二开项目后端
lee
2024-12-18 921461a3f906d74403aeb6a27051deb77eca10fc
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
/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许,不可做商业用途!
 *
 * 版权所有,侵权必究!
 */
package com.yami.shop.api.controller;
 
 
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.app.dto.ProdCommDto;
import com.yami.shop.bean.app.param.ProdCommParam;
import com.yami.shop.bean.enums.OrderStatus;
import com.yami.shop.bean.enums.ReturnMoneyStsType;
import com.yami.shop.bean.model.Order;
import com.yami.shop.bean.model.OrderItem;
import com.yami.shop.bean.model.ProdComm;
import com.yami.shop.bean.model.Product;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.i18n.I18nMessage;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.common.util.RedisUtil;
import com.yami.shop.security.api.util.SecurityUtils;
import com.yami.shop.service.OrderItemService;
import com.yami.shop.service.OrderService;
import com.yami.shop.service.ProdCommService;
import com.yami.shop.service.ProductService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
 
import javax.validation.Valid;
import java.util.List;
import java.util.Objects;
 
/**
 * @author LGH
 */
@RestController
@RequestMapping("/p/prodComm")
@Tag(name = "商品评论接口")
@AllArgsConstructor
public class ProdCommController {
 
    private final ProdCommService prodCommService;
 
    private final OrderItemService orderItemService;
 
    private final OrderService orderService;
 
    private final ProductService productService;
 
    @GetMapping("/prodCommPageByUser")
    @Operation(summary = "根据用户返回评论分页数据" , description = "传入页码")
    public ServerResponseEntity<IPage<ProdCommDto>> getProdCommPage(PageParam page) {
        return ServerResponseEntity.success(prodCommService.getProdCommDtoPageByUserId(page, SecurityUtils.getUser().getUserId()));
    }
 
    @PostMapping
    @Operation(summary = "添加评论")
    public ServerResponseEntity<Void> saveProdCommPage(@Valid @RequestBody ProdCommParam prodCommParam) {
        Long orderItemId = prodCommParam.getOrderItemId();
        OrderItem orderItem = orderItemService.getByOrderItemId(orderItemId);
        if (orderItem == null) {
            // 订单项不存在
            throw new YamiShopBindException("yami.address.deleted");
        }
        if (Objects.equals(orderItem.getCommSts(), 1)) {
            // 改订单项已评论,请勿重复评论
            throw new YamiShopBindException("yami.item.has.commented");
        }
        String userId = SecurityUtils.getUser().getUserId();
        String limitKey = "COMM_"+orderItemId;
        Long commonNum = RedisUtil.incr(limitKey, 1);
        if(commonNum==1){
            RedisUtil.expire(limitKey,2);
        }else {
            throw new YamiShopBindException("yami.item.com.repeated");
        }
 
        Order order = orderService.getOrderByOrderNumberAndUserId(orderItem.getOrderNumber(), userId, true);
        if (!Objects.equals(order.getStatus(), OrderStatus.SUCCESS.value()) && !Objects.equals(orderItem.getReturnMoneySts(), ReturnMoneyStsType.SUCCESS.value())) {
            // 请确认收货后再进行评论
            throw new YamiShopBindException("yami.confirm.receipt");
        }
        Product product = productService.getProductByProdId(orderItem.getProdId(), I18nMessage.getDbLang());
        if (Objects.isNull(product)) {
            String takeOff = I18nMessage.getMessage("yami.product.no.exist");
            // 商品不存在
            throw new YamiShopBindException(takeOff);
        }
        if (StrUtil.isBlank(prodCommParam.getPics())) {
            prodCommParam.setPics(null);
        }
        prodCommService.comm(orderItem, prodCommParam);
        return ServerResponseEntity.success();
    }
 
    @GetMapping("/getProdComment")
    @Operation(summary = "根据订单项Id列表获取评论")
    @Parameter(name = "orderItemIds", description = "订单项Id列表" )
    public ServerResponseEntity<List<ProdComm>> getProdComment(@RequestParam("orderItemIds") List<Long> orderItemIds) {
        List<ProdComm> prodCommList = prodCommService.list(new LambdaUpdateWrapper<ProdComm>()
                .in(ProdComm::getOrderItemId, orderItemIds)
                .eq(ProdComm::getUserId, SecurityUtils.getUser().getUserId()));
        if (CollectionUtil.isEmpty(prodCommList)) {
            throw new YamiShopBindException("yami.comment.is.delete");
        }
        return ServerResponseEntity.success(prodCommList);
    }
}