liulin
2025-07-01 c311cb1a9ecad57ac15cca5ea4581f0aa2574762
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package com.lunhan.water.service;
 
import com.lunhan.water.common.ExecutedResult;
import com.lunhan.water.common.PagerResult;
import com.lunhan.water.common.jwt.LoginUserDTO;
import com.lunhan.water.common.model.Tuple;
import com.lunhan.water.common.util.*;
import com.lunhan.water.entity.enums.EState;
import com.lunhan.water.entity.request.ReqSetSort;
import com.lunhan.water.entity.request.notice.ReqCreateNotice;
import com.lunhan.water.entity.request.notice.ReqModifyNotice;
import com.lunhan.water.entity.search.SearchNotice;
import com.lunhan.water.repository.impl.AdminUserMapperImpl;
import com.lunhan.water.repository.impl.NoticeMapperImpl;
import com.lunhan.water.repository.po.NoticePO;
import com.lunhan.water.repository.vo.NoticeVO;
import com.lunhan.water.service.dto.NoticeDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
 
/**
 * 公告相关服务
 *
 * @author lin.liu
 * @date 2023/02/26
 */
@Service
public class NoticeService extends BaseService {
    @Autowired
    private NoticeMapperImpl dao;
    @Autowired
    private AdminUserMapperImpl adminUserDao;
 
    public ExecutedResult<Long> create(ReqCreateNotice request, LoginUserDTO user) {
        Long now = LocalDateTimeUtil.nowTimeStamp();
 
        // 转换po
        NoticePO item = CopierUtil.mapTo(request, NoticePO.class);
        // 设置发布人
        item.setPublishUser(user.getNickName());
        // 截取摘要
        item.setSummary(request.getContent().substring(0, 80));
        // 设置记录创建时间
        item.setCreateTime(now);
        item.setStatus(EState.NORMAL.getValue());
        item.setSort(NumericUtil.tryParseInt(request.getSort(), 0));
 
        int rowCount = dao.insert(item);
        if (rowCount == 1) {
            return ExecutedResult.success(item.getId());
        }
        return ExecutedResult.failed("创建[公告]失败");
    }
 
    public ExecutedResult<String> modify(ReqModifyNotice request) {
        // 验证记录是否存在
        ExecutedResult<NoticePO> checkExists = this.check4Id(request.getId());
        if (checkExists.isFailed()) {
            return ExecutedResult.failed(checkExists.getMsg());
        }
 
        // 转换po
        NoticePO item = CopierUtil.mapTo(request, NoticePO.class);
        // 截取摘要
        item.setSummary(request.getContent().substring(0, 80));
        item.setStatus(NumericUtil.tryParseInt(request.getStatus(), EState.NORMAL.getValue()));
        item.setSort(NumericUtil.tryParseInt(request.getSort(), 0));
 
        int rowCount = this.dao.updateById(item);
        if (rowCount == 1) {
            return ExecutedResult.failed("编辑[公告]失败。");
        }
        return ExecutedResult.success();
    }
 
    public ExecutedResult<NoticeDetail> get(Long id) {
        NoticeDetail result = new NoticeDetail();
 
        NoticePO find = dao.get(id);
        if (null != find) {
            result = CopierUtil.mapTo(find, NoticeDetail.class);
        }
        return ExecutedResult.success(result);
    }
 
    public ExecutedResult<String> changeStatus(Long id, EState state) {
        // 验证记录是否存在
        ExecutedResult<NoticePO> checkExists = this.check4Id(id);
        if (checkExists.isFailed()) {
            return ExecutedResult.failed(checkExists.getMsg());
        }
        NoticePO item = new NoticePO();
        item.setId(id);
        item.setStatus(state.getValue());
 
        int rowCount = this.dao.updateById(item);
        if (rowCount == 1) {
            return ExecutedResult.failed("[公告]状态修改失败。");
        }
        return ExecutedResult.success();
    }
 
    public ExecutedResult<String> setSort(ReqSetSort request) {
        // 验证记录是否存在
        ExecutedResult<NoticePO> checkExists = this.check4Id(request.getId());
        if (checkExists.isFailed()) {
            return ExecutedResult.failed(checkExists.getMsg());
        }
        NoticePO item = new NoticePO();
        item.setId(request.getId());
        item.setSort(request.getSort());
 
        int rowCount = this.dao.updateById(item);
        if (rowCount == 1) {
            return ExecutedResult.failed("[公告]排序值设置失败。");
        }
        return ExecutedResult.success();
    }
 
    public ExecutedResult<List<NoticeVO>> getList(List<Long> listId) {
        List<NoticeVO> result = new ArrayList<>();
 
        List<NoticePO> list = this.dao.getList(listId);
        if (ListUtil.isNotNullOrEmpty(list)) {
            result = CopierUtil.mapTo(list, NoticeVO.class);
        }
        return ExecutedResult.success(result);
    }
 
    public ExecutedResult<PagerResult<NoticeVO>> search(SearchNotice search) {
        // 处理创建时间范围-查询参数
        Tuple<String, String> createTimeRange = ParameterUtil.getTimeRange(search.getCreateTimeRange());
        if (StringUtil.isNotNullOrEmpty(createTimeRange.getItem1())) {
            search.setCreateTimeStart(LocalDateTimeUtil.getTimeStamp(createTimeRange.getItem1()).getTime());
        }
        if (StringUtil.isNotNullOrEmpty(createTimeRange.getItem2())) {
            search.setCreateTimeEnd(LocalDateTimeUtil.getTimeStamp(createTimeRange.getItem2()).getTime());
        }
        if (NumericUtil.tryParseInt(search.getStatus(), 0).compareTo(0) < 1 && ListUtil.isNullOrEmpty(search.getListStatus())) {
            search.setStatus(EState.NORMAL.getValue());
        }
 
 
        PagerResult<NoticePO> pageList = dao.search(search);
        List<NoticeVO> listVo = new ArrayList<>();
        List<NoticePO> list = pageList.getList();
        if (ListUtil.isNotNullOrEmpty(list)) {
            pageList.setLastId(list.get(list.size() - 1).getId());
            // 转换vo
            listVo = CopierUtil.mapTo(list, NoticeVO.class);
        }
        PagerResult<NoticeVO> result = new PagerResult<>(pageList.getLimit(), pageList.getPage(), pageList.getTotal(), listVo);
        result.setLastId(pageList.getLastId());
        return ExecutedResult.success(result);
    }
 
    protected ExecutedResult<NoticePO> check4Id(Long id) {
        NoticePO exists = dao.get(id);
        if (Objects.isNull(exists)) {
            return ExecutedResult.failed("[公告]不存在:" + id);
        }
        return ExecutedResult.success(exists);
    }
}