/*
 * @Author: Kane
 * @Date: 2022-04-22 10:53:49
 * @LastEditors: Kane
 * @LastEditTime: 2022-04-22 16:22:12
 * @FilePath: \DisasterWarning\src\main\java\com\cpic\xim\wechat\officalAccount\sendMessage.java
 * @Description: 用来推送公众号消息的程序库。
 * 
 * Copyright (c) ${2022} by Kane, All Rights Reserved. 
 */
package com.cpic.xim.wechat.officalAccount;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.*;

import java.net.URL;
import java.net.HttpURLConnection;
import java.nio.charset.StandardCharsets;
import java.util.Map;

/**
 * 推送微信公众号消息。
 * 
 * @author Kane
 */
public class sendMessage
{
    /**
     * 向公众号推送通知消息。接口文档参考 彭奕洁 编写《消息发送接口调用文档》
     *
     * @param wechatOfficalAccountURL 公众号接口网址。
     * @param title 通知的标题。
     * @param notifyType 通知类型。
     * @param notifyMessage 通知文本内容。
     */
    public static void postNotifyMessageJSON( String wechatOfficalAccountURL, String title,
            String notifyType, String notifyMessage )
    {
        // 设置推送内容
        WechatOfficalAccountMessageParameter param = new WechatOfficalAccountMessageParameter();

        param.setFirst( title );
        param.setKeyword1( notifyType );
        param.setKeyword1Color( "#ff0000" );
        param.setKeyword2( notifyMessage );

        // 转换成json
        ObjectMapper mapper;
        String json = null;

        try
        {
            mapper = new ObjectMapper();
            mapper.setSerializationInclusion( JsonInclude.Include.NON_NULL );

            json = mapper.writeValueAsString( param );

            if ( json.isEmpty())
            {

            }

            System.out.println( json );
        }
        catch (JsonProcessingException error)
        {
            error.printStackTrace();

            return;
        }

        // 推送数据
        URL url;
        HttpURLConnection connection = null;

        StringBuilder result = new StringBuilder();
        OutputStreamWriter out = null;
        BufferedReader in = null;

        try
        {
            url = new URL( wechatOfficalAccountURL );
            connection = (HttpURLConnection) url.openConnection();

            connection.setRequestMethod( "POST" );
            connection.setRequestProperty( "accept", "*/*" );
            connection.setRequestProperty( "Connection", "Keep-Alive" );
            connection.setRequestProperty( "Content-Type", "application/json" );
            connection.setDoInput( true );
            connection.setDoOutput( true );
            connection.connect();

            out = new OutputStreamWriter( connection.getOutputStream(), StandardCharsets.UTF_8 );
            out.write( json );
            out.flush();

            in = new BufferedReader( new InputStreamReader( connection.getInputStream() ) );

            String line = in.readLine();

            while (line != null)
            {
                result.append( line );

                line = in.readLine();
            }
        }
        catch (Exception error)
        {
            error.printStackTrace();
        }
        finally
        {
            try
            {
                if ( in != null)
                {
                    in.close();
                }

                if ( out != null)
                {
                    out.close();
                }

                if ( connection != null)
                {
                    connection.disconnect();
                }
            }
            catch (Exception error)
            {
                error.printStackTrace();
            }
        }

    }

    /**
     * @
     */
    public static void postNotifyMessage( String URL, Map<String, String> header, String param )
    {

    }
}


class WechatOfficalAccountMessageParameter
{

    WechatOfficalAccountMessageParameter()
    {
        tplID = 57;
        groupID = 1;
    }

    @JsonProperty( "tplid")
    private int tplID;

    @JsonProperty( "groupid")
    private int groupID;

    @JsonProperty( "first")
    private String first;

    @JsonProperty( "keyword1")
    private String keyword1;

    @JsonProperty( "keyword1color")
    private String keyword1Color;

    @JsonProperty( "keyword2")
    private String keyword2;

    @JsonProperty( "keyword2color")
    private String keyword2Color;

    @JsonProperty( "remark")
    private String remark;

    @JsonProperty( "remarkcolor")
    private String remarkColor;

    @JsonProperty( "url")
    private String url;

    @JsonProperty( "appid")
    private String appid;

    @JsonProperty( "pagepath")
    private String pagePath;

    public int getTplID()
    {
        return tplID;
    }

    public void setTplID( int tplID )
    {
        this.tplID = tplID;
    }

    public int getGroupID()
    {
        return groupID;
    }

    public void setGroupID( int groupID )
    {
        this.groupID = groupID;
    }

    public String getFirst()
    {
        return first;
    }

    public void setFirst( String first )
    {
        this.first = first;
    }

    public String getKeyword1()
    {
        return keyword1;
    }

    public void setKeyword1( String keyword1 )
    {
        this.keyword1 = keyword1;
    }

    public String getKeyword1Color()
    {
        return keyword1Color;
    }

    public void setKeyword1Color( String keyword1Color )
    {
        this.keyword1Color = keyword1Color;
    }

    public String getKeyword2()
    {
        return keyword2;
    }

    public void setKeyword2( String keyword2 )
    {
        this.keyword2 = keyword2;
    }

    public String getKeyword2Color()
    {
        return keyword2Color;
    }

    public void setKeyword2Color( String keyword2Color )
    {
        this.keyword2Color = keyword2Color;
    }

    public String getRemark()
    {
        return remark;
    }

    public void setRemark( String remark )
    {
        this.remark = remark;
    }

    public String getRemarkColor()
    {
        return remarkColor;
    }

    public void setRemarkColor( String remarkColor )
    {
        this.remarkColor = remarkColor;
    }

    public String getUrl()
    {
        return url;
    }

    public void setUrl( String url )
    {
        this.url = url;
    }

    public String getAppid()
    {
        return appid;
    }

    public void setAppid( String appid )
    {
        this.appid = appid;
    }

    public String getPagePath()
    {
        return pagePath;
    }

    public void setPagePath( String pagePath )
    {
        this.pagePath = pagePath;
    }
}
