cloudroam
2024-11-27 0d0a028920745bcdc5d9ddbf0ab3cbccfe167c63
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
package com.mzl.flower.service.impl.comment;
 
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mzl.flower.config.exception.ValidationException;
import com.mzl.flower.config.security.SecurityUtils;
import com.mzl.flower.dto.request.comment.*;
import com.mzl.flower.dto.response.comment.FlowerCommentStatisVO;
import com.mzl.flower.dto.response.comment.FlowerCommentVO;
import com.mzl.flower.entity.FlowerCommentDO;
import com.mzl.flower.entity.customer.Customer;
import com.mzl.flower.entity.payment.Order;
import com.mzl.flower.entity.payment.OrderItem;
import com.mzl.flower.entity.supplier.Supplier;
import com.mzl.flower.enums.FlowerCommentShowEnum;
import com.mzl.flower.enums.TrueOrFalseEnum;
import com.mzl.flower.mapper.comment.FlowerCommentMapper;
import com.mzl.flower.mapper.comment.FlowerCommentMapperCustom;
import com.mzl.flower.mapper.customer.CustomerMapper;
import com.mzl.flower.mapper.payment.OrderItemMapper;
import com.mzl.flower.mapper.payment.OrderMapper;
import com.mzl.flower.mapper.supplier.SupplierMapper;
import com.mzl.flower.service.comment.FlowerCommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
 
/**
 * <p>
 * 商品评论表 服务实现类
 * </p>
 *
 * @author @TaoJie
 * @since 2024-09-29
 */
@Service
public class FlowerCommentServiceImpl extends ServiceImpl<FlowerCommentMapper, FlowerCommentDO> implements FlowerCommentService {
 
    @Autowired
    private FlowerCommentMapperCustom flowerCommentMapperCustom;
 
    @Autowired
    private OrderItemMapper orderItemMapper;
 
    @Autowired
    private SupplierMapper supplierMapper;
 
    @Autowired
    private CustomerMapper customerMapper;
 
    @Autowired
    private OrderMapper orderMapper;
 
 
    @Transactional
    @Override
    public boolean createFlowerComment(CreateFlowerCommentDTO dto) {
        final FlowerCommentVO byOrderItemId = getByOrderItemId(dto.getOrderItemId());
        if(null!=byOrderItemId){
            throw new ValidationException("当前商品已经评论过!");
        }
 
        FlowerCommentDO flowerCommentDO=new FlowerCommentDO();
        BeanUtils.copyProperties(dto,flowerCommentDO);
        flowerCommentDO.setId(IdUtil.simpleUUID());
        flowerCommentDO.create(SecurityUtils.getUserId());
 
        final OrderItem orderItem = orderItemMapper.selectById(dto.getOrderItemId());
        if(null==orderItem){
            throw new ValidationException("当前订单项目不存在!");
        }
        flowerCommentDO.setOrderId(orderItem.getOrderId());
        flowerCommentDO.setCustomerUserId(orderItem.getCreateBy());
        flowerCommentDO.setSupplierId(orderItem.getSupplierId());
        flowerCommentDO.setFlowerId(orderItem.getFlowerId());
        flowerCommentDO.setFlowerCover(orderItem.getFlowerCover());
 
        // 补充商户信息
        Customer customer = customerMapper.selectOne(new QueryWrapper<Customer>()
                .eq("user_id", orderItem.getCreateBy()));
        if(null!=customer && null!=customer.getId()){
            flowerCommentDO.setCustomerId(customer.getId());
        }
 
 
        // 补充供应商信息
        Supplier supplier = supplierMapper.selectById(orderItem.getSupplierId());
        if(null!=supplier && StringUtils.isNotBlank(supplier.getUserId())){
            flowerCommentDO.setSupplierUserId(supplier.getUserId());
        }
 
        // 设置显示状态为显示
        flowerCommentDO.setShowFlag(FlowerCommentShowEnum.SHOW.getFlag());
        return baseMapper.insert(flowerCommentDO)>0;
    }
 
    @Transactional
    @Override
    public boolean updateFlowerComment(UpdateFlowerCommentDTO dto) {
        FlowerCommentDO flowerCommentDO=baseMapper.selectById(dto.getId());
        BeanUtils.copyProperties(dto,flowerCommentDO);
        flowerCommentDO.update(SecurityUtils.getUserId());
        
        return baseMapper.updateById(flowerCommentDO)>0;
    }
 
    @Override
    public boolean deleteFlowerComment(String id) {
        return baseMapper.deleteById(id)>0;
    }
 
    @Override
    public FlowerCommentVO getDetailById(String id) {
        QueryFlowerCommentDTO dto=new QueryFlowerCommentDTO();
        dto.setId(id);
        final List<FlowerCommentVO> list = getList(dto);
        if(CollectionUtils.isNotEmpty(list)){
            return list.get(0);
        }
        return null;
    }
 
    @Override
    public Page<FlowerCommentVO> getPage(Page page, QueryFlowerCommentDTO dto) {
        List<FlowerCommentVO> list=flowerCommentMapperCustom.getPage(page,dto);
        return page.setRecords(list);
    }
 
    @Override
    public List<FlowerCommentVO> getList(QueryFlowerCommentDTO dto) {
        return flowerCommentMapperCustom.getList(dto);
    }
 
    @Override
    public FlowerCommentVO getByOrderItemId(String orderItemId) {
        QueryFlowerCommentDTO dto=new QueryFlowerCommentDTO();
        dto.setOrderItemId(orderItemId);
        final List<FlowerCommentVO> list = getList(dto);
        if(CollectionUtils.isNotEmpty(list)){
            return list.get(0);
        }
        return null;
    }
 
    @Override
    public BigDecimal getSupplierAvgScore(Long supplierId) {
        return flowerCommentMapperCustom.getSupplierAvgScore(supplierId);
    }
 
    @Transactional
    @Override
    public boolean createFlowerCommentBatch(CreateFlowerCommentBatchDTO dto) {
        if(CollectionUtils.isNotEmpty(dto.getList())){
            dto.getList().forEach(item->{
                // 保存评论信息
                final FlowerCommentVO byOrderItemId = getByOrderItemId(item.getOrderItemId());
                if(null!=byOrderItemId){
                    // 更新
                    UpdateFlowerCommentDTO updateFlowerCommentDTO=new UpdateFlowerCommentDTO();
                    BeanUtils.copyProperties(item,updateFlowerCommentDTO);
                    updateFlowerCommentDTO.setId(byOrderItemId.getId());
                    updateFlowerComment(updateFlowerCommentDTO);
                }else{
                    createFlowerComment(item);
                }
 
            });
            // 设置订单的评论状态为评论的下个阶段
            Order order=orderMapper.selectById(dto.getOrderId());
 
            return true;
        }
        return false;
    }
 
    @Override
    public FlowerCommentStatisVO getSupplierStatis(Long supplierId) {
        FlowerCommentStatisVO vo=new FlowerCommentStatisVO();
 
        // 获取平均分
        final BigDecimal supplierAvgScore = flowerCommentMapperCustom.getSupplierAvgScore(supplierId);
        vo.setAvg(supplierAvgScore);
 
        //当前商家评论总数
        final Integer amount = baseMapper.selectCount(new QueryWrapper<FlowerCommentDO>().lambda()
                        .eq(FlowerCommentDO::getDeleted, TrueOrFalseEnum.FALSE)
                        .eq(FlowerCommentDO::getShowFlag,FlowerCommentShowEnum.SHOW.getFlag())
                        .eq(FlowerCommentDO::getSupplierId, supplierId));
        vo.setCommentAmount(amount);
        return vo;
    }
 
    @Override
    public boolean updateShowFlowerComment(ShowFlowerCommentDTO dto) {
        FlowerCommentDO flowerCommentDO = baseMapper.selectById(dto.getId());
        if (null == flowerCommentDO) {
            throw new ValidationException("评论不存在");
        }
        flowerCommentDO.setShowFlag(dto.getShowFalg());
        flowerCommentDO.setUpdateBy(SecurityUtils.getUserId());
        return baseMapper.updateById(flowerCommentDO)>0;
    }
 
    @Override
    public boolean updateReplayFlowerComment(ReplayFlowerCommentDTO dto) {
        FlowerCommentDO flowerCommentDO = baseMapper.selectById(dto.getId());
        if (null == flowerCommentDO) {
            throw new ValidationException("评论不存在");
        }
        flowerCommentDO.setReplayContent(dto.getReplayContent());
        flowerCommentDO.setReplayBy(SecurityUtils.getUserId());
        flowerCommentDO.setReplayTime(LocalDateTime.now());
        flowerCommentDO.setUpdateBy(SecurityUtils.getUserId());
        return baseMapper.updateById(flowerCommentDO)>0;
    }
 
 
}