GovernmentInfoServiceImpl.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. package com.ruoyi.web.service.impl;
  2. import cn.hutool.core.bean.BeanUtil;
  3. import cn.hutool.core.util.ObjectUtil;
  4. import cn.hutool.core.util.StrUtil;
  5. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  6. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  7. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  8. import com.ruoyi.common.core.domain.entity.SysUser;
  9. import com.ruoyi.common.enums.BusinessType;
  10. import com.ruoyi.common.exception.ServiceException;
  11. import com.ruoyi.common.utils.SecurityUtils;
  12. import com.ruoyi.system.service.ISysUserService;
  13. import com.ruoyi.web.domain.dto.government.GovernmentInfoAddRequest;
  14. import com.ruoyi.web.domain.dto.government.GovernmentInfoEditRequest;
  15. import com.ruoyi.web.domain.dto.government.GovernmentInfoQueryRequest;
  16. import com.ruoyi.web.domain.entity.GovernmentInfo;
  17. import com.ruoyi.web.domain.enums.GovernmentInfoEnum;
  18. import com.ruoyi.web.domain.vo.GovernmentInfoVO;
  19. import com.ruoyi.web.mapper.GovernmentInfoMapper;
  20. import com.ruoyi.web.service.GovernmentInfoService;
  21. import org.springframework.beans.factory.annotation.Autowired;
  22. import org.springframework.stereotype.Service;
  23. import java.util.*;
  24. import java.util.stream.Collectors;
  25. /**
  26. *
  27. */
  28. @Service
  29. public class GovernmentInfoServiceImpl extends ServiceImpl<GovernmentInfoMapper, GovernmentInfo>
  30. implements GovernmentInfoService {
  31. @Autowired
  32. private ISysUserService userService;
  33. @Override
  34. public Integer addGovernmentInfo(GovernmentInfoAddRequest governmentInfoAddRequest) {
  35. if (governmentInfoAddRequest == null) {
  36. throw new ServiceException("请求参数为空");
  37. }
  38. try {
  39. // 转换为实体对象
  40. GovernmentInfo governmentInfo = new GovernmentInfo();
  41. BeanUtil.copyProperties(governmentInfoAddRequest, governmentInfo);
  42. //数据校验
  43. validGovernmentInfo(governmentInfo, BusinessType.INSERT);
  44. Integer status = governmentInfo.getStatus();
  45. if (status.equals(GovernmentInfoEnum.PUBLISHED.getValue())) {
  46. // 填充发布日期和发布人
  47. governmentInfo.setDate(new Date()); // 设置发布日期为当前时间
  48. Long userId = SecurityUtils.getUserId();
  49. governmentInfo.setIssuerId(userId.intValue());// 设置发布人ID
  50. governmentInfo.setStatus(GovernmentInfoEnum.PUBLISHED.getValue());
  51. }
  52. // 保存到数据库
  53. this.save(governmentInfo);
  54. return governmentInfo.getId();
  55. } catch (Exception e) {
  56. log.error("添加政务信息失败", e);
  57. throw new ServiceException("添加政务信息失败");
  58. }
  59. }
  60. @Override
  61. public boolean deleteGovernmentInfo(String ids) {
  62. if (StrUtil.isBlank(ids)) {
  63. throw new ServiceException("id不能为空或id异常");
  64. }
  65. // 2. 分割ID字符串为List<Integer>
  66. List<Long> idList = Arrays.stream(ids.split(","))
  67. .map(String::trim)
  68. .filter(StrUtil::isNotBlank)
  69. .map(Long::parseLong)
  70. .collect(Collectors.toList());
  71. if (idList.isEmpty()) {
  72. throw new ServiceException("ID格式异常");
  73. }
  74. // 3. 构建删除条件
  75. QueryWrapper<GovernmentInfo> queryWrapper = new QueryWrapper<>();
  76. queryWrapper.in("id", idList);
  77. // 4. 执行删除(返回是否删除成功)
  78. return remove(queryWrapper);
  79. }
  80. @Override
  81. public void editGovernmentInfo(GovernmentInfoEditRequest governmentInfoEditRequest) {
  82. // 判断是否存在
  83. Integer id = governmentInfoEditRequest.getId();
  84. GovernmentInfo oldGovernmentInfo = this.getById(id);
  85. if (oldGovernmentInfo == null) {
  86. throw new ServiceException("没有找到政务信息");
  87. }
  88. GovernmentInfo governmentInfo = new GovernmentInfo();
  89. BeanUtil.copyProperties(governmentInfoEditRequest, governmentInfo);
  90. governmentInfo.setUpdateTime(new Date());
  91. //如果修改状态为 发布 时,填充 发布日期 和 发布人
  92. if (GovernmentInfoEnum.PUBLISHED.getValue() == governmentInfo.getStatus()) {
  93. // 填充发布日期和发布人
  94. governmentInfo.setDate(new Date()); // 设置发布日期为当前时间
  95. Long userId = SecurityUtils.getUserId();
  96. governmentInfo.setIssuerId(userId.intValue());// 设置发布人ID
  97. }
  98. // 数据校验
  99. validGovernmentInfo(governmentInfo, BusinessType.UPDATE);
  100. // 操作数据库
  101. boolean result = this.updateById(governmentInfo);
  102. if (!result) {
  103. throw new ServiceException("修改政务信息操作失败");
  104. }
  105. }
  106. /**
  107. * 发布
  108. *
  109. * @param id
  110. * @return
  111. */
  112. @Override
  113. public boolean publishGovernmentInfo(int id) {
  114. if (id <= 0) {
  115. throw new ServiceException("参数错误");
  116. }
  117. GovernmentInfo governmentInfo = this.getById(id);
  118. if (governmentInfo == null) {
  119. throw new ServiceException("没有找到政务信息");
  120. }
  121. Integer status = governmentInfo.getStatus();
  122. if (status.equals(GovernmentInfoEnum.NON_PUBLISHED.getValue())) {
  123. // 填充发布日期和发布人
  124. governmentInfo.setDate(new Date()); // 设置发布日期为当前时间
  125. Long userId = SecurityUtils.getUserId();
  126. governmentInfo.setIssuerId(userId.intValue());// 设置发布人ID
  127. governmentInfo.setStatus(GovernmentInfoEnum.PUBLISHED.getValue());
  128. this.updateById(governmentInfo);
  129. } else {
  130. throw new ServiceException("只有未发布的才能发布");
  131. }
  132. return true;
  133. }
  134. /**
  135. * 下架
  136. *
  137. * @param id
  138. * @return
  139. */
  140. @Override
  141. public boolean removeGovernmentInfo(int id) {
  142. if (id <= 0) {
  143. throw new ServiceException("参数错误");
  144. }
  145. GovernmentInfo governmentInfo = this.getById(id);
  146. if (governmentInfo == null) {
  147. throw new ServiceException("没有找到政务信息");
  148. }
  149. Integer status = governmentInfo.getStatus();
  150. if (status.equals(GovernmentInfoEnum.PUBLISHED.getValue())) {
  151. governmentInfo.setStatus(GovernmentInfoEnum.REMOVED.getValue());
  152. } else {
  153. throw new ServiceException("发布的才能下架");
  154. }
  155. this.updateById(governmentInfo);
  156. return true;
  157. }
  158. /**
  159. * 校验政务信息
  160. *
  161. * @param governmentInfo
  162. */
  163. public void validGovernmentInfo(GovernmentInfo governmentInfo, BusinessType type) {
  164. if (ObjectUtil.isEmpty(governmentInfo)) {
  165. throw new ServiceException("数据为空");
  166. }
  167. Integer id = governmentInfo.getId();
  168. String title = governmentInfo.getTitle();
  169. String tag = governmentInfo.getTag();
  170. String serviceType = governmentInfo.getType();
  171. String content = governmentInfo.getContent();
  172. // 添加id无需校验,修改时,id 不能为空
  173. if (type != BusinessType.INSERT && ObjectUtil.isEmpty(id)) {
  174. throw new ServiceException("id不能为空");
  175. }
  176. if (StrUtil.isBlank(title)) {
  177. throw new ServiceException("标题不能为空");
  178. }
  179. if (StrUtil.isBlank(tag)) {
  180. throw new ServiceException("标签不能为空");
  181. }
  182. if (StrUtil.isBlank(serviceType)) {
  183. throw new ServiceException("类型不能为空");
  184. }
  185. if (StrUtil.isBlank(content)) {
  186. throw new ServiceException("内容不能为空");
  187. }
  188. }
  189. @Override
  190. public GovernmentInfoVO getGovernmentInfoById(int id) {
  191. if (id <= 0 || ObjectUtil.isEmpty(id)) {
  192. throw new ServiceException("id不能为空或id异常");
  193. }
  194. GovernmentInfo governmentInfo = this.getById(id);
  195. if (governmentInfo == null) {
  196. return null;
  197. }
  198. return getGovernmentInfoVO(governmentInfo);
  199. }
  200. @Override
  201. public Page<GovernmentInfoVO> getListGovernmentInfoByPage(GovernmentInfoQueryRequest governmentInfoQueryRequest) {
  202. long current = governmentInfoQueryRequest.getPageNum();
  203. long size = governmentInfoQueryRequest.getPageSize();
  204. Page<GovernmentInfo> servicePage = this.page(new Page<>(current, size),
  205. getQueryWrapper(governmentInfoQueryRequest));
  206. // 创建VO分页对象
  207. Page<GovernmentInfoVO> voPage = new Page<>(current, size, servicePage.getTotal());
  208. // 转换为VO列表
  209. List<GovernmentInfoVO> voList = new ArrayList<>();
  210. for (GovernmentInfo service : servicePage.getRecords()) {
  211. voList.add(getGovernmentInfoVO(service));
  212. }
  213. voPage.setRecords(voList);
  214. return voPage;
  215. }
  216. /**
  217. * 将实体转换为VO,并填充发布人信息
  218. */
  219. @Override
  220. public GovernmentInfoVO getGovernmentInfoVO(GovernmentInfo governmentInfo) {
  221. GovernmentInfoVO vo = new GovernmentInfoVO();
  222. BeanUtil.copyProperties(governmentInfo, vo);
  223. // 如果有发布人ID,获取发布人信息
  224. Integer issuerId = governmentInfo.getIssuerId();
  225. if (issuerId != null) {
  226. try {
  227. SysUser user = userService.selectUserById(issuerId.longValue());
  228. if (user != null) {
  229. Map<String, String> issuer = new HashMap<>();
  230. issuer.put("nickName", user.getNickName());
  231. // 填充其他信息
  232. vo.setIssuer(issuer);
  233. }
  234. } catch (Exception e) {
  235. log.error("获取发布人信息失败", e);
  236. throw new ServiceException("获取发布人信息失败");
  237. }
  238. }
  239. return vo;
  240. }
  241. @Override
  242. public QueryWrapper<GovernmentInfo> getQueryWrapper(GovernmentInfoQueryRequest governmentInfoQueryRequest) {
  243. QueryWrapper<GovernmentInfo> queryWrapper = new QueryWrapper<>();
  244. if (governmentInfoQueryRequest == null) {
  245. return queryWrapper;
  246. }
  247. // 从对象中取值
  248. String tag = governmentInfoQueryRequest.getTag();
  249. String keyword = governmentInfoQueryRequest.getKeyword();
  250. Date startTime = governmentInfoQueryRequest.getStartTime();
  251. Date endTime = governmentInfoQueryRequest.getEndTime();
  252. Integer status = governmentInfoQueryRequest.getStatus();
  253. String sortField = governmentInfoQueryRequest.getSortField();
  254. String sortOrder = governmentInfoQueryRequest.getSortOrder();
  255. queryWrapper.eq(ObjectUtil.isNotEmpty(status), "status", status)
  256. .eq(StrUtil.isNotEmpty(tag), "tag", tag);
  257. // 如果关键字不为空,添加模糊查询条件 标题或内容
  258. if (StrUtil.isNotBlank(keyword)) {
  259. queryWrapper.like("title", keyword)
  260. .or()
  261. .like("content", keyword);
  262. }
  263. // 日期范围查询(优先判断范围)
  264. if (startTime != null && endTime != null) {
  265. queryWrapper.between("date", startTime, endTime);
  266. } else if (startTime != null) {
  267. queryWrapper.ge("date", startTime); // >= 开始日期
  268. } else if (endTime != null) {
  269. queryWrapper.le("date", endTime); // <= 结束日期
  270. }
  271. // 排序
  272. queryWrapper.orderBy(StrUtil.isNotEmpty(sortField), "ascend".equals(sortOrder), sortField);
  273. return queryWrapper;
  274. }
  275. }