package com.hzs.third.email.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.domain.third.email.TEmail;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.third.email.config.EmailConfig;
import com.hzs.third.email.service.ITEmailService;
import com.hzs.third.email.mapper.TEmailMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Date;

/**
 * 邮件服务
 */
@Service
public class TEmailServiceImpl extends ServiceImpl<TEmailMapper, TEmail> implements ITEmailService {

//    @Value("${spring.mail.from}")
//    private String from; // 发送发邮箱地址
    @Autowired
    private EmailConfig emailConfig;
    @Autowired
    private JavaMailSender mailSender;

    /**
     * 发送邮件
     * @param mail  邮件账号
     * @param title 邮件标题
     * @param content 邮件内容
     * @param isRecordLog 是否记录日志  true 记录 false 不记录
     * @param emailType 邮件类型 EmailType 枚举
     * @return
     */
    @Override
    public Boolean sendMail(String mail,String title,String content,Boolean isRecordLog,Integer emailType) {
        String from = emailConfig.getFrom();
        try {
            // 创建一个邮件对象
            SimpleMailMessage msg = new SimpleMailMessage();
            msg.setFrom(from); // 设置发送发
            msg.setTo(mail); // 设置接收方
            msg.setSubject(title); // 设置邮件主题
            msg.setText(content); // 设置邮件内容
            // 发送邮件
            mailSender.send(msg);
            if (isRecordLog){
                // 记录邮箱日志
                TEmail tEmail = new TEmail();
                tEmail.setSender(from);
                tEmail.setReceiver(mail);
                tEmail.setSubject(title);
                tEmail.setContent(content);
                tEmail.setPkCreator(SecurityUtils.getUserId());
                tEmail.setCreationTime(new Date());
                tEmail.setEmailType(emailType);
                baseMapper.insert(tEmail);
            }
            return true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public Boolean sendMessageCarryFiles(String to, String subject, String content, File[] files) {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        String from = emailConfig.getFrom();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
            helper.setFrom(from); // 设置发送发
            helper.setTo(to); // 设置接收方
            helper.setSubject(subject); // 设置邮件主题
            helper.setText(content); // 设置邮件内容
            if (files != null && files.length > 0) { // 添加附件（多个）
                for (File file : files) {
                    helper.addAttachment(file.getName(), file);
                }
            }
            // 发送邮件
            mailSender.send(mimeMessage);
            return true;
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public Boolean sendMessageCarryFile(String to, String subject, String content, File file) {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        String from = emailConfig.getFrom();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
            helper.setFrom(from); // 设置发送发
            helper.setTo(to); // 设置接收方
            helper.setSubject(subject); // 设置邮件主题
            helper.setText(content); // 设置邮件内容
            helper.addAttachment(file.getName(), file); // 单个附件
            // 发送邮件
            mailSender.send(mimeMessage);
            return true;
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        return false;
    }


}
