刘光辉
15 小时以前 34981c30a78e8bbd7791131059a9210f9928b62c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package jnpf.limsService.impl;
 
import jnpf.limsEntity.LimsHaocaiAlertRow;
import jnpf.limsEntity.LimsMessageEvent;
import jnpf.limsEntity.LimsMessageRequest;
import jnpf.limsEntity.LimsMessageSendResult;
import jnpf.limsEntity.LimsRecipientRef;
import jnpf.limsMapper.LimsHaocaiAlertMapper;
import jnpf.limsService.LimsHaocaiAlertService;
import jnpf.limsService.LimsMessageService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
 
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
 
@Slf4j
@Service
@RequiredArgsConstructor
public class LimsHaocaiAlertServiceImpl implements LimsHaocaiAlertService {
    private static final String QC_ADMIN_ROLE = "ROLE.005";
 
    private final LimsHaocaiAlertMapper alertMapper;
    private final LimsMessageService messageService;
 
    @Value("${lims.message.enabled:false}")
    private boolean messageEnabled;
 
    @Scheduled(cron = "${lims.haocai.alert.cron:0 0 1 * * ?}")
    public void cronTrigger() {
        if (!messageEnabled) {
            log.info("[haocai-alert] skipped because lims.message.enabled=false");
            return;
        }
        log.info("[haocai-alert] cron triggered");
        scanAndAlert(false);
    }
 
    @Override
    public Map<String, Integer> scanAndAlert(boolean dryRun) {
        List<LimsHaocaiAlertRow> rows = alertMapper.selectBelowThreshold();
        int sent = 0;
        for (LimsHaocaiAlertRow row : rows) {
            LimsMessageSendResult result = messageService.send(LimsMessageRequest.builder()
                    .event(LimsMessageEvent.CONSUMABLE_STOCK_LOW)
                    .tenantId(row.getTenantId())
                    .bizId(row.getId())
                    .remindStage("DAILY")
                    .occurrenceKey(LimsMessageEvent.CONSUMABLE_STOCK_LOW.getCode() + ":" + row.getId()
                            + ":" + LocalDate.now().format(DateTimeFormatter.BASIC_ISO_DATE))
                    .recipients(Arrays.asList(LimsRecipientRef.roleEnCode(QC_ADMIN_ROLE)))
                    .parameters(parameters(row))
                    .dryRun(dryRun)
                    .build());
            if (result.isAccepted() && result.getRecipientCount() > 0
                    && result.getSentCount() == result.getRecipientCount()
                    && result.getFailedCount() == 0) {
                sent++;
            }
        }
        Map<String, Integer> stats = new LinkedHashMap<>();
        stats.put("hit", rows.size());
        stats.put("sent", sent);
        stats.put("skipped", rows.size() - sent);
        log.info("[haocai-alert] done, dryRun={}, stats={}", dryRun, stats);
        return stats;
    }
 
    private Map<String, Object> parameters(LimsHaocaiAlertRow row) {
        Map<String, Object> parameters = new HashMap<>();
        parameters.put("bizId", row.getId());
        parameters.put("bizName", row.getMingcheng() == null ? "" : row.getMingcheng());
        parameters.put("currentStock", plain(row.getJieyuSum()));
        parameters.put("warningStock", row.getQuehuoYujing() == null ? "" : row.getQuehuoYujing());
        return parameters;
    }
 
    private String plain(BigDecimal value) {
        return value == null ? "0" : value.stripTrailingZeros().toPlainString();
    }
}