using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using SA.Entity.Request; using SA.Core.Util; using SA.Entity.zxdcrm_audit_Models; using SA.Core.Init; using System.Text.Json; namespace SA.AppDomain.SHCheck { public class SHCheckOrder { private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); private static Dictionary midproductnames = new Dictionary(); private static Dictionary shproductnames = new Dictionary(); private static Dictionary courseModuleids = new Dictionary(); public enum CheckType { CheckIgnore, CheckExpire, CheckFail, CheckTest , CheckSuccess,CheckMissing} public void InitSH() { var begintime= InitDB.auditDb.Queryable().Max(s => s.trade_time); var begintimstr = ""; if (begintime.HasValue) { begintimstr= begintime.Value.Date.ToString("yyyy-MM-dd"); } else { begintimstr = "2022-01-01"; } SHRequest.shQueryList shQueryList = new SHRequest.shQueryList() { pageSize = 500, pageCurrent = 0, beginPayTime = begintimstr, endPayTime=DateTime.Now.Date.ToString("yyyy-MM-dd") }; logger.Debug(JsonSerializer.Serialize(shQueryList)); while (true) { var html = GetListHtml(shQueryList); logger.Debug(html); var shlistNum = GetShListNum(html); if (shlistNum.total == 0) { logger.Debug("查询数量为0"); break; } var shorders = GetSh_Orders(html); var i = InitDB.auditDb.Storageable(shorders).SplitIgnore(d => d.Any()).WhereColumns(d => d.gg_orderno) .SplitInsert(d => d.NotAny()).ExecuteCommand(); Thread.Sleep(10000); shQueryList.pageCurrent= shlistNum.pageCurrent+1; if (shlistNum.pageCurrent * shQueryList.pageSize > shlistNum.total) break; } } public void CheckSHFromSH() { var endDateTime = InitDB.auditDb.Queryable().Max(d => d.otime); //var endDateTime = DateTime.Parse("2022-07-01"); var shlist = InitDB.auditDb.Queryable().Where(d => d.endtime >= DateTime.Parse("2022-01-01")) .Where(d => d.trade_time < endDateTime.Value.AddDays(1)) .OrderByDescending(d => d.trade_time).ToList(); long[] otderstatus = new long[] { 220, 200, 205, 90 }; var xinmeiti3list = InitDB.auditDb.Queryable().Where(d => otderstatus.Contains(d.orderstatus.Value)) .Where(d => d.endtime3 >= DateTime.Parse("2022-01-01")) .ToList(); var resList = InitDB.auditDb.Queryable().Where(d => d.customerfrom.Equals("27100")).ToList(); var reslistKeyFromResid = resList.ToDictionary(d => d.resid, d => d.lastnum3); var reslistKeyFromMbile = resList.GroupBy(d => d.lastnum3).ToDictionary(d => d.Key, d => d.ToList()); logger.Debug(string.Format("查询sh_order数据{0}条,xinmeiti3数据{1}条,开始比对", shlist.Count, xinmeiti3list.Count)); var shNoresultList = new List(); Dictionary checkRight = new Dictionary(); List shCheckResultList = new List(); List shCheckIgnoreList = new List(); List sh_Orderchecks = new List(); //产品名称缓存 var wx_Szzysubproducts = InitDB.auditDb.Queryable().ToList(); foreach (var item in wx_Szzysubproducts) { midproductnames.TryAdd(item.mid.Value, item.subproductname); } var shproducts = InitDB.auditDb.Queryable().ToList(); foreach (var item in shproducts) { shproductnames.TryAdd(item.module_id.Value, item.course); courseModuleids.TryAdd(item.course, item.module_id.Value); } // var courses = shlist.Select(d => d.course).Distinct(); var shproduct_courses = shproducts.Select(d => d.course).Distinct(); //筛选人员 //测试人员 Dictionary TestUsers = new Dictionary(); //暂停人员 Dictionary TimeoutUsers = new Dictionary(); //4.1前赠送投资时钟网络课人员 Dictionary TzszUsers = new Dictionary(); //var timeoutlist=shlist.Where(d => d.remark.Contains("暂停")); foreach (var user in shlist) { if (user.remark.Contains("暂停")) TimeoutUsers.TryAdd(user.userno, user.user); if (user.trade_time < DateTime.Parse("2022-04-01") && user.course.Equals("投资时钟网络课")) TzszUsers.TryAdd(user.userno, user.user); if (user.remark.Contains("测试")|| user.remark.Contains("合规")) TestUsers.TryAdd(user.userno, user.user); } foreach (var item in shlist) { if (item.gg_orderno.Equals("20220630135231367")) { var begincheck = ""; } var shCheckResult = new sh_ordercheck() { sh_orderno = item.gg_orderno, sh_orderid=item.orderid.ToString(), course=item.course, userno=item.userno, user=item.user, username= Getusername(item.remark), endtime =item.endtime.Value, sh_opendays=item.opendays.Value, diffdays=6666, checktime=DateTime.Now }; //检查orderid if (item.orderid.HasValue && item.orderid > 0) { var xinmeiti3 = xinmeiti3list.Where(d => d.orderid.Equals(item.orderid.ToString())).ToList(); if (xinmeiti3.Count >0) { //填写订单号 CheckXinmeiti3List(xinmeiti3, shCheckResult, ref checkRight); } } if (!checkRight.ContainsKey(item.gg_orderno)) { var xinmeiti3 = xinmeiti3list.Where(d => d.bussiness_order_id.Equals(item.orderid.ToString())).ToList(); if (xinmeiti3.Count > 0) { //填写订单号 CheckXinmeiti3List(xinmeiti3, shCheckResult, ref checkRight); } } if (shCheckResult.diffdays > 10 || shCheckResult.diffdays < -10) { //未填写订单号 if (!checkRight.ContainsKey(item.gg_orderno)) { //找相同用户手机号 var residList = new List(); var lastnum3 = item.user.Trim().Insert(4, "*").Remove(8, 1); if (reslistKeyFromMbile.TryGetValue(lastnum3, out residList)) { foreach (var res in residList) { var resxinmeiti3s = xinmeiti3list.Where(d => d.resid.Equals(res.resid)).ToList(); if (resxinmeiti3s.Count == 0) continue; if (shCheckResult.checktype.Equals(CheckType.CheckSuccess.ToString())) continue; CheckXinmeiti3List(resxinmeiti3s, shCheckResult, ref checkRight); } } } if (!checkRight.ContainsKey(item.gg_orderno)) { //通过用户名查找 if (!string.IsNullOrEmpty(shCheckResult.username)) { var resxinmeiti3s = xinmeiti3list.Where(d => d.cname.Equals(shCheckResult.username)).ToList(); CheckXinmeiti3List(resxinmeiti3s, shCheckResult, ref checkRight); } } } if (!checkRight.ContainsKey(item.gg_orderno)) { ///忽略,忽略同用户的同课程成功 var userchecks = checkRight.Values.Where(d => d.userno == item.userno).ToList(); bool ignore = false; foreach (var usercheck in userchecks) { if (!ignore&&usercheck.course.Equals(item.course)) { shCheckResult.checktype =CheckType.CheckIgnore.ToString(); sh_Orderchecks.Add(shCheckResult); logger.Debug($"【{shCheckResult.checktype}】:{SerializeHelper.ToJson(shCheckResult)}"); ignore = true; } } //忽略同订单成功 var orderSuccess = checkRight.Values.Where(d => d.orderid == item.orderid.ToString()).ToList(); if (!ignore&&orderSuccess.Count > 0) { shCheckResult.checktype = CheckType.CheckIgnore.ToString(); sh_Orderchecks.Add(shCheckResult); shCheckResult.checkinfo = "同订单号成功了"; logger.Debug($"【{shCheckResult.checktype}】同订单号:{SerializeHelper.ToJson(shCheckResult)}"); ignore = true; } if (!ignore) { if (item.remark.Contains("测试") || TestUsers.ContainsKey(item.userno)) { shCheckResult.checktype = CheckType.CheckTest.ToString(); shCheckResult.checkinfo = item.remark; } else if (shCheckResult.endtime < endDateTime && (!shCheckResult.endtime3.HasValue || shCheckResult.endtime3 < endDateTime)) { shCheckResult.checktype = CheckType.CheckExpire.ToString(); } else if (item.remark.Contains("暂停") || item.remark.Contains("换") || TimeoutUsers.ContainsKey(item.userno)) { shCheckResult.checktype = CheckType.CheckFail.ToString(); shCheckResult.checkinfo = $"TimeoutUsers:{item.remark}"; } else if (TzszUsers.ContainsKey(item.userno)) { shCheckResult.checktype = CheckType.CheckFail.ToString(); shCheckResult.checkinfo = $"4.1日前赠送过投资时钟网络课:{item.remark}"; } else { shCheckResult.checktype = CheckType.CheckFail.ToString(); //shCheckResult.checkinfo = $"remark:{item.remark}"; } sh_Orderchecks.Add(shCheckResult); logger.Debug($"【{shCheckResult.checktype}】:{SerializeHelper.ToJson(shCheckResult)}"); } } } var noresult= getxinmeiti3CheckResult(checkRight.Values.ToList(), xinmeiti3list, sh_Orderchecks); sh_Orderchecks.AddRange(noresult); foreach (var item in checkRight.Values) { item.checktype = CheckType.CheckSuccess.ToString(); sh_Orderchecks.Add(item); } var i = InitDB.auditDb.Storageable(sh_Orderchecks).SplitUpdate(d => d.Any()).WhereColumns(d => d.sh_orderno) .SplitInsert(d => d.NotAny()).ExecuteCommand(); } public List getxinmeiti3CheckResult(List sh_OrderchecksRight, List xinmeiti3list, List sh_OrderchecksAll) { List sh_OrderchecksNoCheck = new List(); var successresult = new Dictionary(); var result = new Dictionary(); foreach (var item in sh_OrderchecksRight) { successresult.TryAdd($"{item.resid}:{item.course}", item.course); } foreach (var item in sh_OrderchecksAll) { result.TryAdd($"{item.orderid}", item.orderid); } foreach (var item in xinmeiti3list) { var xincourse = ""; shproductnames.TryGetValue(item.module_id.Value, out xincourse); if (successresult.ContainsKey($"{item.resid}:{xincourse}")) continue; if (result.ContainsKey($"{item.orderid}")) continue; var day = DiffDay(item.endtime3, item.endtime_xinmeiti3); if (day <= 1 && day >= -1) continue; sh_ordercheck sh_OrdercheckNo = new sh_ordercheck(); sh_OrdercheckNo.orderid = item.orderid; sh_OrdercheckNo.resid = item.resid; sh_OrdercheckNo.cname = item.cname; sh_OrdercheckNo.subproductname = item.subproductname; sh_OrdercheckNo.otime = item.otime.Value; sh_OrdercheckNo.opendays = Int32.Parse(item.opendays); sh_OrdercheckNo.endtime3 = item.endtime3.Value; sh_OrdercheckNo.checktype = CheckType.CheckMissing.ToString(); sh_OrdercheckNo.diffdays = day; sh_OrderchecksNoCheck.Add(sh_OrdercheckNo); } return sh_OrderchecksNoCheck; } public void CheckXinmeiti3List(List xinmeiti3s , sh_ordercheck sh_CheckOrder,ref Dictionary checkRight) { foreach (var x3item in xinmeiti3s.OrderBy(d=>d.endtime3)) { if (checkSHProductName(x3item, sh_CheckOrder.course)) { sh_CheckOrder.orderid = x3item.orderid; sh_CheckOrder.resid = x3item.resid; sh_CheckOrder.cname = x3item.cname; sh_CheckOrder.subproductname = x3item.subproductname; sh_CheckOrder.otime = x3item.otime.Value; sh_CheckOrder.opendays = Int32.Parse(x3item.opendays); sh_CheckOrder.endtime3 = x3item.endtime3.Value; var day = DiffDay(x3item.endtime3, sh_CheckOrder.endtime); sh_CheckOrder.diffdays = day; if (day <= 1 && day >= -1) { sh_CheckOrder.checktype = CheckType.CheckSuccess.ToString(); checkRight.TryAdd(sh_CheckOrder.sh_orderno, sh_CheckOrder); logger.Debug($"CheckRight:{SerializeHelper.ToJson(sh_CheckOrder)}"); break; } else { logger.Debug($"【Checking】:{SerializeHelper.ToJson(sh_CheckOrder)}"); } } else { logger.Debug($"【CheckingcourseF】:sh_orderno:{sh_CheckOrder.sh_orderno},course:{sh_CheckOrder.course},x3itemorderid:{x3item.orderid},moduileid:{x3item.module_id},subproductname:{x3item.subproductname}"); } } } public string Getusername(string remark) { if (remark.Contains("测试") || remark.Contains("合规")) return ""; if (string.IsNullOrEmpty(remark)) return ""; if (remark.Length < 4) return remark; if (remark.Contains(',')) { return remark.Substring(0, remark.IndexOf(',')); } if (remark.Contains(',')) { return remark.Substring(0, remark.IndexOf(',')); } if (remark.Contains(' ')) { return remark.Substring(0, remark.IndexOf(' ')); } if (remark.Contains('临')) { return remark.Substring(0, remark.IndexOf('临')); } return ""; } public int DiffDay(DateTime? xin3endtime,DateTime? endTime) { if (!xin3endtime.HasValue) return 1000; if (!endTime.HasValue) return -1000; TimeSpan timeSpan = xin3endtime.Value - endTime.Value; var day= Convert.ToInt32(timeSpan.TotalDays); return day; } public bool checkSHProductName(xinmeiti3 xinmeiti3,string course) { var xincourse = ""; if(shproductnames.TryGetValue(xinmeiti3.module_id!.Value, out xincourse)) { if(xincourse.Equals(course))return true; } var moudleid = 0; if(courseModuleids.TryGetValue(course,out moudleid)) { if(xinmeiti3.module_id== moudleid) return true; } var xinproductName = ""; if (!midproductnames.TryGetValue(xinmeiti3.module_id.Value, out xinproductName)) { xinproductName = xinmeiti3.productname; } if (course.Equals(xinproductName))return true; if(xinproductName.Contains(course.Replace("课",""))) return true; if (xinproductName.Contains(course.Replace("投资时钟网络课", "《8888夏令营》网络课"))) return true; if (xinproductName.Contains(course.Replace("投资时钟", "《8888夏令营》网络课"))) return true; return false; } public List GetSh_Orders(string html) { List sh_orders = new List(); HtmlAgilityPack.HtmlDocument htmlDocument = new HtmlAgilityPack.HtmlDocument(); htmlDocument.LoadHtml(html); HtmlAgilityPack.HtmlNodeCollection list = htmlDocument.DocumentNode.SelectNodes("//tbody//tr"); foreach (var node in list) { var nodehtml = node.InnerHtml; HtmlAgilityPack.HtmlDocument nodehtmlDocument = new HtmlAgilityPack.HtmlDocument(); nodehtmlDocument.LoadHtml(nodehtml); var href = nodehtmlDocument.DocumentNode.SelectSingleNode("//td[2]/a").Attributes["href"].Value; sh_order sh_Order= new sh_order(); sh_Order.gg_orderid = href.Substring(href.IndexOf('=') + 1, href.Length - href.IndexOf('=') - 1); sh_Order.gg_orderno = nodehtmlDocument.DocumentNode.SelectSingleNode("//td[2]/a").InnerText; var td1 = nodehtmlDocument.DocumentNode.SelectSingleNode("//td[2]").InnerText.Trim(); sh_Order.gg_ordertype= td1.Substring(td1.IndexOf('\t'), td1.Length - td1.IndexOf('\t')).Trim(); int orderid = 0; Int32.TryParse(nodehtmlDocument.DocumentNode.SelectSingleNode("//td[3]").InnerText.Trim(),out orderid); sh_Order.orderid = orderid; sh_Order.course = nodehtmlDocument.DocumentNode.SelectSingleNode("//td[4]").InnerText.Trim().Split('\t')[0].Trim(); sh_Order.refno = nodehtmlDocument.DocumentNode.SelectSingleNode("//td[4]/a").Attributes["href"].Value.Trim().Split('&')[0].Split('=')[1].Trim(); sh_Order.lectureruser = nodehtmlDocument.DocumentNode.SelectSingleNode("//td[5]/a").InnerText.Trim(); sh_Order.lectureruserno = nodehtmlDocument.DocumentNode.SelectSingleNode("//td[5]/a").Attributes["href"].Value.Trim().Split('=')[1].Trim(); sh_Order.userno = nodehtmlDocument.DocumentNode.SelectSingleNode("//td[6]/a").Attributes["href"].Value.Trim().Split('=')[1].Trim(); sh_Order.user = nodehtmlDocument.DocumentNode.SelectSingleNode("//td[6]/a").InnerText.Trim(); sh_Order.paytype = nodehtmlDocument.DocumentNode.SelectSingleNode("//td[7]").InnerText.Trim().Split('【')[0].Trim(); sh_Order.payprice = decimal.Parse(nodehtmlDocument.DocumentNode.SelectSingleNode("//td[7]").InnerText.Trim().Split('¥')[1].TrimEnd('】')); sh_Order.trade_channel = nodehtmlDocument.DocumentNode.SelectSingleNode("//td[8]").InnerText.Trim(); sh_Order.order_status = nodehtmlDocument.DocumentNode.SelectSingleNode("//td[9]").InnerText.Trim(); sh_Order.trade_status = nodehtmlDocument.DocumentNode.SelectSingleNode("//td[10]").InnerText.Trim(); var endtime = new DateTime(); DateTime.TryParse(nodehtmlDocument.DocumentNode.SelectSingleNode("//td[11]").InnerText.Trim(),out endtime); sh_Order.endtime = endtime; int opendays = 0; Int32.TryParse(nodehtmlDocument.DocumentNode.SelectSingleNode("//td[12]").InnerText.Trim(),out opendays); sh_Order.opendays = opendays; sh_Order.remark = nodehtmlDocument.DocumentNode.SelectSingleNode("//td[13]").InnerText.Trim(); sh_Order.trade_time =DateTime.Parse(nodehtmlDocument.DocumentNode.SelectSingleNode("//td[14]").InnerText.Trim()); sh_orders.Add(sh_Order); } return sh_orders; } public SHRequest.shListNum GetShListNum(string html) { SHRequest.shListNum sh_listNum = new SHRequest.shListNum(); HtmlAgilityPack.HtmlDocument htmlDocument = new HtmlAgilityPack.HtmlDocument(); htmlDocument.LoadHtml(html); HtmlAgilityPack.HtmlNodeCollection list = htmlDocument.DocumentNode.SelectNodes("//div[@class='pagination-box']"); var total = 0; Int32.TryParse(htmlDocument.DocumentNode.SelectSingleNode("//div[@class='pagination-box']").Attributes["data-total"].Value.Trim(),out total); sh_listNum.total = total; if ( total == 0) { return sh_listNum; } sh_listNum.pageSize = Int32.Parse(htmlDocument.DocumentNode.SelectSingleNode("//div[@class='pagination-box']").Attributes["data-page-size"].Value.Trim()); sh_listNum.pageCurrent = Int32.Parse(htmlDocument.DocumentNode.SelectSingleNode("//div[@class='pagination-box']").Attributes["data-page-current"].Value.Trim()); return sh_listNum; } public string GetListHtml(SHRequest.shQueryList shQuery) { var kvs=DictionaryHelper.GetKeyValues(shQuery); var kvstr = DictionaryHelper.GetkvString(kvs); HttpContent httpContent = new StringContent(kvstr); httpContent.Headers.Add("Cookie", "bjui_theme=blue; SESSION=N2E3M2NiOGUtNGZiOC00MTNlLWJhMWEtNTc3YmQ2MGRjN2Jj"); var url = "https://sboss.tvlicai.com/course/orderInfo/list"; var result = HttpHelper.HttpPost(url, httpContent, "application/x-www-form-urlencoded"); return result; } public string get() { var url = "http://192.168.11.141:24434/v1/api/open/data/sync/organization?appid=crm_tg_dng8&update_time="; HttpRequestMessage httpRequestMessage = new HttpRequestMessage(); httpRequestMessage.RequestUri= new Uri(url); httpRequestMessage.Method = HttpMethod.Get; httpRequestMessage.Headers.TryAddWithoutValidation("authorization", "crm_tg_dng8:b461e65d3a707c2f79cf1d7892a4cb95"); httpRequestMessage.Headers.TryAddWithoutValidation("timestamps", "1656532966"); HttpContent httpContent = new StringContent(""); httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); // httpContent.Headers.Add("authorization", "crm_tg_dng8:1ba9915985bdead397849b8dffa48f5e"); httpRequestMessage.Content = httpContent; var result = HttpHelper.Send(httpRequestMessage); return result; } } }