using Core.Web.App_Start; using Core.Web.WebHelper; using CRM.Core.BLL.Base; using CRM.Core.BLL.Wx; using CRM.Core.Common.WebHelper; using CRM.Core.DTO; using CRM.Core.Model; using CRM.Core.Model.Enum; using System; using System.Collections.Generic; using System.Linq; using System.Web.Mvc; using WX.CRM.Common; namespace Core.Web.Controllers { /// /// 业绩预测工具 /// public class PerformanceForecastController : BaseController { BAS_PARAMETER_BL paramter_bl = new BAS_PARAMETER_BL(); [AuthorizeRedirect(RightsConfig.CONST_业绩估算, ToolBarConfig.CONST_NotButton, true)] public ActionResult Index() { string json = paramter_bl.GetValue_Parameter(Parameter.Sys_rpt_json_config);//获得JSON配置 //ViewBag.JsonConfig1 = JsonHelper.JsonDivertToObj(json); ViewBag.JsonConfig1 = json; return View(); } [AuthorizeRedirect(RightsConfig.CONST_业绩估算, ToolBarConfig.CONST_NotButton, false)] /// /// 财务估算 /// /// public JsonResult Generate(PerformanceYCInfo modellist) { List orderlist = new List(); string erromessage = ""; retMsgNew ret = new retMsgNew() { result = true }; erromessage = "估算成功!"; try { string json = paramter_bl.GetValue_Parameter(Parameter.Sys_rpt_json_config);//获得JSON配置 JsonConfig config = JsonHelper.JsonDivertToObj(json); Wx_Test testbl = new Wx_Test(); ///============================形成订单========================= foreach (HtmlOrder m in modellist.mydata) { decimal todec = Convert.ToDecimal(m.arrarrivalpay); //DateTimeFormatInfo dtFormat = new DateTimeFormatInfo(); //dtFormat.ShortDatePattern = "yyyyMMdd"; var monthstr = m.month.ToString(); int year = Convert.ToInt32(monthstr.Substring(0, 4)); int month = Convert.ToInt32(monthstr.Substring(4, 2)); DateTime nowdate = new DateTime(year, month, 1); //DateTime nowdate = Convert.ToDateTime(m.month.ToString() + "01", dtFormat);//为首月当天 if (m.ordertype == "ql" || m.ordertype == "zz") { Order order = ReturnQLOrder(todec, nowdate, m.opendays); order.ptype = m.ordertype; order.smallType = m.key;//产品实际类型 orderlist.Add(order); } else if (m.ordertype == "tn") { Order order = ReturnTNOrder(todec, nowdate, m.opendays); order.ptype = m.ordertype; order.smallType = m.key;//产品实际类型 orderlist.Add(order); } else if (m.ordertype == "ys") { Order order = ReturnYSOrder(todec, nowdate, m.opendays); order.ptype = m.ordertype; order.smallType = m.key;//产品实际类型 orderlist.Add(order); } } ///===============================订单分割================== #region 订单拆分 List orderdetialList = new List(); string pici = DateTime.Now.ToString("yyyyMMddHHssfffff"); foreach (Order item in orderlist) { item.ordermonthINT = Convert.ToInt32(item.ordermonth.Replace("-", "")); string orderid = string.Format("{0}_{1}_{2}_days", item.ptype, item.opendays, item.ordermonth.Replace("-", ""));//开通时间 DateTime opday = item.otime;//开通时间 DateTime endday = item.endday;//结束日期 DateTime monlastday = item.monlastday;//月最后一天2020 decimal jiazhi = item.jiazhi;//平均值 DateTime curdate = new DateTime(opday.Year, opday.Month, 1);//当月第一天 DateTime lasdate = new DateTime(endday.Year, endday.Month, 1);//当月第一天 string channel = item.ordertype; string orderyear = item.orderyear;// item["orderyear"].ToString(); string ordermonth = item.ordermonth;// item["ordermonth"].ToString(); int i = 0; string smallproductname = "";//产品名称 SmallProduct product = config.product.FirstOrDefault(m => m.producttype == item.ordertype.ToUpper() & m.ntype == item.ptype & m.days == item.opendays); if (product != null) { if (product.ntype == "ql") { smallproductname = string.Format("擒龙{0}", product.name); } else if (product.ntype == "zz") { smallproductname = string.Format("至尊{0}", product.name); } else if (product.ntype == "tn") { smallproductname = string.Format("多赢{0}", product.name); } else if (product.ntype == "ys") { smallproductname = string.Format("爱赢{0}", product.name); } } while (i > -1) { OrderDetial2 model = new OrderDetial2() { orderid = orderid, pjz = jiazhi, channel = channel, opyear = orderyear, opmonth = ordermonth, otime = item.orderday, pici = pici, type = 0, ordertype = item.ptype, smallType = item.smallType, smallTypeName = smallproductname }; model.oyear = ("" + curdate.Year); model.omonth = (curdate.Year + "-" + (curdate.Month < 10 ? "0" + curdate.Month : "" + curdate.Month)); if (i == 0 && curdate == lasdate)//最后一个月 { model.odays = (endday - opday).Days;//间隔天数 i = -5; } else if (i == 0) { model.odays = (monlastday - opday).Days + 1;//间隔天数 if (curdate == lasdate)//最后一个月 { i = -5; } } else { if (curdate == lasdate)//最后一个月 { model.odays = (endday - curdate).Days;//间隔天数 i = -5; } else { model.odays = (curdate.AddMonths(1) - curdate).Days;//间隔天数 } } model.jzcount = model.odays * model.pjz; if (model.odays != 0)//如果天数为零,可以删掉 { orderdetialList.Add(model);//添加进去 testbl.InsetTest2(model); } i++; curdate = curdate.AddMonths(1);//加一个月 } if (item.ordertype == "dn")//懂牛产品需要添加当约60%的进来 { OrderDetial2 model = new OrderDetial2() { orderid = orderid, omonth = item.ordermonth, oyear = item.orderyear, pjz = jiazhi, jzcount = item.firstjine, channel = channel, odays = 0, opyear = orderyear, opmonth = ordermonth, otime = item.orderday, pici = pici, type = 1, ordertype = item.ptype, smallType = item.smallType, smallTypeName = smallproductname }; orderdetialList.Add(model); testbl.InsetTest2(model); } } #endregion //=============================此时可以统计报表了============ #region 当月金额,非当约金额,累计订单总额计算 List orderday = orderdetialList.GroupBy(m => m.omonth).Select(m => m.Key).OrderBy(m => m).ToList();//月份 List rptlistDN = new List();//懂牛的产品报表 List rptlistTN = new List();//淘牛邦的产品报表 List rptlistYS = new List();//永顺邦的产品报表 List qlType = orderdetialList.Where(m => m.ordertype == "ql").GroupBy(m => new { m.smallType, m.smallTypeName }).Select(m => new PorductGoroup { smallType = m.Key.smallType, smallTypeName = m.Key.smallTypeName }).OrderBy(m => m.smallType).ToList();//擒龙版类型列表 List zzType = orderdetialList.Where(m => m.ordertype == "zz").GroupBy(m => new { m.smallType, m.smallTypeName }).Select(m => new PorductGoroup { smallType = m.Key.smallType, smallTypeName = m.Key.smallTypeName }).OrderBy(m => m.smallType).ToList();//至尊版类型列表 List tnType = orderdetialList.Where(m => m.ordertype == "tn").GroupBy(m => new { m.smallType, m.smallTypeName }).Select(m => new PorductGoroup { smallType = m.Key.smallType, smallTypeName = m.Key.smallTypeName }).OrderBy(m => m.smallType).ToList();//淘牛版类型列表 List ysType = orderdetialList.Where(m => m.ordertype == "ys").GroupBy(m => new { m.smallType, m.smallTypeName }).Select(m => new PorductGoroup { smallType = m.Key.smallType, smallTypeName = m.Key.smallTypeName }).OrderBy(m => m.smallType).ToList();//永顺版类型列表 foreach (string item in orderday) { int nowmont = Convert.ToInt32(item.Replace("-", "")); //===========懂牛产品 outRpt rptDN = new outRpt() { month = item, monthINT = Convert.ToInt32(item.Replace("-", "")), ordertype = "ALL", smallType = "ALL", smallTypeName = "汇总" };//合计报表 rptDN.dangyueSR = orderdetialList.Where(m => m.omonth == item && m.channel == "dn" && m.omonth == m.opmonth).Sum(m => m.jzcount);//懂牛的当约收入 rptDN.noSR = orderdetialList.Where(m => m.omonth == item && m.channel == "dn" && m.omonth != m.opmonth).Sum(m => m.jzcount);//懂牛的当约 rptDN.OrderAll = orderlist.Where(m => m.ordertype == "dn" && m.ordermonthINT <= nowmont).Sum(m => m.arrarrivalpay);//回去订单总金额 if (rptDN.dangyueSR != 0 || rptDN.noSR != 0) { rptlistDN.Add(rptDN); //细化子产品 if (qlType.Count > 0) { foreach (PorductGoroup ntype in qlType) { outRpt rptDN1 = new outRpt() { month = item, monthINT = Convert.ToInt32(item.Replace("-", "")), ordertype = "dn", smallType = ntype.smallType, smallTypeName = ntype.smallTypeName };//合计报表 rptDN1.dangyueSR = orderdetialList.Where(m => m.omonth == item && m.smallType == ntype.smallType && m.omonth == m.opmonth).Sum(m => m.jzcount);//懂牛的当约收入 rptDN1.noSR = orderdetialList.Where(m => m.omonth == item && m.smallType == ntype.smallType && m.omonth != m.opmonth).Sum(m => m.jzcount);//懂牛的当约 rptDN1.OrderAll = orderlist.Where(m => m.smallType == ntype.smallType && m.ordermonthINT <= nowmont).Sum(m => m.arrarrivalpay);//回去订单总金额 rptlistDN.Add(rptDN1); } } //细化子产品 if (zzType.Count > 0) { foreach (PorductGoroup ntype in zzType) { outRpt rptDN1 = new outRpt() { month = item, monthINT = Convert.ToInt32(item.Replace("-", "")), ordertype = "dn", smallType = ntype.smallType, smallTypeName = ntype.smallTypeName };//合计报表 rptDN1.dangyueSR = orderdetialList.Where(m => m.omonth == item && m.smallType == ntype.smallType && m.omonth == m.opmonth).Sum(m => m.jzcount);//懂牛的当约收入 rptDN1.noSR = orderdetialList.Where(m => m.omonth == item && m.smallType == ntype.smallType && m.omonth != m.opmonth).Sum(m => m.jzcount);//懂牛的当约 rptDN1.OrderAll = orderlist.Where(m => m.smallType == ntype.smallType && m.ordermonthINT <= nowmont).Sum(m => m.arrarrivalpay);//回去订单总金额 rptlistDN.Add(rptDN1); } } } //============淘牛产品 outRpt rptTN = new outRpt() { month = item, monthINT = Convert.ToInt32(item.Replace("-", "")), ordertype = "ALL", smallType = "ALL", smallTypeName = "汇总" };//合计报表 rptTN.dangyueSR = orderdetialList.Where(m => m.omonth == item && m.channel == "tn" && m.omonth == m.opmonth).Sum(m => m.jzcount);//懂牛的当约收入 rptTN.noSR = orderdetialList.Where(m => m.omonth == item && m.channel == "tn" && m.omonth != m.opmonth).Sum(m => m.jzcount);//懂牛的当约 rptTN.OrderAll = orderlist.Where(m => m.ordertype == "tn" && m.ordermonthINT <= nowmont).Sum(m => m.arrarrivalpay);//回去订单总金额 if (rptTN.dangyueSR != 0 || rptTN.noSR != 0) { rptlistTN.Add(rptTN); //细化子产品 if (tnType.Count > 0) { foreach (PorductGoroup ntype in tnType) { outRpt rptDN1 = new outRpt() { month = item, monthINT = Convert.ToInt32(item.Replace("-", "")), ordertype = "dn", smallType = ntype.smallType, smallTypeName = ntype.smallTypeName };//合计报表 rptDN1.dangyueSR = orderdetialList.Where(m => m.omonth == item && m.smallType == ntype.smallType && m.omonth == m.opmonth).Sum(m => m.jzcount);//懂牛的当约收入 rptDN1.noSR = orderdetialList.Where(m => m.omonth == item && m.smallType == ntype.smallType && m.omonth != m.opmonth).Sum(m => m.jzcount);//懂牛的当约 rptDN1.OrderAll = orderlist.Where(m => m.smallType == ntype.smallType && m.ordermonthINT <= nowmont).Sum(m => m.arrarrivalpay);//回去订单总金额 rptlistTN.Add(rptDN1); } } } //===========永顺产品 outRpt rptYS = new outRpt() { month = item, monthINT = Convert.ToInt32(item.Replace("-", "")), ordertype = "ALL", smallType = "ALL", smallTypeName = "汇总" };//合计报表 rptYS.dangyueSR = orderdetialList.Where(m => m.omonth == item && m.channel == "ys" && m.omonth == m.opmonth).Sum(m => m.jzcount);//懂牛的当约收入 rptYS.noSR = orderdetialList.Where(m => m.omonth == item && m.channel == "ys" && m.omonth != m.opmonth).Sum(m => m.jzcount);//懂牛的当约 rptYS.OrderAll = orderlist.Where(m => m.ordertype == "ys" && m.ordermonthINT <= nowmont).Sum(m => m.arrarrivalpay);//回去订单总金额 if (rptYS.dangyueSR != 0 || rptYS.noSR != 0) { rptlistYS.Add(rptYS); //细化子产品 if (ysType.Count > 0) { foreach (PorductGoroup ntype in ysType) { outRpt rptDN1 = new outRpt() { month = item, monthINT = Convert.ToInt32(item.Replace("-", "")), ordertype = "dn", smallType = ntype.smallType, smallTypeName = ntype.smallTypeName };//合计报表 rptDN1.dangyueSR = orderdetialList.Where(m => m.omonth == item && m.smallType == ntype.smallType && m.omonth == m.opmonth).Sum(m => m.jzcount);//懂牛的当约收入 rptDN1.noSR = orderdetialList.Where(m => m.omonth == item && m.smallType == ntype.smallType && m.omonth != m.opmonth).Sum(m => m.jzcount);//懂牛的当约 rptDN1.OrderAll = orderlist.Where(m => m.smallType == ntype.smallType && m.ordermonthINT <= nowmont).Sum(m => m.arrarrivalpay);//回去订单总金额 rptlistYS.Add(rptDN1); } } } } #endregion #region 留存余额计算 //=========懂牛 留存余额计算 foreach (outRpt item in rptlistDN) { if (item.ordertype == "ALL")//合计 { item.SRAll = rptlistDN.Where(m => m.monthINT <= item.monthINT && m.smallType == "ALL").Sum(m => m.dangyueSR + m.noSR); item.YE = item.OrderAll - item.SRAll;//留存余额 } else//详细产品 { item.SRAll = rptlistDN.Where(m => m.monthINT <= item.monthINT && m.smallType == item.smallType).Sum(m => m.dangyueSR + m.noSR); item.YE = item.OrderAll - item.SRAll;//留存余额 } } //=========淘牛留存余额计算 foreach (outRpt item in rptlistTN) { if (item.ordertype == "ALL")//合计 { item.SRAll = rptlistTN.Where(m => m.monthINT <= item.monthINT && m.smallType == "ALL").Sum(m => m.dangyueSR + m.noSR); item.YE = item.OrderAll - item.SRAll;//留存余额 } else//详细产品 { item.SRAll = rptlistTN.Where(m => m.monthINT <= item.monthINT && m.smallType == item.smallType).Sum(m => m.dangyueSR + m.noSR); item.YE = item.OrderAll - item.SRAll;//留存余额 } } //=========永顺 留存余额计算 foreach (outRpt item in rptlistYS) { //item.SRAll = rptlistYS.Where(m => m.monthINT <= item.monthINT).Sum(m => m.dangyueSR + m.noSR); //item.YE = item.OrderAll - item.SRAll;//留存余额 if (item.ordertype == "ALL")//合计 { item.SRAll = rptlistYS.Where(m => m.monthINT <= item.monthINT && m.smallType == "ALL").Sum(m => m.dangyueSR + m.noSR); item.YE = item.OrderAll - item.SRAll;//留存余额 } else//详细产品 { item.SRAll = rptlistYS.Where(m => m.monthINT <= item.monthINT && m.smallType == item.smallType).Sum(m => m.dangyueSR + m.noSR); item.YE = item.OrderAll - item.SRAll;//留存余额 } } #endregion #region 转换成字符串发到前端 outStr jsonStr = new outStr(); List rptStrlistDN = new List(); List rptStrlistTN = new List(); List rptStrlistYS = new List(); //转换成 字符串 懂牛 foreach (outRpt item in rptlistDN) { rptStrlistDN.Add(new outRptStr() { month = item.month, ordertype = item.ordertype, smallType = item.smallType, smallTypeName = item.smallTypeName, dangyueSR = item.dangyueSR.ToString("#0.00"), noSR = item.noSR.ToString("#0.00"), OrderAll = item.OrderAll.ToString("#0.00"), SRAll = item.SRAll.ToString("#0.00"), YE = item.YE.ToString("#0.00"), monthINT = item.monthINT }); } //转换成 字符串 淘牛 foreach (outRpt item in rptlistTN) { rptStrlistTN.Add(new outRptStr() { month = item.month, ordertype = item.ordertype, smallType = item.smallType, smallTypeName = item.smallTypeName, dangyueSR = item.dangyueSR.ToString("#0.00"), noSR = item.noSR.ToString("#0.00"), OrderAll = item.OrderAll.ToString("#0.00"), SRAll = item.SRAll.ToString("#0.00"), YE = item.YE.ToString("#0.00"), monthINT = item.monthINT }); } //转换成 字符串 永顺 foreach (outRpt item in rptlistYS) { rptStrlistYS.Add(new outRptStr() { month = item.month, ordertype = item.ordertype, smallType = item.smallType, smallTypeName = item.smallTypeName, dangyueSR = item.dangyueSR.ToString("#0.00"), noSR = item.noSR.ToString("#0.00"), OrderAll = item.OrderAll.ToString("#0.00"), SRAll = item.SRAll.ToString("#0.00"), YE = item.YE.ToString("#0.00"), monthINT = item.monthINT }); } #endregion jsonStr.dnList = rptStrlistDN; jsonStr.tnList = rptStrlistTN; jsonStr.ysList = rptStrlistYS; ret.data = jsonStr; } catch (Exception exa) { ret.result = false; erromessage += "出现系统错误"; LogHelper.Error(exa); } ret.retmsg = erromessage; return Json(ret, JsonRequestBehavior.AllowGet); } #region 创建订单 /// /// 返回懂牛擒龙和至尊版本订单 /// /// public Order ReturnQLOrder(decimal todec, DateTime nowdate, int orderdays) { Order order = new Order { arrarrivalpay = todec, bfzss = todec * Convert.ToDecimal(0.4), firstjine = todec * Convert.ToDecimal(0.6), opendays = orderdays, endday = nowdate.AddDays(orderdays), monlastday = nowdate.AddMonths(1).AddDays(-1), orderday = nowdate.ToString("yyyy-MM-dd"), ordermonth = nowdate.ToString("yyyy-MM"), orderyear = nowdate.ToString("yyyy"), ordertype = "dn", otime = nowdate }; order.jiazhi = order.bfzss / order.opendays; return order; } /// /// 返回淘牛版订单 /// /// public Order ReturnTNOrder(decimal todec, DateTime nowdate, int orderdays) { Order order = new Order { arrarrivalpay = todec, bfzss = todec * Convert.ToDecimal(1.0), firstjine = todec * Convert.ToDecimal(0.0),//套牛百分之一百分摊 opendays = orderdays, endday = nowdate.AddDays(orderdays), monlastday = nowdate.AddMonths(1).AddDays(-1), orderday = nowdate.ToString("yyyy-MM-dd"), ordermonth = nowdate.ToString("yyyy-MM"), ordertype = "tn", otime = nowdate }; order.jiazhi = order.bfzss / order.opendays; return order; } /// /// 返回永顺订单 /// /// public Order ReturnYSOrder(decimal todec, DateTime nowdate, int orderdays) { Order order = new Order { arrarrivalpay = todec, bfzss = todec * Convert.ToDecimal(1.0), firstjine = todec * Convert.ToDecimal(0.0),//套牛百分之一百分摊 opendays = orderdays, endday = nowdate.AddDays(orderdays), monlastday = nowdate.AddMonths(1).AddDays(-1), orderday = nowdate.ToString("yyyy-MM-dd"), ordermonth = nowdate.ToString("yyyy-MM"), ordertype = "ys", otime = nowdate }; order.jiazhi = order.bfzss / order.opendays; return order; } } #endregion public class PorductGoroup { public string smallType { get; set; } public string smallTypeName { get; set; } } public class PerformanceYCInfo { public List mydata { get; set; } } //报表 表 public class outRpt { public string month { get; set; } public int monthINT { get; set; } public string ordertype { get; set; } /// /// 喜欢产品分类 例如:ql_90_days /// public string smallType { get; set; } /// /// 喜欢产品分类名称 例如:擒龙季度版 /// public string smallTypeName { get; set; } /// /// 当月收入 /// public decimal dangyueSR { get; set; } /// /// 非当月收入 /// public decimal noSR { get; set; } /// /// 累计留存金额 /// public decimal YE { get; set; } /// /// 当约之前订单总额 /// public decimal OrderAll { get; set; } /// /// 当约之前累计收入 /// public decimal SRAll { get; set; } } public class outStr { /// /// 懂牛列表 /// public List dnList { get; set; } /// /// 淘牛列表 /// public List tnList { get; set; } /// /// 永顺列表 /// public List ysList { get; set; } } //报表 字符串表 public class outRptStr { public string month { get; set; } public int monthINT { get; set; } /// /// 当月收入 /// public string dangyueSR { get; set; } /// /// 非当月收入 /// public string noSR { get; set; } /// /// 累计留存金额 /// public string YE { get; set; } /// /// 当约之前订单总额 /// public string OrderAll { get; set; } /// /// 当约之前累计收入 /// public string SRAll { get; set; } public string ordertype { get; set; } /// /// 喜欢产品分类 例如:ql_90_days /// public string smallType { get; set; } /// /// 喜欢产品分类名称 例如:擒龙季度版 /// public string smallTypeName { get; set; } } /// /// 传上来的订单 /// public class HtmlOrder { //订单类型 例:ql public string ordertype { get; set; } //订单天数 例:30 public int opendays { get; set; } //月份 例202003 public int month { get; set; } //金额 例12000 public string arrarrivalpay { get; set; } /// /// 产品 例:ql_90_days /// public string key { get; set; } } public class Order { /// /// 开通时间 /// public DateTime otime { get; set; } /// /// 订单日子(只取第一天) 例:20120-03-01 /// public string orderday { get; set; } /// /// 订单月份 例:20120-03 /// public string ordermonth { get; set; } public int ordermonthINT { get; set; } /// /// 订单年份 例 :2020 /// public string orderyear { get; set; } /// /// 金额 /// public decimal arrarrivalpay { get; set; } /// /// 订单天输 /// public int opendays { get; set; } /// /// 每天价值价值金额 /// public decimal jiazhi { get; set; } /// /// 首月金额 /// public decimal firstjine { get; set; } /// /// 百分之40 /// public decimal bfzss { get; set; } /// /// 当月最后一天 /// public DateTime monlastday { get; set; } /// /// 到期时间 /// public DateTime endday { get; set; } /// /// dn:懂牛 tn:套牛 ys:永顺 /// public string ordertype { get; set; } /// /// 产品小类型 zz:至尊 ql:擒龙版本 tn:淘牛版本 ys:永顺版本 /// public string ptype { get; set; } /// /// 喜欢产品分类 例如:ql_90_days /// public string smallType { get; set; } } public class JsonConfig { public List module { get; set; } public List product { get; set; } } /// /// 大产品配置 /// public class ProductModule { /// /// "DN" /// public string producttype { get; set; } /// /// ": "智赢系列", /// public string name { get; set; } /// /// ": "懂牛产品60%算当月40%每天平分", /// public string memo { get; set; } /// /// : 0.6, /// public decimal dangyuebi { get; set; } /// /// ": 0.4 /// public decimal qitabi { get; set; } } public class SmallProduct { /// /// 一月版" /// public string name { get; set; } /// /// 30 /// public int days { get; set; } /// /// DN /// public string producttype { get; set; } /// /// ql /// public string ntype { get; set; } } }