using Microsoft.Data.SqlClient; using Microsoft.EntityFrameworkCore; using Newtonsoft.Json.Schema; using Quartz; using System; using System.Collections.Generic; using System.Data; using System.Diagnostics; using System.Dynamic; using System.IO; using System.Linq; using System.Net.Http; using System.Net.Http.Json; using System.Text.Json; using System.Threading; using System.Threading.Tasks; using wispro.sp.entity; using wispro.sp.utility; namespace wispro.sp.api.Job { public class UpdateJXDataFromIPEasyJob : IJob { public dynamic GetItemFromIPEasyDB(PerformanceItem Item, spDbContext spDb) { return wispro.sp.utility.IPEasyUtility.GetPerformanceRecord(Item.CaseNo, Item.DoItem,Item.CaseStage); #region 从维德系统服务上的API获取数据 //HttpClient http = new HttpClient(); ////http.GetFromJsonAsync($"{ConfigHelper.GetSectionValue("IPEasySetting:ipEeasyApi")}/api/ipEasy/GetItemFromIPEasyDB?CaseNo={Item.CaseNo}&DoItem={Item.DoItem}").Wait(); //var response = http.GetFromJsonAsync($"{ConfigHelper.GetSectionValue("IPEasySetting:ipEeasyApi")}/api/ipEasy/GetItemFromIPEasyDB?CaseNo={Item.CaseNo}&DoItem={Item.DoItem}").Result; //dynamic retItem = new ExpandoObject(); //JsonElement json = ((System.Text.Json.JsonElement)response); //retItem.CaseNo = json.GetProperty("CaseNo").GetString(); //retItem.ApplicationType = json.GetProperty("ApplicationType").GetString(); //retItem.BusinessType = json.GetProperty("BusinessType").GetString(); //retItem.DoItem = json.GetProperty("DoItem").GetString(); //retItem.CaseStage = json.GetProperty("CaseStage").GetString(); //retItem.CaseCoefficient = json.GetProperty("CaseCoefficient").GetString(); //retItem.DoItemCoefficient = json.GetProperty("DoItemCoefficient").GetString(); //retItem.DoPersons = json.GetProperty("DoPersons").GetString(); //retItem.Reviewer = json.GetProperty("Reviewer").GetString(); //retItem.CustomerName = json.GetProperty("CustomerName").GetString(); //retItem.FinishedDate = json.GetProperty("FinishedDate").GetString(); //retItem.WordCount = json.GetProperty("WordCount").GetString(); //retItem.ReturnDate = json.GetProperty("ReturnDate").GetString(); //retItem.CaseType = json.GetProperty("CaseType").GetString(); //retItem.CaseState = json.GetProperty("CaseState").GetString(); //retItem.DoItemMemo = json.GetProperty("DoItemMemo").GetString(); //retItem.DoItemState = json.GetProperty("DoItemState").GetString(); //retItem.CaseName = json.GetProperty("CaseName").GetString(); //retItem.EntrustingDate = json.GetProperty("EntrustingDate").GetString(); //retItem.CustomerLimitDate = json.GetProperty("CustomerLimitDate").GetString(); //retItem.InternalDate = json.GetProperty("InternalDate").GetString(); //retItem.FirstDraftDate = json.GetProperty("FirstDraftDate").GetString(); //retItem.CaseMemo = json.GetProperty("CaseMemo").GetString(); //retItem.FinalizationDate = json.GetProperty("FinalizationDate").GetString(); //return retItem; #endregion #region old code 直接从数据库中获取数据 //dynamic retItem = new ExpandoObject(); //var dt = new DataTable(); //using (var conn = new SqlConnection(ConfigHelper.GetSectionValue("IPEasySetting:ConnectionStrings"))) //{ // try // { // conn.Open(); // string strSql = @"SELECT distinct p_case_info.case_volume as case_volume, // i_apply_type.apply_type_zh_cn as apply_type_zh_cn, // i_business_type.business_type_zh_cn as business_type_zh_cn, // i_ctrl_proc.ctrl_proc_zh_cn as ctrl_proc_zh_cn, // (select case_status_zh_cn from i_case_status where case_status_id=p_proc_info.review_stage) as review_stage, // i_case_coefficient.case_coefficient_zh_cn as case_coefficient, // i_proc_coefficient.proc_coefficient_zh_cn as proc_coefficient, // (select proc_coefficient_zh_cn from p_proc_info pr // left join i_proc_coefficient pc on pc.proc_coefficient_id=pr.proc_coefficient_id // where case_id=p_case_info.case_id and ctrl_proc_id='8b96378e-05a0-4a8d-b3d1-39af92fddaf5' // and pr.seq= // (select max(seq) from p_proc_info pr where case_id=p_case_info.case_id // and ctrl_proc_id='8b96378e-05a0-4a8d-b3d1-39af92fddaf5'and seq 1) // { // bool bRet = false; // foreach (DataRow row in dt.Rows) // { // retItem.FinishedDate = row["finish_date"].ToString(); // DateTime temDate = DateTime.Now; // DateTime.TryParse(retItem.FinishedDate,out temDate); // if ((Item.FinishedDate.HasValue && Item.FinishedDate.Value == temDate) || // (!string.IsNullOrEmpty(Item.CaseStage) && Item.CaseStage == row["review_stage"].ToString())) // { // GetItemData(retItem, row); // bRet = true; // break; // } // } // if (!bRet) // { // DataRow row = dt.Rows[0]; // GetItemData(retItem, row); // } // } // else // { // if (dt.Rows.Count > 0) // { // DataRow row = dt.Rows[0]; // GetItemData(retItem, row); // } // } // } // } // } // catch (Exception ex) // { // // error handling // throw; // } // finally // { // conn.Close(); // } //} //return retItem; #endregion } private static void GetItemData(dynamic retItem, DataRow row) { retItem.CaseNo = row["case_volume"].ToString(); retItem.ApplicationType = row["apply_type_zh_cn"].ToString(); retItem.BusinessType = row["business_type_zh_cn"].ToString(); retItem.DoItem = row["ctrl_proc_zh_cn"].ToString(); ; retItem.CaseStage = row["review_stage"].ToString(); retItem.CaseCoefficient = row["case_coefficient"].ToString(); retItem.DoItemCoefficient = row["proc_coefficient"].ToString(); retItem.DoPersons = row["pic"].ToString(); retItem.Reviewer = row["reviser"].ToString(); retItem.CustomerName = row["customer_name"].ToString(); retItem.FinishedDate = row["finish_date"].ToString(); retItem.WordCount = row["translate_count"].ToString(); retItem.ReturnDate = row["back_date"].ToString(); retItem.CaseType = row["case_type_zh_cn"].ToString(); retItem.CaseState = row["case_status_zh_cn"].ToString(); retItem.DoItemState = row["proc_status_zh_cn"].ToString(); retItem.DoItemMemo = row["proc_note"].ToString(); retItem.CaseName = row["case_name"].ToString(); retItem.EntrustingDate = row["charge_date"].ToString(); retItem.CustomerLimitDate = row["cus_due_date"].ToString(); retItem.InternalDate = row["int_due_date"].ToString(); retItem.FirstDraftDate = row["first_doc_date"].ToString(); retItem.CaseMemo = row["case_remark"].ToString(); retItem.FinalizationDate = row["finish_doc_date"].ToString(); } public void UpdateFromIPEasy(PerformanceItem Item,spDbContext spDb) { List caseNos = new List() { "AHK2211737", "PAHK2211739", "PACN2120893-TS", "PAKR2117244", "PAIN2117242", "PAEPO1912286", "PAUS2116581-转所", "PATW2111942", "PACN2024870-TS客户自行递交", "PAUS2010428", "PAEPO2111073", "PAUS1915675", "PAUS2111071", "PAAU2210170", "PAUS2010164", "PACN1914374", "PAUS2210315", "PAUS2016669-结案", "PAEPO1510129", "PAUS2016669-结案", "PACN2026619-TS", "PACN2123589-TS", "PACN2125458" }; if (caseNos.Contains(Item.CaseNo)) { System.Diagnostics.Debug.WriteLine(""); } dynamic retObj = GetItemFromIPEasyDB(Item, spDb); //utility.IPEasyUtility.GetPerformanceRecord(Item.CaseNo, Item.DoItem, string.IsNullOrEmpty(Item.CaseStage) ? null : Item.CaseStage); var appealAJXS = spDb.AppealTypes.FirstOrDefault(p => p.Name.Contains("案件系数")); var appealCLSXXS = spDb.AppealTypes.FirstOrDefault(p => p.Name.Contains("处理事项系数")); var caseXS = (spDb.AppealRecords.FirstOrDefault(p => p.ItemId == Item.Id && p.TypeId == appealAJXS.Id && p.State == 1) == null); var doItemXS = (spDb.AppealRecords.FirstOrDefault(p => p.ItemId == Item.Id && p.TypeId == appealCLSXXS.Id && p.State == 1) == null); IDictionary keyValuePairs = (IDictionary)retObj; if (keyValuePairs.ContainsKey("DoItemCoefficient") && Item.DoItemCoefficient != retObj.DoItemCoefficient && doItemXS) { Item.DoItemCoefficient = retObj.DoItemCoefficient; } if (keyValuePairs.ContainsKey("WordCount")) { if (!string.IsNullOrEmpty(retObj.WordCount)) { var wordCount = int.Parse(retObj.WordCount); if (wordCount != Item.WordCount) Item.WordCount = wordCount; } } if (keyValuePairs.ContainsKey("Reviewer") && (Item.Reviewer == null || Item.Reviewer.Name != retObj.Reviewer) && !string.IsNullOrEmpty(retObj.Reviewer)) { string name = retObj.Reviewer; if (!string.IsNullOrEmpty(name)) { string temName = name.Split('-')[0].Trim(); if (!name.Contains("君龙")) { temName = name; } var temReviewer = spDb.Staffs.Where(s => s.Name == temName).FirstOrDefault(); if (temReviewer == null) { //Item.Reviewer = new Staff() { Name = retObj.Reviewer }; } else { //Item.Reviewer = temReviewer; Item.ReviewerId = temReviewer.Id; } } } if (keyValuePairs.ContainsKey("ApplicationType") && Item.ApplicationType != retObj.ApplicationType && !string.IsNullOrEmpty(retObj.ApplicationType)) { Item.ApplicationType = retObj.ApplicationType; } if (keyValuePairs.ContainsKey("CaseCoefficient") && Item.CaseCoefficient != retObj.CaseCoefficient && caseXS) { if(!(retObj.CaseCoefficient == null && string.IsNullOrEmpty(Item.CaseCoefficient))) Item.CaseCoefficient = retObj.CaseCoefficient; } if (keyValuePairs.ContainsKey("CaseType") && Item.CaseType != retObj.CaseType && !string.IsNullOrEmpty(retObj.CaseType)) { Item.CaseType = retObj.CaseType; } if (spDb.Entry(Item).State != EntityState.Unchanged) { if (Item.AgentFeedbackMemo != "特殊点数申诉") { new Controllers.PerformanceItemController(spDb, new Services.FileTaskCacheService()).RefreshPoint(Item); } } } public void RefreshFromIPEasy(int type) { System.Threading.Thread t = new Thread(new ParameterizedThreadStart(RefreshFromIPEasy_BatchThread)); t.Start(type); } /// /// 批量从IPEasy中更新数据 /// /// /// 0:所有; /// 1:BasePoint为空记录; /// 2:新申请案件系数为空记录; /// 3:BasePoint为空记录 或者 新申请案件系数为空记录 /// /// private void RefreshFromIPEasy_BatchThread(object type) { spDbContext spDb = new spDbContext(); var Results = spDb.PerformanceItems.Include(p=>p.Customer).Where(p => (p.AgentFeedbackMemo != "已算绩效" || p.AgentFeedbackMemo == null) && p.CalMonth.Status == 0 && !p.CaseNo.StartsWith("J")); switch (type.ToString()) { case "1": Results = Results.Where(p => p.BasePoint == null); break; case "2": Results = Results.Where(p => p.Type == "新申请" && p.CaseCoefficient == ""); break; case "3": Results = Results.Where(p => p.BasePoint == null || (p.DoItem == "新申请" && p.CaseCoefficient == "")); break; } var listItems = Results.ToList(); int i = 0; foreach (var Item in listItems) { int iTryCount = 0; TryAgain: try { iTryCount++; UpdateFromIPEasy(Item, spDb); Log($"{DateTime.Now}\t{++i}/{listItems.Count}\t{Item.CaseNo}"); System.Diagnostics.Debug.WriteLine($"{DateTime.Now}\t{i}/{listItems.Count}\t{Item.CaseNo}"); //} } catch (Exception ex) { if (iTryCount <= 3) { goto TryAgain; } System.Diagnostics.Debug.WriteLine(ex.ToString()); Log($"{DateTime.Now}\t{++i}/{listItems.Count}\t{Item.CaseNo}\t更新失败!"); } } } public Task Execute(IJobExecutionContext context) { spDbContext spDb = new spDbContext(); var lstItem = spDb.PerformanceItems.Where(p => ((p.AgentFeedbackMemo != "已算绩效" || p.AgentFeedbackMemo==null ) && p.CalMonth.Status == 0 && !p.CaseNo.StartsWith("J"))) .Include(p=>p.Reviewer) .Include(p=>p.CalMonth) .Include(p=>p.Customer) .Include(p=>p.ItemStaffs).ThenInclude(p=>p.DoPerson) .ToList(); if (lstItem != null) { int i = 0; foreach (var Item in lstItem) { System.Threading.Thread.Sleep(100); int iTryCount = 0; TryAgain: try { iTryCount++; UpdateFromIPEasy(Item, spDb); Log($"{DateTime.Now}\t{++i}/{lstItem.Count}\t{Item.CaseNo}"); System.Diagnostics.Debug.WriteLine($"{DateTime.Now}\t{i}/{lstItem.Count}\t{Item.CaseNo}"); //} } catch(Exception ex) { if(iTryCount <= 3) { goto TryAgain; } System.Diagnostics.Debug.WriteLine(ex.ToString()); Log($"{DateTime.Now}\t{++i}\t{Item.CaseNo}\r\n{ex.ToString()}"); } } } return Task.CompletedTask; } private void Log(string strMessage) { StreamWriter sw = File.AppendText("c:\\temp\\log.txt"); sw.WriteLine($"{strMessage}"); sw.Flush(); sw.Close(); sw.Dispose(); } } }