钉钉消息发送(安装dotnet-sdk-3.1.426-win-x64)
yangle
2024-12-05 b8df53771425ac543c4cb094186cd2e39ea1fd11
DingDingMsg_EarlyWarning/Program.cs
@@ -15,13 +15,15 @@
        static async Task Main(string[] args)
        {
            //本地
            string appKey = "dingrsrzhdyn3mlaof95";
            string appSecret = "RAqH6YtZnPLCpDbuqfaYQkKkVtVdS0wqfC8I26X6qiS-8eoCJCNrzx3fubGND4Sq";
            //string appKey = "dingrsrzhdyn3mlaof95";
            //string appSecret = "RAqH6YtZnPLCpDbuqfaYQkKkVtVdS0wqfC8I26X6qiS-8eoCJCNrzx3fubGND4Sq";
            //斯莫尔
            //string appKey = "dingkdddbhdcssk7jduw";
            //string appSecret = "iv07c-GLfJPnzfJaNAAOfJDl3Z-eODvDAhlInMZCZhGorkle5Evbaxx3ImylvdjQ";
            string appKey = "dingkdddbhdcssk7jduw";
            string appSecret = "iv07c-GLfJPnzfJaNAAOfJDl3Z-eODvDAhlInMZCZhGorkle5Evbaxx3ImylvdjQ";
            Console.WriteLine("程序2正在执行,请不要关闭!!!!");
            string HDeptName = "";
            string HWarningDate = "10";
            string HRecordDate = DateTime.MinValue.ToString(); ;
            // 记录上次执行新定时任务的时间
            while (true)
            {
@@ -123,9 +125,9 @@
                            string accessToken = responseJson["accessToken"].ToString();
                            //调用方法发送消息           
                            string agentId = "3118119317";//钉钉后台建立的小程序id
                            //string agentId = "3118119317";//钉钉后台建立的小程序id
                            //斯莫尔
                            //string agentId = "3151454458";//钉钉后台建立的小程序id
                            string agentId = "3151454458";//钉钉后台建立的小程序id
                            string userIdList = HName;//钉钉人员的id
                            string deptIdList = "0"; // 空字符串表示不指定部门
                            string toAllUser = "false";
@@ -146,6 +148,114 @@
                        }
                    }
                }
                DBHelper.CustomWriteLog("设备采集异常开始!", DateTime.Now.ToString("yyyy-MM-dd"));
                //设备采集异常时(开工后,设备持续10分钟未采集),进行消息预警
                //获取 为预警  并且状态为开工的 设备 和工艺参数
                dt = oCN.RunProcReturn("exec  h_p_Sb_EquiMessageWarn", "h_p_Sb_EquiMessageWarn");
                if (dt.Tables[0].Rows.Count > 0 && (DateTime.Now - DateTime.Parse(HRecordDate)).TotalMinutes >= int.Parse(HWarningDate))
                {
                    string HEquipFileName = "";
                    string HRemark = "";
                    //为预警的设备 和 工艺参数 进行循环
                    for (int i = 0; i < dt.Tables[0].Rows.Count; i++)
                    {
                        HRecordDate = dt.Tables[0].Rows[i]["HRecordDate"].ToString();//记录时间
                        HWarningDate = dt.Tables[0].Rows[i]["HWarningDate"].ToString();//预警时间
                        string HCollectionDate = dt.Tables[0].Rows[i]["HCollectionDate"].ToString();//采集时间
                        string HEquipFileNo = dt.Tables[0].Rows[i]["HEquipFileNo"].ToString();//设备编号
                        string HParameterName = dt.Tables[0].Rows[i]["HName"].ToString();//工艺参数
                        if ((DateTime.Now - DateTime.Parse(HRecordDate)).TotalMinutes >= int.Parse(HWarningDate))
                        {
                            DataSet ds = oCN.RunProcReturn("exec h_p_Sb_EquipMentTechParam_Temp '" + HEquipFileNo + "', '" + HParameterName + "', '" + HCollectionDate + "'", "h_p_Sb_EquipMentTechParam_Temp");
                            if (ds.Tables[0].Rows.Count > 0)
                            {
                                string HMaxCreateTime = ds.Tables[0].Rows[0]["HCreateTime"].ToString();
                                if ((DateTime.Now - DateTime.Parse(HMaxCreateTime)).TotalMinutes >= int.Parse(HWarningDate))
                                {
                                    if (HEquipFileName != HEquipFileNo)
                                    {
                                        HRemark += "设备编码:" + HEquipFileNo + "---";
                                        HEquipFileName = HEquipFileNo;
                                    }
                                    DBHelper.CustomWriteLog("最大时间:" + HMaxCreateTime + "," + i, DateTime.Now.ToString("yyyy-MM-dd"));
                                    //发送消息
                                    HRemark += HParameterName + ",";
                                    DBHelper.CustomWriteLog("更新时间:" + HEquipFileNo, DateTime.Now.ToString("yyyy-MM-dd"));
                                    //更新记录时间
                                    oCN.RunProc("update Gy_EquipFileBillMain set HRecordDate=getdate() where HEquipFileNo='" + HEquipFileNo + "'");
                                }
                                DBHelper.CustomWriteLog("设备采集异常开始---不需要预警!", DateTime.Now.ToString("yyyy-MM-dd"));
                            }
                            else
                            {
                                if (HEquipFileName != HEquipFileNo)
                                {
                                    HRemark += "设备编码:" + HEquipFileNo + "---";
                                    HEquipFileName = HEquipFileNo;
                                }
                                //发送消息
                                HRemark += HParameterName + ",";
                                //更新记录时间
                                oCN.RunProc("update Gy_EquipFileBillMain set HRecordDate=getdate() where HEquipFileNo='" + HEquipFileNo + "'");
                            }
                        }
                    }
                    HRecordDate = DateTime.Now.ToString();
                    if (HRemark.Length > 0)
                    {
                        HRemark += "没有采集到数据!";
                        string sql2 = "select a.HDingDingUserID 钉钉id,a.Czymc 接收人 from Gy_Czygl a left join System_UserGroupInfo b on a.Czybm = b.UserId" +
                              "  left join System_UserGroup c on b.GroupId = c.GroupID  where c.GroupName = '参数预警'";
                        dts = oCN.RunProcReturn(sql2, "Gy_Czygl");
                        for (int j = 0; j < dts.Tables[0].Rows.Count; j++)
                        {
                            string HName = dts.Tables[0].Rows[j]["钉钉id"].ToString();
                            string Stares = dts.Tables[0].Rows[j]["接收人"].ToString();
                            MSG msg = new MSG();
                            //获取企业的access_token的值
                            string response = msg.GetAccessToken(appKey, appSecret);
                            JObject responseJson = JObject.Parse(response);
                            // 获取access_token的值
                            string accessToken = responseJson["accessToken"].ToString();
                            //调用方法发送消息
                            //string agentId = "3118119317";//钉钉后台建立的小程序id
                            //斯莫尔
                            string agentId = "3151454458";//钉钉后台建立的小程序id
                            string userIdList = HName;//钉钉人员的id
                            string deptIdList = "0"; // 空字符串表示不指定部门
                            string toAllUser = "false";
                            string message = HRemark;
                            // 调用方法发送消息
                            response = await msg.SendTextMessage(accessToken, agentId, userIdList, deptIdList, toAllUser, message);
                            ResponseData responseData = JsonConvert.DeserializeObject<ResponseData>(response);
                            if (responseData.errcode == 0)
                            {
                                DBHelper.CustomWriteLog("钉钉发送信息成功!", DateTime.Now.ToString("yyyy-MM-dd"));
                            }
                            else
                            {
                                Console.WriteLine(responseData.errcode);
                                DBHelper.CustomWriteLog("钉钉返回信息:" + response, DateTime.Now.ToString("yyyy-MM-dd"));
                            }
                        }
                    }
                }
                DBHelper.CustomWriteLog("设备采集异常结束!", DateTime.Now.ToString("yyyy-MM-dd"));
                // 等待一分钟
                await Task.Delay(TimeSpan.FromMinutes(1));
            }