package com.ruoyi.business.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.business.domain.TEbsRecord;
import com.ruoyi.business.domain.TMiStock;
import com.ruoyi.business.service.ITEbsRecordService;
import com.ruoyi.business.service.ITMiStockService;
import com.ruoyi.common.utils.http.HttpUtils;
import lombok.Value;
import microsoft.exchange.webservices.data.core.ExchangeService;
import microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion;
import microsoft.exchange.webservices.data.core.exception.service.local.ServiceLocalException;
import microsoft.exchange.webservices.data.core.service.item.EmailMessage;
import microsoft.exchange.webservices.data.credential.ExchangeCredentials;
import microsoft.exchange.webservices.data.credential.WebCredentials;
import microsoft.exchange.webservices.data.property.complex.MessageBody;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.mail.*;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.net.URI;
import java.net.URISyntaxException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;

public class MailUtils {
    /**
     *
     * @param email     接收者邮箱
     * @param subject   邮件主题
     * @param emailMsg  邮件内容
     * @throws AddressException
     * @throws MessagingException
     */
    public static void sendMailSmtp (String email, String subject, String emailMsg)
            throws AddressException, MessagingException {
        //创建配置文件
        Properties props = new Properties();
        //设置发送时遵从SMTP协议
        props.setProperty("mail.transport.protocol", "SMTP");
        /*
         * 发送邮件的域名
         * smtp.xx.com
         * smtp.qq.com则代表发送邮件时使用的邮箱域名来自qq
         * smtp.163.com则代表发送邮件时使用的邮箱域名来自163
         */
        props.setProperty("mail.host", "smtp.qq.com");
        //设置用户的认证方式auth
        props.setProperty("mail.smtp.auth", "true");
        Authenticator auth = new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
                //return new PasswordAuthentication("用户名", "密码");
                //注意qq邮箱需要去qq邮箱的设置中获取授权码，并将授权码作为密码来填写
                return new PasswordAuthentication("liangzhou.mail@qq.com", "xxxxxx");
            }
        };
        //创建session域
        Session session = Session.getInstance(props, auth);
        Message message = new MimeMessage(session);
        //设置邮件发送者,与PasswordAuthentication中的邮箱一致即可
        message.setFrom(new InternetAddress("liangzhou.mail@qq.com"));
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(email));
        //设置邮件主题
        message.setSubject(subject);
        //设置邮件内容
        message.setContent(emailMsg, "text/html;charset=utf-8");
        //发送邮件
        Transport.send(message);
    }

    /**
     *
     * @param recipientTo     接收者邮箱
     * @param subject   邮件主题
     * @param message  邮件内容
     * @throws AddressException
     * @throws MessagingException
     */
    public static String sendMailExchange (String recipientTo, String subject, String message) {
        ExchangeClient exchangeClient = new ExchangeClient.ExchangeClientBuilder()
                .hostname("exmail.scc.com.cn")
                .exchangeVersion(ExchangeVersion.Exchange2010)
                .username("sys@scc.com")
                .password("scc@123456")
                .subject(subject)
                .message(message)
                .recipientTo(recipientTo)
                .build();
        return exchangeClient.sendExchange();
    }

    // 有效期不足30天时发邮件:

//    public static void main(String[] args) throws MessagingException {
//        StringBuilder content = new StringBuilder("<html><head></head><body>");
//        content.append("<h3>以下物料剩余有效期已不足12天</h3>");
//        content.append("<table border=\"1\" style=\"width:1000px; height:100px;border:solid 1px #E8F2F9;font-size=14px;font-size:18px;\">");
//        content.append("<tr style=\"align:center\"><th>序号</th><th>物料编码</th><th>物料描述</th><th>供应商</th><th>批次号</th><th>生产日期</th><th>有效日期</th><th>单位</th><th>数量</th></tr>");
//        content.append("<tr>" +
//                "<td><span>" + 1 + "</span></td>" +
//                "<td><span>" + "test001" + "</span></td>" +
//                "<td><span>" + "测试物料" + "</span></td>" +
//                "<td><span>" + "测试供应商" + "</span></td>" +
//                "<td><span>" + "test" + "</span></td>" +
//                "<td><span>" + "20221105" + "</span></td>" +
//                "<td><span>" + "20221125" + "</span></td>" +
//                "<td><span>" + "张" + "</span></td>" +
//                "<td><span>" + 100 + "</span></td>" +
//                "</tr>");
//        content.append("</table>");
//        content.append("</body></html>");
//
//        sendMailSmtp("liangzhou.mail@qq.com", "测试", content.toString());
//
////        String result = sendMailExchange("lihongbao@scc.com.cn", "测试", "test");
////        System.out.println(new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()));
////        String resultWmsSubInvProc = "{'success':true,'msg':'','obj':{'total':1,'rows':[{'requestId':'20221120110300673','processStatus':'PROCESSING','errorMsg':'waiting for processing','lastUpdateDate':'2022-11-20 11:01:40','lastUpdatedBy':-1,'creationDate':'2022-11-20 11:01:40','createdBy':-1,'itemId':1665637,'quantity':4.0,'subInventory':'312-104','organizationId':469,'transDate':'2022-11-20 11:03:00','transUom':'张','transTypeId':2,'transLotNumber':'IBG22701','sourceHeaderId':1,'sourceLineId':1,'transSubInventory':'312-124','id':338145}]}}";
////        System.out.println(JSON.parseObject(JSON.parseObject(resultWmsSubInvProc).getString("obj")).getIntValue("total"));
//    }

//    @Autowired
//    private ITMiStockService tMiStockService;
//    public static void main(String[] args) throws Exception {
//        // 发送邮件
//        StringBuilder content = new StringBuilder("<html><head></head><body>");
//        content.append("<h3>以下物料即将过期</h3>");
//        content.append("<table border=\"1\" style=\" border:solid 0.6px #E8F2F9; width: auto; font-size=12px; text-align: center; font-size:12px;\">");
//        content.append("<tr><th>序号</th><th>物料编码</th><th>物料描述</th><th>供应商</th><th>批次号</th><th>生产日期</th><th>有效日期</th><th>单位</th><th>数量</th><th>目前总库存数量</th></tr>");
//            content.append("<tr>" +
//                    "<td><span>" + 1 + "</span></td>" +
//                    "<td><span>" + "100001" + "</span></td>" +
//                    "<td><span>" + "测试物料测试物料测试物料测试物料测试物料测试物料测试物料测试物料测试物料测试物料测试物料测试物料测试物料测试物料" + "</span></td>" +
//                    "<td><span>" + "1001" + "</span></td>" +
//                    "<td><span>" + "test100001" + "</span></td>" +
//                    "<td><span>" + "20221107" + "</span></td>" +
//                    "<td><span>" + "20221125" + "</span></td>" +
//                    "<td><span>" + "张" + "</span></td>" +
//                    "<td><span>" + 1 + "</span></td>" +
//                    "<td><span>" + 100 + "</span></td>" +
//                    "</tr>");
//        content.append("</table>");
//        content.append("</body></html>");
//        for (int i = 0; i < 5; i++) {
//            String result = MailUtils.sendMailExchange("lihongbao@scc.com.cn", "无锡W6B铜箔库即将物料清单-W6B-312", content.toString());
//            if ("邮件发送成功".equals(result)) {
//                break;
//            }
//        }
//    }
}