1
zrg
3 天以前 088f1c56e08047c21dcaebbab475d975531905bd
DingDingMsg_EarlyWarning/Program.cs
@@ -23,7 +23,8 @@
            Console.WriteLine("程序2正在执行,请不要关闭!!!!");
            string HDeptName = "";
            string HWarningDate = "10";
            string HRecordDate = DateTime.MinValue.ToString(); ;
            string HRecordDate = DateTime.MinValue.ToString();
            string HRecordDate_spc = "";
            // 记录上次执行新定时任务的时间
            while (true)
            {
@@ -94,12 +95,12 @@
                    "LEFT JOIN Gy_MouldFileMain c ON a.HMouldID = c.HInterID " +
                    "LEFT JOIN Gy_Department d ON a.HDeptID = d.HItemID " +
                    " WHERE  b.HErrBeginQty <= (select b.HUseNowQty+sum(HUseLife) from Sc_MouldLifeUseBillSub WHERE HMaterID = a.HMouldID) and b.HSendFlag = '0'";
                DBHelper.CustomWriteLog("查询模具保养计划:"+newsql, DateTime.Now.ToString("yyyy-MM-dd"));
                DBHelper.CustomWriteLog("查询模具保养计划:" + newsql, DateTime.Now.ToString("yyyy-MM-dd"));
                dt = oCN.RunProcReturn(newsql, "Sb_EquipMaintainPlanBillMain");
                //模具保养预警自动发信息
                if (dt.Tables[0].Rows.Count > 0)
                {
                    string HContext = "";
                    string HContext = "";
                    for (int i = 0; i < dt.Tables[0].Rows.Count; i++)
                    {
@@ -169,14 +170,15 @@
                        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)
@@ -215,6 +217,7 @@
                    {
                        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");
@@ -256,6 +259,81 @@
                }
                DBHelper.CustomWriteLog("设备采集异常结束!", DateTime.Now.ToString("yyyy-MM-dd"));
                DBHelper.CustomWriteLog("spc异常发消息!", DateTime.Now.ToString("yyyy-MM-dd"));
                //spc 异常表有未发消息的数据时 一小时 发送一次消息
                //获取 异常表 有未发消息的数据
                dt = oCN.RunProcReturn("select * from h_v_QC_SendMessageList", "h_v_QC_SendMessageList");
                if (HRecordDate_spc == "")
                {
                    HRecordDate_spc = DateTime.Now.ToString();
                }
                //根据30分钟时间 去发送消息
                if (dt.Tables[0].Rows.Count > 0 && (DateTime.Now - DateTime.Parse(HRecordDate_spc)).TotalMinutes >= 30)
                {
                    //为预警的设备 和 工艺参数 进行循环
                    for (int i = 0; i < dt.Tables[0].Rows.Count; i++)
                    {
                        string HMaterID = dt.Tables[0].Rows[i]["HMaterID"].ToString();
                        string HQCCheckItemID = dt.Tables[0].Rows[i]["HQCCheckItemID"].ToString();
                        //string HMaterNumber = dt.Tables[0].Rows[i]["物料代码"].ToString();
                        //string HQCCheckItemNumber = dt.Tables[0].Rows[i]["项目代码"].ToString();
                        string HMouldNum = dt.Tables[0].Rows[i]["穴号"].ToString();
                        string HRemark = dt.Tables[0].Rows[i]["错误数据"].ToString();
                        if (HRemark.Length > 0)
                        {
                            // 根据 角色 去查询需要发消息的钉钉号
                            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 = 'SPC数据异常预警'";
                            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;
                                DBHelper.CustomWriteLog("spc异常 当前时间:"+ HRecordDate_spc, DateTime.Now.ToString("yyyy-MM-dd"));
                                // 调用方法发送消息
                                response = await msg.SendTextMessage(accessToken, agentId, userIdList, deptIdList, toAllUser, message);
                                ResponseData responseData = JsonConvert.DeserializeObject<ResponseData>(response);
                                if (responseData.errcode == 0)
                                {
                                    DBHelper.CustomWriteLog("spc异常发息成功!", DateTime.Now.ToString("yyyy-MM-dd"));
                                    string sql3 = "update QC_SendMessage set HMessageIsSend=1 where HMaterID=" + HMaterID + " and HQCCheckItemID=" + HQCCheckItemID + " and HMouldNum='" + HMouldNum + "'";
                                    oCN.RunProc(sql3);
                                }
                                else
                                {
                                    Console.WriteLine(responseData.errcode);
                                    DBHelper.CustomWriteLog("spc异常返回信息:" + response, DateTime.Now.ToString("yyyy-MM-dd"));
                                }
                            }
                            HRecordDate_spc = DateTime.Now.ToString();
                        }
                    }
                }
                DBHelper.CustomWriteLog("spc异常发息结束!", DateTime.Now.ToString("yyyy-MM-dd"));
                // 等待一分钟
                await Task.Delay(TimeSpan.FromMinutes(1));
            }