using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using WX.CRM.BLL.Application.UserComBox; using WX.CRM.BLL.Util; using WX.CRM.Common; using WX.CRM.IBLL.Wx; using WX.CRM.Model.DTO; using WX.CRM.Model.MAP; namespace WX.CRM.BLL.Wx { public class WX_COMMISSION_BL : IWX_COMMISSION { DAL.Wx.WX_COMMISSION_DAL _dal = new DAL.Wx.WX_COMMISSION_DAL(); private readonly CACHE_BL _cache = new CACHE_BL(); private readonly WX_SZZYMIDPRODUCT_BL _midProduct = new WX_SZZYMIDPRODUCT_BL(); public IEnumerable GetSaleUserReport(DateTime? sTime, DateTime? eTime, QueryUserComboDto usercomboDto) { var data = new List(); var ds = _dal.GetSaleUserCommission(usercomboDto, sTime, eTime); var dt1 = ds.Tables[0].ToList(); var dt2 = ds.Tables[1].ToList(); var dt3 = ds.Tables[2].ToList(); dt1 = dt1.Where(p => p.isdismiss == 0 || p.AllOrderCount > 0).ToList(); var midProductList = _midProduct.GetList(); foreach (var item in dt1) { var info = new UserOrderView { Eid = item.eid, UName = item.uname, IsDismiss = item.isdismiss == 1 ? "离职" : "在职", GName = item.gname, NewOrderCount = item.NewOrderCount, NewOrderPay = item.NewOrderPay, RenewOrderCount = item.RenewOrderCount, RenewOrderPay = item.RenewOrderPay, UpOrderCount = item.UpOrderCount, UpOrderPay = item.UpOrderPay, AllOrderCount = item.AllOrderCount, AllOrderPay = item.AllOrderPay, RefundOrderCount = dt3.Where(p => p.inneruserid == item.pkid).Sum(p => p.RefundOrderCount), RefundPrice = dt3.Where(p => p.inneruserid == item.pkid).Sum(p => p.RefundPrice), }; info.JsOrderCount = info.AllOrderCount - info.RefundOrderCount; info.JsPrice = info.AllOrderPay - info.RefundPrice; var dic = new Dictionary(); foreach (var product1 in midProductList) { var dicCount = dt2.Where(p => p.inneruserid == item.pkid && p.midproductid == product1.MIDPRODUCTID).Select(p => p.ProductOrderCount).DefaultIfEmpty(0).Sum(); var dicPrice = dt2.Where(p => p.inneruserid == item.pkid && p.midproductid == product1.MIDPRODUCTID).Select(p => p.ProductArrivalPay).DefaultIfEmpty(0).Sum(); dic.Add(product1.MIDPRODUCTNAME + "数量", dicCount); dic.Add(product1.MIDPRODUCTNAME + "金额", dicPrice); } info.OrderProductList = dic; data.Add(info); } var users = _cache.GetUserList(); var userGroup = _cache.GetList_InnerUserGroup(); var groupList = _cache.GetGroupList(); if (usercomboDto.userId.HasValue) { var user = users.FirstOrDefault(p => p.PKID == usercomboDto.userId); if (user != null) { data = data.Where(m => m.Eid == user.EID).ToList(); } } else if (!string.IsNullOrEmpty(usercomboDto.groupIds)) { var _groupids = OperationUtil.ConvertToDecimal(usercomboDto.groupIds.Split(',')); data = (from a in data join u in users on a.Eid equals u.EID join b in userGroup on u.PKID equals b.INNERUSERID where b.GID != null && _groupids.Contains(b.GID.Value) select a).ToList(); } else if (!string.IsNullOrEmpty(usercomboDto.deptId)) { UserServices userservices = new UserServices(); List ALLdeptIDS = new List(); List ALLgidS = new List(); userservices.GetAllDeptIDOrGidByDeptStr(usercomboDto.deptId, ref ALLdeptIDS, ref ALLgidS);//获取所有的组别和gid data = (from d in data join u in users on d.Eid equals u.EID join ug in userGroup on u.PKID equals ug.INNERUSERID where ALLdeptIDS.Contains(ug.DEPTID.Value) || (ug.GID!= null && ALLgidS.Contains(ug.GID.Value)) select d).ToList(); } return data.OrderByDescending(p => p.AllOrderPay).ThenByDescending(p => p.AllOrderCount); } public DataSet GetSaleUserMonthReport(string groupId, string inneruserId, decimal eId, decimal isDismiss) { var ds = _dal.GetSaleUserMonthCommission(groupId, inneruserId, eId, isDismiss); return ds; } public IEnumerable GetSaleGroupReport(QueryUserComboDto usercomboDto, DateTime? sTime, DateTime? eTime) { var ds = _dal.GetSaleGroupCommission(usercomboDto, sTime, eTime); var data = new List(); var dt1 = ds.Tables[0].ToList(); var dt2 = ds.Tables[1].ToList(); var dt3 = ds.Tables[2].ToList(); var dt4 = ds.Tables[3].ToList(); //dt1 = dt1.Where(p => p.AllOrderPay > 0).ToList(); var midProductList = _midProduct.GetList(); foreach (var item in dt1) { var info = new GroupOrderView { GId = item.gid, GName = item.gname, NewOrderCount = item.NewOrderCount, NewOrderPay = item.NewOrderPay, RenewOrderCount = item.RenewOrderCount, RenewOrderPay = item.RenewOrderPay, UpOrderCount = item.UpOrderCount, UpOrderPay = item.UpOrderPay, AllOrderCount = item.AllOrderCount, AllOrderPay = item.AllOrderPay, RefundOrderCount = dt3.Where(p => p.gid == item.gid).Sum(p => p.RefundOrderCount), RefundPrice = dt3.Where(p => p.gid == item.gid).Sum(p => p.RefundPrice), UserOrderCount = dt4.Where(p => p.gid == item.gid).Sum(p => p.UserOrderCount), UserOrderPrice = dt4.Where(p => p.gid == item.gid).Sum(p => p.UserPerformanceAmount), }; info.JsOrderCount = info.AllOrderCount - info.RefundOrderCount - info.UserOrderCount; info.JsPrice = info.AllOrderPay - info.RefundPrice - info.UserOrderPrice; var dic = new Dictionary(); foreach (var product1 in midProductList) { var dicCount = dt2.Where(p => p.gid == item.gid && p.midproductid == product1.MIDPRODUCTID).Select(p => p.ProductOrderCount).DefaultIfEmpty(0).Sum(); var dicPrice = dt2.Where(p => p.gid == item.gid && p.midproductid == product1.MIDPRODUCTID).Select(p => p.ProductArrivalPay).DefaultIfEmpty(0).Sum(); dic.Add(product1.MIDPRODUCTNAME + "数量", dicCount); dic.Add(product1.MIDPRODUCTNAME + "金额", dicPrice); } info.OrderProductList = dic; data.Add(info); } //var users = _cache.GetUserList(); //var userGroup = _cache.GetList_InnerUserGroup(); var groupList = _cache.GetGroupList(); if (!string.IsNullOrEmpty(usercomboDto.groupIds)) { var _groupids = OperationUtil.ConvertToDecimal(usercomboDto.groupIds.Split(',')); data = data.Where(p => _groupids.Contains(p.GId)).ToList(); } else if (!string.IsNullOrEmpty(usercomboDto.deptId)) { UserServices userservices = new UserServices(); List ALLdeptIDS = new List(); List ALLgidS = new List(); userservices.GetAllDeptIDOrGidByDeptStr(usercomboDto.deptId,ref ALLdeptIDS,ref ALLgidS);//获取所有的组别和gid data = (from a in data join g in groupList on a.GId equals g.GID where ALLdeptIDS.Contains(g.SALEDEPTID.Value) || ALLgidS.Contains(a.GId) select a).ToList(); } return data.OrderByDescending(p => p.AllOrderPay).ThenByDescending(p => p.AllOrderCount); } public DataSet GetUserCommissionDetail(DateTime sTime, DateTime eTime, decimal inneruserId) { return _dal.GetUserCommissionDetail(inneruserId, sTime, eTime); } public IEnumerable GetUserNewsOrderReport(QueryUserComboDto usercomboDto, DateTime? sTime, DateTime? eTime) { var data = new List(); var ds = _dal.GetUserNewsOrderReport(usercomboDto, sTime, eTime); var dt1 = ds.Tables[0].ToList(); var dt2 = ds.Tables[1].ToList(); var newsOrderProduct = dt1.OrderBy(p => p.rightperiod).Select(p => p.rightperiod).Distinct(); //var refundOrderProduct = dt2.OrderBy(p => p.rightperiod).Select(p => p.subproductname).Distinct(); //LogHelper.Info(newsOrderProduct.ToJson()); dt1 = dt1.Where(p => p.isdismiss == 0 || p.ordercount > 0 || dt2.Select(c => c.eid).Contains(p.eid)).ToList(); var dt1G = dt1.GroupBy(p => new { p.eid, p.uname, p.isdismiss, p.gname }); foreach (var item1 in dt1G) { var info = new UserNewsOrderView { Eid = item1.Key.eid, UName = item1.Key.uname, IsDismiss = item1.Key.isdismiss, GName = item1.Key.gname, AllOrderCount = dt1.Where(p => p.eid == item1.Key.eid).Sum(p => p.ordercount), AllOrderCount2 = dt1.Where(p => p.eid == item1.Key.eid).Sum(p => p.ordercount2), AllOrderPrice = dt1.Where(p => p.eid == item1.Key.eid).Sum(p => p.arrivalpay), RefundOrderCount = dt2.Where(p => p.eid == item1.Key.eid).Sum(p => p.ordercount), RefundOrderCount2 = dt2.Where(p => p.eid == item1.Key.eid).Sum(p => p.ordercount2) }; var dic1 = new Dictionary>(); var dic11 = new Dictionary(); foreach (var product1 in newsOrderProduct) { if (product1 > 0) { var productName = product1.ToString(); //if (product1 == 3800) //{ // productName = "3800"; //} //else if (product1 == 5800) //{ // productName = "5800"; //} //else if (product1 == 9800) //{ // productName = "9800"; //} var dic11Count = dt1.Where(p => p.rightperiod == product1 && p.eid == info.Eid).Select(p => p.ordercount).DefaultIfEmpty(0).Sum(); dic11.Add(productName, dic11Count); } } dic1.Add(info.Eid, dic11); info.OrderProductCount = dic1; //var dic2 = new Dictionary>(); //var dic22 = new Dictionary(); //foreach (var product2 in refundOrderProduct) //{ // if (!string.IsNullOrEmpty(product2)) // { // var dic22Count = dt2.Where(p => p.subproductname == product2 && p.eid == info.Eid).Select(p => p.ordercount).DefaultIfEmpty(0).Sum(); // dic22.Add(product2, dic22Count); // } //} //dic2.Add(info.Eid, dic22); //info.RefundOrderProductCount = dic2; data.Add(info); } foreach (var item in data) { item.Ranking = data.Where(p => p.AllOrderCount2 > item.AllOrderCount2).Count() + 1; } var users = _cache.GetUserList(); var userGroup = _cache.GetList_InnerUserGroup(); var groupList = _cache.GetGroupList(); if (usercomboDto.userId.HasValue) { var user = users.FirstOrDefault(p => p.PKID == usercomboDto.userId); if (user != null) { data = data.Where(m => m.Eid == user.EID).ToList(); } } else if (!string.IsNullOrEmpty(usercomboDto.groupIds)) { var _groupids = OperationUtil.ConvertToDecimal(usercomboDto.groupIds.Split(',')); data = (from a in data join u in users on a.Eid equals u.EID join b in userGroup on u.PKID equals b.INNERUSERID where _groupids.Contains(b.GID.Value) select a).ToList(); } else if (!string.IsNullOrEmpty(usercomboDto.deptId)) { var depts = OperationUtil.ConvertToDecimal(usercomboDto.deptId.Split(',')); data = (from d in data join u in users on d.Eid equals u.EID join ug in userGroup on u.PKID equals ug.INNERUSERID join g in groupList on ug.GID equals g.GID where depts.Contains(g.SALEDEPTID.Value) select d).ToList(); } return data.OrderByDescending(p => p.AllOrderCount2).ThenByDescending(p => p.AllOrderPrice); } public IEnumerable GetGroupNewsOrderReport(QueryUserComboDto usercomboDto, DateTime? sTime, DateTime? eTime) { var data = new List(); var ds = _dal.GetGroupNewsOrderReport(usercomboDto, sTime, eTime); var dt1 = ds.Tables[0].ToList(); var dt2 = ds.Tables[1].ToList(); var newsOrderProduct = dt1.OrderBy(p => p.rightperiod).Select(p => p.rightperiod).Distinct(); //var refundOrderProduct = dt2.OrderBy(p => p.rightperiod).Select(p => p.subproductname).Distinct(); var businessDept = _cache.GetValue_Parameter("BusinessDept"); dt1 = dt1.Where(p => p.ordercount > 0 || dt2.Select(c => c.gid).Contains(p.gid)).ToList(); //if (string.IsNullOrEmpty(businessDept)) //{ // dt1 = dt1.Where(p => p.ordercount > 0 || dt2.Select(c => c.gid).Contains(p.gid)).ToList(); //} //else //{ // var businessArr = businessDept.Split(new char[1] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(x => Convert.ToDecimal(x)).ToArray(); // dt1 = dt1.Where(p => businessArr.Contains(p.gid)).ToList(); //} var dt1G = dt1.GroupBy(p => new { p.gid, p.gname }); foreach (var item1 in dt1G) { var info = new GroupNewsOrderView() { Gid = item1.Key.gid, GName = item1.Key.gname, AllOrderCount = dt1.Where(p => p.gid == item1.Key.gid).Sum(p => p.ordercount), AllOrderCount2 = dt1.Where(p => p.gid == item1.Key.gid).Sum(p => p.ordercount2), AllOrderPrice = dt1.Where(p => p.gid == item1.Key.gid).Sum(p => p.arrivalpay), RefundOrderCount = dt2.Where(p => p.gid == item1.Key.gid).Sum(p => p.ordercount), RefundOrderCount2 = dt2.Where(p => p.gid == item1.Key.gid).Sum(p => p.ordercount2) }; var dic1 = new Dictionary>(); var dic11 = new Dictionary(); foreach (var product1 in newsOrderProduct) { if (product1 > 0) { var productName = product1.ToString(); //if (product1 == 3800) //{ // productName = "3800"; //} //else if (product1 == 5800) //{ // productName = "5800"; //} //else if (product1 == 9800) //{ // productName = "9800"; //} var dic11Count = dt1.Where(p => p.rightperiod == product1 && p.gid == info.Gid).Select(p => p.ordercount).DefaultIfEmpty(0).Sum(); dic11.Add(productName, dic11Count); } } dic1.Add(info.Gid, dic11); info.OrderProductCount = dic1; //var dic2 = new Dictionary>(); //var dic22 = new Dictionary(); //foreach (var product2 in refundOrderProduct) //{ // if (!string.IsNullOrEmpty(product2)) // { // var dic22Count = dt2.Where(p => p.subproductname == product2 && p.gid == info.Gid).Select(p => p.ordercount).DefaultIfEmpty(0).Sum(); // dic22.Add(product2, dic22Count); // } //} //dic2.Add(info.Gid, dic22); //info.RefundOrderProductCount = dic2; data.Add(info); } return data.OrderByDescending(p => p.AllOrderCount2).ThenByDescending(p => p.AllOrderPrice); } } public class UserNewsOrder { public decimal eid { get; set; } public string uname { get; set; } public decimal isdismiss { get; set; } public string gname { get; set; } public decimal ordercount { get; set; } public decimal arrivalpay { get; set; } public decimal subproductid { get; set; } public string subproductname { get; set; } public decimal rightperiod { get; set; } public decimal ordercount2 { get; set; } } public class UserRefundOrder { public decimal eid { get; set; } public string uname { get; set; } public decimal ordercount { get; set; } public decimal subproductid { get; set; } public string subproductname { get; set; } public decimal refundprice { get; set; } public decimal rightperiod { get; set; } public decimal ordercount2 { get; set; } } public class GroupNewsOrder { public decimal gid { get; set; } public string gname { get; set; } public decimal ordercount { get; set; } public decimal arrivalpay { get; set; } public decimal subproductid { get; set; } public string subproductname { get; set; } public decimal rightperiod { get; set; } public decimal ordercount2 { get; set; } } public class GroupRefundOrder { public decimal gid { get; set; } public string gname { get; set; } public decimal ordercount { get; set; } public decimal arrivalpay { get; set; } public decimal subproductid { get; set; } public string subproductname { get; set; } public decimal rightperiod { get; set; } public decimal ordercount2 { get; set; } } public class UserOrderCommissionDto { public decimal pkid { get; set; } public decimal eid { get; set; } public string uname { get; set; } public decimal isdismiss { get; set; } public string gname { get; set; } public decimal NewOrderPay { get; set; } public decimal NewOrderCount { get; set; } public decimal RenewOrderPay { get; set; } public decimal RenewOrderCount { get; set; } public decimal UpOrderPay { get; set; } public decimal UpOrderCount { get; set; } public decimal AllOrderPay { get; set; } public decimal AllOrderCount { get; set; } } public class UserProductOrderCommissionDto { public decimal inneruserid { get; set; } public decimal ProductOrderCount { get; set; } public decimal ProductArrivalPay { get; set; } public decimal midproductid { get; set; } } public class UserOrderRefundDto { public decimal inneruserid { get; set; } public decimal RefundOrderCount { get; set; } public decimal RefundPrice { get; set; } } public class GroupOrderCommissionDto { public decimal gid { get; set; } public string gname { get; set; } public decimal NewOrderPay { get; set; } public decimal NewOrderCount { get; set; } public decimal RenewOrderPay { get; set; } public decimal RenewOrderCount { get; set; } public decimal UpOrderPay { get; set; } public decimal UpOrderCount { get; set; } public decimal AllOrderPay { get; set; } public decimal AllOrderCount { get; set; } } public class GroupProductOrderCommissionDto { public decimal gid { get; set; } public decimal ProductOrderCount { get; set; } public decimal ProductArrivalPay { get; set; } public decimal midproductid { get; set; } } public class GroupOrderRefundDto { public decimal gid { get; set; } public decimal RefundOrderCount { get; set; } public decimal RefundPrice { get; set; } } public class GrouperOrderCommissionDto { public decimal gid { get; set; } public decimal UserPerformanceAmount { get; set; } public decimal UserOrderCount { get; set; } } }