linwe
2024-08-09 8b7258c9427882bb1798f1502eaa35184c6e374e
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
/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.property.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.logging.log4j.util.Strings;
import org.springblade.common.utils.SpringUtils;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.modules.property.entity.PropertyCompanyCommentEntity;
import org.springblade.modules.property.entity.PropertyCompanyEntity;
import org.springblade.modules.property.entity.PropertyCompanyScoreEntity;
import org.springblade.modules.property.service.IPropertyCompanyScoreService;
import org.springblade.modules.property.service.IPropertyCompanyService;
import org.springblade.modules.property.vo.PropertyCompanyCommentVO;
import org.springblade.modules.property.mapper.PropertyCompanyCommentMapper;
import org.springblade.modules.property.service.IPropertyCompanyCommentService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.modules.words.WorksService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.transaction.annotation.Transactional;
 
import java.math.BigDecimal;
import java.util.Map;
 
/**
 * 物业公司评论表 服务实现类
 *
 * @author BladeX
 * @since 2024-01-05
 */
@Service
public class PropertyCompanyCommentServiceImpl extends ServiceImpl<PropertyCompanyCommentMapper, PropertyCompanyCommentEntity> implements IPropertyCompanyCommentService {
 
    @Autowired
    private IPropertyCompanyScoreService propertyCompanyScoreService;
 
    @Autowired
    private WorksService worksService;
 
    @Override
    public IPage<PropertyCompanyCommentVO> selectPropertyCompanyCommentPage(IPage<PropertyCompanyCommentVO> page, PropertyCompanyCommentVO propertyCompanyComment) {
        if (null!=propertyCompanyComment.getIsRec()){
            // 递归查询
            return page.setRecords(baseMapper.selectPropertyCompanyCommentPageRec(page, propertyCompanyComment));
        }
        return page.setRecords(baseMapper.selectPropertyCompanyCommentPage(page, propertyCompanyComment));
    }
 
    /**
     * 物业公司评论表 自定义新增或修改
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String saveOrUpdatePropertyCompanyComment(PropertyCompanyCommentVO propertyCompanyComment) {
        boolean flag = false;
        // 先进行敏感词过滤
        Map<String, Object> map = worksService.interceptWords(propertyCompanyComment.getContent());
        // 获取敏感词校验结果
        String iswords = map.get("iswords").toString();
        if (iswords.equals("false")){
            // 审核通过
            propertyCompanyComment.setCheckStatus(2);
            flag = save(propertyCompanyComment);
            // 更新评分
            if(flag) {
                updatePropertyCompanyScore(propertyCompanyComment);
            }
            // 返回
            return "操作成功";
        }else {
            // 审核不通过
            propertyCompanyComment.setCheckStatus(3);
            // 设置审核不通过的说明
            propertyCompanyComment.setCheckRemark(map.get("words").toString());
            // 保存
            save(propertyCompanyComment);
            // 返回
            return "当前评论内容中带有敏感词,当前评论无法生效!";
        }
    }
 
    /**
     * 更新评分
     * @param propertyCompanyComment
     */
    public void updatePropertyCompanyScore(PropertyCompanyCommentVO propertyCompanyComment) {
        // 查询当前的物业公司,当前人是否已经评分,如果已经评分,则更新,否则新增
        if (null!=propertyCompanyComment.getScore()){
            QueryWrapper<PropertyCompanyScoreEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("is_deleted",0).eq("create_user", AuthUtil.getUserId())
                .eq("property_company_id",propertyCompanyComment.getPropertyCompanyId());
            PropertyCompanyScoreEntity one = propertyCompanyScoreService.getOne(queryWrapper);
            if (null!=one){
                one.setScore(propertyCompanyComment.getScore());
                // 更新
                propertyCompanyScoreService.updateById(one);
            }else {
                PropertyCompanyScoreEntity propertyCompanyScoreEntity = new PropertyCompanyScoreEntity();
                propertyCompanyScoreEntity.setScore(propertyCompanyComment.getScore());
                propertyCompanyScoreEntity.setPropertyCompanyId(propertyCompanyComment.getPropertyCompanyId());
                propertyCompanyScoreEntity.setCreateUser(AuthUtil.getUserId());
                // 新增
                 propertyCompanyScoreService.save(propertyCompanyScoreEntity);
            }
            // t 计算物业公司评价平均分
 
            QueryWrapper<PropertyCompanyScoreEntity> queryWrapperTwo = new QueryWrapper<>();
            queryWrapperTwo.eq("is_deleted",0).eq("property_company_id",propertyCompanyComment.getPropertyCompanyId());
            // t统计物业公司总评价数
            long count = propertyCompanyScoreService.count(queryWrapperTwo);
            // 统计物业公司总评价分
            Integer score = propertyCompanyScoreService.getScore(propertyCompanyComment.getPropertyCompanyId());
            // 计算评价分
            BigDecimal divide = BigDecimal.valueOf(score).divide(BigDecimal.valueOf(count));
            // 更新物业评价平均分
            PropertyCompanyEntity propertyCompanyEntity = new PropertyCompanyEntity();
            propertyCompanyEntity.setId(propertyCompanyComment.getPropertyCompanyId());
            propertyCompanyEntity.setEvaluateScore(divide);
            IPropertyCompanyService bean = SpringUtils.getBean(IPropertyCompanyService.class);
            bean.updateById(propertyCompanyEntity);
        }
    }
}