using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Data; using System.Threading.Tasks; namespace DingDingMsg_MES { class Program { //内部任务汇报 发送钉钉消息 static DBHelper oCN = new DBHelper(); static DataSet dt = new DataSet(); static async Task Main(string[] args) { string sExeReturnInfo = ""; string BillType = "4510"; //本地 string appKey = "dingrsrzhdyn3mlaof95"; string appSecret = "RAqH6YtZnPLCpDbuqfaYQkKkVtVdS0wqfC8I26X6qiS-8eoCJCNrzx3fubGND4Sq"; MSG msg = new MSG(); Console.WriteLine("钉钉消息推送正在执行,请不要关闭!!!!"); while (true) { //获取企业的access_token的值 string response = msg.GetAccessToken(appKey, appSecret); JObject responseJson = JObject.Parse(response); // 获取access_token的值 string accessToken = responseJson["accessToken"].ToString(); DateTime now = DateTime.Now; // 如果当前时间是周六(6)或周日(7),跳过 if (now.DayOfWeek == DayOfWeek.Saturday || now.DayOfWeek == DayOfWeek.Sunday) { // 等待直到下一个周一 await WaitUntilNextMonday(); continue; } // 如果是每天的 16:45 执行操作 if (now.Hour == 16 && now.Minute == 45) { //查数据 string sql = @"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 = '每日任务与每周计划钉钉提醒'"; dt = oCN.RunProcReturn(sql, "Gy_Czygl"); if (dt.Tables[0].Rows.Count > 0) { foreach (DataRow row in dt.Tables[0].Rows) { string userId = row["钉钉id"].ToString(); string receiverName = row["接收人"].ToString(); string HContext = $"人员:{receiverName} 请及时编制当日的工作汇报!发送人为admin,接收人为 {receiverName}"; //获取单据内码 long HInterID = oCN.CreateBillID_Prod(BillType, ref sExeReturnInfo); string HBillNo = oCN.CreateBillCode(BillType, ref sExeReturnInfo, true); //插入主子数据 string sql2 = "INSERT INTO OA_EmpMessageBillMain (HYear,HPeriod,HBillType,HInterID,HDate,HBillNo,HBillStatus,HSendMan,HReceiveMan,HDescription,HMaker,HMakeDate)" + "VALUES('"+DateTime.Now.Year+ "','" + DateTime.Now.Month + "','"+ BillType + "','" + HInterID+"',getdate(),'"+HBillNo+"','1','admin','"+ receiverName + "','"+ HContext + "','自动生成',getdate())"; string sql3 = "INSERT INTO OA_EmpMessageBillSub (HInterID,HEntryID,HReceiveMan,HReadFlag,HSendFlag)" + "VALUES('" + HInterID + "','1','" + receiverName + "',0,0)"; oCN.RunProc(sql2); oCN.RunProc(sql3); //调用方法发送消息 string agentId = "3118119317";//钉钉后台建立的小程序id string userIdList = userId;//钉钉人员的id string deptIdList = "0"; // 空字符串表示不指定部门 string toAllUser = "false"; string message = HContext; // 调用方法发送消息 response = await msg.SendTextMessage(accessToken, agentId, userIdList, deptIdList, toAllUser, message); ResponseData responseData = JsonConvert.DeserializeObject(response); if (responseData.errcode == 0) { //更新状态 string sql1 = "update OA_EmpMessageBillSub set HSendFlag='1' where HReceiveMan ='" + receiverName + "'"; oCN.RunProc(sql1); DBHelper.CustomWriteLog("钉钉返回信息:" + response + " 更新子表语句:" + sql1, DateTime.Now.ToString("yyyy-MM-dd")); } else { Console.WriteLine(responseData.errcode); DBHelper.CustomWriteLog("钉钉返回信息:" + response, DateTime.Now.ToString("yyyy-MM-dd")); } } } } // 如果是每周一上午 9:00 执行操作 if (now.DayOfWeek == DayOfWeek.Monday && now.Hour == 9 && now.Minute == 0) { //查数据 string sql = @"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 = '每日任务与每周计划钉钉提醒'"; dt = oCN.RunProcReturn(sql, "Gy_Czygl"); if (dt.Tables[0].Rows.Count > 0) { foreach (DataRow row in dt.Tables[0].Rows) { string userId = row["钉钉id"].ToString(); string receiverName = row["接收人"].ToString(); string HContext = $"人员:{receiverName} 请及时编制本周的周计划!发送人为admin,接收人为 {receiverName}"; //获取单据内码 long HInterID = oCN.CreateBillID_Prod(BillType, ref sExeReturnInfo); string HBillNo = oCN.CreateBillCode(BillType, ref sExeReturnInfo, true); //插入主子数据 string sql2 = "INSERT INTO OA_EmpMessageBillMain (HYear,HPeriod,HBillType,HInterID,HDate,HBillNo,HBillStatus,HSendMan,HReceiveMan,HDescription,HMaker,HMakeDate)" + "VALUES('" + DateTime.Now.Year + "','" + DateTime.Now.Month + "','" + BillType + "','" + HInterID + "',getdate(),'" + HBillNo + "','1','admin','" + receiverName + "','" + HContext + "','自动生成',getdate())"; string sql3 = "INSERT INTO OA_EmpMessageBillSub (HInterID,HEntryID,HReceiveMan,HReadFlag,HSendFlag)" + "VALUES('" + HInterID + "','1','" + receiverName + "',0,0)"; oCN.RunProc(sql2); oCN.RunProc(sql3); //调用方法发送消息 string agentId = "3118119317";//钉钉后台建立的小程序id string userIdList = userId;//钉钉人员的id string deptIdList = "0"; // 空字符串表示不指定部门 string toAllUser = "false"; string message = HContext; // 调用方法发送消息 response = await msg.SendTextMessage(accessToken, agentId, userIdList, deptIdList, toAllUser, message); ResponseData responseData = JsonConvert.DeserializeObject(response); if (responseData.errcode == 0) { //更新状态 string sql1 = "update OA_EmpMessageBillSub set HSendFlag='1' where HReceiveMan ='" + receiverName + "'"; oCN.RunProc(sql1); DBHelper.CustomWriteLog("钉钉返回信息:" + response + " 更新子表语句:" + sql1, DateTime.Now.ToString("yyyy-MM-dd")); } else { Console.WriteLine(responseData.errcode); DBHelper.CustomWriteLog("钉钉返回信息:" + response, DateTime.Now.ToString("yyyy-MM-dd")); } } } } // 等待一分钟 await Task.Delay(TimeSpan.FromMinutes(1)); } } // 等待直到下一个周一上午9点 private static async Task WaitUntilNextMonday() { DateTime now = DateTime.Now; // 获取下一个周一的日期 DateTime nextMonday = now.AddDays((int)DayOfWeek.Monday - (int)now.DayOfWeek + 7) // 通过添加7天获取下一个周一 .Date.AddHours(9); // 设置为上午9点 TimeSpan delay = nextMonday - now; await Task.Delay(delay); // 等待直到下一个周一上午9点 } public class ResponseData { public int errcode { get; set; } public string errmsg { get; set; } public long task_id { get; set; } public string request_id { get; set; } } } }