地质所 沉降监测网建设项目
zmk
2024-05-15 9e3afc6d0fa514f986d3fea40fa23124e6fb5070
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package com.javaweb.cms.service.impl;
 
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import cn.hutool.cache.Cache;
import cn.hutool.cache.CacheUtil;
import com.google.common.collect.Lists;
import com.javaweb.cms.domain.ArticleRegionType;
import com.javaweb.cms.domain.Category;
import com.javaweb.cms.domain.Tags;
import com.javaweb.cms.mapper.TagsMapper;
import com.javaweb.cms.service.ICategoryService;
import com.javaweb.common.utils.DateUtils;
import com.javaweb.common.utils.Guid;
import com.javaweb.common.utils.StringUtils;
import com.javaweb.ehcache.util.EhCacheUtils;
import com.javaweb.framework.util.ShiroUtils;
import com.javaweb.system.domain.SysUser;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.javaweb.cms.mapper.ArticleMapper;
import com.javaweb.cms.domain.Article;
import com.javaweb.cms.service.IArticleService;
import com.javaweb.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;
 
/**
 * 文章管理Service业务层处理
 * 
 * @author wujiyue
 * @date 2019-10-28
 */
@Service
public class ArticleServiceImpl implements IArticleService 
{
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private TagsMapper tagsMapper;
    @Autowired
    private ICategoryService categoryService;
 
    private Cache<String, Category> categoryCache= CacheUtil.newLFUCache(100);
    private Cache<String, Tags> tagCache= CacheUtil.newLFUCache(100);
    /**
     * 查询文章管理
     * 
     * @param id 文章管理ID
     * @return 文章管理
     */
    @Override
    public Article selectArticleById(String id)
    {
        Article article=articleMapper.selectArticleById(id);
        Map<String, Object> m = articleMapper.getArticleContent(id);
        if(m!=null){
            article.setContent(String.valueOf(m.get("content")));
            article.setContent_markdown_source(String.valueOf(m.get("content_markdown_source")));
        }
        selectCategory(article);
        selectTags(article);
        return article;
    }
 
    /**
     * 查询文章管理列表
     * 
     * @param article 文章管理
     * @return 文章管理
     */
    @Override
    public List<Article> selectArticleList(Article article)
    {
        List<Article> articles=articleMapper.selectArticleList(article);
        selectTags(articles);
        selectCategory(articles);
        return articles;
    }
 
    /**
     * 新增文章管理
     * 
     * @param article 文章管理
     * @return 结果
     */
    @Override
    @Transactional
    public int insertArticle(Article article)
    {
        article.setId(Guid.get());
        article.setCreateTime(DateUtils.getNowDate());
        article.setUpdateTime(DateUtils.getNowDate());
        SysUser user= ShiroUtils.getSysUser();
        article.setYhid(user.getUserId().toString());
        article.setDeleted(0);
        String tags=article.getTags();
        if(StringUtils.isNotEmpty(tags)){
            if(!tags.endsWith(",")){
                tags+=",";
                article.setTags(tags);
            }
        }
        article.setAuthor(user.getUserName());
 
        if(article.getCommentFlag()==null){
            article.setCommentFlag("0");
        }
        if("on".equals(article.getCommentFlag())){
            article.setCommentFlag("1");
        }
        if("off".equals(article.getCommentFlag())){
            article.setCommentFlag("0");
        }
 
        int n=articleMapper.insertArticle(article);
        n=articleMapper.insertArticleContent(article);
        return n;
    }
 
    /**
     * 修改文章管理
     * 
     * @param article 文章管理
     * @return 结果
     */
    @Override
    @Transactional
    public int updateArticle(Article article)
    {
        article.setUpdateTime(DateUtils.getNowDate());
        String tags=article.getTags();
        if(StringUtils.isNotEmpty(tags)){
            if(!tags.endsWith(",")){
                tags+=",";
                article.setTags(tags);
            }
        }
        if(article.getCommentFlag()==null){
            article.setCommentFlag("0");
        }
        if("on".equals(article.getCommentFlag())){
            article.setCommentFlag("1");
        }
        if("off".equals(article.getCommentFlag())){
            article.setCommentFlag("0");
        }
        int n=articleMapper.updateArticle(article);
        n=articleMapper.updateArticleContent(article);
        return n;
    }
 
    /**
     * 删除文章管理对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteArticleByIds(String ids)
    {
        articleMapper.deleteArticleContentByIds(Convert.toStrArray(ids));
        return articleMapper.deleteArticleByIds(Convert.toStrArray(ids));
    }
 
    /**
     * 删除文章管理信息
     * 
     * @param id 文章管理ID
     * @return 结果
     */
    @Override
    public int deleteArticleById(String id)
    {
        articleMapper.deleteArticleContentById(id);
        return articleMapper.deleteArticleById(id);
    }
 
    @Override
    public List<Article> selectArticlesByArticleRegionType(ArticleRegionType articleRegionType) {
        List<Article> list=(List<Article>) EhCacheUtils.getSysInfo("ArticleRegionType_", articleRegionType.getVal());
        if(CollectionUtils.isEmpty(list)){
            list=this.selectArticlesByArticleRegionTypeInDb(articleRegionType);
            EhCacheUtils.putSysInfo("ArticleRegionType_", articleRegionType.getVal(), list);
        }
        //selectCategory(list);
        return list;
    }
 
    @Override
    public List<Article> selectArticlesRegionNotNull(Article article) {
        List<Article> list=articleMapper.selectArticlesRegionNotNull(article);
        selectCategory(list);
        return list;
    }
 
    @Override
    public List<Article> selectArticlesRegionIsNull(Article article) {
        List<Article> list=articleMapper.selectArticlesRegionIsNull(article);
        //selectCategory(list);
        return list;
    }
 
    @Override
    public int upVote(String id) {
        return articleMapper.upVote(id);
    }
 
    @Override
    public int articleLook(String id) {
        return articleMapper.articleLook(id);
    }
 
    private List<Article> selectArticlesByArticleRegionTypeInDb(ArticleRegionType articleRegionType) {
        Article queryForm=new Article();
        queryForm.setArticleRegion(articleRegionType.getVal());
        List<Article> list = this.selectArticleList(queryForm);
        selectCategory(list);
        return list;
    }
    private void selectCategory(List<Article> list){
        list.forEach(a->{
            String cid = a.getCategoryId();
            Category category=categoryCache.get(cid);
            if(category==null){
                category=categoryService.selectCategoryById(Long.valueOf(cid));
                if(category!=null){
                    categoryCache.put(cid,category);
                }
            }
            if(category!=null){
                a.setCategory(category);
            }
        });
    }
    private void selectCategory(Article a){
 
            String cid = a.getCategoryId();
            Category category=categoryCache.get(cid);
            if(category==null){
                category=categoryService.selectCategoryById(Long.valueOf(cid));
                if(category!=null){
                    categoryCache.put(cid,category);
                }
            }
            if(category!=null){
                a.setCategory(category);
            }
    }
    private void selectTags(List<Article> articles){
 
        //转换标签名称,这部分可以使用缓存提升性能
        articles.forEach(a->{
            String tagsStr=a.getTags();
            if(StringUtils.isNotEmpty(tagsStr)){
                String[] tagsArr=Convert.toStrArray(tagsStr);
                String tags_name="";
                List<Tags> tags= Lists.newArrayList();
                for(String id:tagsArr){
                    if(StringUtils.isNotEmpty(id)){
 
                        Tags tag=tagCache.get(id);
                        if(tag==null){
                            tag=tagsMapper.selectTagsById(Long.valueOf(id));
                            tagCache.put(id,tag);
                        }
                        tags.add(tag);
                        if(tag!=null){
                            tags_name+=tag.getTagName()+",";
                        }
                    }
                }
                if(tags_name.endsWith(",")){
                    tags_name=StringUtils.substring(tags_name,0,tags_name.length()-1);
                }
                a.setTags_name(tags_name);
                a.setTagList(tags);
            }
        });
 
 
 
    }
    private void selectTags(Article a){
        String tids = a.getTags();//tagIds
        if(StringUtils.isEmpty(tids)){
            return;
        }
        String[] arr=Convert.toStrArray(tids);
        List<Tags> tags= Lists.newArrayList();
        for(String tid:arr){
            if (StringUtils.isEmpty(tid)) {
                continue;
            }
            Tags tag=tagCache.get(tid);
            if(tag==null){
                tag=tagsMapper.selectTagsById(Long.valueOf(tid));
                tagCache.put(tid,tag);
            }
            tags.add(tag);
        }
        a.setTagList(tags);
    }
 
    @Override
    public List<Article> selectArticleListByAll(Article article) {
        List<Article> articles=articleMapper.selecArticleListByAll(article);
        selectTags(articles);
        selectCategory(articles);
        return articles;
    }
 
    @Override
    public List<Article> selectArticleCount() {
        // TODO Auto-generated method stub
        return articleMapper.selectArticleCount();
    }
}