package com.project.zcustom.controller.backManager;

import com.project.common.core.domain.AjaxResult;
import com.project.zcustom.domain.addional.ManagerEmail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.mail.internet.MimeMessage;
import java.io.File;

@RestController
@RequestMapping("/zcustom/email")
public class LargeEmailController {
    /**
     * 邮件发送接口
     */
    @Autowired
    private JavaMailSender mailSender;

    /**
     * 邮件发送人
     */
    @Value("${spring.mail.username}")
    private String sender;

    /**
     * 邮件发送方法
     */
    @PostMapping("/send")
    public AjaxResult sendEmail(@RequestBody ManagerEmail managerEmail) {
        //创建简单邮件消息
        SimpleMailMessage message = new SimpleMailMessage();
        //谁发的
        message.setFrom(sender);
        //谁要接收
        message.setTo(managerEmail.getReceiver());
        //邮件标题
        message.setSubject(managerEmail.getSubject());
        //邮件内容
        message.setText(managerEmail.getContent());
        try {
            mailSender.send(message);
            return AjaxResult.success("发送邮件成功");
        } catch (MailException e) {
            e.printStackTrace();
            return AjaxResult.error("发送邮件失败");
        }
    }

    @PostMapping("/complexSend")
    public AjaxResult enclosureEmail(@RequestParam MultipartFile multipartFile, @RequestParam String receiver, @RequestParam String subject, @RequestParam String content) {
        //创建一个MINE消息
        MimeMessage message = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            //谁发
            helper.setFrom(sender);
            //谁接收
            helper.setTo(receiver);
            //邮件主题
            helper.setSubject(subject);
            //邮件内容   true 表示带有附件或html
            helper.setText(content, true);
            File multipartFileToFile = MultipartFileToFile(multipartFile);
            if (multipartFileToFile == null) {
                return AjaxResult.error("文件转换失败");
            }
            FileSystemResource file = new FileSystemResource(multipartFileToFile);
            String filename = file.getFilename();
            //添加附件
            if (filename == null) {
                return AjaxResult.error("文件名称获取失败");
            }
            helper.addAttachment(filename, file);
            mailSender.send(message);
            return AjaxResult.success("发送邮件成功");
        }  catch (javax.mail.MessagingException e) {
            e.printStackTrace();
            return AjaxResult.error("发送邮件失败");
        }
    }

    private File MultipartFileToFile(MultipartFile multiFile) {
        // 获取文件名
        String fileName = multiFile.getOriginalFilename();
        // 获取文件后缀
        String prefix = fileName.substring(fileName.lastIndexOf("."));
        // 若需要防止生成的临时文件重复,可以在文件名后添加随机码

        try {
            File file = File.createTempFile(fileName, prefix);
            multiFile.transferTo(file);
            return file;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

