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

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hys.app.framework.exception.ServiceException;
import com.hys.app.model.erp.dos.MessageReceiveDO;
import com.hys.app.model.erp.dos.SmsMailTemplate;
import com.hys.app.model.system.dos.AdminUser;
import com.hys.app.service.erp.MessageReceiveManager;
import com.hys.app.service.erp.SmsMailManager;
import lombok.AllArgsConstructor;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.springframework.stereotype.Service;

import java.util.List;

@AllArgsConstructor
@Service
public class MsgUtilClient {

    private SmsMailManager smsMailManager;
    private MessageReceiveManager messageReceiveManager;

    /**
     * 发系统消息
     */
    public void sendSys(String templateCode, AdminUser adminUser, Object... format) {
        try {
            SmsMailTemplate one = smsMailManager.getOne(new LambdaQueryWrapper<SmsMailTemplate>().eq(SmsMailTemplate::getCode, templateCode));
            if (ObjectUtil.isNull(one)) {
                throw new ServiceException("未找到系统消息模板");
            }
            String formatContent = StrUtil.format(one.getContent(), format);
            MessageReceiveDO anEnum = new MessageReceiveDO();
            anEnum.setType(MessageReceiveTypeEnum.SYSTEM);
            anEnum.setContent(formatContent);
            anEnum.setUserId(adminUser.getId());
            anEnum.setCompanyId(adminUser.getCompanyId());
            messageReceiveManager.save(anEnum);
        } catch (Exception e) {
        }
    }

    /**
     * 发邮件
     */
    public void sendMail(String templateCode, AdminUser adminUser, Object... format) {
        try {
            SmsMailTemplate one = smsMailManager.getOne(new LambdaQueryWrapper<SmsMailTemplate>().eq(SmsMailTemplate::getCode, templateCode));
            if (ObjectUtil.isNull(one)) {
                throw new ServiceException("未找到邮件消息模板");
            }
            String formatContent = StrUtil.format(one.getContent(), format);
            MailSmtpUtil.sendEmail(adminUser.getEmail(), one.getName(), formatContent);
            MessageReceiveDO anEnum = new MessageReceiveDO();
            anEnum.setType(MessageReceiveTypeEnum.MAIL);
            anEnum.setContent(formatContent);
            anEnum.setMail(adminUser.getEmail());
            anEnum.setCompanyId(adminUser.getCompanyId());
            messageReceiveManager.save(anEnum);
        } catch (Exception e) {
        }
    }


    /**
     * 发公众号
     */
    public void sendOfficial(String code, AdminUser adminUser, Object... format) {
        try {
            SmsMailTemplate one = smsMailManager.getOne(new LambdaQueryWrapper<SmsMailTemplate>().eq(SmsMailTemplate::getTemplateCode, code));
            if (ObjectUtil.isNull(one)) {
                throw new ServiceException("未找到公众号消息模板");
            }
            if (StrUtil.isBlank(adminUser.getOpenid())) {
                throw new ServiceException("公众号通知失败，当前用户未绑定微信(前往个人信息里面进行绑定)");
            }
            WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                    .toUser(adminUser.getOpenid())
                    .templateId(one.getTemplateCode())
                    .build();

            //"character_string6--{}--#173177"  {}里面就是需要替换的内容
            String formatContent = StrUtil.format(one.getRemark(), format);
            String[] strings = formatContent.split(",");

            for (String string : strings) {
                String[] split = string.split("--");
                //公众号平台的字段名，字段值，颜色 添加模板的时候--连接
                WxMpTemplateData data = new WxMpTemplateData(split[0], split[1], "#173177");
                templateMessage.addData(data);
            }
            WxMpService wxService = new WxMpServiceImpl();
            WxMpDefaultConfigImpl config = new WxMpDefaultConfigImpl();
            config.setAppId(AliSmsUtil.access_id);  // 替换为你的 AppID
            config.setSecret(AliSmsUtil.access_secret);  // 替换为你的 AppSecret
            wxService.setWxMpConfigStorage(config);
            wxService.getTemplateMsgService().sendTemplateMsg(templateMessage);

            MessageReceiveDO anEnum = new MessageReceiveDO();
            anEnum.setType(MessageReceiveTypeEnum.OFFICIAL);
            anEnum.setContent(formatContent);
            anEnum.setOpenid(adminUser.getOpenid());
            anEnum.setCompanyId(adminUser.getCompanyId());
            messageReceiveManager.save(anEnum);
        } catch (Exception e) {
            System.out.println("11111111111111");
        }
    }


    /**
     * 发短信
     */
    public void sendSms(String templateCode, AdminUser adminUser, Object... format) {
        SmsMailTemplate one = smsMailManager.getOne(new LambdaQueryWrapper<SmsMailTemplate>().eq(SmsMailTemplate::getCode, templateCode));
        if (ObjectUtil.isNull(one)) {
            throw new ServiceException("未找到短信消息模板");
        }
        String formatContent = StrUtil.format(one.getRemark(), format);
        //添加模板消息的时候模板内容 应该为json格式    String aa = "{\"code\":{}\"}";
        AliSmsUtil.sendSms(adminUser.getPhone(), one.getTemplateCode(), formatContent);

        MessageReceiveDO anEnum = new MessageReceiveDO();
        anEnum.setType(MessageReceiveTypeEnum.SMS);
        anEnum.setContent(formatContent);
        anEnum.setPhone(adminUser.getPhone());
        anEnum.setCompanyId(adminUser.getCompanyId());
        messageReceiveManager.save(anEnum);
    }


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


}
