/*
 * @Author: Kane
 * @Date: 2022-04-22 10:53:49
 * @LastEditors: Kane
 * @LastEditTime: 2022-04-25 19:20:32
 * @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.JsonProperty;

import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Vector;

import com.cpic.xim.notify.disaster.*;
import com.cpic.xim.httpUtil.HttpUtils;

/**
 * 推送微信公众号消息。
 * 
 * @author Kane
 */
public class sendMessage
{
    private static HashMap<String, String> warningLevel;

    /**
     * 推送天气灾害预警！接口文档参考 彭奕洁 编写《消息发送接口调用文档》
     * 
     * @param officalAccountURL 产险厦门分公司公众号接口网址
     * @param warning           灾害预警对象
     */
    public static void sendWeatherDisasterWarning( String officalAccountURL,
            QWeatherDisasterWarning warning )
    {
        Vector<QWeatherDisasterWarningItem> warningItems = warning.getWarning();
        StringBuilder requestBody = null;

        // 设置请求头参数
        HashMap<String, String> headers = new HashMap<String, String>();

        headers.put( "accept", "*/*" );
        headers.put( "Connection", "Keep-Alive" );
        headers.put( "Content-Type", "application/x-www-form-urlencoded;charset=UTF-8" );

        // 遍历消息，将消息推送出去。
        for ( QWeatherDisasterWarningItem item : warningItems)
        {
            // 拼接消息内容
            String title = item.getTitle();
            String text = item.getText();
            String color = warningLevel.keySet().contains( item.getLevel() )
                    ? warningLevel.get( item.getLevel() )
                    : "#000000";
            requestBody = new StringBuilder();

            requestBody.append( "tplid=57&groupid=1&first=尊敬的#realname#，您好！&" );
            requestBody.append( "keyword1=" ).append( title ).append( "&" );
            requestBody.append( "keyword1color=" ).append( color ).append( "&" );
            requestBody.append( "keyword2=" ).append( text );

            try
            {
                HttpUtils.postHttpRequest( officalAccountURL, headers, requestBody.toString() );
            }
            catch (MalformedURLException error)
            {
                error.printStackTrace();
            }
        }
    }

    /**
     * 
     * @param warningID
     * @return
     */
    private boolean checkWarningHasSended( String warningID )
    {
        boolean result = false;



        return result;
    }

    static
    {
        // 预警级别色彩
        warningLevel = new HashMap<>();

        warningLevel.put( "蓝色", "#0000FF" );
        warningLevel.put( "黄色", "#FFFF00" );
        warningLevel.put( "橙色", "#FFA500" );
        warningLevel.put( "红色", "#FF0000" );
    }
}


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;
    }
}
