package cn.ibizlab.businesscentral.core.extensions.service;

import cn.ibizlab.businesscentral.core.odoo_mail.domain.Mail_message;
import cn.ibizlab.businesscentral.core.odoo_mail.service.impl.Mail_messageServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.Random;

@Slf4j
@Primary
@Service("Mail_messageExService")
public class Mail_messageExService extends Mail_messageServiceImpl {

    @Override
    protected Class currentModelClass() {
        return com.baomidou.mybatisplus.core.toolkit.ReflectionKit.getSuperClassGenericType(this.getClass().getSuperclass(), 1);
    }

    @Override
    public Mail_message generate_tracking_message_id(Mail_message et) {
        System.out.println(System.currentTimeMillis()/1000);

        et.setMessageId(String.format("<%s.%s.%s-openerp-%s@%s>",getRandom(),System.currentTimeMillis()/1000,getRandom(),et.getMessageId(),"a3f3484997ea"));
        return super.generate_tracking_message_id(et);
    }

    static private String getRandom(){
        Random random = new Random(System.currentTimeMillis());
        StringBuffer str = new StringBuffer();
        for (int i = 0; i < 15; i++) {
            int num = random.nextInt(9);
            str.append(num);
        }
        return str.toString();
    }

}
