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