|
|
@@ -0,0 +1,216 @@
|
|
|
+package org.dromara.business.service.impl;
|
|
|
+
|
|
|
+import org.dromara.business.domain.NewsInfo;
|
|
|
+import org.dromara.business.domain.bo.NewsInfo2Bo;
|
|
|
+import org.dromara.business.domain.bo.NewsInfoBo;
|
|
|
+import org.dromara.business.domain.vo.NewsCategoryVo;
|
|
|
+import org.dromara.business.domain.vo.NewsInfoVo;
|
|
|
+import org.dromara.business.mapper.NewsInfoMapper;
|
|
|
+import org.dromara.business.service.INewsCategoryService;
|
|
|
+import org.dromara.business.service.INewsInfoService;
|
|
|
+import org.dromara.common.core.domain.dto.DictTypeDTO;
|
|
|
+import org.dromara.common.core.service.DictService;
|
|
|
+import org.dromara.common.core.utils.MapstructUtils;
|
|
|
+import org.dromara.common.core.utils.StringUtils;
|
|
|
+import org.dromara.common.mybatis.core.page.TableDataInfo;
|
|
|
+import org.dromara.common.mybatis.core.page.PageQuery;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Collection;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 新闻信息Service业务层处理
|
|
|
+ *
|
|
|
+ * @author Lion Li
|
|
|
+ * @date 2025-09-15
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@RequiredArgsConstructor
|
|
|
+@Service
|
|
|
+public class NewsInfoServiceImpl implements INewsInfoService {
|
|
|
+
|
|
|
+ private final NewsInfoMapper baseMapper;
|
|
|
+
|
|
|
+ private final INewsCategoryService newsCategoryService;
|
|
|
+
|
|
|
+ private final DictService dictService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询新闻信息
|
|
|
+ *
|
|
|
+ * @param id 主键
|
|
|
+ * @return 新闻信息
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public NewsInfoVo queryById(Long id){
|
|
|
+ return baseMapper.selectNewsInfoById(id);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 分页查询新闻信息列表
|
|
|
+ *
|
|
|
+ * @param bo 查询条件
|
|
|
+ * @param pageQuery 分页参数
|
|
|
+ * @return 新闻信息分页列表
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public TableDataInfo<NewsInfoVo> queryPageList(NewsInfoBo bo, PageQuery pageQuery) {
|
|
|
+ LambdaQueryWrapper<NewsInfo> lqw = buildQueryWrapper(bo);
|
|
|
+ Page<NewsInfoVo> result = baseMapper.selectNewsInfoListPage(pageQuery.build(), lqw);
|
|
|
+ List<NewsInfoVo> resultRecords = result.getRecords();
|
|
|
+ for (NewsInfoVo resultRecord : resultRecords) {
|
|
|
+ NewsCategoryVo newsCategoryVo = newsCategoryService.queryById(resultRecord.getCategoryId());
|
|
|
+ if(newsCategoryVo!=null){
|
|
|
+ String newsCategoryVoName = newsCategoryVo.getName();
|
|
|
+ resultRecord.setCategoryName(newsCategoryVoName);
|
|
|
+ }
|
|
|
+ String newsTypeText = dictService.getDictLabel("news_type", resultRecord.getNewsType());
|
|
|
+ resultRecord.setNewsTypeText(newsTypeText);
|
|
|
+
|
|
|
+
|
|
|
+ String positionText= dictService.getDictLabel("new_config_place", resultRecord.getPositionCode());
|
|
|
+ resultRecord.setPositionText(positionText);
|
|
|
+
|
|
|
+ String statusText= dictService.getDictLabel("news_status", resultRecord.getStatus());
|
|
|
+ resultRecord.setStatusText(statusText);
|
|
|
+
|
|
|
+ String contentText=replaceHtml(resultRecord.getContent());
|
|
|
+ resultRecord.setContentText(contentText);
|
|
|
+ }
|
|
|
+ return TableDataInfo.build(result);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询符合条件的新闻信息列表
|
|
|
+ *
|
|
|
+ * @param bo 查询条件
|
|
|
+ * @return 新闻信息列表
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public List<NewsInfoVo> queryList(NewsInfoBo bo) {
|
|
|
+ LambdaQueryWrapper<NewsInfo> lqw = buildQueryWrapper(bo);
|
|
|
+ return baseMapper.selectNewsInfoList(lqw);
|
|
|
+ }
|
|
|
+
|
|
|
+ private LambdaQueryWrapper<NewsInfo> buildQueryWrapper(NewsInfoBo bo) {
|
|
|
+ Map<String, Object> params = bo.getParams();
|
|
|
+ LambdaQueryWrapper<NewsInfo> lqw = Wrappers.lambdaQuery();
|
|
|
+ lqw.orderByAsc(NewsInfo::getId);
|
|
|
+ lqw.eq(StringUtils.isNotBlank(bo.getTitle()), NewsInfo::getTitle, bo.getTitle());
|
|
|
+ lqw.eq(StringUtils.isNotBlank(bo.getSummary()), NewsInfo::getSummary, bo.getSummary());
|
|
|
+ lqw.eq(bo.getCategoryId() != null, NewsInfo::getCategoryId, bo.getCategoryId());
|
|
|
+ lqw.eq(StringUtils.isNotBlank(bo.getContent()), NewsInfo::getContent, bo.getContent());
|
|
|
+ lqw.eq(StringUtils.isNotBlank(bo.getImageUrl()), NewsInfo::getImageUrl, bo.getImageUrl());
|
|
|
+ lqw.eq(StringUtils.isNotBlank(bo.getExternalLink()), NewsInfo::getExternalLink, bo.getExternalLink());
|
|
|
+ lqw.eq(bo.getIsDefault() != null, NewsInfo::getIsDefault, bo.getIsDefault());
|
|
|
+ lqw.eq(StringUtils.isNotBlank(bo.getNewsType()), NewsInfo::getNewsType, bo.getNewsType());
|
|
|
+ lqw.eq(StringUtils.isNotBlank(bo.getPositionCode()), NewsInfo::getPositionCode, bo.getPositionCode());
|
|
|
+ lqw.eq(StringUtils.isNotBlank(bo.getStatus()), NewsInfo::getStatus, bo.getStatus());
|
|
|
+ lqw.eq(bo.getCreatedAt() != null, NewsInfo::getCreatedAt, bo.getCreatedAt());
|
|
|
+ lqw.eq(bo.getUpdatedAt() != null, NewsInfo::getUpdatedAt, bo.getUpdatedAt());
|
|
|
+ return lqw;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 新增新闻信息
|
|
|
+ *
|
|
|
+ * @param bo 新闻信息
|
|
|
+ * @return 是否新增成功
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public Boolean insertByBo(NewsInfoBo bo) {
|
|
|
+ NewsInfo add = MapstructUtils.convert(bo, NewsInfo.class);
|
|
|
+ validEntityBeforeSave(add);
|
|
|
+ if(bo.getIsDefault()==1L){
|
|
|
+ if(StringUtils.isEmpty(bo.getPositionCode())){
|
|
|
+ throw new RuntimeException("默认新闻请选择配置位置!");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //如果之前有主和副的 都进行覆盖 把之前的变为null
|
|
|
+ Map<String, Object> map=new HashMap<>();
|
|
|
+ map.put("positionCode",bo.getPositionCode());
|
|
|
+ NewsInfoVo newsInfoListByParam = baseMapper.selectNewsInfoListByParam(map);
|
|
|
+ if(newsInfoListByParam!=null){
|
|
|
+ baseMapper.updateDefaultNews(bo.getPositionCode());
|
|
|
+ }
|
|
|
+ boolean flag = baseMapper.insertNewsInfo(add) > 0;
|
|
|
+ if (flag) {
|
|
|
+ bo.setId(add.getId());
|
|
|
+ }
|
|
|
+ return flag;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 修改新闻信息
|
|
|
+ *
|
|
|
+ * @param bo 新闻信息
|
|
|
+ * @return 是否修改成功
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public Boolean updateByBo(NewsInfoBo bo) {
|
|
|
+ NewsInfo update = MapstructUtils.convert(bo, NewsInfo.class);
|
|
|
+ validEntityBeforeSave(update);
|
|
|
+ if(bo.getIsDefault()==1L){
|
|
|
+ if(StringUtils.isEmpty(bo.getPositionCode())){
|
|
|
+ throw new RuntimeException("默认新闻请选择配置位置!");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Map<String, Object> map=new HashMap<>();
|
|
|
+ map.put("positionCode",bo.getPositionCode());
|
|
|
+ NewsInfoVo newsInfoListByParam = baseMapper.selectNewsInfoListByParam(map);
|
|
|
+ if(newsInfoListByParam!=null){
|
|
|
+ baseMapper.updateDefaultNews(bo.getPositionCode());
|
|
|
+ }
|
|
|
+ return baseMapper.updateNewsInfoById(update) > 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 保存前的数据校验
|
|
|
+ */
|
|
|
+ private void validEntityBeforeSave(NewsInfo entity){
|
|
|
+ //TODO 做一些数据校验,如唯一约束
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 校验并批量删除新闻信息信息
|
|
|
+ *
|
|
|
+ * @param ids 待删除的主键集合
|
|
|
+ * @param isValid 是否进行有效性校验
|
|
|
+ * @return 是否删除成功
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
|
|
|
+ if(isValid){
|
|
|
+ //TODO 做一些业务上的校验,判断是否需要校验
|
|
|
+ }
|
|
|
+ return baseMapper.deleteNewsInfoById(ids) > 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Boolean publishNews(NewsInfo2Bo bo) {
|
|
|
+ NewsInfo update = MapstructUtils.convert(bo, NewsInfo.class);
|
|
|
+ validEntityBeforeSave(update);
|
|
|
+ update.setStatus("published");
|
|
|
+ return baseMapper.publishNews(update) > 0;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static String replaceHtml(String html) {
|
|
|
+ if (html == null || html.isEmpty()) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ // 正则表达式移除所有的HTML标签
|
|
|
+ return html.replaceAll("<[^>]*>", "");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|