.NET 同步钉钉接口的排班,和审批,并用审批回改排班,上班还是休息,请假或加班上午下午

定义变量

        string DingAppkey = "dingdul5161354SDF5131";//钉钉  应用的唯一标识key
        string DingAppsecret = "zU6w54Wg21DieRC3SSDF15SDFDSF15SDF1DS415S4DF8A791SD4";//钉钉  应用的密钥
        public static string dingAccessToken = ""; //钉钉接口调用凭证

获取钉钉接口调用凭证access_token

        #region 获取钉钉接口调用凭证:access_token
        /// <summary>
        /// 获取钉钉接口调用凭证:access_token
        /// </summary>
        /// <returns></returns>
        public string GetDingAccessToken()
        {
            string access_token = string.Empty;
            try
            {
                string accessToken = string.Empty;
                DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
                OapiGettokenRequest request = new OapiGettokenRequest();
                request.Appkey = DingAppkey;//钉钉  应用的唯一标识key
                request.Appsecret = DingAppsecret;//钉钉  应用的密钥
                request.SetHttpMethod("GET");
                OapiGettokenResponse response = client.Execute(request);
                access_token = response.AccessToken;
            }
            catch (Exception ex)
            {
 
 
            }
            return access_token;
        }
        #endregion

同步钉钉排班

        #region 同步钉钉排班
        void TongBuDDPaiBan(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (DateTime.Now.Day == 18)//每月一号执行
            {
                dingAccessToken = GetDingAccessToken();//获取钉钉接口调用凭证
                
                #region 取钉钉模块CODE
                
                //IDingTalkClient clienttest = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/process/get_by_name");
                //OapiProcessGetByNameRequest reqtest = new OapiProcessGetByNameRequest();
                //reqtest.Name = "商丘请假";
                //OapiProcessGetByNameResponse rsptest = clienttest.Execute(reqtest, dingAccessToken);
                //Console.WriteLine(rsptest.Body);//PROC-5B6E8BBF-48FB-4FFE-940A-AB49AB6D0241
                #endregion

                #region 钉钉模块ID
                /*
                 IT请假           PROC-5B6E8BBF-48FB-4FFE-940A-AB49AB6D0241
                      
                总部请假    PROC-CFYJPO3V-TVFYHFGWO4FDG244L7332-NPWDQRLJ-J
                总部加班    PROC-8KYJ2QBV-06L0HXR50ROMA8MGY8HW3-IDV5PUOJ-62
                商丘专科门诊部请假   PROC-CFYJ1MWU-D3K0W02C0W63M8RET6952-3DXLBTOJ-U1
                商丘专科门诊部加班   PROC-8KYJ2QBV-ILM07I93140LP8FDA8B33-03V8PUOJ-F
                商丘请假    PROC-FF6Y4BE1N2-1J4OWLXLMODUV13SDD4V1-Z6WWS97J-3
                商丘加班    PROC-JFYJKTEV-B2D0ZOJFZG4TC8A96NYN1-8DP6ZGOJ-11
                民权请假    PROC-FF6YBV6WQ2-39NIEPLPNJPATXHZVF8L1-TVWBWKZI-12
                民权加班    PROC-FF6YBV6WQ2-39NIEPLPNJPATXHZVF8L1-CUWBWKZI-X1
                 */
                //return;

                #endregion
                try
                {

                    StringBuilder sbDelSQL = new StringBuilder();
                    StringBuilder sbInsert = new StringBuilder();
                    StringBuilder sbUpdate = new StringBuilder();
                    StringBuilder sbdduser = new StringBuilder();//存放钉钉的员工ID拼接字符串
                    StringBuilder sbInsertJianCe = new StringBuilder();//检测有没有重复存在的员工记录
                    //StringBuilder sbProcessInstanceId = new StringBuilder();//审批详情ID集合
                    //string strdduser ="";//存放钉钉的员工ID拼接字符串
                    List<string> dduserlist = new List<string>();//存放钉钉的员工ID集合
                    List<string> ProcessInstanceIdlist = new List<string>();//存放钉钉的审批ID集合
                    List<OapiAttendanceScheduleListbyusersResponse.TopScheduleVoDomain> sch = null; //钉钉排班的结果LIST集合
                    
                    //先取员工所有钉钉ID
                    #region 取员工所有钉钉ID

                    List<MyUserModel> listuser = MyUserBLL.Select(" and dingdingUserid!='' ");
                    for (int i = 0; i < listuser.Count; i++)
                    {
                        sbdduser.Append(listuser[i].DingdingUserid + ",");
                        dduserlist.Add(listuser[i].DingdingUserid);
                    }
                    //strdduser = sbdduser.ToString().Substring(0, sbdduser.ToString().Length-1);//去掉最后一个,号
                    #endregion
                    
                    #region 遍历用户取钉钉排班

                    for (int iduser = 0; iduser < dduserlist.Count; iduser++)
                    {
                        int iday01 = 0;
                        for (int iall = 0; iall < 5; iall++)
                        {
                            #region 钉钉排班
                            //string pbBegintime = "2020-08-01 00:00:00";//排班开始时间
                            //string pbendtime = "2020-08-31 00:00:00";//结束时间
                            IDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/attendance/schedule/listbyusers");
                            OapiAttendanceScheduleListbyusersRequest req = new OapiAttendanceScheduleListbyusersRequest();
                            req.OpUserId = "2410456929267501";
                            req.Userids = dduserlist[iduser].ToString();
                            req.FromDateTime = Common.timeunix(DateTime.Now.AddDays(iday01)) * 1000;
                            req.ToDateTime = Common.timeunix(DateTime.Now.AddDays(iday01 + 6)) * 1000;
                            //req.FromDateTime = Common.timeunix(Convert.ToDateTime(pbBegintime)) * 1000;
                            //req.ToDateTime = Common.timeunix(Convert.ToDateTime(pbendtime)) * 1000;
                            req.SetHttpMethod("POST");
                            OapiAttendanceScheduleListbyusersResponse rsp = client.Execute(req, dingAccessToken);
                            if (rsp.Result != null)
                            {
                                sch = rsp.Result;
                                for (int i = 0; i < sch.Count; i++)
                                {
                                    string userid1 = sch[i].Userid;
                                    string is_rest = sch[i].IsRest;
                                    string work_date = sch[i].WorkDate;
                                    int i_is_rest = is_rest == "Y" ? 1 : 0;
                                    if (!string.IsNullOrEmpty(userid1))
                                    {
                                        sbDelSQL.Append(" delete from DDPaiBan where userid='" + userid1 + "' and work_date>=cast(convert(char(10),'" + Common.unixtime(Convert.ToInt64(req.FromDateTime)) + "',120)+' 00:00:00' as datetime) and  work_date<=cast(convert(char(10),'" + Common.unixtime(Convert.ToInt64(req.ToDateTime)) + "',120)+' 23:59:59' as datetime); ");
                                    }
                                    //string linshi = " insert into DDPaiBan (userid,is_rest,work_date) values ('" + userid1 + "'," + i_is_rest + ",'" + work_date + "') ; ";
                                    if (sbInsertJianCe.ToString().IndexOf(userid1 + work_date.Replace(" ", "")) < 0)//如果员工ID不存在,则追加ddpaiban
                                    {
                                        sbInsertJianCe.Append(userid1+ work_date.Replace(" ",""));
                                        sbInsert.Append(" insert into DDPaiBan (userid,is_rest,work_date) values ('" + userid1 + "'," + i_is_rest + ",'" + work_date + "') ; ");
                                    }
                                }
                                iday01 = iday01 + 7;
                            }

                            #endregion
                        }

                    }
                    #endregion

                    #region 钉钉审批,请假,加班
                    for (int i = 0; i < dduserlist.Count; i++)
                    {
                        #region 总部请假

                        IDingTalkClient client02 = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/processinstance/listids");
                        OapiProcessinstanceListidsRequest req02 = new OapiProcessinstanceListidsRequest();
                        req02.ProcessCode = "PROC-CFYJPO3V-TVFYHFGWO4FDG244L7332-NPWDQRLJ-J";
                        req02.StartTime = Common.timeunix(DateTime.Now.AddDays(-31)) * 1000;
                        req02.EndTime = Common.timeunix(DateTime.Now) * 1000;
                        //req02.StartTime = Common.timeunix(Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " 00:00:00")) * 1000;
                        //req02.EndTime = Common.timeunix(Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " 23:59:59")) * 1000;
                        //req02.StartTime = Common.timeunix(Convert.ToDateTime("2020-08-01 00:00:00")) * 1000;
                        //req02.EndTime = Common.timeunix(Convert.ToDateTime("2020-08-31 00:00:00")) * 1000;
                        req02.Size = 20;
                        req02.Cursor = 0;
                        req02.UseridList = dduserlist[i].ToString();//2410456929267501//012524601729258924
                        req02.SetHttpMethod("POST");
                        OapiProcessinstanceListidsResponse rsp02 = client02.Execute(req02, dingAccessToken);
                        if (rsp02.Result != null)
                        {
                            OapiProcessinstanceListidsResponse.PageResultDomain sch02 = rsp02.Result;
                            StringBuilder sbInsert02 = new StringBuilder();
                            ProcessInstanceIdlist.Add(sch02.List[0].ToString());
                        }

                        #endregion

                        #region 民权请假

                        IDingTalkClient client_mqqj = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/processinstance/listids");
                        OapiProcessinstanceListidsRequest req_mqqj = new OapiProcessinstanceListidsRequest();
                        req_mqqj.ProcessCode = "PROC-FF6YBV6WQ2-39NIEPLPNJPATXHZVF8L1-TVWBWKZI-12";
                        req_mqqj.StartTime = Common.timeunix(DateTime.Now.AddDays(-31)) * 1000;
                        req_mqqj.EndTime = Common.timeunix(DateTime.Now) * 1000;
                        //req_mqqj.StartTime = Common.timeunix(Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " 00:00:00")) * 1000;
                        //req_mqqj.EndTime = Common.timeunix(Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " 23:59:59")) * 1000;
                        //req_mqqj.StartTime = Common.timeunix(Convert.ToDateTime("2020-08-01 00:00:00")) * 1000;
                        //req_mqqj.EndTime = Common.timeunix(Convert.ToDateTime("2020-08-31 00:00:00")) * 1000;
                        req_mqqj.Size = 20;
                        req_mqqj.Cursor = 0;
                        req_mqqj.UseridList = dduserlist[i].ToString();//2410456929267501//012524601729258924
                        req_mqqj.SetHttpMethod("POST");
                        OapiProcessinstanceListidsResponse rsp_mqqj = client_mqqj.Execute(req_mqqj, dingAccessToken);
                        if (rsp_mqqj.Result != null)
                        {
                            OapiProcessinstanceListidsResponse.PageResultDomain sch_mqqj = rsp_mqqj.Result;
                            StringBuilder sbInsert_mqqj = new StringBuilder();
                            ProcessInstanceIdlist.Add(sch_mqqj.List[0].ToString());
                        }

                        #endregion

                        #region 民权加班

                        IDingTalkClient client_mqjb = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/processinstance/listids");
                        OapiProcessinstanceListidsRequest req_mqjb = new OapiProcessinstanceListidsRequest();
                        req_mqjb.ProcessCode = "PROC-FF6YBV6WQ2-39NIEPLPNJPATXHZVF8L1-CUWBWKZI-X1";
                        req_mqjb.StartTime = Common.timeunix(DateTime.Now.AddDays(-31)) * 1000;
                        req_mqjb.EndTime = Common.timeunix(DateTime.Now) * 1000;
                        //req_mqjb.StartTime = Common.timeunix(Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " 00:00:00")) * 1000;
                        //req_mqjb.EndTime = Common.timeunix(Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " 23:59:59")) * 1000;
                        //req_mqjb.StartTime = Common.timeunix(Convert.ToDateTime("2020-08-01 00:00:00")) * 1000;
                        //req_mqjb.EndTime = Common.timeunix(Convert.ToDateTime("2020-08-31 00:00:00")) * 1000;
                        req_mqjb.Size = 20;
                        req_mqjb.Cursor = 0;
                        req_mqjb.UseridList = dduserlist[i].ToString();//2410456929267501//012524601729258924
                        req_mqjb.SetHttpMethod("POST");
                        OapiProcessinstanceListidsResponse rsp_mqjb = client_mqjb.Execute(req_mqjb, dingAccessToken);
                        if (rsp_mqjb.Result != null)
                        {
                            OapiProcessinstanceListidsResponse.PageResultDomain sch_mqjb = rsp_mqjb.Result;
                            StringBuilder sbInsert_mqjb = new StringBuilder();
                            ProcessInstanceIdlist.Add(sch_mqjb.List[0].ToString());
                        }

                        #endregion

                        #region 商丘加班

                        IDingTalkClient client_sqjb = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/processinstance/listids");
                        OapiProcessinstanceListidsRequest req_sqjb = new OapiProcessinstanceListidsRequest();
                        req_sqjb.ProcessCode = "PROC-JFYJKTEV-B2D0ZOJFZG4TC8A96NYN1-8DP6ZGOJ-11";
                        req_sqjb.StartTime = Common.timeunix(DateTime.Now.AddDays(-31)) * 1000;
                        req_sqjb.EndTime = Common.timeunix(DateTime.Now) * 1000;
                        //req_sqjb.StartTime = Common.timeunix(Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " 00:00:00")) * 1000;
                        //req_sqjb.EndTime = Common.timeunix(Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " 23:59:59")) * 1000;
                        //req_sqjb.StartTime = Common.timeunix(Convert.ToDateTime("2020-08-01 00:00:00")) * 1000;
                        //req_sqjb.EndTime = Common.timeunix(Convert.ToDateTime("2020-08-31 00:00:00")) * 1000;
                        req_sqjb.Size = 20;
                        req_sqjb.Cursor = 0;
                        req_sqjb.UseridList = dduserlist[i].ToString();//2410456929267501//012524601729258924
                        req_sqjb.SetHttpMethod("POST");
                        OapiProcessinstanceListidsResponse rsp_sqjb = client_sqjb.Execute(req_sqjb, dingAccessToken);
                        if (rsp_sqjb.Result != null)
                        {
                            OapiProcessinstanceListidsResponse.PageResultDomain sch_sqjb = rsp_sqjb.Result;
                            StringBuilder sbInsert_sqjb = new StringBuilder();
                            ProcessInstanceIdlist.Add(sch_sqjb.List[0].ToString());
                        }

                        #endregion

                        #region 总部加班

                        IDingTalkClient client_zbjb = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/processinstance/listids");
                        OapiProcessinstanceListidsRequest req_zbjb = new OapiProcessinstanceListidsRequest();
                        req_zbjb.ProcessCode = "PROC-8KYJ2QBV-06L0HXR50ROMA8MGY8HW3-IDV5PUOJ-62";
                        req_zbjb.StartTime = Common.timeunix(DateTime.Now.AddDays(-31)) * 1000;
                        req_zbjb.EndTime = Common.timeunix(DateTime.Now) * 1000;
                        //req_zbjb.StartTime = Common.timeunix(Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " 00:00:00")) * 1000;
                        //req_zbjb.EndTime = Common.timeunix(Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " 23:59:59")) * 1000;
                        //req_zbjb.StartTime = Common.timeunix(Convert.ToDateTime("2020-08-01 00:00:00")) * 1000;
                        //req_zbjb.EndTime = Common.timeunix(Convert.ToDateTime("2020-08-31 00:00:00")) * 1000;
                        req_zbjb.Size = 20;
                        req_zbjb.Cursor = 0;
                        req_zbjb.UseridList = dduserlist[i].ToString();//2410456929267501//012524601729258924
                        req_zbjb.SetHttpMethod("POST");
                        OapiProcessinstanceListidsResponse rsp_zbjb = client_zbjb.Execute(req_zbjb, dingAccessToken);
                        if (rsp_zbjb.Result != null)
                        {
                            OapiProcessinstanceListidsResponse.PageResultDomain sch_zbjb = rsp_zbjb.Result;
                            StringBuilder sbInsert_zbjb = new StringBuilder();
                            ProcessInstanceIdlist.Add(sch_zbjb.List[0].ToString());
                        }

                        #endregion

                        #region 商丘专科门诊部请假

                        IDingTalkClient client_sqzkmzbqj = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/processinstance/listids");
                        OapiProcessinstanceListidsRequest req_sqzkmzbqj = new OapiProcessinstanceListidsRequest();
                        req_sqzkmzbqj.ProcessCode = "PROC-CFYJ1MWU-D3K0W02C0W63M8RET6952-3DXLBTOJ-U1";
                        req_sqzkmzbqj.StartTime = Common.timeunix(DateTime.Now.AddDays(-31)) * 1000;
                        req_sqzkmzbqj.EndTime = Common.timeunix(DateTime.Now) * 1000;
                        //req_sqzkmzbqj.StartTime = Common.timeunix(Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " 00:00:00")) * 1000;
                        //req_sqzkmzbqj.EndTime = Common.timeunix(Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " 23:59:59")) * 1000;
                        //req_sqzkmzbqj.StartTime = Common.timeunix(Convert.ToDateTime("2020-08-01 00:00:00")) * 1000;
                        //req_sqzkmzbqj.EndTime = Common.timeunix(Convert.ToDateTime("2020-08-31 00:00:00")) * 1000;
                        req_sqzkmzbqj.Size = 20;
                        req_sqzkmzbqj.Cursor = 0;
                        req_sqzkmzbqj.UseridList = dduserlist[i].ToString();//2410456929267501//012524601729258924
                        req_sqzkmzbqj.SetHttpMethod("POST");
                        OapiProcessinstanceListidsResponse rsp_sqzkmzbqj = client_sqzkmzbqj.Execute(req_sqzkmzbqj, dingAccessToken);
                        if (rsp_sqzkmzbqj.Result != null)
                        {
                            OapiProcessinstanceListidsResponse.PageResultDomain sch_sqzkmzbqj = rsp_sqzkmzbqj.Result;
                            StringBuilder sbInsert_sqzkmzbqj = new StringBuilder();
                            ProcessInstanceIdlist.Add(sch_sqzkmzbqj.List[0].ToString());
                        }

                        #endregion

                        #region 商丘专科门诊部加班

                        IDingTalkClient client_sqzkmzbjb = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/processinstance/listids");
                        OapiProcessinstanceListidsRequest req_sqzkmzbjb = new OapiProcessinstanceListidsRequest();
                        req_sqzkmzbjb.ProcessCode = "PROC-8KYJ2QBV-ILM07I93140LP8FDA8B33-03V8PUOJ-F";
                        req_sqzkmzbjb.StartTime = Common.timeunix(DateTime.Now.AddDays(-31)) * 1000;
                        req_sqzkmzbjb.EndTime = Common.timeunix(DateTime.Now) * 1000;
                        //req_sqzkmzbjb.StartTime = Common.timeunix(Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " 00:00:00")) * 1000;
                        //req_sqzkmzbjb.EndTime = Common.timeunix(Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " 23:59:59")) * 1000;
                        //req_sqzkmzbjb.StartTime = Common.timeunix(Convert.ToDateTime("2020-08-01 00:00:00")) * 1000;
                        //req_sqzkmzbjb.EndTime = Common.timeunix(Convert.ToDateTime("2020-08-31 00:00:00")) * 1000;
                        req_sqzkmzbjb.Size = 20;
                        req_sqzkmzbjb.Cursor = 0;
                        req_sqzkmzbjb.UseridList = dduserlist[i].ToString();//2410456929267501//012524601729258924
                        req_sqzkmzbjb.SetHttpMethod("POST");
                        OapiProcessinstanceListidsResponse rsp_sqzkmzbjb = client_sqzkmzbjb.Execute(req_sqzkmzbjb, dingAccessToken);
                        if (rsp_sqzkmzbjb.Result != null)
                        {
                            OapiProcessinstanceListidsResponse.PageResultDomain sch_sqzkmzbjb = rsp_sqzkmzbjb.Result;
                            StringBuilder sbInsert_sqzkmzbjb = new StringBuilder();
                            ProcessInstanceIdlist.Add(sch_sqzkmzbjb.List[0].ToString());
                        }

                        #endregion

                        #region 商丘请假

                        IDingTalkClient client_sqqj = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/processinstance/listids");
                        OapiProcessinstanceListidsRequest req_sqqj = new OapiProcessinstanceListidsRequest();
                        req_sqqj.ProcessCode = "PROC-FF6Y4BE1N2-1J4OWLXLMODUV13SDD4V1-Z6WWS97J-3";
                        req_sqqj.StartTime = Common.timeunix(DateTime.Now.AddDays(-31)) * 1000;
                        req_sqqj.EndTime = Common.timeunix(DateTime.Now) * 1000;
                        //req_sqqj.StartTime = Common.timeunix(Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " 00:00:00")) * 1000;
                        //req_sqqj.EndTime = Common.timeunix(Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " 23:59:59")) * 1000;
                        //req_sqqj.StartTime = Common.timeunix(Convert.ToDateTime("2020-08-01 00:00:00")) * 1000;
                        //req_sqqj.EndTime = Common.timeunix(Convert.ToDateTime("2020-08-31 00:00:00")) * 1000;
                        req_sqqj.Size = 20;
                        req_sqqj.Cursor = 0;
                        req_sqqj.UseridList = dduserlist[i].ToString();//2410456929267501//012524601729258924
                        req_sqqj.SetHttpMethod("POST");
                        OapiProcessinstanceListidsResponse rsp_sqqj = client_sqqj.Execute(req_sqqj, dingAccessToken);
                        if (rsp_sqqj.Result != null)
                        {
                            OapiProcessinstanceListidsResponse.PageResultDomain sch_sqqj = rsp_sqqj.Result;
                            StringBuilder sbInsert_sqqj = new StringBuilder();
                            ProcessInstanceIdlist.Add(sch_sqqj.List[0].ToString());
                        }

                        #endregion


                    }


                    #endregion

                    #region 钉钉审批详情
                    for (int i = 0; i < ProcessInstanceIdlist.Count; i++)
                    {
                        IDingTalkClient client03 = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/processinstance/get");
                        OapiProcessinstanceGetRequest req03 = new OapiProcessinstanceGetRequest();
                        req03.ProcessInstanceId = ProcessInstanceIdlist[i].ToString();//a49230f0-e48b-46e4-a37b-5e33d5b3e079//6680a656-376e-4eb8-b2da-c3f41cdaa4a1//strProcessInstanceId审批ID
                        req03.SetHttpMethod("POST");
                        OapiProcessinstanceGetResponse rsp03 = client03.Execute(req03, dingAccessToken);
                        if (rsp03.ProcessInstance != null)
                        {
                            ProcessInstanceTopVoDomain sch03 = rsp03.ProcessInstance;
                            StringBuilder sbInsert03 = new StringBuilder();
                            string userid = "";//要请假的员工ID
                            string create_time = "";//开始时间
                            string finish_time = "";//结束时间
                            string timeNum = "";//时长
                            userid = sch03.OriginatorUserid;
                            string lintime = "";
                            if (sch03.Title.IndexOf("加班") >= 0)
                            {
                                lintime = sch03.FormComponentValues[0].Value;
                            }
                            else if (sch03.Title.IndexOf("请假") >= 0)
                            {
                                lintime = sch03.FormComponentValues[1].Value;
                            }
                            create_time = lintime.Split(',')[0];
                            finish_time = lintime.Split(',')[1];
                            timeNum = lintime.Split(',')[2];
                            if (userid != "")
                            {
                                DateTime dt1 = Convert.ToDateTime(create_time.Replace("[\"", "").Replace("\"", ""));
                                DateTime dt112 = Convert.ToDateTime(dt1.ToString("yyyy-MM-dd") + " 12:00:00");
                                DateTime dt2 = Convert.ToDateTime(finish_time.Replace("[\"", "").Replace("\"", ""));
                                DateTime dt212 = Convert.ToDateTime(dt2.ToString("yyyy-MM-dd") + " 14:00:00");
                                TimeSpan span01 = dt112.Subtract(dt1);
                                double dayDiff01 = span01.TotalMinutes;
                                TimeSpan span02 = dt212.Subtract(dt2);
                                double dayDiff02 = span02.TotalMinutes;

                                //如果开始时间小于12点。则上午算休息
                                //如果结束时间大于14点。则下午算休息
                                //如果开始时间小于12点,并且如果结束时间大于14点,则一天算休息

                                //先计算请假开始时间和结束时间,有没有跨天
                                DateTime dt3 = Convert.ToDateTime(dt1.ToString("yyyy-MM-dd"));
                                DateTime dt4 = Convert.ToDateTime(dt2.ToString("yyyy-MM-dd"));
                                TimeSpan span03 = dt4.Subtract(dt3);
                                double dayDiff03 = span03.Days;

                                if (sch03.Title.IndexOf("加班") >= 0)
                                {
                                    if (dayDiff03 > 0)//如果跨天了
                                    {
                                        //跨天,先处理开始时间的当天
                                        if (dayDiff01 > 0)//开始时间小于12点,是全天上班。否则是下午上班
                                        {
                                            sbUpdate.Append(" update DDPaiBan set is_rest=0,jbType=1,beginTime='" + dt1.ToString() + "',endTime='" + dt2.ToString() + "' where  userid='" + userid + "' and cast(convert(char(10),work_date,120)+' 00:00:00' as datetime) = cast(convert(char(10),'" + create_time.Replace("[\"", "").Replace("\"", "") + "',120)+' 00:00:00' as datetime)  ; ");
                                        }
                                        else
                                        {
                                            sbUpdate.Append(" update DDPaiBan set is_rest=5,jbType=1,beginTime='" + dt1.ToString() + "',endTime='" + dt2.ToString() + "' where  userid='" + userid + "' and cast(convert(char(10),work_date,120)+' 00:00:00' as datetime) = cast(convert(char(10),'" + create_time.Replace("[\"", "").Replace("\"", "") + "',120)+' 00:00:00' as datetime)  ; ");
                                        }
                                        //跨天,再处理结束时间的当天
                                        if (dayDiff02 < 0)//结束时间大于14点,是全天上班。否则是上午上班
                                        {
                                            sbUpdate.Append(" update DDPaiBan set is_rest=0,jbType=1,beginTime='" + dt1.ToString() + "',endTime='" + dt2.ToString() + "' where  userid='" + userid + "' and cast(convert(char(10),work_date,120)+' 00:00:00' as datetime) = cast(convert(char(10),'" + finish_time.Replace("[\"", "").Replace("\"", "") + "',120)+' 00:00:00' as datetime)  ; ");
                                        }
                                        else
                                        {
                                            sbUpdate.Append(" update DDPaiBan set is_rest=6,jbType=1,beginTime='" + dt1.ToString() + "',endTime='" + dt2.ToString() + "' where  userid='" + userid + "' and cast(convert(char(10),work_date,120)+' 00:00:00' as datetime) = cast(convert(char(10),'" + finish_time.Replace("[\"", "").Replace("\"", "") + "',120)+' 00:00:00' as datetime)  ; ");
                                        }
                                    }
                                    else if (dayDiff03 == 0)//如果是当天内
                                    {
                                        if (dayDiff01 > 0 && dayDiff02 < 0)//全天上班
                                        {
                                            sbUpdate.Append(" update DDPaiBan set is_rest=0,jbType=1,beginTime='" + dt1.ToString() + "',endTime='" + dt2.ToString() + "' where  userid='" + userid + "' and cast(convert(char(10),work_date,120)+' 00:00:00' as datetime) = cast(convert(char(10),'" + create_time.Replace("[\"", "").Replace("\"", "") + "',120)+' 00:00:00' as datetime)  ; ");
                                        }
                                        else if (dayDiff01 > 0)//上午上班
                                        {
                                            sbUpdate.Append(" update DDPaiBan set is_rest=6,jbType=1,beginTime='" + dt1.ToString() + "',endTime='" + dt2.ToString() + "' where  userid='" + userid + "' and cast(convert(char(10),work_date,120)+' 00:00:00' as datetime) = cast(convert(char(10),'" + create_time.Replace("[\"", "").Replace("\"", "") + "',120)+' 00:00:00' as datetime)  ; ");
                                        }
                                        else if (dayDiff02 < 0)//下午上班
                                        {
                                            sbUpdate.Append(" update DDPaiBan set is_rest=5,jbType=1,beginTime='" + dt1.ToString() + "',endTime='" + dt2.ToString() + "' where  userid='" + userid + "' and cast(convert(char(10),work_date,120)+' 00:00:00' as datetime) = cast(convert(char(10),'" + create_time.Replace("[\"", "").Replace("\"", "") + "',120)+' 00:00:00' as datetime)  ; ");
                                        }

                                    }

                                    //除去开始时间,和结束时间,把中间的天数,全改成请假上班0
                                    sbUpdate.Append(" update DDPaiBan set is_rest=0,jbType=1,beginTime='" + dt1.ToString() + "',endTime='" + dt2.ToString() + "' where  userid='" + userid + "' and work_date> cast(convert(char(10),'" + create_time.Replace("[\"", "").Replace("\"", "") + "',120)+' 23:59:59' as datetime) and work_date< cast(convert(char(10),'" + finish_time.Replace("[\"", "").Replace("\"", "") + "',120)+' 00:00:00' as datetime)  ; ");

                                }
                                else if (sch03.Title.IndexOf("请假") >= 0)
                                {
                                    if (dayDiff03 > 0)//如果跨天了
                                    {
                                        //跨天,先处理开始时间的当天
                                        if (dayDiff01 > 0)//开始时间小于12点,是全天休息。否则是下午休息
                                        {
                                            sbUpdate.Append(" update DDPaiBan set is_rest=2,jbType=2,beginTime='" + dt1.ToString() + "',endTime='" + dt2.ToString() + "' where  userid='" + userid + "' and cast(convert(char(10),work_date,120)+' 00:00:00' as datetime) = cast(convert(char(10),'" + create_time.Replace("[\"", "").Replace("\"", "") + "',120)+' 00:00:00' as datetime)  ; ");
                                        }
                                        else
                                        {
                                            sbUpdate.Append(" update DDPaiBan set is_rest=6,jbType=2,beginTime='" + dt1.ToString() + "',endTime='" + dt2.ToString() + "' where  userid='" + userid + "' and cast(convert(char(10),work_date,120)+' 00:00:00' as datetime) = cast(convert(char(10),'" + create_time.Replace("[\"", "").Replace("\"", "") + "',120)+' 00:00:00' as datetime)  ; ");
                                        }
                                        //跨天,再处理结束时间的当天
                                        if (dayDiff02 < 0)//结束时间大于14点,是全天休息。否则是上午休息
                                        {
                                            sbUpdate.Append(" update DDPaiBan set is_rest=2,jbType=2,beginTime='" + dt1.ToString() + "',endTime='" + dt2.ToString() + "' where  userid='" + userid + "' and cast(convert(char(10),work_date,120)+' 00:00:00' as datetime) = cast(convert(char(10),'" + finish_time.Replace("[\"", "").Replace("\"", "") + "',120)+' 00:00:00' as datetime)  ; ");
                                        }
                                        else
                                        {
                                            sbUpdate.Append(" update DDPaiBan set is_rest=5,jbType=2,beginTime='" + dt1.ToString() + "',endTime='" + dt2.ToString() + "' where  userid='" + userid + "' and cast(convert(char(10),work_date,120)+' 00:00:00' as datetime) = cast(convert(char(10),'" + finish_time.Replace("[\"", "").Replace("\"", "") + "',120)+' 00:00:00' as datetime)  ; ");
                                        }

                                    }
                                    else if (dayDiff03 == 0)//如果是当天内
                                    {
                                        if (dayDiff01 > 0 && dayDiff02 < 0)//全天休息
                                        {
                                            sbUpdate.Append(" update DDPaiBan set is_rest=2,jbType=2,beginTime='" + dt1.ToString() + "',endTime='" + dt2.ToString() + "' where  userid='" + userid + "' and cast(convert(char(10),work_date,120)+' 00:00:00' as datetime) = cast(convert(char(10),'" + create_time.Replace("[\"", "").Replace("\"", "") + "',120)+' 00:00:00' as datetime)  ; ");
                                        }
                                        else if (dayDiff01 > 0)//上午休息
                                        {
                                            sbUpdate.Append(" update DDPaiBan set is_rest=5,jbType=2,beginTime='" + dt1.ToString() + "',endTime='" + dt2.ToString() + "' where  userid='" + userid + "' and cast(convert(char(10),work_date,120)+' 00:00:00' as datetime) = cast(convert(char(10),'" + create_time.Replace("[\"", "").Replace("\"", "") + "',120)+' 00:00:00' as datetime)  ; ");
                                        }
                                        else if (dayDiff02 < 0)//下午休息
                                        {
                                            sbUpdate.Append(" update DDPaiBan set is_rest=6,jbType=2,beginTime='" + dt1.ToString() + "',endTime='" + dt2.ToString() + "' where  userid='" + userid + "' and cast(convert(char(10),work_date,120)+' 00:00:00' as datetime) = cast(convert(char(10),'" + create_time.Replace("[\"", "").Replace("\"", "") + "',120)+' 00:00:00' as datetime)  ; ");
                                        }
                                    }

                                    //除去开始时间,和结束时间,把中间的天数,全改成请假休息2
                                    sbUpdate.Append(" update DDPaiBan set is_rest=2,jbType=2,beginTime='" + dt1.ToString() + "',endTime='" + dt2.ToString() + "' where  userid='" + userid + "' and work_date> cast(convert(char(10),'" + create_time.Replace("[\"", "").Replace("\"", "") + "',120)+' 23:59:59' as datetime) and work_date< cast(convert(char(10),'" + finish_time.Replace("[\"", "").Replace("\"", "") + "',120)+' 00:00:00' as datetime)  ; ");

                                }

                            }
                        }
                    }



                    #endregion
                    
                    string str011 = sbDelSQL.ToString();
                    string str012 = sbInsert.ToString();
                    string str013 = sbUpdate.ToString();
                    string str00 = "";
                    List<string> list = new List<string>();
                    list.Add(str011);
                    list.Add(str012);
                    list.Add(str013);


                    //int idel = SqlHelper.ExecuteNonQuery(delSQL, CommandType.Text);
                    //int iinsert = SqlHelper.ExecuteNonQuery(sbInsert.ToString(), CommandType.Text);

                    //删除,新增,修改放入事务执行

                    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Sql"].ConnectionString);//获取数据库连bai接
                    con.Open();//打开连接
                    SqlTransaction sqltra = con.BeginTransaction();//开始事务
                    SqlCommand cmd = new SqlCommand();//实例du化
                    cmd.Connection = con;//获取数据连zhi接
                    cmd.Transaction = sqltra;//在执行SQL时,
                    try
                    {
                        cmd.CommandText = str011 + str012 + str013;
                        cmd.ExecuteNonQuery();
                        sqltra.Commit();
                    }
                    catch (Exception ex)
                    {
                        Common.CreateWebLog("同步钉钉排班异常", ex.ToString());
                        sqltra.Rollback();
                    }



                }
                catch (Exception ex)
                {
                    Common.CreateWebLog("同步排班异常", ex.ToString());
                }
            }



        }
        #endregion

 

posted @ 2020-09-18 11:42  离。  阅读(141)  评论(0编辑  收藏