fix
Helius
2022-07-12 c331c21472fa85fd7b6c7847c82722b6e35cb533
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
228
229
230
231
232
233
234
235
package com.xcong.farmer.cms.modules.system.service.Impl;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import com.xcong.farmer.cms.common.utils.FileUtils;
import com.xcong.farmer.cms.configurations.properties.CmsProperties;
import com.xcong.farmer.cms.modules.core.service.ICmsCoreService;
import com.xcong.farmer.cms.modules.system.entity.ArticleEntity;
import com.xcong.farmer.cms.modules.system.entity.ColumnEntity;
import com.xcong.farmer.cms.modules.system.entity.CompanyEntity;
import com.xcong.farmer.cms.modules.system.entity.WebSettingEntity;
import com.xcong.farmer.cms.modules.system.mapper.ArticleMapper;
import com.xcong.farmer.cms.modules.system.mapper.ColumnMapper;
import com.xcong.farmer.cms.modules.system.mapper.CompanyMapper;
import com.xcong.farmer.cms.modules.system.mapper.WebSetMapper;
import com.xcong.farmer.cms.modules.system.service.IReleaseService;
import com.xcong.farmer.cms.modules.system.util.LoginUserUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
 
/**
 * @author wzy
 * @TODO 发布时,index存在重复发布的情况,可优化。用线程notify唤醒。
 * @date 2022-07-05
 **/
@Slf4j
@Service
public class ReleaseServiceImpl implements IReleaseService {
 
    @Autowired
    private ArticleMapper articleMapper;
 
    @Autowired
    private ColumnMapper columnMapper;
 
    @Autowired
    private ICmsCoreService cmsCoreService;
 
    @Autowired
    private CmsProperties cmsProperties;
 
    @Autowired
    private CompanyMapper companyMapper;
 
    @Autowired
    private WebSetMapper webSetMapper;
 
    private final Executor executor = new ThreadPoolExecutor(5, 20, 600, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
 
    @Override
    public void releaseArticle(Long id, Long companyId) {
        ArticleEntity article = articleMapper.selectById(id);
        ColumnEntity column = columnMapper.selectById(article.getColumnId());
 
        Long beforeColumnId = article.getBeforeColumnId();
        article.setBeforeColumnId(-1L);
        articleMapper.updateById(article);
 
        Map<String, Object> data = new Hashtable<>();
        data.put("id", article.getId());
        data.put("companyId", companyId);
 
        executor.execute(() -> {
            log.info("执行文章发布");
            cmsCoreService.articleProcess(data, column.getArticleTemplate(), column.getPath());
            this.releaseColumn(column.getId(), 1, companyId);
            if (column.getParentId() != 0L) {
                releaseColumn(column.getParentId(), 1, companyId);
            }
 
            if (beforeColumnId != null && !article.getColumnId().equals(beforeColumnId)) {
                ColumnEntity beforeColumn = columnMapper.selectById(beforeColumnId);
                this.releaseColumn(beforeColumnId, 1, companyId);
                if (beforeColumn.getParentId() != 0L) {
                    this.releaseColumn(beforeColumn.getParentId(), 1, companyId);
                }
            }
 
            this.releaseIndex(companyId, false);
        });
    }
 
    @Override
    public void releaseColumn(Long id, int type, Long companyId) {
        ColumnEntity columnEntity = columnMapper.selectById(id);
        Map<String, Object> map = buildColumnData(columnEntity);
 
        if (columnEntity.getBeforeParentId() != -1 || !"-1".equals(columnEntity.getBeforeColumnCode())) {
            CompanyEntity company = companyMapper.selectById(companyId);
            if (!"-1".equals(columnEntity.getBeforeColumnCode())) {
                String outputPath = FileUtils.path(cmsProperties.getOutputPath(), company.getCode()) + "/" + columnEntity.getBeforeColumnCode();
                log.info("删除路径为:{}", outputPath);
                FileUtil.del(outputPath);
            }
 
            if (columnEntity.getBeforeParentId() != -1) {
                ColumnEntity parent = columnMapper.selectById(columnEntity.getBeforeParentId());
                String path = cmsProperties.getOutputPath() + parent.getPath() + "/" + columnEntity.getColumnCode();
                log.info("删除路径为:{}", path);
                FileUtil.del(path);
            }
 
            columnEntity.setBeforeColumnCode("-1");
            columnEntity.setBeforeParentId(-1L);
            columnMapper.updateById(columnEntity);
            releaseAll(companyId);
            return;
        }
 
        executor.execute(() -> {
            List<ArticleEntity> articles = articleMapper.selectArticleByColumnId(columnEntity.getId(), companyId, type);
            if (CollUtil.isNotEmpty(articles)) {
                for (ArticleEntity article : articles) {
                    if (article.getReleaseStatus() == 0) {
                        article.setReleaseStatus(1);
                        articleMapper.updateById(article);
                    }
 
                    if (article.getType() == 1) {
                        map.put("id", article.getId());
                        cmsCoreService.articleProcess(map, columnEntity.getArticleTemplate(), columnEntity.getPath());
                    }
                }
            }
 
            map.put("id", columnEntity.getId());
 
            cmsCoreService.columnProcess(map, columnEntity.getListTemplate());
            if (columnEntity.getParentId() != 0L) {
                ColumnEntity parentColumn = columnMapper.selectById(columnEntity.getParentId());
                Map<String, Object> parentMap = buildColumnData(parentColumn);
                parentMap.put("id", parentColumn.getId());
                cmsCoreService.columnProcess(parentMap, parentColumn.getListTemplate());
            }
 
            releaseIndex(companyId, false);
        });
 
        if (type != 1) {
            executor.execute(() -> {
                if (columnEntity.getParentId() == 0L) {
                    List<ColumnEntity> columns = columnMapper.selectColumnByParentId(columnEntity.getId(), companyId, 2);
                    if (CollUtil.isNotEmpty(columns)) {
                        for (ColumnEntity column : columns) {
                            List<ArticleEntity> articles = articleMapper.selectArticleByColumnId(column.getId(), companyId, type);
                            if (CollUtil.isNotEmpty(articles)) {
                                for (ArticleEntity article : articles) {
                                    if (article.getReleaseStatus() == 0) {
                                        article.setReleaseStatus(1);
                                        articleMapper.updateById(article);
                                    }
 
                                    if (article.getType() == 1) {
                                        map.put("id", article.getId());
                                        cmsCoreService.articleProcess(map, column.getArticleTemplate(), column.getPath());
                                    }
                                }
                            }
 
                            Map<String, Object> data = buildColumnData(column);
                            cmsCoreService.columnProcess(data, column.getListTemplate());
                        }
                    }
                }
 
                releaseIndex(companyId, false);
            });
 
        }
    }
 
    @Override
    public void releaseColumns(int type, Long companyId) {
        List<ColumnEntity> columns = columnMapper.selectColumnByParentId(0L, companyId, 2);
 
        if (CollUtil.isEmpty(columns)) {
            return;
        }
 
        for (ColumnEntity column : columns) {
            releaseColumn(column.getId(), type, companyId);
        }
    }
 
    private Map<String, Object> buildColumnData(ColumnEntity columnEntity) {
        Map<String, Object> map = new Hashtable<>();
        map.put("id", columnEntity.getId());
        map.put("code", columnEntity.getColumnCode());
        if (columnEntity.getParentId() == 0L) {
            map.put("parentCode", columnEntity.getColumnCode());
        } else {
            ColumnEntity parent = columnMapper.selectById(columnEntity.getParentId());
            map.put("parentCode", parent.getColumnCode());
        }
        map.put("columnTitle", columnEntity.getColumnName());
        map.put("templatePath", columnEntity.getPath());
        map.put("companyId", columnEntity.getCompanyId());
        return map;
    }
 
    @Override
    public void releaseIndex(Long companyId, boolean hasSearch) {
        Map<String, Object> map = new HashMap<>();
        map.put("companyId", companyId);
 
        WebSettingEntity webSetting = webSetMapper.selectByCompanyId(companyId);
 
        cmsCoreService.indexProcess(map, webSetting.getIndexTemplate());
        if (hasSearch) {
            executor.execute(() -> {
                cmsCoreService.process(map, "search", webSetting.getSearchTemplate());
                cmsCoreService.process(map, "message", webSetting.getMsgTemplate());
            });
        }
    }
 
    @Override
    public void releaseAll(Long companyId) {
        Map<String, Object> map = new HashMap<>();
        map.put("companyId", companyId);
 
        releaseColumns(4, companyId);
        releaseIndex(companyId, true);
    }
}