package com.hys.app.controller.erp.use.msg;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hys.app.controller.erp.use.msg.entity.MessageReceiveTypeEnum;
import com.hys.app.controller.erp.use.msg.msgTemplate.MailMsgContant;
import com.hys.app.model.erp.dos.MessageReceiveDO;
import com.hys.app.model.erp.dos.SmsMailTemplate;
import com.hys.app.service.erp.MessageReceiveManager;
import com.hys.app.service.erp.SmsMailManager;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

@AllArgsConstructor
@Service
public class MsgUtilClient {

    private SmsMailManager smsMailManager;
    private MessageReceiveManager messageReceiveManager;

    /**
     * 发系统消息
     */
    public  void sendSys(String contentSys, String defContent, Long userId, Long companyId, String... format) {
//        try {
            SmsMailTemplate one = smsMailManager.getOne(new LambdaQueryWrapper<SmsMailTemplate>().eq(SmsMailTemplate::getCode, contentSys));
            String content = ObjectUtil.isNotNull(one) ? one.getContent() : defContent;
            String formatContent = StrUtil.format(content, format);
            MessageReceiveDO anEnum = new MessageReceiveDO();
            anEnum.setType(MessageReceiveTypeEnum.SYSTEM);
            anEnum.setContent(formatContent);
            anEnum.setUserId(userId);
            anEnum.setCompanyId(companyId);
            messageReceiveManager.save(anEnum);
//        }catch (Exception e){
//
//        }
    }

    /**
     * 发邮件
     */
    public  void sendMail(String contentSys, String defContent, String subHead,String toMail, Long companyId, String... format)   {
        try {
            SmsMailTemplate one = smsMailManager.getOne(new LambdaQueryWrapper<SmsMailTemplate>().eq(SmsMailTemplate::getCode, contentSys));
            String content = ObjectUtil.isNotNull(one) ? one.getContent() : defContent;
            String formatContent = StrUtil.format(content, format);
            String header = ObjectUtil.isNotNull(one) ? one.getName() : subHead;
            MailSmtpUtil.sendEmail(toMail,header,formatContent);
            MessageReceiveDO anEnum = new MessageReceiveDO();
            anEnum.setType(MessageReceiveTypeEnum.MAIL);
            anEnum.setContent(formatContent);
            anEnum.setMail(toMail);
            anEnum.setCompanyId(companyId);
            messageReceiveManager.save(anEnum);
        }catch (Exception e){
            System.out.println("11111111111111");
        }
    }

    /**
     * 发短信
     */
    public  void sendSms() {


    }


//    /**
//     * 发短信 发系统消息
//     */
//    public static void sendSmsSys() {
//
//    }
//
//
//    /**
//     * 发短信 发邮件
//     */
//    public static void sendSmsMail() {
//
//    }
//
//
//    /**
//     * 发邮件 发系统消息
//     */
//    public static void sendSysMail() {
//
//    }
//
//    /**
//     * 发邮件 发系统消息  发短信
//     */
//    public static void sendSysMailSms() {
//
//    }


}
