1
zrg
5 天以前 088f1c56e08047c21dcaebbab475d975531905bd
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
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<ResponseData>(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<ResponseData>(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; }
        }
    }
}