|
@@ -0,0 +1,102 @@
|
|
|
|
|
+package com.supervision.safety.service.impl;
|
|
|
|
|
+
|
|
|
|
|
+import com.supervision.safety.domain.*;
|
|
|
|
|
+import com.supervision.safety.mapper.SafetyIssueMapper;
|
|
|
|
|
+import com.supervision.safety.service.SafetyService;
|
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
|
|
+
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
|
|
+import java.util.*;
|
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
+
|
|
|
|
|
+@Service
|
|
|
|
|
+public class SafetyServiceImpl implements SafetyService {
|
|
|
|
|
+
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private SafetyIssueMapper mapper;
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public Long createIssue(CreateIssueReq req) {
|
|
|
|
|
+ SafetyIssue i = new SafetyIssue();
|
|
|
|
|
+ i.setDescription(req.getDescription());
|
|
|
|
|
+ i.setRiskLevel(req.getRiskLevel());
|
|
|
|
|
+ i.setStatus(IssueStatus.PENDING);
|
|
|
|
|
+ i.setFoundAt(req.getFoundAt());
|
|
|
|
|
+ i.setRectifiedAt(null);
|
|
|
|
|
+ i.setDeleted(false);
|
|
|
|
|
+ i.setCreateTime(new Date());
|
|
|
|
|
+ i.setUpdateTime(new Date());
|
|
|
|
|
+ mapper.insert(i);
|
|
|
|
|
+ return i.getId();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void updateIssueStatus(Long id, IssueStatus status) {
|
|
|
|
|
+ Date rectifiedAt = (status == IssueStatus.RECTIFIED) ? new Date() : null;
|
|
|
|
|
+ int n = mapper.updateStatus(id, status, rectifiedAt);
|
|
|
|
|
+ if (n == 0) throw new RuntimeException("Issue not found or deleted");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void deleteIssue(Long id) {
|
|
|
|
|
+ mapper.logicalDelete(id);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public PageResult<SafetyIssue> search(String keyword, RiskLevel level, IssueStatus status,
|
|
|
|
|
+ Date from, Date to, int pageNum, int pageSize) {
|
|
|
|
|
+ int limit = pageSize;
|
|
|
|
|
+ int offset = (Math.max(pageNum, 1) - 1) * pageSize;
|
|
|
|
|
+ List<SafetyIssue> list = mapper.search(keyword, level, status, from, to, limit, offset);
|
|
|
|
|
+ long total = mapper.count(keyword, level, status, from, to);
|
|
|
|
|
+ return new PageResult<>(total, pageNum, pageSize, list);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public Map<String, Long> counters() {
|
|
|
|
|
+ Map<String, Long> map = new HashMap<>();
|
|
|
|
|
+ map.put("HIGH", 0L); map.put("MEDIUM", 0L); map.put("LOW", 0L);
|
|
|
|
|
+ List<Map<String, Object>> rows = mapper.countPendingByLevel();
|
|
|
|
|
+ if (!CollectionUtils.isEmpty(rows)) {
|
|
|
|
|
+ for (Map<String,Object> r : rows) {
|
|
|
|
|
+ String lvl = String.valueOf(r.get("lvl"));
|
|
|
|
|
+ Long cnt = ((Number) r.get("cnt")).longValue();
|
|
|
|
|
+ map.put(lvl, cnt);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return map;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public List<Map<String, Object>> weeklyTrend(int days) {
|
|
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
|
|
+ Date to = cal.getTime();
|
|
|
|
|
+ cal.add(Calendar.DATE, -days + 1);
|
|
|
|
|
+ Date from = cal.getTime();
|
|
|
|
|
+
|
|
|
|
|
+ List<Map<String, Object>> rows = mapper.trendByDay(from, to);
|
|
|
|
|
+ Map<String, Long> hit = new HashMap<>();
|
|
|
|
|
+ for (Map<String, Object> r : rows) {
|
|
|
|
|
+ hit.put(String.valueOf(r.get("d")), ((Number) r.get("c")).longValue());
|
|
|
|
|
+ }
|
|
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
|
|
+ List<Map<String, Object>> out = new ArrayList<>();
|
|
|
|
|
+ Calendar c = Calendar.getInstance(); c.setTime(from);
|
|
|
|
|
+ while (!c.getTime().after(to)) {
|
|
|
|
|
+ String key = sdf.format(c.getTime());
|
|
|
|
|
+ Map<String,Object> item = new HashMap<>();
|
|
|
|
|
+ item.put("date", key);
|
|
|
|
|
+ item.put("count", hit.getOrDefault(key, 0L));
|
|
|
|
|
+ out.add(item);
|
|
|
|
|
+ c.add(Calendar.DATE, 1);
|
|
|
|
|
+ }
|
|
|
|
|
+ return out;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public List<SafetyIssue> top5High() {
|
|
|
|
|
+ return mapper.top5High();
|
|
|
|
|
+ }
|
|
|
|
|
+}
|