using CRM.Core.BLL.B; using CRM.Core.BLL.Wx; using CRM.Core.DAL; using CRM.Core.Model; using CRM.Core.Model.Entity; using CRM.Core.Model.EntityAudit; using CRM.Core.Model.QueryModels; using NPOI.SS.Formula.Functions; using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Data; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; using WX.CRM.Common; namespace CRM.Core.BLL.Application.Order { public class QueryCashFlow { private static DateTime queryTime; private static int queryTimespanSecond = 30; private static ConcurrentDictionary CashFlowQueryKVS = new ConcurrentDictionary(); private static NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger(); B_Audit_Items_BL b_Audit_Items_bl = new B_Audit_Items_BL(); public List cashFlowQueryResult(DateTime day) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); var daystr=day.Date.ToShortDateString(); CashFlow.CashFlowQueryTimes cashFlowQueryTimes = new CashFlow.CashFlowQueryTimes(); // cashFlowQueryTimes.cashFlowQueries = new List(); if (CashFlowQueryKVS.TryGetValue(daystr,out cashFlowQueryTimes)) { if (cashFlowQueryTimes.QueryTime.AddSeconds(queryTimespanSecond)> DateTime.Now) { return cashFlowQueryTimes.cashFlowQueries; } else { CashFlowQueryKVS.TryRemove(daystr, out cashFlowQueryTimes); } } cashFlowQueryTimes = new CashFlow.CashFlowQueryTimes(); cashFlowQueryTimes.cashFlowQueries = cashFlowQueries(day); cashFlowQueryTimes.QueryTime = DateTime.Now; CashFlowQueryKVS.TryAdd(daystr, cashFlowQueryTimes); stopwatch.Stop(); Logger.Debug($"cashFlowQueryResult 查询{daystr}耗时:{stopwatch.ElapsedMilliseconds}"); return cashFlowQueryTimes.cashFlowQueries; } public List cashFlowQueries(DateTime day) { List cashFlowQueries = new List(); try { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); B_Audit_Items_BL b_Audit_Items_bl = new B_Audit_Items_BL(); var deposits = QueryDeposit(day); var depositstimes = stopwatch.ElapsedMilliseconds; stopwatch.Restart(); var refunds = QueryRefund(day); var refundstimes = stopwatch.ElapsedMilliseconds; stopwatch.Restart(); var orders = QueryOrder(day); var orderstimes = stopwatch.ElapsedMilliseconds; stopwatch.Restart(); var depConsumes= QuerydepConsumes(day); var depConsumestimes = stopwatch.ElapsedMilliseconds; stopwatch.Restart(); var b_Audit_Items = b_Audit_Items_bl.GetB_Audit_Items(); foreach (var item in b_Audit_Items) { var cashFlowQuery = new CashFlow.CashFlowQuery { Day=day.ToString("yyyy-MM-dd"), itemId = item.Id, parentId=item.ParentId, sort=item.Sort, item = item.Item, }; CashFlow.CashFlowQuery cashFlowQuerytemp; if (deposits.TryGetValue(item.Id,out cashFlowQuerytemp)) { cashFlowQuery.payprice = cashFlowQuerytemp.payprice; cashFlowQuery.monPayprice = cashFlowQuerytemp.monPayprice; } if(refunds.TryGetValue(item.Id, out cashFlowQuerytemp)) { cashFlowQuery.Refundprice = cashFlowQuerytemp.Refundprice; cashFlowQuery.monRefundprice = cashFlowQuerytemp.monRefundprice; cashFlowQuery.monRefundOrderNum = cashFlowQuerytemp.monRefundOrderNum; } if(orders.TryGetValue(item.Id, out cashFlowQuerytemp)) { cashFlowQuery.orderNum = cashFlowQuerytemp.orderNum; cashFlowQuery.monOrderNum = cashFlowQuerytemp.monOrderNum; } if(depConsumes.TryGetValue(item.Id, out cashFlowQuerytemp)) { cashFlowQuery.DepConsume = cashFlowQuerytemp.DepConsume; cashFlowQuery.monDepConsume = cashFlowQuerytemp.monDepConsume; } cashFlowQueries.Add(cashFlowQuery); } cashFlowQueries = CashFlow.SortTrees(cashFlowQueries); stopwatch.Stop(); Logger.Debug($"cashFlowQueries 查询deposits,耗时:{depositstimes},查询refunds,耗时:{refundstimes},查询orders,耗时:{orderstimes},查询depConsumes,耗时:{depConsumestimes},"); } catch (Exception ex) { Logger.Error(ex); } return cashFlowQueries; } /// /// 查询到账 /// /// public Dictionary QueryDeposit(DateTime day) { var startDate = new DateTime(day.Year, day.Month, 1).ToString("yyyy-MM-dd"); var endDate = new DateTime(day.Year, day.Month + 1, 1); var endDay = day.Date.AddDays(1).ToString("yyyy-MM-dd"); WX_SzzyOrderDeposit_BL _orderDeposit = new WX_SzzyOrderDeposit_BL(); var sql = $"SELECT paydate,payprice,channel From wx_szzyorderdeposit where paydate>='{startDate}' and paydate<'{endDay}' and auditstatus=1"; var ds = MySqlDbHelper.DataQueray(ConStringHelper.ZxdCRMConn, CommandType.Text, sql); List depositlist = ds.Tables[0].ToList(); // var depositlist = _orderDeposit.GetList(d => d.paydate >= startDate && d.paydate < endDay && d.auditstatus == 1 ); var depositGroupBy = depositlist.Select(d => new { d.paydate.Date, d.channel, d.payprice }).GroupBy(d => d.channel); List cashFlowQueriesTempGroup = new List(); foreach (var item in depositGroupBy) { var b_Audit_Items = b_Audit_Items_bl.GetAudit_ItemsFromChannel(item.Key); //b_Audit_Items.Add(new B_Audit_Items { Item = "总数据", Id = 1 }); foreach (var b_Audit_Item in b_Audit_Items) { var cashFlowQuery = new CashFlow.CashFlowQuery { // Day = day.ToString("yyyy-MM-dd"), itemId = b_Audit_Item.Id, item = b_Audit_Item.Item, payprice = item.Where(d => d.Date == day.Date).Sum(d => d.payprice), monPayprice = item.Sum(d => d.payprice) }; cashFlowQueriesTempGroup.Add(cashFlowQuery); } } Dictionary cashFlowQueries = new Dictionary(); foreach (var item in cashFlowQueriesTempGroup.GroupBy(d => new { d.itemId, d.item })) { var cashFlowQuery = new CashFlow.CashFlowQuery { itemId = item.Key.itemId, item=item.Key.item, payprice = item.Sum(d => d.payprice), monPayprice = item.Sum(d => d.monPayprice) }; cashFlowQueries.Add(cashFlowQuery.itemId, cashFlowQuery); } return cashFlowQueries; } /// /// 查询退款 /// /// public Dictionary QueryRefund(DateTime day) { var startDate = new DateTime(day.Year, day.Month, 1); var endDate = new DateTime(day.Year, day.Month + 1, 1); var endDay = day.Date.AddDays(1); WX_SzzyOrderRefund_BL _SzzyOrderRefund_BL = new WX_SzzyOrderRefund_BL(); B_Audit_Items_BL b_Audit_Items_bl = new B_Audit_Items_BL(); var depositlist = _SzzyOrderRefund_BL.GetList(d => d.auditstatus == 1 && d.isacturalrefund == 1 && d.audittime >= startDate && d.audittime < endDay); var depositGroupBy = depositlist.Select(d => new { d.audittime.Value.Date, d.channel, d.refundprice }).GroupBy(d => d.channel); List cashFlowQueriesTempGroup = new List(); foreach (var item in depositGroupBy) { var b_Audit_Items = b_Audit_Items_bl.GetAudit_ItemsFromChannel(item.Key.Value); //b_Audit_Items.Add(new B_Audit_Items { Item = "总数据", Id = 1 }); foreach (var b_Audit_Item in b_Audit_Items) { var cashFlowQuery = new CashFlow.CashFlowQuery { // Day = day.ToString("yyyy-MM-dd"), itemId = b_Audit_Item.Id, item = b_Audit_Item.Item, Refundprice = item.Where(d => d.Date == day.Date).Sum(d => d.refundprice), monRefundprice = item.Sum(d => d.refundprice), monRefundOrderNum = item.Count() }; cashFlowQueriesTempGroup.Add(cashFlowQuery); } } Dictionary cashFlowQueries = new Dictionary(); foreach (var item in cashFlowQueriesTempGroup.GroupBy(d => new { d.itemId, d.item })) { var cashFlowQuery = new CashFlow.CashFlowQuery { itemId = item.Key.itemId, item = item.Key.item, Refundprice = item.Sum(d => d.Refundprice), monRefundprice = item.Sum(d => d.monRefundprice), monRefundOrderNum = item.Sum(d=>d.monRefundOrderNum) }; cashFlowQueries.Add(cashFlowQuery.itemId,cashFlowQuery); } return cashFlowQueries; } /// /// 查询订单 /// /// public Dictionary QueryOrder(DateTime day) { var startDate = new DateTime(day.Year, day.Month, 1).ToString("yyyy-MM-dd"); var endDate = new DateTime(day.Year, day.Month + 1, 1); var endDay = day.Date.AddDays(1).ToString("yyyy-MM-dd"); WX_SZZYORDER_BL _SZZYORDER_BL = new WX_SZZYORDER_BL(); B_Audit_Items_BL b_Audit_Items_bl = new B_Audit_Items_BL(); List orderstatus=new List { "220", "205", "80", "90" }; // var depositlist = _SZZYORDER_BL.GetList(d => orderstatus.Contains(d.ORDERSTATUS) && d.ARRIVALTIME >= startDate && d.ARRIVALTIME < endDay); var sql = $"SELECT orderid,arrivaltime,channel From wx_szzyorder where ARRIVALTIME>='{startDate}' and ARRIVALTIME<'{endDay}' and orderstatus in ('220','205','80','90');"; var ds = MySqlDbHelper.DataQueray(ConStringHelper.ZxdCRMConn, CommandType.Text, sql); List sZZYORDERs = ds.Tables[0].ToList(); var sZZYORDERsGroupBy = sZZYORDERs.Select(d => new { d.orderid, d.channel, d.arrivaltime }).GroupBy(d => d.channel); List cashFlowQueriesTempGroup = new List(); foreach (var item in sZZYORDERsGroupBy) { var b_Audit_Items = b_Audit_Items_bl.GetAudit_ItemsFromChannel(item.Key); foreach (var b_Audit_Item in b_Audit_Items) { var cashFlowQuery = new CashFlow.CashFlowQuery { // Day = day.ToString("yyyy-MM-dd"), itemId = b_Audit_Item.Id, item = b_Audit_Item.Item, orderNum = item.Where(d => d.arrivaltime == day.Date).Count(), monOrderNum = item.Count(), }; cashFlowQueriesTempGroup.Add(cashFlowQuery); } } Dictionary cashFlowQueries = new Dictionary(); foreach (var item in cashFlowQueriesTempGroup.GroupBy(d => new { d.itemId, d.item })) { var cashFlowQuery = new CashFlow.CashFlowQuery { itemId = item.Key.itemId, item = item.Key.item, orderNum = item.Sum(d => d.orderNum), monOrderNum = item.Sum(d => d.monOrderNum) }; cashFlowQueries.Add(cashFlowQuery.itemId, cashFlowQuery); } return cashFlowQueries; } /// /// 查询退款 /// /// public Dictionary QuerydepConsumes(DateTime day) { var startDate = new DateTime(day.Year, day.Month, 1).ToString("yyyy-MM-dd"); var endDate = new DateTime(day.Year, day.Month + 1, 1).ToString("yyyy-MM-dd"); var endDay = day.Date.AddDays(1).ToString("yyyy-MM-dd"); WX_SZZYORDER_BL _SZZYORDER_BL = new WX_SZZYORDER_BL(); B_Audit_Items_BL b_Audit_Items_bl = new B_Audit_Items_BL(); List orderstatus = new List { "220", "205", "80", "90" }; var sql = $"SELECT dep.title,con.deptid,dep.campainid ,con.ctime,con.price From dncmspromotion.channelconsumetotaldept con LEFT JOIN `dncmsbase`.`deptment` dep on dep.id = con.deptid where con.ctime>='{startDate}' and con.ctime<'{endDay}'"; var ds = MySqlDbHelper.DataQueray(ConStringHelper.UserCenterConn, CommandType.Text, sql); List depConsumes = ds.Tables[0].ToList(); var depConsumesGroup = depConsumes.GroupBy(d => d.campainid); List cashFlowQueriesTempGroup = new List(); foreach (var item in depConsumesGroup) { var b_Audit_Items = b_Audit_Items_bl.GetAudit_ItemsFromChannel(item.Key); foreach (var b_Audit_Item in b_Audit_Items) { var cashFlowQuery = new CashFlow.CashFlowQuery { // Day = day.ToString("yyyy-MM-dd"), itemId = b_Audit_Item.Id, item = b_Audit_Item.Item, DepConsume = item.Where(d => d.ctime == day.Date).Sum(d=>d.price), monDepConsume = item.Sum(d => d.price) }; cashFlowQueriesTempGroup.Add(cashFlowQuery); } } Dictionary cashFlowQueries = new Dictionary(); foreach (var item in cashFlowQueriesTempGroup.GroupBy(d => new { d.itemId, d.item })) { var cashFlowQuery = new CashFlow.CashFlowQuery { itemId = item.Key.itemId, item = item.Key.item, DepConsume = Math.Round( item.Sum(d => d.DepConsume),2), monDepConsume = Math.Round(item.Sum(d => d.monDepConsume),2) }; cashFlowQueries.Add(cashFlowQuery.itemId, cashFlowQuery); } return cashFlowQueries; } } }