package org.dromara.web.service.impl;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.common.mail.utils.MailUtils;
import org.dromara.web.domain.bo.EmailSendBo;
import org.dromara.web.service.EmailService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Base64;

/**
 * @Author: Huanyi
 * @CreateTime: 2025-08-13
 * @Description:
 * @Version: 1.0
 */

@Slf4j
@RequiredArgsConstructor
@Service
public class EmailServiceImpl implements EmailService {

    @Override
    public R send(EmailSendBo bo) {

        String base64 = bo.getFileBase64();
        byte[] bytes = Base64.getDecoder().decode(base64);

        try {

            File goal = File.createTempFile("prefix", ".tmp");
            goal.deleteOnExit();

            FileOutputStream fos = new FileOutputStream(goal);
            fos.write(bytes);

            MailUtils.sendText(bo.getEmail(), "测试", bo.getMessage(), goal);
        } catch (Exception e) {
            log.error("邮箱发送失败 : {}", e.getMessage());
            throw new RuntimeException("邮箱发送失败 : " + e.getMessage());
        }

        return R.ok();
    }
}
