package cn.service.system.impl;

import java.util.regex.Pattern;
import java.util.stream.Stream;
import javax.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import cn.service.system.IMailService;
import cn.util.core.LogKit;

/**
 * <p>
 * 服务类
 * </p>
 *
 * @author zks
 * @since 2021-05-18
 */
@Service
public class MailServiceImpl implements IMailService {
   /* @Resource
    private OfficesitePaymentDao officesitePaymentDao;

    @Resource
    private OfficesiteDao officesiteDao;

    @Value("${spring.mail.username}")
    private String from;

    @Value("${officesite.mailReceiver}")
    private String to;

    @Value("${officesite.mailSendTime}")
    private ArrayList<Integer> mailSendTime;
*/
    @Resource
    private JavaMailSender mailSender;

    private final Pattern MAIL_PATTERN = Pattern.compile("^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$");

    public void batchSendMail(String from, String to, String title, String content) {
        Stream.of(to.split(",")).map(String::trim).filter(MAIL_PATTERN.asPredicate()).forEach(t -> {
            SimpleMailMessage message = new SimpleMailMessage();
            //发送人
            message.setFrom(from);
            //收件人
            message.setTo(t);
            //邮件名
            message.setSubject(title);
            //邮件内容
            message.setText(content);
            mailSender.send(message);
        });
        LogKit.info("办公区邮件已经发送, 收件人:[" + to + "]");
    }

    /**
     * 扫表然后发邮件
     */
    @Override
    public void scanAndSendMails() {

       /* List<OfficesitePayment> paymentRecords = new ArrayList<>();
        for (Integer delay : mailSendTime) {
            Date currentDate = new Date();
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(currentDate);
            //把日期往后增加7天,整数  往后推,负数往前移动
            calendar.add(Calendar.DATE, delay);
            //这个时间就是日期往后推7天的结果
            Date futureDate = calendar.getTime();
           // paymentRecords.addAll(officesitePaymentDao.selectByPayday(futureDate));
        }*/

        StringBuilder sb = new StringBuilder();
        sb.append(StringUtils.rightPad("序号", 6))
                .append(StringUtils.rightPad("办公区及办事处", 40))
                .append(StringUtils.rightPad("待付款类型", 20))
                .append(StringUtils.rightPad("待付款日期", 20))
                .append(StringUtils.rightPad("待付款金额", 20))
                .append("\n");
        //AtomicInteger num = new AtomicInteger(1);

      /*  paymentRecords.forEach(payment -> {
            String index, officesiteName, paymentType, payday, moneyAmount;
            index = String.valueOf(num.getAndIncrement());
            officesiteName = payment.getOfficesite().getOfficesiteName();
            paymentType = payment.getPaymentType();
            payday = DateUtil.format(payment.getPayDay(), DateFormat.STANDARD_DATE_FORMAT);
            moneyAmount = String.valueOf(payment.getMoneyAmount());
            sb.append(StringUtils.rightPad(index, 6))
                    .append(StringUtils.rightPad(officesiteName, 40))
                    .append(StringUtils.rightPad(paymentType, 20))
                    .append(StringUtils.rightPad(payday, 20))
                    .append(StringUtils.rightPad(moneyAmount, 20))
                    .append("\n");

        });*/
       LogKit.info("[办公区payment邮件发送提醒] 内容: \n" + sb.toString());
       //batchSendMail(from, to, "【办公区/办事处待付款提醒】_" + DateUtil.formatDate(new Date()), sb.toString());
    }
}
