NewsDictServiceImpl.java 5.46 KB
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;
    }


}