AlarmMailApiController.java 8.77 KB
package com.huaheng.common.mail.alarmmail.controller;


import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.mail.SendMailText;
import com.huaheng.common.mail.alarmmail.domain.AlarmType;
import com.huaheng.common.mail.alarmmail.domain.SendEmail;
import com.huaheng.common.mail.alarmmail.domain.Sender;
import com.huaheng.common.mail.alarmmail.mapper.SendEmailMapperAuto;
import com.huaheng.common.mail.alarmmail.service.AlarmScanningService;
import com.huaheng.common.support.Convert;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.aspectj.lang.annotation.Log;
import com.huaheng.framework.aspectj.lang.constant.BusinessType;
import com.huaheng.framework.web.controller.BaseController;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.framework.web.page.TableDataInfo;
import com.huaheng.pc.config.containercapacity.domain.ContainerCapacity;
import com.huaheng.pc.general.material.domain.Material;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * 邮件表 处理接口
 *
 * @author ZengBo
 * @date 2019-03-08
 */

@Controller
@RequestMapping("/administration/SendMail")
public class AlarmMailApiController extends BaseController {

    private String prefix = "administration/SendMail";

    @Resource
    private SendEmailMapperAuto sendEmailMapperAuto;

    @Autowired
    AlarmScanningService alarmScanningService;

    @RequiresPermissions("administration:SendMail:view")
    @GetMapping()
    public String SendMail()
    {
        return prefix + "/SendMail";
    }

    /**
     * 查询邮件发送列表
     */
    @RequiresPermissions("administration:SendMail:list")
    @Log(title = "管理-报警邮件", operating = "查看报警邮件列表", action = BusinessType.GRANT)
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SendEmail sendEmail)
    {
        sendEmail.setDeleted(false);
        startPage();
        List<SendEmail> list = sendEmailMapperAuto.selectListEntityByLike(sendEmail) ;
        return getDataTable(list);
    }

    /**
     * id查询
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap) {
        SendEmail sendEmail = sendEmailMapperAuto.selectEntityById(id);
        mmap.put("sendEmail", sendEmail);
        return prefix + "/edit";
    }

    /**
     * 修改
     */
    @RequiresPermissions("administration:SendMail:edit")
    @Log(title = "管理-报警类型修改", operating = "管理-报警类型修改", action = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SendEmail sendEmail) {
        sendEmail.setLastUpdatedBy(ShiroUtils.getLoginName());
        return toAjax(sendEmailMapperAuto.updateByModel(sendEmail));
    }

    /**
     * 删除
     */
    @RequiresPermissions("administration:SendMail:remove")
    @Log(title = "管理-报警类型删除", operating = "管理-报警类型删除", action = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        if (StringUtils.isEmpty(ids))
            return AjaxResult.error("id不能为空");
        for (Integer id : Convert.toIntArray(ids)) {
            SendEmail sendEmail = new SendEmail();
            sendEmail.setId(id);
            sendEmail.setDeleted(true);
            sendEmailMapperAuto.updateByModel(sendEmail);
        }
        return AjaxResult.success("删除成功");
    }

    /**
     * 报警主控制API
     */
    @Log(title = "配置-报警控制", operating = "报警控制", action = BusinessType.OTHER)
    @PostMapping("/main")
    @ResponseBody
    @Transactional
    public AjaxResult main() throws Exception {
        return alarmScanningService.Mastercontrol();
    }


    /**
     * 邮件发送
     */
    @Log(title = "配置-报警邮件发送", operating = "报警邮件发送", action = BusinessType.OTHER)
    @PostMapping("/send")
    @ResponseBody
    @Transactional
    public AjaxResult sendMail(Integer id) throws Exception {
        Sender sender = new Sender();
        //发件人地址
        sender.setSenderAddress("wms@huahengweld.com");
        //发件人账户名
        sender.setSenderAccount("wms");
        //发件人邮箱密码
        sender.setSenderPassword("nkfwhfi986+/214G2");
        //查询出所有未发送,未删除,未处理的邮件
        SendEmail sendEmail = new SendEmail();
        sendEmail.setEnable(false);
        sendEmail.setDeleted(false);
        sendEmail.setHandle(false);
        List<SendEmail> sendEmailList = new ArrayList<>();
        SendEmail rs = new SendEmail();
        rs.setEnable(true);
        try {
            if (id == null) {
                sendEmailList = sendEmailMapperAuto.selectListEntityByEqual(sendEmail);
                for (int i = 0; i < sendEmailList.size(); i++) {
                    rs.setId(sendEmailList.get(i).getId());
                    Integer result = sendEmailMapperAuto.updateByModel(rs);
                    if (result < 1) {
                        return AjaxResult.error("失败!");
                    }
                }
            } else {
                sendEmailList.add(sendEmailMapperAuto.selectEntityById(id));
                rs.setId(id);
                Integer result = sendEmailMapperAuto.updateByModel(rs);
                if (result < 1) {
                    return AjaxResult.error("失败!");
                }
            }
        }catch (Exception e){
            throw new ServiceException("失败!");
        }
        for (SendEmail item : sendEmailList) {
            //1、连接邮件服务器的参数配置
            Properties props = new Properties();
            //设置用户的认证方式
            props.setProperty("mail.smtp.auth", "true");
            //设置传输协议
            props.setProperty("mail.transport.protocol", "smtp");
            //设置发件人的SMTP服务器地址,使用前PING一下mail.huahengweld.com
            props.setProperty("mail.smtp.host", "172.16.0.2");
            //2、创建定义整个应用程序所需的环境信息的 Session 对象
            Session session = Session.getInstance(props);
            //设置调试信息在控制台打印出来
            session.setDebug(true);
            //3、创建邮件的实例对象
            SendMailText sendMailText = new SendMailText();
            sender.setTitle(item.getTitle());
            sender.setBody(item.getBody());
            //拆分收件人和收件人邮箱
//            String addressee = item.getAddressee();
            String addresseeemail = item.getAddresseeemail();
//            String[] addresseelist = addressee.split(";");
            String[] addresseeemaillist = addresseeemail.split(";");
            Message msg = sendMailText.getMimeMessage(session, sender,addresseeemaillist);
            //4、根据session对象获取邮件传输对象Transport
            Transport transport = session.getTransport();
            //设置发件人的账户名和密码
            transport.connect(sender.getSenderAccount(), sender.getSenderPassword());
            //发送邮件,并发送到所有收件人地址,message.getAllRecipients() 获取到的是在创建邮件对象时添加的所有收件人, 抄送人, 密送人
            transport.sendMessage(msg, msg.getAllRecipients());
            //5、关闭邮件连接
            transport.close();
        }
        return AjaxResult.success("发送成功!!");
    }

    /**
     * 邮件处理情况扫描
     */
    @Log(title = "配置-库存报警级别升级", operating = "库存报警级别升级", action = BusinessType.UPDATE)
    @PostMapping("/level")
    @ResponseBody
    @Transactional
    public AjaxResult level() throws Exception {
        SendEmail sendEmail = new SendEmail();
        sendEmail.setDeleted(false);
        sendEmail.setHandle(false);
        List<SendEmail> list = sendEmailMapperAuto.selectListEntityByEqual(sendEmail);
        for (SendEmail item : list){
            Integer i = item.getWarninglevel()+1;
            item.setWarninglevel(i);
            try {
                sendEmailMapperAuto.updateByModel(item);
            }catch (Exception e){
                throw new Exception("库存报警级别升级失败!");
            }
        }
        return AjaxResult.success("库存报警级别升级成功!");
    }
}