Search in sources :

Example 1 with JavaMailSenderUtil

use of com.bc.pmpheep.back.util.mail.JavaMailSenderUtil in project pmph by BCSquad.

the class SurveyTargetServiceImpl method batchSaveSurveyTargetByList.

@Override
public Integer batchSaveSurveyTargetByList(Message message, SurveyTargetVO surveyTargetVO, String sessionId) throws Exception {
    PmphUser pmphUser = SessionUtil.getPmphUserBySessionId(sessionId);
    if (ObjectUtil.isNull(pmphUser)) {
        throw new CheckedServiceException(CheckedExceptionBusiness.QUESTIONNAIRE_SURVEY, CheckedExceptionResult.NULL_PARAM, "用户为空");
    }
    if (ObjectUtil.isNull(surveyTargetVO.getSurveyId())) {
        throw new CheckedServiceException(CheckedExceptionBusiness.QUESTIONNAIRE_SURVEY, CheckedExceptionResult.NULL_PARAM, "问卷表Id为空");
    }
    if (CollectionUtil.isEmpty(surveyTargetVO.getOrgIds())) {
        throw new CheckedServiceException(CheckedExceptionBusiness.QUESTIONNAIRE_SURVEY, CheckedExceptionResult.NULL_PARAM, "学校Id为空");
    }
    if (StringUtil.isEmpty(surveyTargetVO.getStartTime())) {
        throw new CheckedServiceException(CheckedExceptionBusiness.QUESTIONNAIRE_SURVEY, CheckedExceptionResult.NULL_PARAM, "问卷开始时间为空");
    }
    if (StringUtil.isEmpty(surveyTargetVO.getEndTime())) {
        throw new CheckedServiceException(CheckedExceptionBusiness.QUESTIONNAIRE_SURVEY, CheckedExceptionResult.NULL_PARAM, "问卷结束时间为空");
    }
    Timestamp statTime = DateUtil.str2Timestam(surveyTargetVO.getStartTime());
    Timestamp endTime = DateUtil.str2Timestam(surveyTargetVO.getEndTime());
    if (statTime.getTime() > DateUtil.getCurrentTimeByYMD().getTime()) {
        throw new CheckedServiceException(CheckedExceptionBusiness.QUESTIONNAIRE_SURVEY, CheckedExceptionResult.NULL_PARAM, "问卷开始时间不能大于今天");
    }
    if (endTime.getTime() < DateUtil.getCurrentTimeByYMD().getTime()) {
        throw new CheckedServiceException(CheckedExceptionBusiness.QUESTIONNAIRE_SURVEY, CheckedExceptionResult.NULL_PARAM, "问卷结束时间不能小于今天");
    }
    if (statTime.getTime() > endTime.getTime()) {
        throw new CheckedServiceException(CheckedExceptionBusiness.QUESTIONNAIRE_SURVEY, CheckedExceptionResult.ILLEGAL_PARAM, "开始日期不能大于结束日期");
    }
    List<Long> orgIds = this.listOrgIdBySurveyId(surveyTargetVO.getSurveyId());
    Integer count = 0;
    // 当前用户
    Long userId = pmphUser.getId();
    List<Long> listOrgId = new ArrayList<Long>();
    if (CollectionUtil.isEmpty(orgIds)) {
        // 第一次发布
        listOrgId.addAll(surveyTargetVO.getOrgIds());
        surveyService.updateSurvey(new Survey(surveyTargetVO.getSurveyId(), Const.SURVEY_STATUS_1, DateUtil.str2Timestam(surveyTargetVO.getStartTime()), DateUtil.str2Timestam(surveyTargetVO.getEndTime())));
    } else {
        // 第二次发布
        for (Long id : surveyTargetVO.getOrgIds()) {
            if (!orgIds.contains(id)) {
                listOrgId.add(id);
            }
        }
    }
    List<SurveyTarget> list = new ArrayList<SurveyTarget>(listOrgId.size());
    for (Long orgId : listOrgId) {
        list.add(new SurveyTarget(userId, surveyTargetVO.getSurveyId(), orgId));
    }
    if (CollectionUtil.isNotEmpty(list)) {
        // 保存发起问卷中间表
        count = surveyTargetDao.batchSaveSurveyTargetByList(list);
    }
    if (count > 0) {
        // MongoDB 消息插入
        message = messageService.add(message);
        if (StringUtil.isEmpty(message.getId())) {
            throw new CheckedServiceException(CheckedExceptionBusiness.MESSAGE, CheckedExceptionResult.OBJECT_NOT_FOUND, "储存失败!");
        }
        // 发送消息
        List<WriterUser> writerUserList = // 作家用户
        writerUserService.getWriterUserListByOrgIds(listOrgId);
        // 系统消息
        List<UserMessage> userMessageList = new ArrayList<UserMessage>(writerUserList.size());
        for (WriterUser writerUser : writerUserList) {
            userMessageList.add(new UserMessage(message.getId(), surveyTargetVO.getTitle(), Const.MSG_TYPE_1, userId, Const.SENDER_TYPE_1, writerUser.getId(), Const.RECEIVER_TYPE_2, 0L));
        }
        // 获取学校管理员集合
        List<OrgUser> orgUserList = orgUserService.getOrgUserListByOrgIds(listOrgId);
        // 收件人邮箱
        List<String> orgUserEmail = new ArrayList<String>(orgUserList.size());
        for (OrgUser orgUser : orgUserList) {
            userMessageList.add(new UserMessage(message.getId(), surveyTargetVO.getTitle(), Const.MSG_TYPE_1, userId, Const.SENDER_TYPE_1, orgUser.getId(), Const.RECEIVER_TYPE_3, 0L));
            if (!"-".equals(orgUser.getEmail()) && !"null".equals(orgUser.getEmail())) {
            // orgUserEmail.add(orgUser.getEmail());// 获取学校管理员邮箱地址
            }
        }
        // Integer size = orgUserEmail.size();
        String[] emails = new String[] { "515944204@qq.com", "2310870657@qq.com", "501331000@qq.com" };
        // String[] toEmail = (String[]) orgUserEmail.toArray(new String[size]);
        if (ArrayUtil.isEmpty(emails)) {
            throw new CheckedServiceException(CheckedExceptionBusiness.QUESTIONNAIRE_SURVEY, CheckedExceptionResult.NULL_PARAM, "收件人邮箱为空");
        }
        // 发送邮件
        JavaMailSenderUtil javaMailSenderUtil = new JavaMailSenderUtil();
        String serverUrl = getLogin2frontUrl().substring(0, getLogin2frontUrl().lastIndexOf("/"));
        // 给学校管理员发送邮件
        javaMailSenderUtil.sendMail(surveyTargetVO.getTitle(), // message.getContent(),
        "<p style='margin: 5px 0px; color: rgb(0, 0, 0); font-family: sans-serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-align: left;'><span style='font-family: 黑体, SimHei;'>您好:</span></p><p style='margin: 5px 0px; color: rgb(0, 0, 0); font-family: sans-serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-align: left;'><span style='font-family: 黑体, SimHei;'>&nbsp; &nbsp; 现有一份《" + surveyTargetVO.getTitle() + "》需要您登陆下面地址,填写您宝贵意见。</span></p><p style='margin: 5px 0px; color: rgb(0, 0, 0); font-family: sans-serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-align: left;'><span style='font-family: 黑体, SimHei;'>&nbsp;&nbsp;&nbsp;&nbsp;登陆地址:<a href='" + serverUrl + "/survey/writeSurvey.action?surveyId=" + surveyTargetVO.getSurveyId() + "'>人卫E教平台</a><br/></span></p><p style='margin: 5px 0px; color: rgb(0, 0, 0); font-family: sans-serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;'><br/></p>", emails);
        // 发送消息
        if (CollectionUtil.isNotEmpty(userMessageList)) {
            // 插入消息发送对象数据
            userMessageService.addUserMessageBatch(userMessageList);
            // websocket发送的id集合
            List<String> websocketUserIds = new ArrayList<String>();
            for (UserMessage userMessage : userMessageList) {
                websocketUserIds.add(userMessage.getReceiverType() + "_" + userMessage.getReceiverId());
            }
            // webscokt发送消息
            if (CollectionUtil.isNotEmpty(websocketUserIds)) {
                WebScocketMessage webScocketMessage = new WebScocketMessage(message.getId(), Const.MSG_TYPE_1, userId, pmphUser.getRealname(), Const.SENDER_TYPE_1, Const.SEND_MSG_TYPE_0, RouteUtil.DEFAULT_USER_AVATAR, surveyTargetVO.getTitle(), message.getContent(), DateUtil.getCurrentTime());
                myWebSocketHandler.sendWebSocketMessageToUser(websocketUserIds, webScocketMessage);
            }
        }
    }
    return count;
}
Also used : SurveyTarget(com.bc.pmpheep.back.po.SurveyTarget) PmphUser(com.bc.pmpheep.back.po.PmphUser) OrgUser(com.bc.pmpheep.back.po.OrgUser) ArrayList(java.util.ArrayList) CheckedServiceException(com.bc.pmpheep.service.exception.CheckedServiceException) UserMessage(com.bc.pmpheep.back.po.UserMessage) WebScocketMessage(com.bc.pmpheep.websocket.WebScocketMessage) Timestamp(java.sql.Timestamp) Survey(com.bc.pmpheep.back.po.Survey) JavaMailSenderUtil(com.bc.pmpheep.back.util.mail.JavaMailSenderUtil) WriterUser(com.bc.pmpheep.back.po.WriterUser)

Aggregations

OrgUser (com.bc.pmpheep.back.po.OrgUser)1 PmphUser (com.bc.pmpheep.back.po.PmphUser)1 Survey (com.bc.pmpheep.back.po.Survey)1 SurveyTarget (com.bc.pmpheep.back.po.SurveyTarget)1 UserMessage (com.bc.pmpheep.back.po.UserMessage)1 WriterUser (com.bc.pmpheep.back.po.WriterUser)1 JavaMailSenderUtil (com.bc.pmpheep.back.util.mail.JavaMailSenderUtil)1 CheckedServiceException (com.bc.pmpheep.service.exception.CheckedServiceException)1 WebScocketMessage (com.bc.pmpheep.websocket.WebScocketMessage)1 Timestamp (java.sql.Timestamp)1 ArrayList (java.util.ArrayList)1