From 30d8e227e8d823b6c38c3b9c90ac2df03b63befe Mon Sep 17 00:00:00 2001 From: admin <weikou2014> Date: 星期二, 25 二月 2025 16:41:22 +0800 Subject: [PATCH] 淘宝转链接口更新 --- fanli/src/main/java/com/yeshi/fanli/service/impl/dynamic/ArticleOfficialServiceImpl.java | 455 +++++++++++++++++++++++++++++--------------------------- 1 files changed, 238 insertions(+), 217 deletions(-) diff --git a/fanli/src/main/java/com/yeshi/fanli/service/impl/dynamic/ArticleOfficialServiceImpl.java b/fanli/src/main/java/com/yeshi/fanli/service/impl/dynamic/ArticleOfficialServiceImpl.java index 526b475..4cd2f57 100644 --- a/fanli/src/main/java/com/yeshi/fanli/service/impl/dynamic/ArticleOfficialServiceImpl.java +++ b/fanli/src/main/java/com/yeshi/fanli/service/impl/dynamic/ArticleOfficialServiceImpl.java @@ -1,217 +1,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.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 { - // 鏂囦欢瑙f瀽 - InputStream inputStream = file.getInputStream(); - String contentType = file.getContentType(); - String type = contentType.substring(contentType.indexOf("/") + 1); - - // 鏂囦欢璺緞 - String filePath = "/img/article/" + 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 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); - } -} +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 { + // 鏂囦欢瑙f瀽 + 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); + } +} -- Gitblit v1.8.0