xiangpei
1 天以前 4f89ece90fca89b667a244376605f9190a234b80
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
package cn.lili.modules.lmk.service.impl;
 
 
import cn.lili.base.Result;
import cn.lili.common.enums.ActivityCoverTypeEnum;
import cn.lili.modules.lmk.domain.entity.Activity;
import cn.lili.modules.lmk.domain.entity.MySubscribe;
import cn.lili.modules.lmk.domain.entity.News;
import cn.lili.modules.lmk.domain.form.ActivityForm;
import cn.lili.modules.lmk.domain.form.NewsForm;
import cn.lili.modules.lmk.domain.query.NewsQuery;
import cn.lili.modules.lmk.domain.vo.ActivityVO;
import cn.lili.modules.lmk.domain.vo.NewsVO;
import cn.lili.modules.lmk.mapper.NewsMapper;
import cn.lili.modules.lmk.service.NewsService;
import cn.lili.utils.PageUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
 
import java.util.*;
import java.util.function.Function;
 
/**
 * lmk-shop-java
 * 新闻服务实现类
 *
 * @author : zxl
 * @date : 2025-06-20 14:55
 **/
@Service
@RequiredArgsConstructor
public class NewsServiceImpl extends ServiceImpl<NewsMapper, News> implements NewsService {
 
    private final NewsMapper newsMapper;
    @Override
    public Result add(NewsForm form) {
        News entity = NewsForm.getEntityByForm(form, null);
        baseMapper.insert(entity);
        return Result.ok("添加成功");
    }
 
    @Override
    public Result update(NewsForm form) {
        News entity = baseMapper.selectById(form.getId());
 
        Assert.notNull(entity, "记录不存在");
        BeanUtils.copyProperties(form, entity);
 
        baseMapper.updateById(entity);
 
 
        return Result.ok("修改成功");
    }
 
    @Override
    public Result remove(List<String> ids) {
        baseMapper.deleteBatchIds(ids);
        return Result.ok("删除成功");
    }
 
    @Override
    public Result removeById(String id) {
        //判断是否发布发布则提示先下架,再删除
        News entity = baseMapper.selectById(id);
        if(entity.getPublish()){
            throw new RuntimeException("该新闻已发布");
        }
 
        baseMapper.deleteById(id);
        return Result.ok("删除成功");
    }
 
    @Override
    public Result page(NewsQuery query) {
        IPage<NewsVO> page = PageUtil.getPage(query, NewsVO.class);
 
        baseMapper.getPage(page, query);
 
        return Result.ok().data(page.getRecords()).total(page.getTotal());
    }
 
    @Override
    public Result detail(String id) {
        NewsVO vo = baseMapper.getById(id);
        Assert.notNull(vo, "记录不存在");
        return Result.ok().data(vo);
    }
 
    @Override
    public Result publish(String id) {
        News entity = baseMapper.selectById(id);
        if (!entity.getPublish()) {
            entity.setPublishDate(new Date());
        }else {
            entity.setPublishDate(null);
        }
        entity.setPublish(!entity.getPublish());
        baseMapper.updateById(entity);
        return Result.ok("成功");
    }
 
 
}