SafetyServiceImpl.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package com.supervision.safety.service.impl;
  2. import com.supervision.safety.domain.*;
  3. import com.supervision.safety.mapper.SafetyIssueMapper;
  4. import com.supervision.safety.service.SafetyService;
  5. import org.springframework.stereotype.Service;
  6. import org.springframework.util.CollectionUtils;
  7. import javax.annotation.Resource;
  8. import java.text.SimpleDateFormat;
  9. import java.util.*;
  10. import java.util.stream.Collectors;
  11. @Service
  12. public class SafetyServiceImpl implements SafetyService {
  13. @Resource
  14. private SafetyIssueMapper mapper;
  15. @Override
  16. public Long createIssue(CreateIssueReq req) {
  17. SafetyIssue i = new SafetyIssue();
  18. i.setDescription(req.getDescription());
  19. i.setRiskLevel(req.getRiskLevel());
  20. i.setStatus(IssueStatus.PENDING);
  21. i.setFoundAt(req.getFoundAt());
  22. i.setRectifiedAt(null);
  23. i.setDeleted(false);
  24. i.setCreateTime(new Date());
  25. i.setUpdateTime(new Date());
  26. mapper.insert(i);
  27. return i.getId();
  28. }
  29. @Override
  30. public void updateIssueStatus(Long id, IssueStatus status) {
  31. Date rectifiedAt = (status == IssueStatus.RECTIFIED) ? new Date() : null;
  32. int n = mapper.updateStatus(id, status, rectifiedAt);
  33. if (n == 0) throw new RuntimeException("Issue not found or deleted");
  34. }
  35. @Override
  36. public void deleteIssue(Long id) {
  37. mapper.logicalDelete(id);
  38. }
  39. @Override
  40. public PageResult<SafetyIssue> search(String keyword, RiskLevel level, IssueStatus status,
  41. Date from, Date to, int pageNum, int pageSize) {
  42. int limit = pageSize;
  43. int offset = (Math.max(pageNum, 1) - 1) * pageSize;
  44. List<SafetyIssue> list = mapper.search(keyword, level, status, from, to, limit, offset);
  45. long total = mapper.count(keyword, level, status, from, to);
  46. return new PageResult<>(total, pageNum, pageSize, list);
  47. }
  48. @Override
  49. public Map<String, Long> counters() {
  50. Map<String, Long> map = new HashMap<>();
  51. map.put("HIGH", 0L); map.put("MEDIUM", 0L); map.put("LOW", 0L);
  52. List<Map<String, Object>> rows = mapper.countPendingByLevel();
  53. if (!CollectionUtils.isEmpty(rows)) {
  54. for (Map<String,Object> r : rows) {
  55. String lvl = String.valueOf(r.get("lvl"));
  56. Long cnt = ((Number) r.get("cnt")).longValue();
  57. map.put(lvl, cnt);
  58. }
  59. }
  60. return map;
  61. }
  62. @Override
  63. public List<Map<String, Object>> weeklyTrend(int days) {
  64. Calendar cal = Calendar.getInstance();
  65. Date to = cal.getTime();
  66. cal.add(Calendar.DATE, -days + 1);
  67. Date from = cal.getTime();
  68. List<Map<String, Object>> rows = mapper.trendByDay(from, to);
  69. Map<String, Long> hit = new HashMap<>();
  70. for (Map<String, Object> r : rows) {
  71. hit.put(String.valueOf(r.get("d")), ((Number) r.get("c")).longValue());
  72. }
  73. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  74. List<Map<String, Object>> out = new ArrayList<>();
  75. Calendar c = Calendar.getInstance(); c.setTime(from);
  76. while (!c.getTime().after(to)) {
  77. String key = sdf.format(c.getTime());
  78. Map<String,Object> item = new HashMap<>();
  79. item.put("date", key);
  80. item.put("count", hit.getOrDefault(key, 0L));
  81. out.add(item);
  82. c.add(Calendar.DATE, 1);
  83. }
  84. return out;
  85. }
  86. @Override
  87. public List<SafetyIssue> top5High() {
  88. return mapper.top5High();
  89. }
  90. }