zrg
2024-06-28 589d28da7824bee9b2d98e39f41c218bc8495109
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
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("Hello World!");
            while (true)
            {
                Console.WriteLine("Hello World!1");
                //查数据
                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);
                        }
                        else
                        {
                            Console.WriteLine(responseData.errcode);
                            //Console.WriteLine();
                        }
 
                    }                  
 
                  
                                    
                }
                        
                // 等待一分钟
                await Task.Delay(TimeSpan.FromMinutes(1));
            }
 
        }
        //public static (string, string,string) Get_User()
        //{
        //    //查询数据库子表所有信息
        //    string sql = "select a.HReceiveMan 接收人,c.HDingDingUserID 钉钉id,b.HDescription 内容 from OA_ErrMsgBackBillSub2 a left join OA_ErrMsgBackBillMain b on a.HInterID=b.HInterID left join Gy_Czygl c on a.HReceiveMan=c.Czymc where HSendFlag = '0'";
        //    dt = oCN.RunProcReturn(sql, "OA_ErrMsgBackBillSub2");
        //    if (dt.Tables[0].Rows.Count>0)
        //    {
        //        string HContext = dt.Tables[0].Rows[0]["内容"].ToString();
        //        List<string> HNames = new List<string>();
        //        List<string> StaresList = new List<string>();
 
        //        for (int i = 0; i < dt.Tables[0].Rows.Count; i++)
        //        {
        //            string HName = dt.Tables[0].Rows[i]["钉钉id"].ToString();
        //            string Stares = dt.Tables[0].Rows[i]["接收人"].ToString();
        //            HNames.Add(HName);
        //            StaresList.Add(Stares);
 
        //        }
        //        string concatenatedNames = string.Join(",", HNames);
        //        string concatenatedStares = string.Join("','", StaresList);
        //        return (concatenatedNames, HContext, concatenatedStares);
        //    }
 
        //    return ("1","1","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; }
        }
    }
}