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);
|
}
|
}
|