yusijie
2024-11-22 3697b0dda739b38c33406d6e164714167e9ad5b5
WebAPI/Controllers/Êý¾Ýͬ²½/¶¤¶¤Í¬²½/DD_DataSynchronizationController.cs
@@ -17,6 +17,8 @@
using DingTalk.Api.Response;
using System.Globalization;
using System.Linq;
using AlibabaCloud.SDK.Dingtalkworkflow_1_0.Models;
using System.Text.RegularExpressions;
namespace WebAPI.Controllers
{
@@ -85,6 +87,13 @@
            public long? TotalCount;
            public long? PageNumber;
            public List<string> Data;
        }
        //oa审批返回类
        public class OA_GetInstanceIDListResponse
        {
           public List<string> list;//获取的id列表
           public string nextToken;
           public bool? success;
        }
        #region åˆ†å±‚审核签到表
@@ -358,8 +367,83 @@
            public List<Sc_MouldRepairWorkBillSub> DetailColMouldRepair = new List<Sc_MouldRepairWorkBillSub>();
        }
        #endregion
        #region å¼‚常工时申请单
        #region ä¸»è¡¨+子表
        public class Pay_ErrWorkTimesRequestBill
        {
            public int HInterID;
            public string HBillNo;
            public string HDate;
            public string HInnerBillNo;
            public int HDeptID;
            public string HDeptName;
            public int HGroupID;
            public string HGroupName;
            public int HMangerID;
            public string HManagerName;
            public int HICMOInterID;
            public string HICMOBillNo;
            public string HExplanation;
            public double HSumRequestTimes;
            public int HMaterID;
            public string HMaterName;
            public int HUnitID;
            public string HUnitName;
            public double HPlanQty;
            public string HBatchNo;
            public string HRemark;
            public string HMaker;
            public string HMakerDate;
            public string HUpdater;
            public string HUpdaterDate;
            public string HChecker;
            public string HCheckerDate;
            public string HCloseMan;
            public string HCloseManDate;
            public string HDeleteMan;
            public string HDeleteManDate;
            public string HBacker;
            public string HBackerDate;
            public string HBackRemark;
            public List<Pay_ErrWorkTimesRequestBillSub> Pay_ErrWorkTimesRequestBillSub = new List<Pay_ErrWorkTimesRequestBillSub>();
        }
        #endregion
        #region å­è¡¨
        public class Pay_ErrWorkTimesRequestBillSub
        {
            public int HEmpID;
            public string HEmpNumber;
            public string HEmpName;
            public int HWorkTypeID;
            public string HWorkTypeNumber;
            public string HWorkTypeName;
            public double HPayMoney;
            public string HBTimes;
            public string HETimes;
            public double HTimes;
            public double HRelTimes;
            public double HRelPay;
            public string HRemark;
            public int HSourceInterID;
            public int HSourceEntryID;
            public string HSourceBillNo;
            public string HSourceBillType;
            public double HRelationQty;
            public double HRelationMoney;
            public string HCloseMan;
            public string HEntryCloseDate;
        }
        #endregion
        #endregion
        #endregion
        #region é’‰é’‰ é€šç”¨æ“ä½œæ–¹æ³•
@@ -722,6 +806,145 @@
            }
        }
        #endregion
        #endregion
        #region oa é€šç”¨æ“ä½œæ–¹æ³•
        #region OA èŽ·å–å®¡æ‰¹å®žä¾‹ID列表(单页)
        public bool getInstanceIDList_Page_OA(string processCode,DateTime startTime, DateTime endTime,long nextToken, int maxResult, List<string> statuses, ref OA_GetInstanceIDListResponse response, ref string msg)
        {
            AlibabaCloud.SDK.Dingtalkworkflow_1_0.Client client = CreateClient1();
            AlibabaCloud.SDK.Dingtalkworkflow_1_0.Models.ListProcessInstanceIdsHeaders listProcessInstanceIdsHeaders = new AlibabaCloud.SDK.Dingtalkworkflow_1_0.Models.ListProcessInstanceIdsHeaders();
            listProcessInstanceIdsHeaders.XAcsDingtalkAccessToken = this.accessToken;
            AlibabaCloud.SDK.Dingtalkworkflow_1_0.Models.ListProcessInstanceIdsRequest listProcessInstanceIdsRequest = new AlibabaCloud.SDK.Dingtalkworkflow_1_0.Models.ListProcessInstanceIdsRequest
            {
                Statuses = statuses,
                StartTime = getTimeMillions(startTime),
                EndTime  = getTimeMillions(endTime),
                ProcessCode = processCode,
                NextToken = nextToken,
                MaxResults = maxResult,
            };
            try
            {
                ListProcessInstanceIdsResponse listProcessInstanceIdsResponse = client.ListProcessInstanceIdsWithOptions(listProcessInstanceIdsRequest, listProcessInstanceIdsHeaders, new AlibabaCloud.TeaUtil.Models.RuntimeOptions());
                response.success = listProcessInstanceIdsResponse.Body.Success;
                response.list = listProcessInstanceIdsResponse.Body.Result.List;
                response.nextToken = listProcessInstanceIdsResponse.Body.Result.NextToken;
                return true;
            }
            catch (TeaException err)
            {
                if (!AlibabaCloud.TeaUtil.Common.Empty(err.Code) && !AlibabaCloud.TeaUtil.Common.Empty(err.Message))
                {
                    // err ä¸­å«æœ‰ code å’Œ message å±žæ€§ï¼Œå¯å¸®åŠ©å¼€å‘å®šä½é—®é¢˜
                }
                msg = "错误代码" + err.Code + ":" + err.Message;
                return false;
            }
            catch (Exception _err)
            {
                TeaException err = new TeaException(new Dictionary<string, object>
                {
                    { "message", _err.Message }
                });
                if (!AlibabaCloud.TeaUtil.Common.Empty(err.Code) && !AlibabaCloud.TeaUtil.Common.Empty(err.Message))
                {
                    // err ä¸­å«æœ‰ code å’Œ message å±žæ€§ï¼Œå¯å¸®åŠ©å¼€å‘å®šä½é—®é¢˜
                }
                msg = "错误代码" + err.Code + ":" + err.Message;
                return false;
            }
        }
        #endregion
        #region èŽ·å– OA指定单据的实例ID列表(全部)
        public bool getInstanceIDList_OA(string processCode, DateTime startTime, DateTime endTime, long nextToken, int maxResult, List<string> statuses, ref List<string> IDList, ref string msg)
        {
            OA_GetInstanceIDListResponse response = new OA_GetInstanceIDListResponse();
            response.nextToken = nextToken.ToString();//赋值初始值
            do
            {
                nextToken = long.Parse(response.nextToken);
                if (getInstanceIDList_Page_OA(processCode, startTime, endTime, nextToken, maxResult, statuses,ref response, ref msg) == false)
                {
                    return false;
                }
                for (int i = 0; i < response.list.Count; i++)
                {
                    IDList.Add(response.list[i]);
                }
            } while (response.nextToken!=null);
            return true;
        }
        #endregion
        #region èŽ·å– OA æ ¹æ®å®žä¾‹ID获取实例详情
        public bool getInstanceDetailByInstanceID_OA(string processinstanceID,ref GetProcessInstanceResponse response, ref string msg)
        {
            AlibabaCloud.SDK.Dingtalkworkflow_1_0.Client client = CreateClient1();
            AlibabaCloud.SDK.Dingtalkworkflow_1_0.Models.GetProcessInstanceHeaders getProcessInstanceHeaders = new AlibabaCloud.SDK.Dingtalkworkflow_1_0.Models.GetProcessInstanceHeaders();
            getProcessInstanceHeaders.XAcsDingtalkAccessToken = this.accessToken;
            AlibabaCloud.SDK.Dingtalkworkflow_1_0.Models.GetProcessInstanceRequest getProcessInstanceRequest = new AlibabaCloud.SDK.Dingtalkworkflow_1_0.Models.GetProcessInstanceRequest
            {
                ProcessInstanceId = processinstanceID,
            };
            try
            {
                response =  client.GetProcessInstanceWithOptions(getProcessInstanceRequest, getProcessInstanceHeaders, new AlibabaCloud.TeaUtil.Models.RuntimeOptions());
                return true;
            }
            catch (TeaException err)
            {
                if (!AlibabaCloud.TeaUtil.Common.Empty(err.Code) && !AlibabaCloud.TeaUtil.Common.Empty(err.Message))
                {
                    // err ä¸­å«æœ‰ code å’Œ message å±žæ€§ï¼Œå¯å¸®åŠ©å¼€å‘å®šä½é—®é¢˜
                }
                msg = "错误代码" + err.Code + ":" + err.Message;
                return false;
            }
            catch (Exception _err)
            {
                TeaException err = new TeaException(new Dictionary<string, object>
                {
                    { "message", _err.Message }
                });
                if (!AlibabaCloud.TeaUtil.Common.Empty(err.Code) && !AlibabaCloud.TeaUtil.Common.Empty(err.Message))
                {
                    // err ä¸­å«æœ‰ code å’Œ message å±žæ€§ï¼Œå¯å¸®åŠ©å¼€å‘å®šä½é—®é¢˜
                }
                msg = "错误代码" + err.Code + ":" + err.Message;
                return false;
            }
        }
        #endregion
        #region èŽ·å– OA æ ¹æ®å®žä¾‹ID列表获取实例详情列表
        public bool getInstanceDetailListByInstanceIDList_OA(List<string> processinstanceIDList, ref List<GetProcessInstanceResponse> responseList, ref string msg)
        {
            GetProcessInstanceResponse getProcessInstanceResponse = new GetProcessInstanceResponse();
            try
            {
                foreach(string processinstanceID in processinstanceIDList)
                {
                    if(!getInstanceDetailByInstanceID_OA(processinstanceID,ref getProcessInstanceResponse,ref msg))
                    {
                        return false;
                    }
                    responseList.Add(getProcessInstanceResponse);
                }
                return true;
            }
            catch (Exception e)
            {
                msg = e.Message;
                return false;
            }
        }
        #endregion
        #endregion
        #region æ•°æ®åŒæ­¥æ–¹æ³•
@@ -2857,6 +3080,272 @@
        #endregion
        #endregion
        #region æ•°æ®åŒæ­¥-异常工时单OA里获取
        #region ç±»åž‹è½¬æ¢æå–response中的value对象
        public class StatValue
        {
            public string Id { get; set; }
            public string Label { get; set; }
            public string Upper { get; set; }
            public string Num { get; set; }
        }
        public class ExtValue
        {
            public List<StatValue> StatValue { get; set; }
            public string ComponentName { get; set; }
        }
        public class RowValue
        {
            public string Label { get; set; }
            public object Value { get; set; }
            public string Key { get; set; }
            // æ³¨æ„ï¼šè¿™é‡Œä¸å®šä¹‰extendValue属性,因为我们不需要它
            // public SomeType ExtendValue { get; set; } // å¦‚果需要,可以注释掉或删除这行
        }
        public class Row
        {
            public List<RowValue> RowValue { get; set; }
            public string RowNumber { get; set; }
            // æ³¨æ„ï¼šè¿™é‡Œå‡è®¾ä½ ä¸éœ€è¦componentName,因为JSON示例中没有直接包含它
            // ä½†å¦‚果你的实际JSON或需求中包含,你应该添加这个属性
            // public string ComponentName { get; set; }
        }
        #endregion
        #region OA异常工时申请单 å®žä¾‹ID列表对应的实例详情
        public bool getInstanceDetailList___ErrWorkTimesRequest_OA(List<string> processinstanceIDList, DateTime startTime, DateTime endTime, ref List<Pay_ErrWorkTimesRequestBill> lsmain,ref string msg)
        {
            try
            {
                List<GetProcessInstanceResponse> getProcessInstanceResponses = new List<GetProcessInstanceResponse>();
                //获取钉钉response
                if (getInstanceDetailListByInstanceIDList_OA(processinstanceIDList,ref getProcessInstanceResponses,ref msg)==false)
                {
                    return false;
                }
                foreach(GetProcessInstanceResponse getProcessInstanceResponse in getProcessInstanceResponses)
                {
                    Pay_ErrWorkTimesRequestBill oItem = new Pay_ErrWorkTimesRequestBill();
                    //主表
                    oItem.HDate = getProcessInstanceResponse.Body.Result.FormComponentValues[1].Value;//单据日期计时时间
                    DateTime dateTime1 = DateTime.ParseExact(oItem.HDate + " 00:00:00", "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);
                    if (!(DateTime.Compare(dateTime1, startTime)>=0&& DateTime.Compare(dateTime1, endTime) <= 0))//控制时间
                    {
                        continue;
                    }
                    string[] deptNameAndGroupName = getProcessInstanceResponse.Body.Result.OriginatorDeptName.Split('-');//获取部门拼接字符串数组
                    oItem.HDeptName = deptNameAndGroupName[1];
                    oItem.HGroupName = deptNameAndGroupName[deptNameAndGroupName.Length-1];
                    //主管-负责人
                    string HManagerValue = getProcessInstanceResponse.Body.Result.FormComponentValues[9].Value;//获取的名字含后缀英文
                    string pattern = @"[\u4e00-\u9fff]+"; // ä¸­æ–‡å­—符的Unicode范围
                    MatchCollection matches = Regex.Matches(HManagerValue, pattern);
                    // åˆ›å»ºä¸€ä¸ªStringBuilder来拼接所有匹配的中文字符
                    System.Text.StringBuilder chineseChars = new System.Text.StringBuilder();
                    foreach (Match match in matches)
                    {
                        chineseChars.Append(match.Value);
                    }
                    string HManagerName = chineseChars.ToString();
                    DataSet ds = oCN.RunProcReturn("select * from Gy_Employee with(nolock)  where HName='" + HManagerName + "'", "Gy_Employee");
                    string HMangerID = "0";
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        HMangerID = ds.Tables[0].Rows[0]["HItemID"].ToString();
                    }
                    oItem.HMangerID = int.Parse(HMangerID);
                    //处理部门和班组名称id
                    ds = oCN.RunProcReturn("select * from Gy_Department with(nolock)  where HName='" + oItem.HDeptName + "'", "Gy_Department");
                    string HDeptID = "0";
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        HDeptID = ds.Tables[0].Rows[0]["HItemID"].ToString();
                    }
                    oItem.HDeptID = int.Parse(HDeptID);
                    ds = oCN.RunProcReturn("select * from Gy_Group with(nolock)  where HName='" + oItem.HGroupName + "'", "Gy_Group");
                    string HGroupID = "0";
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        HGroupID = ds.Tables[0].Rows[0]["HItemID"].ToString();
                    }
                    oItem.HGroupID = int.Parse(HGroupID);
                    //反序列化明细内容
                    string tableFieldValue = getProcessInstanceResponse.Body.Result.FormComponentValues[11].Value;
                    List<Row> rows = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Row>>(tableFieldValue);
                    ExtValue extValue = Newtonsoft.Json.JsonConvert.DeserializeObject<ExtValue>(getProcessInstanceResponse.Body.Result.FormComponentValues[11].ExtValue);
                    oItem.HBatchNo = rows[0].RowValue[1].Value.ToString();//批次号-订单号码
                    //任务描述-任务类型+工作内容
                    oItem.HExplanation = getProcessInstanceResponse.Body.Result.FormComponentValues[2].Value + "-" + rows[0].RowValue[0].Value;
                    oItem.HSumRequestTimes = double.Parse(extValue.StatValue[1].Num);//总时长
                    oItem.HPlanQty = 0;//计划数量
                    oItem.HRemark = "钉钉导入" + getProcessInstanceResponse.Body.Result.Title;//备注
                    //循环遍历获取职员名称id,工时
                    for (int i = 0; i < rows.Count; i++)
                    {
                        string[] nameArray = rows[i].RowValue[4].Value.ToString().Split(',');//获取每个人的人名
                        oItem.HPlanQty += double.Parse(rows[i].RowValue[2].Value.ToString());//计划数量
                        for (int j = 0; j < int.Parse(rows[i].RowValue[5].Value.ToString()); j++)
                        {
                            Pay_ErrWorkTimesRequestBillSub pay_ErrWorkTimesRequestBillSub = new Pay_ErrWorkTimesRequestBillSub();
                            ds = oCN.RunProcReturn("select * from Gy_Employee with(nolock)  where HName='" + nameArray[j] + "'", "Gy_Employee");
                            string HEmpID = "0";
                            //设置子表职员id名称
                            if (ds.Tables[0].Rows.Count > 0)
                            {
                                HEmpID = ds.Tables[0].Rows[0]["HItemID"].ToString();
                            }
                            pay_ErrWorkTimesRequestBillSub.HEmpName = nameArray[j];
                            pay_ErrWorkTimesRequestBillSub.HEmpID = int.Parse(HEmpID);
                            pay_ErrWorkTimesRequestBillSub.HRelTimes = double.Parse(rows[i].RowValue[6].Value.ToString());
                            pay_ErrWorkTimesRequestBillSub.HTimes = double.Parse(rows[i].RowValue[6].Value.ToString());
                            pay_ErrWorkTimesRequestBillSub.HRelPay = double.Parse(rows[i].RowValue[10].Value.ToString());
                            pay_ErrWorkTimesRequestBillSub.HPayMoney = pay_ErrWorkTimesRequestBillSub.HRelPay / pay_ErrWorkTimesRequestBillSub.HTimes;
                            pay_ErrWorkTimesRequestBillSub.HRemark = "钉钉导入";
                            oItem.Pay_ErrWorkTimesRequestBillSub.Add(pay_ErrWorkTimesRequestBillSub);
                        }
                    }
                    lsmain.Add(oItem);
                }
                return true;
            }catch(Exception e)
            {
                msg = e.Message;
                return false;
            }
        }
        #endregion
        #region æ•°æ®åŒæ­¥
        public bool getData_ErrWorkTimesRequest(DateTime startTime, DateTime endTime,ref string msg)
        {
            string processCode = "PROC-21371550-FEF1-4EDD-A827-639746E22E43";
            long nextToken = 1;
            int maxResult = 20;//最大20
            List<string> statues = new List<string>{"COMPLETED"};
            this.AppKey = "ding7miekiaqn4rc2ert";
            this.AppSecret = "81qMrC5SvkgjT9t2A3wUyEEENzyl9jJLeXOpjv2SNULc_HSlFZF4NE21YFCXik4K";
            getAccessToken();//设置accessToken
            //获取工时申请的实例ID列表
            List<string> IDList = new List<string>();
            if (getInstanceIDList_OA(processCode,startTime,endTime,nextToken,maxResult,statues,ref IDList, ref msg) == false)
            {
                return false;
            }
            //获取详细信息并附加到列表lsmain中
            List<Pay_ErrWorkTimesRequestBill> lsmain = new List<Pay_ErrWorkTimesRequestBill>();
            if (getInstanceDetailList___ErrWorkTimesRequest_OA(IDList,startTime,endTime, ref lsmain, ref msg) == false)
            {
                return false;
            }
            try
            {
                oCN.BeginTran();
                //删除时间范围内的导入单据
                DataSet dataSet = oCN.RunProcReturn(@"select HInterID from Pay_ErrWorkTimesRequestBillMain a with(nolock) where a.HRemark like '%钉钉导入%' and HDate >= '" + startTime + "' and HDate<='" + endTime + "'", "Pay_ErrWorkTimesRequestBillMain");
                if(dataSet != null && dataSet.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow row in dataSet.Tables[0].Rows)
                    {
                        int HInterID = row.Field<int>("HInterID");
                        oCN.RunProc(@"Delete From Pay_ErrWorkTimesRequestBillMain where HInterID = " + HInterID);
                        oCN.RunProc(@"Delete From Pay_ErrWorkTimesRequestBillSub where HInterID = " + HInterID);
                    }
                }
                //插入数据
                foreach(Pay_ErrWorkTimesRequestBill oItem in lsmain)
                {
                    if (oItem.Pay_ErrWorkTimesRequestBillSub.Count != 0)
                    {
                        string HBillType = "2231";
                        long HInterID = DBUtility.ClsPub.CreateBillID(HBillType, ref DBUtility.ClsPub.sExeReturnInfo);
                        string HBillNo = DBUtility.ClsPub.CreateBillCode_Prod(HBillType, ref DBUtility.ClsPub.sExeReturnInfo, true);
                        int HYear = int.Parse(oItem.HDate.Split('-')[0]);
                        int HPeriod = int.Parse(oItem.HDate.Split('-')[1]);
                        //主表
                        string sql = "insert into Pay_ErrWorkTimesRequestBillMain" +
                        "(HYear,HPeriod,HBillType,HBillSubType,HBillStatus,HInterID,HBillNo,HDate,HInnerBillNo,HDeptID,HGroupID,HMangerID,HICMOInterID,HICMOBillNo,HExplanation,HSumRequestTimes,HMaterID,HUnitID,HPlanQty,HBatchNo,HRemark,HMaker,HMakeDate) " +
                        "values(" +
                        "" + HYear +
                        "," + HPeriod +
                        ",'" + HBillType +
                        "','" + " " +
                        "'," + 1 +
                        "," + HInterID +
                        ",'" + HBillNo +
                        "','" + oItem.HDate +
                        "','" + " " +
                        "'," + oItem.HDeptID +
                        "," + oItem.HGroupID +
                        "," + oItem.HMangerID +
                        "," + "0" +
                        ",'" + " " +
                        "','" + oItem.HExplanation +
                        "'," + oItem.HSumRequestTimes +
                        "," + 0 +
                        "," + 0 +
                        "," + oItem.HPlanQty +
                        ",'" + oItem.HBatchNo +
                        "','" + oItem.HRemark +
                        "','" + "System" +
                        "'," + "GETDATE()" +
                        ")";
                        //主表
                        LogService.Write(sql);
                        oCN.RunProc(sql);
                        int i = 1;
                        foreach (Pay_ErrWorkTimesRequestBillSub oItem_Detail in oItem.Pay_ErrWorkTimesRequestBillSub)
                        {
                            sql = "insert into Pay_ErrWorkTimesRequestBillSub" +
                            "(HInterID,HEntryID,HEmpID,HWorkTypeID,HPayMoney,HBTimes,HETimes,HTimes,HRelTimes,HRelPay,HRemark,HSourceInterID,HSourceEntryID,HSourceBillNo" +
                            ",HSourceBillType,HRelationQty,HRelationMoney) " +
                            "values(" +
                            "" + HInterID +
                            "," + i +
                            "," + oItem_Detail.HEmpID +
                            "," + 0 +
                            "," + oItem_Detail.HPayMoney +
                            ",'" + "" +
                            "','" + "" +
                            "'," + oItem_Detail.HTimes +
                            "," + oItem_Detail.HRelTimes +
                            "," + oItem_Detail.HRelPay +
                            ",'" + oItem_Detail.HRemark +
                            "'," + 0 +
                            "," + 0 +
                            ",'" + " " +
                            "','" + " " +
                            "'," + 0 +
                            "," + 0 +
                            ")";
                            LogService.Write(sql);
                            oCN.RunProc(sql);
                            i += 1;
                        }
                    }
                }
                oCN.Commit();
                return true;
            }
            catch (Exception e)
            {
                LogService.Write(e.Message);
                msg = e.Message;
                oCN.RollBack();
                return false;
            }
        }
        #endregion
        #endregion
        #endregion
@@ -2932,5 +3421,39 @@
        }
        #endregion
        #region é’‰é’‰æ•°æ®åŒæ­¥OA
        [Route("DD_DataSynchronization/DataSynchronization_DingDing_OA")]
        [HttpGet]
        public object DataSynchronization_DingDing_OA(DateTime startTime, DateTime endTime, string user)
        {
            string msg = "";
            try
            {
                if (getData_ErrWorkTimesRequest(startTime,endTime,ref msg) == false)
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "Exception!" + msg;
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = "Sucess!";
                objJsonResult.data = null;
                return objJsonResult;
            }
            catch (Exception e)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "Exception!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        #endregion
    }
}