NewsDictServiceImpl.java
5.46 KB
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
package com.laowu.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.laowu.dto.label.AddNewsDTO;
import com.laowu.dto.label.NewsDTO;
import com.laowu.mapper.NewsDictMapper;
import com.laowu.mapper.NewsLabelsMapper;
import com.laowu.model.NewsDictDO;
import com.laowu.model.NewsLabelDictDO;
import com.laowu.model.NewsLabelsDO;
import com.laowu.service.NewsDictService;
import com.laowu.util.ConstantUtils;
import com.laowu.vo.lables.NewsVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
/**
* <p>
* 服务实现类
* </p>
*
* @author Tuyp
* @since 2023-05-16
*/
@Service
public class NewsDictServiceImpl extends ServiceImpl<NewsDictMapper, NewsDictDO> implements NewsDictService {
@Autowired
private NewsLabelsMapper newsLabelsMapper;
public IPage<NewsVO> news(NewsDTO newsDTO) {
Page pager = new Page(newsDTO.getPageNum(), newsDTO.getPageSize());
IPage<NewsVO> page = this.baseMapper.news(pager, newsDTO.getLabelId(), newsDTO.getTitle());
List<NewsVO> newsVOS = page.getRecords();
for (NewsVO newsVO : newsVOS) {
//标签
List<NewsLabelDictDO> newsLabelsDOS = this.baseMapper.newsLabels(newsVO.getId());
newsVO.setNewsLabelDictDOS(newsLabelsDOS);
}
return page;
}
@Transactional(rollbackFor = Exception.class)
public String top(NewsDictDO newsDictDO) {
this.baseMapper.updateTopStatus();
NewsDictDO newsDictDO1 = this.baseMapper.selectById(newsDictDO.getId());
newsDictDO1.setTopFlag(1);
this.baseMapper.updateById(newsDictDO1);
return ConstantUtils.SUCCESS_UPDATE;
}
@Transactional(rollbackFor = Exception.class)
public String deleteNews(NewsDictDO newsDictDO) {
this.baseMapper.deleteById(newsDictDO.getId());
newsLabelsMapper.delete(new QueryWrapper<NewsLabelsDO>()
.lambda()
.eq(NewsLabelsDO::getNewsId, newsDictDO.getId()));
return ConstantUtils.DELETE_SUCCESS;
}
@Transactional(rollbackFor = Exception.class)
public String addNews(AddNewsDTO addNewsDTO) {
NewsDictDO newsDictDO = new NewsDictDO();
BeanUtils.copyProperties(addNewsDTO, newsDictDO);
newsDictDO.setStatus("已隐藏");
newsDictDO.setTopFlag(0);
this.baseMapper.insert(newsDictDO);
List<Long> labelIds = addNewsDTO.getLabelIds();
if (labelIds.size() > 0) {
for (Long labelId : labelIds) {
NewsLabelsDO newsLabelsDO = new NewsLabelsDO();
newsLabelsDO.setLabelId(labelId);
newsLabelsDO.setNewsId(newsDictDO.getId());
newsLabelsMapper.insert(newsLabelsDO);
}
}
return ConstantUtils.ADD_SUCCESS;
}
public String updateNews(AddNewsDTO updateNewsDTO) {
NewsDictDO newsDictDO = new NewsDictDO();
BeanUtils.copyProperties(updateNewsDTO, newsDictDO);
this.baseMapper.updateById(newsDictDO);
if (null != updateNewsDTO.getLabelIds()) {
//找到已经有的work
List<Long> oldIds = newsLabelsMapper.labelIds(updateNewsDTO.getId());
List<Long> newIds = updateNewsDTO.getLabelIds();
List<Long> newIds2 = new ArrayList<Long>();
for (Long id : newIds) {
newIds2.add(id);
}
newIds.removeAll(oldIds); //新增的人在newIds里面
if (newIds.size() > 0) {
for (Long id : newIds) {
NewsLabelsDO newsLabelsDO = new NewsLabelsDO();
newsLabelsDO.setNewsId(updateNewsDTO.getId());
newsLabelsDO.setLabelId(id);
newsLabelsMapper.insert(newsLabelsDO);
}
}
oldIds.removeAll(newIds2);//删掉以前的
if (oldIds.size() > 0) {
for (Long id : oldIds) {
newsLabelsMapper.delete(new QueryWrapper<NewsLabelsDO>()
.lambda()
.eq(NewsLabelsDO::getNewsId, updateNewsDTO.getId())
.eq(NewsLabelsDO::getLabelId, id));
}
}
}
return ConstantUtils.SUCCESS_UPDATE;
}
public NewsVO oneNews(NewsDictDO newsDictDO) {
NewsVO newsVO = this.baseMapper.oneNews(newsDictDO.getId());
//标签
List<NewsLabelDictDO> newsLabelsDOS = this.baseMapper.newsLabels(newsVO.getId());
newsVO.setNewsLabelDictDOS(newsLabelsDOS);
return newsVO;
}
public IPage<NewsVO> noTopNews(NewsDTO newsDTO) {
Page pager = new Page(newsDTO.getPageNum(), newsDTO.getPageSize());
IPage<NewsVO> page = this.baseMapper.noTopNews(pager, newsDTO.getLabelId(), newsDTO.getTitle());
List<NewsVO> newsVOS = page.getRecords();
for (NewsVO newsVO : newsVOS) {
//标签
List<NewsLabelDictDO> newsLabelsDOS = this.baseMapper.newsLabels(newsVO.getId());
newsVO.setNewsLabelDictDOS(newsLabelsDOS);
}
return page;
}
}