package com.jkcredit.invoice.hub.task;

import com.jkcredit.invoice.hub.model.dto.userBalance.UserBalanceForMailDto;
import com.jkcredit.invoice.hub.service.user.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author xusonglin
 * @version V1.0
 **/
@Component
@Slf4j
public class SendMailTask {
    @Value("${mail.host}")
    private String host;
    @Value("${mail.username}")
    private String username;
    @Value("${mail.password}")
    private String password;
    @Value("${mail.sendFrom}")
    private String sendFrom;
    @Value("${mail.sendTo}")
    private String sendTo;
    @Value("${mail.sendCc}")
    private String sendCc;
    @Value("${mail.authentication}")
    private String authentication;

    @Autowired
    UserService userService;

    @Scheduled(cron = "0 0 0 * * ?", zone = "Asia/Shanghai")
    public void sendBalanceMail() {
        List<UserBalanceForMailDto> userBalanceForMailDtoList = userService.getUserBalance();
        //核心类
        SimpleMailMessage message = new SimpleMailMessage();
        //邮件发送方
        message.setFrom(sendFrom);
        //邮件接收方
        message.setTo(sendTo);
        //标题
        message.setSubject("客户余额");
        //静态网页
        String text = getUserBalanceText(userBalanceForMailDtoList);
        log.info(text);
        message.setText(text);
        message.setCc(sendCc);
        try {
            //JavaMailSender接口的实例化对象
            JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
            // 设置邮件服务主机
            mailSender.setHost(host);
            // 发送者邮箱的用户名
            mailSender.setUsername(username);
            // 发送者邮箱的密码 就是网站发给你的  授权码
            mailSender.setPassword(authentication);
            //发送邮件
            mailSender.send(message);
        } catch (Exception e) {
            e.printStackTrace();

        }
    }

    private String getUserBalanceText(List<UserBalanceForMailDto> userBalanceForMailDtoList) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        StringBuilder builder = new StringBuilder();
        builder.append(format.format(new Date())).append("日客户余额").append(System.getProperty("line.separator")).append(System.getProperty("line.separator"));
        for (UserBalanceForMailDto dto : userBalanceForMailDtoList) {
            builder.append("公司:").append(dto.getCompany()).append("    ");
            builder.append("appKey:").append(dto.getAppKey()).append("    ");
            builder.append("余额:").append(dto.getBalance());
            builder.append(",");
            builder.append(System.getProperty("line.separator"));
        }
        return builder.toString();
    }
}
