zrg
2026-02-27 7ca0d964d2df19bf56e8a8a96f34f26a139b078d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
using com.igetui.api.openservice;
using com.igetui.api.openservice.igetui;
using com.igetui.api.openservice.igetui.template;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Data;
 
namespace MesMessage
{
    public static class GeTuiService
    {
        // 个推接口地址(使用 HTTPS)
        private static readonly string HOST = "https://api.getui.com/apiex.htm";
        private static readonly string APPID = "X3I10VqaROAH8C7ElIc1L5";
        private static readonly string APPKEY = "V425beWn5K65BcOYKTcXx8";
        private static readonly string MASTERSECRET = "LOXAgRbXS98NxHKUXmlJQ";
        static DBHelper oCN = new DBHelper();
        static DataSet ds = new DataSet();
 
        // 注意:不再声明静态 IGtPush 字段,避免类型初始化异常
 
        /// <summary>
        /// 单条消息推送(工作联系单)
        /// </summary>
        public static void PubishSingle(int HInterID)
        {
 
 
            try
            {
                JObject NameMsgKVP = new JObject();
                ds = oCN.RunProcReturn($@"
                    select * from h_v_OA_WorkLinkBillAllList where hmainid = {HInterID}
                    select * from Gy_UserClientIdRelation
                ", "h_v_OA_WorkLinkBillAllList");
                NotificationTemplate template = null;
                ConcurrentDictionary<string, List<string>> userClientIDDictionary = new ConcurrentDictionary<string, List<string>>();
                // 将用户设备关联表 收集为 Map
                if (ds.Tables[1].Rows.Count > 0)
                {
                    foreach (DataRow row in ds.Tables[1].Rows)
                    {
                        if (userClientIDDictionary.ContainsKey(row["HUserName"].ToString()))
                        {
                            userClientIDDictionary[row["HUserName"].ToString()].Add(row["HClientID"].ToString());
                        }
                        else
                        {
                            userClientIDDictionary.TryAdd(row["HUserName"].ToString(), new List<string>() { row["HClientID"].ToString() });
                        }
                    }
                }
                if (ds.Tables[0].Rows.Count > 0)
                {
                    string title = ds.Tables[0].Rows[0]["主题"].ToString();
                    string content = ds.Tables[0].Rows[0]["内容"].ToString();
                    template = GetNotificationTemplate(title, content, "/pages/ZLGL/OA_WorkLink/OA_WorkLinkBillQuery", "0"); // 跳转到工作联系单查询页面
 
                    bool pushSuccess = false;
                    // 广播
                    if (ds.Tables[0].Rows[0]["发送类型"].ToString() == "公共")
                    {
                        string rule = GetSystemRule();
                        if (rule == "华舟")
                        {
                            // 推送给接收人 和 抄送人
                            foreach (DataRow row in ds.Tables[0].Rows)
                            {
                                List<string> clientIDList = new List<string>();
                                if (userClientIDDictionary.ContainsKey(row["接收人"].ToString()))
                                {
                                    clientIDList = userClientIDDictionary[row["接收人"].ToString()];
                                }
                                else if (userClientIDDictionary.ContainsKey(row["抄送接收人"].ToString()))
                                {
                                    clientIDList = userClientIDDictionary[row["抄送接收人"].ToString()];
                                }
 
 
                                foreach (string clientID in clientIDList)
                                {
                                    pushSuccess = PushMessageToSingle(template, clientID);
 
                                }
                            }
                        }
                        else
                        {
                            // 遍历 全推送
                            foreach (DataRow row in ds.Tables[1].Rows)
                            {
                                pushSuccess = PushMessageToSingle(template, row["HClientID"].ToString());
                            }
                        }
                    }
                    else // 单独推送
                    {
                        // 推送给接收人 和 抄送人
                        foreach (DataRow row in ds.Tables[0].Rows)
                        {
                            List<string> clientIDList = new List<string>();
                            if (userClientIDDictionary.ContainsKey(row["接收人"].ToString()))
                            {
                                clientIDList = userClientIDDictionary[row["接收人"].ToString()];
                            }
                            else if (userClientIDDictionary.ContainsKey(row["抄送接收人"].ToString()))
 
                            {
                                clientIDList = userClientIDDictionary[row["抄送接收人"].ToString()];
                            }
 
 
                            foreach (string clientID in clientIDList)
                            {
                                pushSuccess = PushMessageToSingle(template, clientID);
 
                            }
                        }
                    }
 
                    if (!pushSuccess)
                    {
                        // 个推 推送不成功(内网环境) 使用webSocket进行推送
                        // TODO: 使用 rabbitMQ 实现消息队列
                        // WebSocketServer.PushOne(HInterID);
                    }
                }
            }
            catch (Exception ex)
            {
                DBHelper.CustomWriteLog(ex.ToString(), DateTime.Now.ToString("yyyy-MM-dd"));
                return;
            }
        }
 
        /// <summary>
        /// 单推消息(通知模板)
        /// </summary>
        private static bool PushMessageToSingle(NotificationTemplate template, string clientId)
        {
            try
            {
                // 每次调用创建新实例,避免共享状态
                IGtPush push = new IGtPush(HOST, APPKEY, MASTERSECRET);
 
                SingleMessage message = new SingleMessage
                {
                    IsOffline = true,
                    OfflineExpireTime = 1000 * 3600 * 12, // 12小时
                    Data = template
                };
 
                Target target = new Target
                {
                    appId = APPID,
                    clientId = clientId
                };
 
                string pushResult = push.pushMessageToSingle(message, target);
                DBHelper.CustomWriteLog($"个推推送结果: {pushResult}", DateTime.Now.ToString("yyyy-MM-dd"));
                return true;
            }
            catch (RequestException ex)
            {
                // 可尝试重发(使用原请求ID)
                DBHelper.CustomWriteLog($"推送异常,请求ID: {ex.RequestId}, 错误: {ex}", DateTime.Now.ToString("yyyy-MM-dd"));
                return false;
            }
            catch (Exception ex)
            {
                DBHelper.CustomWriteLog($"推送异常: {ex}", DateTime.Now.ToString("yyyy-MM-dd"));
                return false;
            }
        }
 
        /// <summary>
        /// 创建通知模板(携带透传数据)
        /// </summary>
        public static NotificationTemplate GetNotificationTemplate(string title, string content, string pagePath, string itemId)
        {
            var template = new NotificationTemplate
            {
                AppId = APPID,
                AppKey = APPKEY,
                Title = title,
                Text = content,
                Logo = "icon",                 // 应用内图标名称(根据实际修改)
                LogoURL = "",                   // 网络图标,可留空
                IsRing = true,
                IsVibrate = true,
                IsClearable = true,
                TransmissionType = 1             // 1=点击通知触发客户端透传回调
            };
 
            // 透传内容:JSON 格式
            var customData = new Dictionary<string, string>
            {
                { "pagePath", pagePath },
                { "itemId", itemId }
            };
            template.TransmissionContent = JsonConvert.SerializeObject(customData);
 
            return template;
        }
 
        /// <summary>
        /// 获取系统参数 OA_AndengFlowRule_XT 的值
        /// </summary>
        private static string GetSystemRule()
        {
            string sql = "select HValue from xt_SystemParameter where HKey='WMS_CampanyName'";
            DataSet ds = oCN.RunProcReturn(sql, "xt_SystemParameter");
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                return ds.Tables[0].Rows[0]["HValue"].ToString();
            return null;
        }
 
    }
}