tuqin
2026-11-03 9494075e6d9d2a4b2b884b4ed740dee0c434734c
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
package cn.iocoder.yudao.module.trade.service.foreign;
 
import cn.iocoder.yudao.module.system.api.notify.NotifyMessageSendApi;
import cn.iocoder.yudao.module.system.api.notify.dto.NotifySendSingleToUserReqDTO;
import cn.iocoder.yudao.module.trade.dal.dataobject.foreign.TradeForeignOrderDO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * 外贸订单补件提醒 Service
 *
 * <p>当外贸订单按出货模式存在必填单据缺件时,向业务员发送站内信「补件提醒」。
 * 采用 {@link Propagation#REQUIRES_NEW} 独立事务,保证即使外层「提交审批/完结」因缺件异常回滚,
 * 提醒消息也已落库,不会被一起回滚。</p>
 *
 * @author mom
 */
@Service
@Slf4j
public class TradeForeignDocNotifyService {
 
    /** 补件提醒站内信模板编号(见 trade_foreign_document_notify_init.sql) */
    public static final String DOC_MISSING_TEMPLATE_CODE = "trade_foreign_doc_missing";
 
    @Resource
    private NotifyMessageSendApi notifyMessageSendApi;
 
    /**
     * 发送补件提醒站内信
     *
     * @param order      外贸订单
     * @param missingDocNames 缺失的必填单据名称
     * @param scene      触发场景(如「提交审批」「完结」)
     */
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void sendDocMissingNotify(TradeForeignOrderDO order, List<String> missingDocNames, String scene) {
        if (order == null || order.getSaleUserId() == null || missingDocNames == null || missingDocNames.isEmpty()) {
            return;
        }
        try {
            Map<String, Object> params = new HashMap<>();
            params.put("orderNo", order.getNo());
            params.put("docs", String.join("、", missingDocNames));
            params.put("scene", scene);
            NotifySendSingleToUserReqDTO reqDTO = new NotifySendSingleToUserReqDTO();
            reqDTO.setUserId(order.getSaleUserId());
            reqDTO.setTemplateCode(DOC_MISSING_TEMPLATE_CODE);
            reqDTO.setTemplateParams(params);
            notifyMessageSendApi.sendSingleMessageToAdmin(reqDTO);
        } catch (Exception e) {
            // 提醒失败不影响主流程
            log.warn("[sendDocMissingNotify][订单({}) 补件提醒发送失败]", order.getNo(), e);
        }
    }
 
}