package com.jpsoft.shinestar.modules.common.utils;

import cn.hutool.core.date.DateUtil;
import com.jpsoft.shinestar.modules.wechat.entity.AccessToken;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
@Service
public class WechatMessageUtil {
    public static final String send_template = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN";

    private static final String TEMPLATE_REDIRECT_PRE_URL = "http://wuyeopenlocktest-wecat.sudaonline.net";

    private static final String TEMPERATURE_ALARM_PRE_URL = "http://wisdomhousewechat.sudaonline.net/prevention/addetail_fll.html?id=";

    public static boolean sendBillInfo(String fullName, BigDecimal amount, String openId,
                                       String detailAmount, String propertyName, String billRemark, String billDetailId, String billTemplateId, String appId, String appSecret) {
        JSONObject sendData = new JSONObject();

        JSONObject first = new JSONObject();
        first.put("value", "您有一个新的账单，点击查看详情！");
        first.put("color", "#FF0000");

        JSONObject keyword1 = new JSONObject();
        keyword1.put("value", fullName);
        keyword1.put("color", "#173177");

        JSONObject keyword2 = new JSONObject();
        keyword2.put("value", "￥" + amount.setScale(2) + "元");
        keyword2.put("color", "#173177");

        JSONObject keyword3 = new JSONObject();
        keyword3.put("value", detailAmount);
        keyword3.put("color", "#173177");


        String remarkStr = "账单来源：" + propertyName;

        if (StringUtils.isNotBlank(billRemark)) {
            remarkStr = remarkStr + "\n备注：" + billRemark + "";
        }

        JSONObject remark = new JSONObject();
        remark.put("value", remarkStr);
        remark.put("color", "#173177");

        sendData.put("first", first);
        sendData.put("keyword1", keyword1);
        sendData.put("keyword2", keyword2);
        sendData.put("keyword3", keyword3);
        sendData.put("remark", remark);

        //w8Zk_VQMFIEVSIBPZid7zSrvHmBdrgnqF76u8PLCZEs cs
        boolean ret = sendTemplate(sendData, appId, appSecret, billTemplateId, openId, TEMPLATE_REDIRECT_PRE_URL + "/billDetail/" + billDetailId + "payTime=0&payType=bill");

        return ret;
    }

    //
    public static boolean sendBillPayedMessage(String fullName, String feeName, Date createTime, BigDecimal amount, String openId, String billDetailId, String payedTemplateId, String appId, String appSecret) {

        JSONObject sendData = new JSONObject();

        JSONObject first = new JSONObject();
        first.put("value", "账单支付成功！");
        first.put("color", "#FF0000");

        JSONObject keyword1 = new JSONObject();
        keyword1.put("value", fullName);
        keyword1.put("color", "#173177");

        JSONObject keyword2 = new JSONObject();
        keyword2.put("value", feeName);
        keyword2.put("color", "#173177");


        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        JSONObject keyword3 = new JSONObject();
        keyword3.put("value", sdf.format(createTime));
        keyword3.put("color", "#173177");

        JSONObject keyword4 = new JSONObject();

        keyword4.put("value", "￥" + amount.setScale(2) + "元");
        keyword4.put("color", "#173177");


        String remarkStr = "感谢您的支持，点击查看缴费详情";

        JSONObject remark = new JSONObject();
        remark.put("value", remarkStr);
        remark.put("color", "#173177");

        sendData.put("first", first);
        sendData.put("keyword1", keyword1);
        sendData.put("keyword2", keyword2);
        sendData.put("keyword3", keyword3);
        sendData.put("keyword4", keyword4);
        sendData.put("remark", remark);

        //w8Zk_VQMFIEVSIBPZid7zSrvHmBdrgnqF76u8PLCZEs cs
        boolean ret = sendTemplate(sendData, appId, appSecret, payedTemplateId, openId, TEMPLATE_REDIRECT_PRE_URL + "/billDetail/" + billDetailId + "payTime=1&payType=bill");

        return ret;
    }

    //
    public static boolean sendScanPayedMessage(String fullName, String feeName, Date createTime, BigDecimal amount, String openId, String independentPayId, String payedTemplateId, String appId, String appSecret) {

        JSONObject sendData = new JSONObject();

        JSONObject first = new JSONObject();
        first.put("value", "账单支付成功！");
        first.put("color", "#FF0000");

        JSONObject keyword1 = new JSONObject();
        keyword1.put("value", fullName);
        keyword1.put("color", "#173177");

        JSONObject keyword2 = new JSONObject();
        keyword2.put("value", feeName);
        keyword2.put("color", "#173177");


        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        JSONObject keyword3 = new JSONObject();
        keyword3.put("value", sdf.format(createTime));
        keyword3.put("color", "#173177");

        JSONObject keyword4 = new JSONObject();

        keyword4.put("value", "￥" + amount.setScale(2) + "元");
        keyword4.put("color", "#173177");


        String remarkStr = "感谢您的支持，点击查看缴费详情";

        JSONObject remark = new JSONObject();
        remark.put("value", remarkStr);
        remark.put("color", "#173177");

        sendData.put("first", first);
        sendData.put("keyword1", keyword1);
        sendData.put("keyword2", keyword2);
        sendData.put("keyword3", keyword3);
        sendData.put("keyword4", keyword4);
        sendData.put("remark", remark);

        boolean ret = sendTemplate(sendData, appId, appSecret, payedTemplateId, openId, TEMPLATE_REDIRECT_PRE_URL + "/billDetail/" + independentPayId + "payTime=1&payType=wechat");

        return ret;
    }

    /**
     * 发送通知信息
     *
     * @param noticeName     通知名称名称
     * @param noticeDetailId 通知详情ID
     * @param openId         发送人
     * @return
     */
    public static boolean sendNoticeMessage(String noticeName, String content, String openId, String noticeDetailId, String noticeTemplateId, String appId, String appSecret) {


        JSONObject sendData = new JSONObject();
        String finallyContent = getContent(content);
        JSONObject first = new JSONObject();
        first.put("value", "您好，园区通知如下：");
        first.put("color", "#173177");

        JSONObject keyword1 = new JSONObject();
        keyword1.put("value", noticeName);
        keyword1.put("color", "#173177");


        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        JSONObject keyword2 = new JSONObject();
        keyword2.put("value", sdf.format(new Date()));
        keyword2.put("color", "#173177");

        JSONObject keyword3 = new JSONObject();
        keyword3.put("value", finallyContent);
        keyword3.put("color", "#173177");

        JSONObject remark = new JSONObject();
        remark.put("value", "点击查看详情");
        remark.put("color", "#173177");

        sendData.put("first", first);
        sendData.put("keyword1", keyword1);
        sendData.put("keyword2", keyword2);
        sendData.put("keyword3", keyword3);
        sendData.put("remark", remark);

        //noticeDetailId
        boolean ret = sendTemplate(sendData, appId, appSecret, noticeTemplateId, openId, TEMPLATE_REDIRECT_PRE_URL + "/#/noticeInfo/" + noticeDetailId);

        return ret;
    }

    /**
     * 发送物业管理通知信息
     *
     * @param noticeName     通知名称
     * @param noticeDetailId 通知详情ID
     * @param openId         发送人
     * @return
     */
    public static boolean sendPropertyNoticeMessage(String noticeName, String content, String openId, String noticeDetailId, String noticeTemplateId, String appId, String appSecret) {


        JSONObject sendData = new JSONObject();
        String finallyContent = getContent(content);
        JSONObject first = new JSONObject();
        first.put("value", "您好，物业管理通知如下：");
        first.put("color", "#173177");

        JSONObject keyword1 = new JSONObject();
        keyword1.put("value", noticeName);
        keyword1.put("color", "#173177");


        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        JSONObject keyword2 = new JSONObject();
        keyword2.put("value", sdf.format(new Date()));
        keyword2.put("color", "#173177");

        JSONObject keyword3 = new JSONObject();
        keyword3.put("value", finallyContent);
        keyword3.put("color", "#173177");

        JSONObject remark = new JSONObject();
        remark.put("value", "点击查看详情");
        remark.put("color", "#173177");

        sendData.put("first", first);
        sendData.put("keyword1", keyword1);
        sendData.put("keyword2", keyword2);
        sendData.put("keyword3", keyword3);
        sendData.put("remark", remark);

        //noticeDetailId
        boolean ret = sendTemplate(sendData, appId, appSecret, noticeTemplateId, openId, TEMPERATURE_ALARM_PRE_URL + "/#/noticeInfo/" + noticeDetailId);

        return ret;
    }

    public static boolean sendAlarmNoticeMessage(String content, Date createTime,String url, String openId, String appId, String appSecret) {
        String noticeTemplateId = "-1He7VlL-468voet__6VHe8WteUHHPlyE53-LJMbKp8";

        JSONObject sendData = new JSONObject();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
//        String finallyContent = getContent(content);
        JSONObject first = new JSONObject();
        first.put("value", "您好，入侵警告通知如下：");
        first.put("color", "#173177");

        JSONObject keyword1 = new JSONObject();
        keyword1.put("value", sdf.format(createTime));
        keyword1.put("color", "#173177");

        JSONObject keyword2 = new JSONObject();
        keyword2.put("value", content);
        keyword2.put("color", "#173177");

        JSONObject remark = new JSONObject();
        remark.put("value", "请尽快查看");
        remark.put("color", "#173177");

        sendData.put("first", first);
        sendData.put("keyword1", keyword1);
        sendData.put("keyword2", keyword2);
        sendData.put("remark", remark);

        boolean ret = sendTemplate(sendData, appId, appSecret, noticeTemplateId, openId, url);

        return ret;
    }

    /**
     * 微信发送模版
     *
     * @param sendData   发送数据
     * @param templateId 模版Id
     * @param openId     发送人
     * @param url        详情跳转地址
     * @return
     */
    public static boolean sendTemplate(JSONObject sendData, String appId, String appSecret, String templateId, String openId, String url) {
        boolean result = false;

        AccessToken accessToken = WeixinUtil.getAccessToken(appId, appSecret);

        if (StringUtils.isEmpty(openId)){
            log.error("openId不能为空！templateId={}",templateId);
            return false;
        }

        if (accessToken != null) {
            //发送模版内容
            String sendTemplateUrl = send_template.replace("ACCESS_TOKEN", accessToken.getToken());

            JSONObject sendTemplateData = new JSONObject();

            sendTemplateData.put("touser", openId);
            sendTemplateData.put("template_id", templateId);
            sendTemplateData.put("url", url);
            sendTemplateData.put("data", sendData);

            log.warn("模版发送Id>>>>" + templateId + ">>>模版发送数据>>>>>" + sendTemplateData.toString());

            String sendTemplateRet = HttpConnectionUtil.requestByPost(sendTemplateUrl, sendTemplateData.toString());

            log.warn("模版发送返回数据>>>>>" + sendTemplateRet);

            if (StringUtils.isNotBlank(sendTemplateRet)) {
                JSONObject sendTemplateJson = JSONObject.fromObject(sendTemplateRet);

                if ("0".equals(sendTemplateJson.getString("errcode"))) {
                    //发送成功
                    result = true;
                }
            }
        }

        return result;
    }

    public static String sendNativeTemplate(String appId,String appSecret,String templateId, String openId,JSONObject sendData,String urlPath) {
        String result = "";

        try {
            AccessToken accessToken = WeixinUtil.getAccessToken(appId, appSecret);

            if (accessToken == null) {
                throw new Exception("token无法获取");
            }

            //发送模版内容
            String sendTemplateUrl = send_template.replace("ACCESS_TOKEN", accessToken.getToken());

            JSONObject sendTemplateData = new JSONObject();

            sendTemplateData.put("touser", openId);
            sendTemplateData.put("template_id", templateId);
            sendTemplateData.put("url", urlPath);
            sendTemplateData.put("data", sendData);

            URL url = new URL(sendTemplateUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setRequestProperty("Charset", "UTF-8");

            // 设置文件类型:
            conn.setRequestProperty("Content-Type","application/json; charset=UTF-8");

            // 设置接收类型否则返回415错误
            //conn.setRequestProperty("accept","*/*")此处为暴力方法设置接受所有类型，以此来防范返回415;
            conn.setRequestProperty("accept","application/json");
            conn.setConnectTimeout(5000);
            conn.setReadTimeout(5000);

            String dataStr = sendTemplateData.toString();

            log.warn("发送数据:" + dataStr);
            // 往服务器里面发送数据
            byte[] buffer = dataStr.getBytes("UTF-8");

            // 设置文件长度
            conn.setRequestProperty("Content-Length", String.valueOf(buffer.length));
            OutputStream output = conn.getOutputStream();
            output.write(buffer);
            output.flush();
            output.close();

            if (conn.getResponseCode() == 200) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));

                StringBuilder sb = new StringBuilder();
                String line = "";

                while ((line = reader.readLine()) != null) {
                    sb.append(line);
                }

                result = sb.toString();

                log.warn("接收数据:" + result);

                reader.close();
            }
        } catch (Exception ex) {
            log.error(ex.getMessage(),ex);
        }

        return result;
    }

    public static String getContent(String content) {
        String finallyContent = content;
        String regex = "<p.*?>(.*?)</p>";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(content);
        while (m.find()) {
            finallyContent = m.group(1);
        }


        return finallyContent;
    }

    /**
     * 发送温度报警模板
     *
     * @param openId
     * @param appId
     * @param appSecret
     * @return
     */
    public static boolean sendTemperatureAlarmInfo(String templateId,String openId,String message, String appId, String appSecret, String url) {
        JSONObject sendData = new JSONObject();

        JSONObject first = new JSONObject();
        first.put("value", "测温预警");
        first.put("color", "#FF0000");

        JSONObject keyword1 = new JSONObject();
        keyword1.put("value", "门禁测温");

        JSONObject keyword2 = new JSONObject();
        keyword2.put("value", DateUtil.format(new Date(), "yyyy-MM-dd HH:mm"));

        JSONObject keyword3 = new JSONObject();
        keyword3.put("value",message);

        String remarkStr = "发现有一例体温异常情况，点击查看详情";

        JSONObject remark = new JSONObject();
        remark.put("value", remarkStr);
        remark.put("color", "#173177");

        sendData.put("first", first);
        sendData.put("keyword1", keyword1);
        sendData.put("keyword2", keyword2);
        sendData.put("keyword3", keyword3);
        sendData.put("remark", remark);

        boolean ret = sendTemplate(sendData, appId, appSecret, templateId, openId, url);

        return ret;
    }

    /**
     * 发送车位成功租出
     *
     * @param openId
     * @param parkingName
     * @param appId
     * @param appSecret
     * @return
     */
    public static boolean sendParkingSuccessLease(String openId, String parkingName,String carNum,String time, String appId, String appSecret,String url,String remarkStr) {


        String templateId = "-T_wngEIyPJqao7pLCj3xvNVLD1KLppmu--XUX81cEI";
        JSONObject sendData = new JSONObject();

        JSONObject first = new JSONObject();
        first.put("value", "车位申请成功通知！");
        first.put("color", "#FF0000");

        JSONObject keyword1 = new JSONObject();
        keyword1.put("value", carNum);
        keyword1.put("color", "#173177");

        JSONObject keyword2 = new JSONObject();
        keyword2.put("value", time);
        keyword2.put("color", "#173177");

     //   String remarkStr = "您的车位申请已通过，申请车位："+parkingName;

        JSONObject remark = new JSONObject();
        remark.put("value", remarkStr);
        remark.put("color", "#173177");

        sendData.put("first", first);
        sendData.put("keyword1", keyword1);
        sendData.put("keyword2", keyword2);
        sendData.put("remark", remark);


        boolean ret = sendTemplate(sendData, appId, appSecret, templateId, openId, url);

        return ret;
    }

    /**
     * 发送车位租出失败
     *
     * @param openId
     * @param reason
     * @param appId
     * @param appSecret
     * @return
     */
    public static boolean sendParkingFailLease(String openId, String reason,String carNum,String time, String appId, String appSecret) {


        String templateId = "-T_wngEIyPJqao7pLCj3xvNVLD1KLppmu--XUX81cEI";
        JSONObject sendData = new JSONObject();

        JSONObject first = new JSONObject();
        first.put("value", "车位申请失败通知！");
        first.put("color", "#FF0000");

        JSONObject keyword1 = new JSONObject();
        keyword1.put("value", carNum);
        keyword1.put("color", "#173177");

        JSONObject keyword2 = new JSONObject();
        keyword2.put("value", time);
        keyword2.put("color", "#173177");

        String remarkStr = "您的车位申请被拒绝，因为："+reason+",您还可以重新申请租用其他剩余车位。";

        JSONObject remark = new JSONObject();
        remark.put("value", remarkStr);
        remark.put("color", "#173177");

        sendData.put("first", first);
        sendData.put("keyword1", keyword1);
        sendData.put("keyword2", keyword2);
        sendData.put("remark", remark);


        boolean ret = sendTemplate(sendData, appId, appSecret, templateId, openId, "");

        return ret;
    }

    /**
     * 发送待办事件
     *
     * @param openId
     * @param personName
     * @param appId
     * @param appSecret
     * @return
     */
    public static boolean sendEvenMessage(String openId,String firstMessage, String personName,String number,String remarkStr,String url, String appId, String appSecret) {


        String templateId = "GxkSsNkOi_43PAAeTAJa8D1qRRI2I2d5KxHlmc-rhas";
        JSONObject sendData = new JSONObject();

        JSONObject first = new JSONObject();
        first.put("value", firstMessage);
        first.put("color", "#FF0000");

        JSONObject keyword1 = new JSONObject();
        keyword1.put("value", personName);
        keyword1.put("color", "#173177");

        JSONObject keyword2 = new JSONObject();
        keyword2.put("value", number);
        keyword2.put("color", "#173177");

        JSONObject keyword3 = new JSONObject();
        keyword3.put("value", DateUtil.format(new Date(),"yyyy-MM-dd HH:mm"));
        keyword3.put("color", "#173177");

        JSONObject remark = new JSONObject();
        remark.put("value", remarkStr);
        remark.put("color", "#173177");

        sendData.put("first", first);
        sendData.put("keyword1", keyword1);
        sendData.put("keyword2", keyword2);
        sendData.put("keyword3", keyword3);
        sendData.put("remark", remark);


        boolean ret = sendTemplate(sendData, appId, appSecret, templateId, openId, url);

        return ret;
    }


    /**
     * 发送用药提醒模板消息
     *
     * @param openId
     * @param keyword1Value
     * @param appId
     * @param appSecret
     * @return
     */
    public static boolean sendMedicationMessage(String openId,String firstMessage, String keyword1Value,String keyword2Value,String remarkStr, String appId, String appSecret) {


        String templateId = "YsqhReXqWbYMCMa7QmAUbaZQRvOuBj9rfucA1rx5f_o";
        JSONObject sendData = new JSONObject();

        JSONObject first = new JSONObject();
        first.put("value", firstMessage);
        first.put("color", "#FF0000");

        JSONObject keyword1 = new JSONObject();
        keyword1.put("value", keyword1Value);
        keyword1.put("color", "#173177");

        JSONObject keyword2 = new JSONObject();
        keyword2.put("value", keyword2Value);
        keyword2.put("color", "#173177");

        JSONObject keyword3 = new JSONObject();
        keyword3.put("value", DateUtil.format(new Date(),"yyyy-MM-dd HH:mm"));
        keyword3.put("color", "#173177");

        JSONObject remark = new JSONObject();
        remark.put("value", remarkStr);
        remark.put("color", "#173177");

        sendData.put("first", first);
        sendData.put("keyword1", keyword1);
        sendData.put("keyword2", keyword2);
        sendData.put("keyword3", keyword3);
        sendData.put("remark", remark);


        boolean ret = sendTemplate(sendData, appId, appSecret, templateId, openId, "");

        return ret;
    }



    /**
     * 发送审核通过或拒绝提醒
     *
     * @param openId
     * @param personName
     * @param appId
     * @param appSecret
     * @return
     */
    public static boolean sendExamine(String openId,String firstMessage, String personName,String status,String statusN,String typeN,String remarkStr,String url, String appId, String appSecret) {

        String templateId = null;
        if ("1".equals(status)){
            templateId = "uiH4UgxS_y1LN71k5L10H6N9G6jQuqAS3m4-mhdKIKc";
        }
        if ("2".equals(status)){
            templateId = "uiH4UgxS_y1LN71k5L10H6N9G6jQuqAS3m4-mhdKIKc";
        }

        JSONObject sendData = new JSONObject();

        JSONObject first = new JSONObject();
        first.put("value", firstMessage);
        first.put("color", "#FF0000");

        JSONObject keyword1 = new JSONObject();
        keyword1.put("value", statusN);
        keyword1.put("color", "#173177");

        JSONObject keyword2 = new JSONObject();
        keyword2.put("value", personName);
        keyword2.put("color", "#173177");

        JSONObject keyword3 = new JSONObject();
        keyword3.put("value", typeN);
        keyword3.put("color", "#173177");

        JSONObject remark = new JSONObject();
        remark.put("value", remarkStr);
        remark.put("color", "#173177");

        sendData.put("first", first);
        sendData.put("keyword1", keyword1);
        sendData.put("keyword2", keyword2);
        sendData.put("keyword3", keyword3);
        sendData.put("remark", remark);


        boolean ret = sendTemplate(sendData, appId, appSecret, templateId, openId, url);

        return ret;
    }


    /**
     * 发送账单缴费提醒

     */
    public static boolean sendPayBill(String openId,String firstMessage, String payType,String totalFee,String cycle,String remarkStr,String url, String appId, String appSecret) {

        String templateId = "Wu8O6PGcW8O45L-HIP4VmpPlqXHWBUcQLmLdvPyz7Qc";

        JSONObject sendData = new JSONObject();

        JSONObject first = new JSONObject();
        first.put("value", firstMessage);
        first.put("color", "#FF0000");

        JSONObject keyword1 = new JSONObject();
        keyword1.put("value", payType);
        keyword1.put("color", "#173177");

        JSONObject keyword2 = new JSONObject();
        keyword2.put("value", totalFee+"元");
        keyword2.put("color", "#173177");

        JSONObject keyword3 = new JSONObject();
        keyword3.put("value", cycle);
        keyword3.put("color", "#173177");

        JSONObject remark = new JSONObject();
        remark.put("value", remarkStr);
        remark.put("color", "#173177");

        sendData.put("first", first);
        sendData.put("keyword1", keyword1);
        sendData.put("keyword2", keyword2);
        sendData.put("keyword3", keyword3);
        sendData.put("remark", remark);


        boolean ret = sendTemplate(sendData, appId, appSecret, templateId, openId, url);

        return ret;
    }

    /**
     * 发送到账提醒通知

     */
    public static boolean sendAccountRemind(String openId,String firstMessage,String payTime, String payType,String totalFee,String orderNo,String remarkStr,String url, String appId, String appSecret) {

        String templateId = "nypyaPUjuuHLWRn0gl-ifIQF5ciNKiKFrUa-ROdRy74";

        JSONObject sendData = new JSONObject();

        JSONObject first = new JSONObject();
        first.put("value", firstMessage);
        first.put("color", "#FF0000");

        JSONObject keyword1 = new JSONObject();
        keyword1.put("value", payTime);
        keyword1.put("color", "#173177");

        JSONObject keyword2 = new JSONObject();
        keyword2.put("value", totalFee+"元");
        keyword2.put("color", "#173177");

        JSONObject keyword3 = new JSONObject();
        keyword3.put("value", payType);
        keyword3.put("color", "#173177");

        JSONObject keyword4 = new JSONObject();
        keyword4.put("value", orderNo);
        keyword4.put("color", "#173177");

        JSONObject remark = new JSONObject();
        remark.put("value", remarkStr);
        remark.put("color", "#173177");

        sendData.put("first", first);
        sendData.put("keyword1", keyword1);
        sendData.put("keyword2", keyword2);
        sendData.put("keyword3", keyword3);
        sendData.put("keyword4", keyword4);
        sendData.put("remark", remark);


        boolean ret = sendTemplate(sendData, appId, appSecret, templateId, openId, url);

        return ret;
    }

    /**
     * 发送返乡登记信息新增

     */
    public static boolean sendReturnMessage(String openId,String firstMessage,String submitTime,String address,String adminName,String remarkStr,String url, String appId, String appSecret) {

        String templateId = "iAbFXWt_-WwYvu9CEFkTDeZwM3XoJqmB3ITIHS99WCI";

        JSONObject sendData = new JSONObject();

        JSONObject first = new JSONObject();
        first.put("value", firstMessage);
        first.put("color", "#FF0000");

        JSONObject keyword1 = new JSONObject();
        keyword1.put("value", submitTime);
        keyword1.put("color", "#173177");

        JSONObject keyword2 = new JSONObject();
        keyword2.put("value", address);
        keyword2.put("color", "#173177");

        JSONObject keyword3 = new JSONObject();
        keyword3.put("value", adminName);
        keyword3.put("color", "#173177");

        JSONObject remark = new JSONObject();
        remark.put("value", remarkStr);
        remark.put("color", "#173177");

        sendData.put("first", first);
        sendData.put("keyword1", keyword1);
        sendData.put("keyword2", keyword2);
        sendData.put("keyword3", keyword3);
        sendData.put("remark", remark);


        boolean ret = sendTemplate(sendData, appId, appSecret, templateId, openId, url);

        return ret;
    }

    /**
     * 发送考勤提醒

     */
    public static boolean sendWorkRemindMessage(String openId,String firstMessage,String name,String time,String desc,String remarkStr,String url, String appId, String appSecret) {

        String templateId = "6CMFjxHJA2lZWOFRKQ6tlVy2F_4Iq18EWsRCSsZbN58";

        JSONObject sendData = new JSONObject();

        JSONObject first = new JSONObject();
        first.put("value", firstMessage);
        first.put("color", "#FF0000");

        JSONObject keyword1 = new JSONObject();
        keyword1.put("value", name);
        keyword1.put("color", "#173177");

        JSONObject keyword2 = new JSONObject();
        keyword2.put("value", time);
        keyword2.put("color", "#173177");

        JSONObject keyword3 = new JSONObject();
        keyword3.put("value", desc);
        keyword3.put("color", "#173177");

        JSONObject remark = new JSONObject();
        remark.put("value", remarkStr);
        remark.put("color", "#173177");

        sendData.put("first", first);
        sendData.put("keyword1", keyword1);
        sendData.put("keyword2", keyword2);
        sendData.put("keyword3", keyword3);
        sendData.put("remark", remark);


        boolean ret = sendTemplate(sendData, appId, appSecret, templateId, openId, url);

        return ret;
    }


    /**
     * 发送抄送提醒

     */
    public static boolean sendCCRemindMessage(String openId,String firstMessage,String name,String examineType,String examineName,String time,String remarkStr,String url, String appId, String appSecret) {

        String templateId = "DZLTZ-D8iCxl3nApKvljjPgkH6Z2OTtS509I0RCzQiM";

        JSONObject sendData = new JSONObject();

        JSONObject first = new JSONObject();
        first.put("value", firstMessage);
        first.put("color", "#FF0000");

        JSONObject keyword1 = new JSONObject();
        keyword1.put("value", name);
        keyword1.put("color", "#173177");

        JSONObject keyword2 = new JSONObject();
        keyword2.put("value", examineType);
        keyword2.put("color", "#173177");

        JSONObject keyword3 = new JSONObject();
        keyword3.put("value", examineName);
        keyword3.put("color", "#173177");

        JSONObject keyword4 = new JSONObject();
        keyword4.put("value", time);
        keyword4.put("color", "#173177");

        JSONObject remark = new JSONObject();
        remark.put("value", remarkStr);
        remark.put("color", "#173177");

        sendData.put("first", first);
        sendData.put("keyword1", keyword1);
        sendData.put("keyword2", keyword2);
        sendData.put("keyword3", keyword3);
        sendData.put("keyword4", keyword4);
        sendData.put("remark", remark);


        boolean ret = sendTemplate(sendData, appId, appSecret, templateId, openId, url);

        return ret;
    }


    /**
     * 发送会议通知

     */
    public static boolean sendMeetingMessage(String openId,String meetingTitle,String meetingName,String meetingDateTime,String addressName,String remarkStr,String url, String appId, String appSecret) {

        String templateId = "_8CriG_FTkQ_hBF0CWhFrjBQSbCzi29ybjNU1ow0Hyw";

        JSONObject sendData = new JSONObject();

        JSONObject first = new JSONObject();
        first.put("value", meetingTitle);
        first.put("color", "#FF0000");

        JSONObject keyword1 = new JSONObject();
        keyword1.put("value", meetingName);
        keyword1.put("color", "#173177");

        JSONObject keyword2 = new JSONObject();
        keyword2.put("value", meetingDateTime);
        keyword2.put("color", "#173177");

        JSONObject keyword3 = new JSONObject();
        keyword3.put("value", addressName);
        keyword3.put("color", "#173177");

        JSONObject remark = new JSONObject();
        remark.put("value", remarkStr);
        remark.put("color", "#173177");

        sendData.put("first", first);
        sendData.put("keyword1", keyword1);
        sendData.put("keyword2", keyword2);
        sendData.put("keyword3", keyword3);
        sendData.put("remark", remark);


        boolean ret = sendTemplate(sendData, appId, appSecret, templateId, openId, url);

        return ret;
    }


}
