SendNoticeUtils.java 2.78 KB
package com.huaheng.common.utils;

import com.huaheng.common.constant.SendTypeConstants;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.mobile.websocket.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) {

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

        }

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