zrg
2024-07-02 341da63fe63e2cae3337c5a090624400d7244c11
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
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.Threading.Tasks;
 
namespace DingDingMsg
{
    class Program
    {
 
        static DBHelper oCN = new DBHelper();
        static DataSet dt = new DataSet();
        static async Task Main(string[] args)
        {
            string appKey = "dingrsrzhdyn3mlaof95";
            string appSecret = "RAqH6YtZnPLCpDbuqfaYQkKkVtVdS0wqfC8I26X6qiS-8eoCJCNrzx3fubGND4Sq";
            Console.WriteLine("程序正在执行,请不要关闭!!!!");
            while (true)
            {
                //查数据
                string sql = "select a.HInterID,a.HReceiveMan 接收人,c.HDingDingUserID 钉钉id,b.HDescription 内容 from OA_ErrMsgBackBillSub2" +
                    " a inner join OA_ErrMsgBackBillMain b on a.HInterID=b.HInterID left join Gy_Czygl c " +
                    "on a.HReceiveMan = c.Czymc where HSendFlag = '0' order by a.HInterID";
                dt = oCN.RunProcReturn(sql, "OA_ErrMsgBackBillSub2");
                if (dt.Tables[0].Rows.Count > 0)
                {
                    string HContext = "";
                   
 
                    for (int i = 0; i < dt.Tables[0].Rows.Count; i++)
                    {                        
                        HContext = dt.Tables[0].Rows[i]["内容"].ToString();
                        string HName = dt.Tables[0].Rows[i]["钉钉id"].ToString();
                        string Stares = dt.Tables[0].Rows[i]["接收人"].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 userIdList = HName;//钉钉人员的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_ErrMsgBackBillSub2 set HSendFlag='1' where HReceiveMan in ('" + Stares + "')";
                            oCN.RunProc(sql1);
                            Console.WriteLine(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));
            }
 
        }      
        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; }
        }
    }
}