inleft
2022-08-31 aae48957ba971ed0a1bfbfa182701cd22bbe8694
commit | author | age
9bcb19 1 /*
I 2 Copyright [2020] [https://www.xiaonuo.vip]
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8   http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15
16 Snowy采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点:
17
18 1.请不要删除和修改根目录下的LICENSE文件。
19 2.请不要删除和修改Snowy源码头部的版权声明。
20 3.请保留源码和相关描述文件的项目出处,作者声明等。
21 4.分发源码时候,请注明软件出处 https://gitee.com/xiaonuobase/snowy
22 5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/xiaonuobase/snowy
23 6.若您的项目无法满足以上几点,可申请商业授权,获取Snowy商业授权许可,请在官网购买授权,地址为 https://www.xiaonuo.vip
24  */
25 package vip.xiaonuo.modular.blogarticle.service.impl;
26
27 import cn.hutool.core.bean.BeanUtil;
20e5fc 28 import cn.hutool.core.collection.CollUtil;
I 29 import cn.hutool.core.date.DateUtil;
9bcb19 30 import cn.hutool.core.util.ObjectUtil;
1e152b 31 import cn.hutool.core.util.PageUtil;
20e5fc 32 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
9bcb19 33 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
20e5fc 34 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
9bcb19 35 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
20e5fc 36 import org.springframework.scheduling.annotation.Async;
I 37 import org.springframework.scheduling.annotation.AsyncResult;
f1b087 38 import org.springframework.stereotype.Service;
I 39 import org.springframework.transaction.annotation.Transactional;
20e5fc 40 import vip.xiaonuo.core.consts.MyConstant;
9bcb19 41 import vip.xiaonuo.core.exception.ServiceException;
I 42 import vip.xiaonuo.core.factory.PageFactory;
43 import vip.xiaonuo.core.pojo.page.PageResult;
44 import vip.xiaonuo.core.util.PoiUtil;
a9c4c9 45 import vip.xiaonuo.modular.blogStatistics.vo.BlogArchiveDetailVo;
9bcb19 46 import vip.xiaonuo.modular.blogarticle.entity.BlogArticle;
20e5fc 47 import vip.xiaonuo.modular.blogarticle.entity.BlogArticleVo;
9bcb19 48 import vip.xiaonuo.modular.blogarticle.enums.BlogArticleExceptionEnum;
I 49 import vip.xiaonuo.modular.blogarticle.mapper.BlogArticleMapper;
50 import vip.xiaonuo.modular.blogarticle.param.BlogArticleParam;
20e5fc 51 import vip.xiaonuo.modular.blogarticle.param.BlogArticleQueryDto;
9bcb19 52 import vip.xiaonuo.modular.blogarticle.service.BlogArticleService;
f1b087 53
20e5fc 54 import java.util.ArrayList;
1e152b 55 import java.util.HashMap;
9bcb19 56 import java.util.List;
0613f2 57 import java.util.Map;
20e5fc 58 import java.util.concurrent.Future;
9bcb19 59
I 60 /**
f1b087 61  * blog文章service接口实现类
9bcb19 62  *
I 63  * @author inleft
f1b087 64  * @date 2022-02-09 18:20:46
9bcb19 65  */
I 66 @Service
67 public class BlogArticleServiceImpl extends ServiceImpl<BlogArticleMapper, BlogArticle> implements BlogArticleService {
68
69     @Override
70     public PageResult<BlogArticle> page(BlogArticleParam blogArticleParam) {
71         QueryWrapper<BlogArticle> queryWrapper = new QueryWrapper<>();
72         if (ObjectUtil.isNotNull(blogArticleParam)) {
73
74             // 根据文章标题 查询
75             if (ObjectUtil.isNotEmpty(blogArticleParam.getTitle())) {
f1b087 76                 queryWrapper.lambda().like(BlogArticle::getTitle, blogArticleParam.getTitle());
9bcb19 77             }
f1b087 78             // 根据文件类型 查询
9bcb19 79             if (ObjectUtil.isNotEmpty(blogArticleParam.getArticleFileType())) {
I 80                 queryWrapper.lambda().eq(BlogArticle::getArticleFileType, blogArticleParam.getArticleFileType());
81             }
f1b087 82             // 根据文章分类 查询
9bcb19 83             if (ObjectUtil.isNotEmpty(blogArticleParam.getArticleTypeId())) {
I 84                 queryWrapper.lambda().eq(BlogArticle::getArticleTypeId, blogArticleParam.getArticleTypeId());
85             }
86             // 根据文章引言 查询
87             if (ObjectUtil.isNotEmpty(blogArticleParam.getIntroduce())) {
f1b087 88                 queryWrapper.lambda().like(BlogArticle::getIntroduce, blogArticleParam.getIntroduce());
9bcb19 89             }
I 90             // 根据发布时间 查询
91             if (ObjectUtil.isNotEmpty(blogArticleParam.getPublishDate())) {
f1b087 92                 queryWrapper.lambda().ge(BlogArticle::getPublishDate, blogArticleParam.getPublishDate());
9bcb19 93             }
f1b087 94             // 根据是否置顶 查询
9bcb19 95             if (ObjectUtil.isNotEmpty(blogArticleParam.getIsTop())) {
I 96                 queryWrapper.lambda().eq(BlogArticle::getIsTop, blogArticleParam.getIsTop());
97             }
f1b087 98             // 根据公开状态 查询
9bcb19 99             if (ObjectUtil.isNotEmpty(blogArticleParam.getAuthStatus())) {
I 100                 queryWrapper.lambda().eq(BlogArticle::getAuthStatus, blogArticleParam.getAuthStatus());
101             }
f1b087 102             // 根据编辑状态 查询
9bcb19 103             if (ObjectUtil.isNotEmpty(blogArticleParam.getEditorStatus())) {
I 104                 queryWrapper.lambda().eq(BlogArticle::getEditorStatus, blogArticleParam.getEditorStatus());
105             }
106             // 根据创建时间 查询
107             if (ObjectUtil.isNotEmpty(blogArticleParam.getCreateDate())) {
f1b087 108                 queryWrapper.lambda().ge(BlogArticle::getCreateDate, blogArticleParam.getCreateDate());
9bcb19 109             }
I 110         }
111         return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
112     }
113
114     @Override
115     public List<BlogArticle> list(BlogArticleParam blogArticleParam) {
116         return this.list();
117     }
118
119     @Override
120     public void add(BlogArticleParam blogArticleParam) {
121         BlogArticle blogArticle = new BlogArticle();
122         BeanUtil.copyProperties(blogArticleParam, blogArticle);
123         this.save(blogArticle);
124     }
125
126     @Transactional(rollbackFor = Exception.class)
127     @Override
128     public void delete(List<BlogArticleParam> blogArticleParamList) {
129         blogArticleParamList.forEach(blogArticleParam -> {
130             this.removeById(blogArticleParam.getId());
131         });
132     }
133
134     @Transactional(rollbackFor = Exception.class)
135     @Override
136     public void edit(BlogArticleParam blogArticleParam) {
137         BlogArticle blogArticle = this.queryBlogArticle(blogArticleParam);
138         BeanUtil.copyProperties(blogArticleParam, blogArticle);
139         this.updateById(blogArticle);
140     }
141
142     @Override
143     public BlogArticle detail(BlogArticleParam blogArticleParam) {
144         return this.queryBlogArticle(blogArticleParam);
145     }
146
147     /**
f1b087 148      * 获取blog文章
9bcb19 149      *
I 150      * @author inleft
f1b087 151      * @date 2022-02-09 18:20:46
9bcb19 152      */
I 153     private BlogArticle queryBlogArticle(BlogArticleParam blogArticleParam) {
154         BlogArticle blogArticle = this.getById(blogArticleParam.getId());
155         if (ObjectUtil.isNull(blogArticle)) {
156             throw new ServiceException(BlogArticleExceptionEnum.NOT_EXIST);
157         }
158         return blogArticle;
159     }
160
161     @Override
162     public void export(BlogArticleParam blogArticleParam) {
163         List<BlogArticle> list = this.list(blogArticleParam);
164         PoiUtil.exportExcelWithStream("SnowyBlogArticle.xls", BlogArticle.class, list);
165     }
166
0613f2 167
I 168     @Override
a9c4c9 169     public List searchList(Map<String, Object> param) {
I 170         return this.baseMapper.searchList(param);
0613f2 171     }
I 172
173     @Override
a9c4c9 174     public long searchListCount(Map<String, Object> param) {
I 175         return this.baseMapper.searchListCount(param);
176     }
177
178     @Override
179     public List<BlogArchiveDetailVo> searchMonthCount(Integer separateYear) {
180         return this.baseMapper.searchMonthCount(separateYear);
181
0613f2 182     }
20e5fc 183
I 184     /**
185      * 异步获取上下相邻日志id
186      */
187     @Async
188     @Override
189     public Future<List<BlogArticleVo>> getAdjoiningRecord(BlogArticleQueryDto queryDto) {
1e152b 190         int pageNo = 0;
I 191         int pageSize = 100;
192         Map<String, Object> param = new HashMap<>(16);
193         param.put("pageNo", PageUtil.getStart(pageNo, pageSize));
194         param.put("pageSize", pageSize);
195         param.put("typeId", queryDto.getTypeId());
196         param.put("fileType", queryDto.getFileType());
20e5fc 197
1e152b 198         List<BlogArticleVo> result = new ArrayList(2) {{
I 199             add(null);
200             add(null);
201         }};
20e5fc 202
1e152b 203         List<BlogArticleVo> tempRecordList;
20e5fc 204         while (true) {
1e152b 205             tempRecordList = this.baseMapper.getAdjoiningRecord(param);
20e5fc 206             if (CollUtil.isEmpty(tempRecordList)) {
I 207                 break;
208             }
209
210             for (int i = 0; i < tempRecordList.size(); i++) {
1e152b 211                 if (queryDto.getId().equals(tempRecordList.get(i).getId())) {
20e5fc 212                     //防止边界
1e152b 213                     if (i - 1 >= 0) {
I 214                         result.set(0, tempRecordList.get(i - 1));
215                     }
216                     if (i + 1 < tempRecordList.size()) {
217                         result.set(1, tempRecordList.get(i + 1));
218                     } else {
219                         //记录恰好在队尾
220                         param.put("pageNo", PageUtil.getStart(++pageNo, pageSize));
221                         tempRecordList = this.baseMapper.getAdjoiningRecord(param);
222                         if (CollUtil.isNotEmpty(tempRecordList)) {
223                             result.set(1, tempRecordList.get(0));
224                         }
225                     }
226                     return new AsyncResult<>(result);
20e5fc 227                 }
I 228
1e152b 229                 //记录此页最后一条作为下一页起始记录(记录恰好在队头)
I 230                 result.set(0, tempRecordList.get(i));
20e5fc 231             }
I 232
1e152b 233             //下一页
I 234             param.put("pageNo", PageUtil.getStart(++pageNo, pageSize));
20e5fc 235         }
I 236
237
238         return new AsyncResult<>(result);
239     }
dc050f 240
I 241     @Override
242     public List<BlogArticleVo> searchPlatformList(Map<String, Object> param) {
243         return this.baseMapper.searchPlatformList(param);
244     }
245
246     @Override
247     public long searchPlatformListCount(Map<String, Object> param) {
248         return this.baseMapper.searchPlatformListCount(param);
249     }
9bcb19 250 }