admin
2025-02-25 30d8e227e8d823b6c38c3b9c90ac2df03b63befe
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
package com.yeshi.fanli.service.impl.dynamic;
 
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;
 
import javax.annotation.Resource;
 
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.yeshi.utils.DateUtil;
import org.yeshi.utils.tencentcloud.COSManager;
 
import com.yeshi.fanli.dao.dynamic.ArticleOfficialDao;
import com.yeshi.fanli.entity.dynamic.ArticleOfficial;
import com.yeshi.fanli.exception.dynamic.ArticleOfficialException;
import com.yeshi.fanli.service.inter.dynamic.ArticleOfficialService;
import com.yeshi.fanli.util.FilePathEnum;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.vo.dynamic.ArticleVO;
 
@Service
public class ArticleOfficialServiceImpl implements ArticleOfficialService {
    
    @Resource
    private ArticleOfficialDao articleOfficialDao;
 
 
    @Override
    public void save(MultipartFile file, ArticleOfficial record) throws ArticleOfficialException {
        Integer state = record.getState();
        if (state == null) {
            state = 0;
        } 
        
        String tilte =record.getTitle();
        String content = record.getContent();
        if (StringUtil.isNullOrEmpty(tilte) || StringUtil.isNullOrEmpty(content)) {
            throw new ArticleOfficialException(1, "标题、内容不能为空");
        }
        
        Integer readNum = record.getReadNum();
        if (readNum == null) {
            throw new ArticleOfficialException(1, "请输入浏览数量(虚拟)");
        }
        
        String tags = record.getTags();
        if (!StringUtil.isNullOrEmpty(tags)) {
            String[] arrayTags = tags.trim().split("\\s+");
            String tagsColour = record.getTagsColour();
            if (!StringUtil.isNullOrEmpty(tagsColour)) {
                String[] arrayTagsColour = tagsColour.trim().split("\\s+");
                if (arrayTags.length != arrayTagsColour.length) {
                    throw new ArticleOfficialException(1, "标签与标签颜色数量不匹配");
                }
                record.setTagsColour(tagsColour.trim());
            }
            record.setTags(tags.trim());
        }
        
        String articlelink = record.getArticlelink();
        if (StringUtil.isNullOrEmpty(articlelink)) {
            throw new ArticleOfficialException(1, "文章链接不能为空");
        }
        
        
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            String startTime_str = record.getStartTimeChar();
            if (!StringUtil.isNullOrEmpty(startTime_str)) {
                startTime_str = startTime_str.replaceAll("T", " ");
                record.setStartTime(format.parse(startTime_str));
            }
            
            String endTime_str = record.getEndTimeChar();
            if (!StringUtil.isNullOrEmpty(endTime_str)) {
                endTime_str = endTime_str.replaceAll("T", " ");
                record.setEndTime(format.parse(endTime_str));
            }
        } catch (Exception e) {
            throw new ArticleOfficialException(1, "时间格式不正确");
        }
        
        Date startTime = record.getStartTime();
        if (startTime == null) {
            record.setStartTime(new Date());
        }
        
        Date endTime = record.getEndTime();
        if (endTime == null) {
            record.setEndTime(DateUtil.plusYears(new Date(), 100));
        }
        
        Boolean videoContain = record.getVideoContain();
        if (videoContain == null) {
            videoContain = false;
        }
        
        Double weight = record.getWeight();
        if (weight == null)
            record.setWeight(1.1);
        
        String picture = null;
        if (file != null) {
            try { // 图片上传
                picture = uploadPicture(file);
            } catch (Exception e) {
                throw new ArticleOfficialException(1, "图片上传失败");
            }
            
            if (!StringUtil.isNullOrEmpty(picture)) {
                record.setPicture(picture);
            }
        }
 
        
        if (StringUtil.isNullOrEmpty(record.getId())) {
            record.setId(UUID.randomUUID().toString().replace("-", ""));
        } else {
            ArticleOfficial resultObj = articleOfficialDao.getById(record.getId());
            if (resultObj == null)
                throw new ArticleOfficialException(1, "修改内容已不存在");
            
            if (!StringUtil.isNullOrEmpty(resultObj.getPicture())) {
                if (!StringUtil.isNullOrEmpty(picture)) {
                    COSManager.getInstance().deleteFile(resultObj.getPicture());
                } else {
                    record.setPicture(resultObj.getPicture());
                }
            }
        }
        articleOfficialDao.save(record);
    }
    
    /**
     * 上传图片
     * 
     * @param file
     * @return
     * @throws Exception
     */
    public String uploadPicture(MultipartFile file) throws Exception {
        // 文件解析
        InputStream inputStream = file.getInputStream();
        String contentType = file.getContentType();
        String type = contentType.substring(contentType.indexOf("/") + 1);
 
        // 文件路径
        String filePath = FilePathEnum.article.getPath() + UUID.randomUUID().toString().replace("-", "") + "." + type;
        // 执行上传
        String fileLink = COSManager.getInstance().uploadFile(inputStream, filePath).getUrl();
 
        return fileLink;
    }
 
    @Override
    public void deleteBatchByPrimaryKey(List<String> list) {
        if (list == null || list.size() == 0) {
            return;
        }
        
        for (String id: list) {
            articleOfficialDao.deleteById(id);
        }
    }
    
    
    @Override
    public void switchState(String id) throws ArticleOfficialException {
        if (StringUtil.isNullOrEmpty(id)) {
            throw new ArticleOfficialException(1, "请传递正确参数");
        }
        
        ArticleOfficial resultObj = articleOfficialDao.getById(id);
        if (resultObj == null) {
            throw new ArticleOfficialException(1, "此内容已不存在");
        }
        
        Integer state = resultObj.getState();
        if (state == null || state == 0) {
            state = 1;
        } else {
            state = 0;
        }
        articleOfficialDao.updateSatate(id, state);
    }
    
    @Override
    public ArticleOfficial getById(String id) {
        return articleOfficialDao.getById(id);
    }
    
    
    @Override
    public void updateReadNum(String id) {
        ArticleOfficial article = articleOfficialDao.getById(id);
        if (article != null) {
            Integer readNum = article.getReadNum();
            if (readNum == null) {
                readNum = 0;
            }
            article.setReadNum(readNum + 1);
            
            Integer readNumReal = article.getReadNumReal();
            if (readNumReal == null) {
                readNumReal = 0;
            }
            article.setReadNumReal(readNumReal + 1);
            
            articleOfficialDao.save(article);
        }
    }
    
    
    @Override
    public List<ArticleOfficial> query(int start, int count, String key,Integer state) {
        return articleOfficialDao.query(start, count, key, state);
    }
 
    @Override
    public long count(String key, Integer state) {
        return articleOfficialDao.count(key, state);
    }
 
    @Override
    @Cacheable(value = "dynamicCache", key = "'queryValid-'+#start+'-'+#key")
    public List<ArticleVO> queryValid(int start, int count, String key) {
        return articleOfficialDao.queryValid(start, count, key);
    }
    
    @Override
    public long countValid(String key) {
        return articleOfficialDao.countValid(key);
    }
}