xiangpei
9 天以前 8065107726ad1fc13591c9bc47819207948bc45c
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
package cn.lili.modules.search.serviceimpl;
 
import cn.lili.common.context.ThreadContextHolder;
import cn.lili.common.enums.ResultCode;
import cn.lili.common.exception.ServiceException;
import cn.lili.common.vo.PageVO;
import cn.lili.modules.search.entity.dos.CustomWords;
import cn.lili.modules.search.entity.vo.CustomWordsVO;
import cn.lili.modules.search.mapper.CustomWordsMapper;
import cn.lili.modules.search.service.CustomWordsService;
import cn.lili.mybatis.util.PageUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.servlet.http.HttpServletResponse;
import java.nio.charset.StandardCharsets;
import java.util.List;
 
/**
 * 自定义分词业务层实现
 * @author paulG
 * @since 2020/10/15
 **/
@Service
public class CustomWordsServiceImpl extends ServiceImpl<CustomWordsMapper, CustomWords> implements CustomWordsService {
 
    @Override
    public String deploy() {
        LambdaQueryWrapper<CustomWords> queryWrapper = new LambdaQueryWrapper<CustomWords>().eq(CustomWords::getDisabled, 1);
        List<CustomWords> list = list(queryWrapper);
 
        HttpServletResponse response = ThreadContextHolder.getHttpResponse();
        StringBuilder builder = new StringBuilder();
        if (list != null && !list.isEmpty()) {
            boolean flag = true;
            for (CustomWords customWords : list) {
                if (flag) {
                    try {
                        response.setHeader("Last-Modified", customWords.getCreateTime().toString());
                        response.setHeader("ETag", Integer.toString(list.size()));
                    } catch (Exception e) {
                        log.error("自定义分词错误",e);
                    }
                    builder.append(customWords.getName());
                    flag = false;
                } else {
                    builder.append("\n");
                    builder.append(customWords.getName());
                }
            }
        }
 
        return new String(builder.toString().getBytes(StandardCharsets.UTF_8));
    }
 
    /**
     * 添加自定义分词
     *
     * @param customWordsVO 自定义分词信息
     * @return 是否添加成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addCustomWords(CustomWordsVO customWordsVO) {
        LambdaQueryWrapper<CustomWords> queryWrapper = new LambdaQueryWrapper<CustomWords>().eq(CustomWords::getName, customWordsVO.getName());
        CustomWords one = this.getOne(queryWrapper, false);
        if (one != null && one.getDisabled().equals(1)) {
            return false;
        } else if (one != null && !one.getDisabled().equals(1)) {
            this.remove(queryWrapper);
        }
        customWordsVO.setDisabled(1);
        return this.save(customWordsVO);
    }
 
    /**
     * 删除自定义分词
     *
     * @param id 自定义分词id
     * @return 是否删除成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteCustomWords(String id) {
        if (this.getById(id) == null) {
            throw new ServiceException(ResultCode.CUSTOM_WORDS_NOT_EXIST_ERROR);
        }
        return this.removeById(id);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBathByName(List<String> names) {
        LambdaQueryWrapper<CustomWords> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(CustomWords::getName, names);
        return this.remove(queryWrapper);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public long insertBatchCustomWords(List<CustomWords> customWordsList) {
        return this.baseMapper.insertIgnoreBatchAllColumn(customWordsList);
    }
 
    /**
     * 修改自定义分词
     *
     * @param customWordsVO 自定义分词信息
     * @return 是否修改成功
     */
    @Override
    public boolean updateCustomWords(CustomWordsVO customWordsVO) {
        if (this.getById(customWordsVO.getId()) == null) {
            throw new ServiceException(ResultCode.CUSTOM_WORDS_NOT_EXIST_ERROR);
        }
        return this.updateById(customWordsVO);
    }
 
    /**
     * 分页查询自定义分词
     *
     * @param words  分词
     * @param pageVo 分页信息
     * @return 自定义分词分页信息
     */
    @Override
    public IPage<CustomWords> getCustomWordsByPage(String words, PageVO pageVo) {
        LambdaQueryWrapper<CustomWords> queryWrapper = new LambdaQueryWrapper<CustomWords>().like(CustomWords::getName, words);
        return this.page(PageUtil.initPage(pageVo), queryWrapper);
    }
 
    @Override
    public boolean existWords(String words) {
        LambdaQueryWrapper<CustomWords> queryWrapper = new LambdaQueryWrapper<CustomWords>().eq(CustomWords::getName, words);
        long count = count(queryWrapper);
        return count > 0;
    }
}