|
|
@@ -0,0 +1,111 @@
|
|
|
+package io.renren.modules.qyh.service.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.map.MapUtil;
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import io.renren.common.enums.IsDeleteEnum;
|
|
|
+import io.renren.common.exception.RRException;
|
|
|
+import io.renren.common.utils.PageUtils;
|
|
|
+import io.renren.common.utils.Query;
|
|
|
+import io.renren.modules.qmjz.utils.BeanCopyUtils;
|
|
|
+import io.renren.modules.qyh.entity.NewsEntity;
|
|
|
+import io.renren.modules.qyh.mapper.NewsMapper;
|
|
|
+import io.renren.modules.qyh.model.dto.NewsDTO;
|
|
|
+import io.renren.modules.qyh.service.ExpertStoreService;
|
|
|
+import io.renren.modules.qyh.service.NewsService;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+@RequiredArgsConstructor
|
|
|
+@Service("newsService")
|
|
|
+public class NewsServiceImpl extends ServiceImpl<NewsMapper, NewsEntity> implements NewsService {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public PageUtils queryPage(Map<String, Object> params) {
|
|
|
+ Integer status = MapUtil.getInt(params, "status");
|
|
|
+ String title = MapUtil.getStr(params, "title");
|
|
|
+ IPage<NewsEntity> page = this.page(new Query<NewsEntity>().getPage(params),
|
|
|
+ new LambdaQueryWrapper<NewsEntity>()
|
|
|
+ .eq(ObjectUtil.isNotNull(status), NewsEntity::getStatus, status)
|
|
|
+ .like(StrUtil.isNotBlank(title), NewsEntity::getTitle, title)
|
|
|
+ .eq(NewsEntity::getIsDelete, IsDeleteEnum.NORMAL.value())
|
|
|
+ .orderByDesc(NewsEntity::getCreateTime));
|
|
|
+ return new PageUtils(page);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public void saveNews(NewsDTO dto) {
|
|
|
+ validateTitleExists(dto);
|
|
|
+
|
|
|
+ NewsEntity entity = new NewsEntity();
|
|
|
+ BeanCopyUtils.copyPropertiesIgnoreNull(dto, entity);
|
|
|
+ entity.setCreateTime(LocalDateTime.now());
|
|
|
+ entity.setUpdateTime(LocalDateTime.now());
|
|
|
+ baseMapper.insert(entity);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public void updateNews(NewsDTO dto) {
|
|
|
+ NewsEntity entity = this.loadById(dto.getId(), true);
|
|
|
+ if (!StrUtil.equals(dto.getTitle(), entity.getTitle())) {
|
|
|
+ validateTitleExists(dto);
|
|
|
+ }
|
|
|
+ BeanCopyUtils.copyPropertiesIgnoreNull(dto, entity);
|
|
|
+ entity.setUpdateTime(LocalDateTime.now());
|
|
|
+ baseMapper.updateById(entity);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public void deleteNewsByIds(Long[] ids) {
|
|
|
+ for (Long id : ids) {
|
|
|
+ NewsEntity entity = this.loadById(id, true);
|
|
|
+ entity.setIsDelete(IsDeleteEnum.DELETE.value());
|
|
|
+ entity.setUpdateTime(LocalDateTime.now());
|
|
|
+ baseMapper.updateById(entity);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public NewsEntity loadById(Long id, Boolean tw) {
|
|
|
+ NewsEntity entity = baseMapper.selectOne(new LambdaQueryWrapper<NewsEntity>().eq(NewsEntity::getId, id)
|
|
|
+ .eq(NewsEntity::getIsDelete, IsDeleteEnum.NORMAL.value()));
|
|
|
+ if (entity == null) {
|
|
|
+ if (tw) {
|
|
|
+ throw new RRException("青创赛不存在");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return entity;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public NewsEntity getByTitle(String title) {
|
|
|
+ return baseMapper.selectOne(new LambdaQueryWrapper<NewsEntity>().eq(NewsEntity::getTitle, title)
|
|
|
+ .eq(NewsEntity::getIsDelete, IsDeleteEnum.NORMAL.value()));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<NewsEntity> getByIdList(List<Long> ids) {
|
|
|
+ return baseMapper.selectList(new LambdaQueryWrapper<NewsEntity>().in(NewsEntity::getId, ids)
|
|
|
+ .eq(NewsEntity::getIsDelete, IsDeleteEnum.NORMAL.value()));
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private void validateTitleExists(NewsDTO dto) {
|
|
|
+ NewsEntity target = this.getByTitle(dto.getTitle());
|
|
|
+ if (ObjectUtil.isNotNull(target) && StrUtil.equals(target.getTitle(), dto.getTitle())) {
|
|
|
+ throw new RRException("青云赛标题重复");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|