Blame view

src/main/java/com/huaheng/common/utils/SendNoticeUtils.java 2.75 KB
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
package com.huaheng.common.utils;

import com.huaheng.common.constant.SendTypeConstants;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.framework.web.service.WebSocketServer;
import com.huaheng.pc.config.sendMail.service.MailService;
import com.huaheng.pc.system.notice.domain.SysNotice;
import com.huaheng.pc.system.notice.service.SysNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.IOException;

/**
 * 发送通知工具类
 * @author mahua
 */
@Component
public class SendNoticeUtils {
    private static SendNoticeUtils staticInstance;

    @Resource
    private MailService mailService;
    @Resource
    private SysNoticeService noticeService;

    public SendNoticeUtils() {
        super();
        // TODO Auto-generated constructor stub
    }
    @PostConstruct
    public void init() {
        staticInstance = this;
        staticInstance.mailService = this.mailService;
        staticInstance.noticeService = this.noticeService;
    }

    public static AjaxResult sendNotice(String subject, String body, Integer type, String[] to, String[] cc) {
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
        if (!type.equals(SendTypeConstants.EMAIL)) {
            if (StringUtils.isNotEmpty(to)) {
                for (String address : to) {
                    SysNotice notice = new SysNotice();
                    notice.setTitle(subject);
                    notice.setType(String.valueOf(type));
                    notice.setUserId(address);
                    notice.setContent(body);
                    notice.setStatus("0");
                    staticInstance.noticeService.save(notice);
                }
            } else {
                SysNotice notice = new SysNotice();
                notice.setTitle(subject);
                notice.setType(String.valueOf(type));
                notice.setContent(body);
                notice.setStatus("0");
                staticInstance.noticeService.save(notice);
            }

        }
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
        if (type.equals(SendTypeConstants.EMAIL)) {
//            mailService.sendSimpleMail(to, subject, body, cc);
        } else if (type.equals(SendTypeConstants.WEBSOCKET_USER)) {
            for (String userId : to) {
                try {
                    WebSocketServer.sendInfo(body, userId);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } else if (type.equals(SendTypeConstants.WEBSOCKET_BROADCAST)) {
            try {
                WebSocketServer.sendInfo(body, "");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return AjaxResult.success();
    }
}