using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using WX.CRM.Common;
using WX.CRM.IBLL.Wx;
using WX.CRM.WebHelper;
namespace WX.CRM.WEB.Controllers.WeiXin
{
public class OrderResourceReportController : BaseController
{
private readonly IWX_WORKACCOUNT _wxWorkaccount;
public OrderResourceReportController(IWX_WORKACCOUNT wxWorkaccount)
{
_wxWorkaccount = wxWorkaccount;
}
[HttpGet]
[AuthorizeRedirect(Roles = InitRights.CONST_微信推广资源统计)]
public ActionResult Index()
{
Table tab = new Table("tablist");
tab.AddHiddenHeadCol("sourcetag", "资源标签");
tab.AddHeadCol("", "35px", " ");
tab.AddHeadCol("sourcetagName", "", "资源名称");
tab.AddHeadCol("totalCount", "", "订单数量");
tab.AddHeadCol("totalCountRate", "", "订单数量比例");
tab.AddHeadCol("totalArrivalpay", "", "订单业绩");
tab.AddHeadCol("totalArrivalpayRate", "", "订单业绩比例");
tab.AddHeadCol("firstCount", "", "新开单数量");
tab.AddHeadCol("firstCountRate", "", "新开单数量比例");
tab.AddHeadCol("firstArrivalpay", "", "新开单业绩");
tab.AddHeadCol("firstArrivalpayRate", "", "新开单业绩比例");
tab.AddHeadCol("moreCount", "", "升级订单数量");
tab.AddHeadCol("moreCountRate", "", "升级订单数量比例");
tab.AddHeadCol("moreArrivalpay", "", "升级订单业绩");
tab.AddHeadCol("moreArrivalpayRate", "", "升级订单业绩比例");
tab.AddHeadRow();
ViewBag.RptList = tab.GetHead();
//ViewBag.inneruserid = UserId;
//ViewBag.userGroupId = userGroupId;
//ViewBag.saleDeptId = saleDeptId;
//ViewBag.roleCodes = DataCacheHelper.GetCache().Get_RoleCodes(userRoleId);
return View();
}
[HttpPost]
[AuthorizeRedirect(Roles = InitRights.CONST_微信推广资源统计)]
public JsonResult Index(string month, string columns)
{
try
{
if (string.IsNullOrEmpty(month))
return JsonHandler.ManageMessage("参数错误,请确认!", false);
DateTime m1;
if (!DateTime.TryParse(month, out m1))
{
return JsonHandler.ManageMessage("参数错误,请确认!", false);
}
Table table = new Table(columns, true);
var m2 = m1.AddMonths(1);
var list = _wxWorkaccount.GetOrderResourceReport(m1, m2);
var tags = from q in list group q by new { q.sourcetag, q.sourcetagName } into g select new { g.Key.sourcetag, g.Key.sourcetagName };
var style1 = "text-align:left;padding-left:10px;";
var style2 = "text-align:left;padding-left:30px;";
var style3 = "text-align:left;padding-left:10px;color:red;font-weight:bold";
var allCount = list.Sum(p => p.totalCount);
var allArrivalpay = list.Sum(p => p.totalArrivalpay);
foreach (var tag in tags)
{
var tagList = list.Where(p => p.sourcetag == tag.sourcetag);
table.AddHiddenCol(tag.sourcetag);
table.AddCol(" ");
table.AddCol(style1, "", tag.sourcetagName);
table.AddCol(style1, "btr", "", tagList.Sum(p => p.totalCount));
table.AddCol(style1, "btr", "", string.Format("{0:p}", tagList.Sum(p => p.totalCount) / allCount));
table.AddCol(style1, "btr", "", tagList.Sum(p => p.totalArrivalpay));
table.AddCol(style1, "btr", "", string.Format("{0:p}", tagList.Sum(p => p.totalArrivalpay) / allArrivalpay));
table.AddCol(style1, "btr2", "", tagList.Sum(p => p.firstCount));
table.AddCol(style1, "btr2", "", string.Format("{0:p}", tagList.Sum(p => p.firstCount) / allCount));
table.AddCol(style1, "btr2", "", tagList.Sum(p => p.firstArrivalpay));
table.AddCol(style1, "btr2", "", string.Format("{0:p}", tagList.Sum(p => p.firstArrivalpay) / allArrivalpay));
table.AddCol(style1, "", tagList.Sum(p => p.moreCount));
table.AddCol(style1, "", string.Format("{0:p}", tagList.Sum(p => p.moreCount) / allCount));
table.AddCol(style1, "", tagList.Sum(p => p.moreArrivalpay));
table.AddCol(style1, "", string.Format("{0:p}", tagList.Sum(p => p.moreArrivalpay) / allArrivalpay));
table.AddRow();
foreach (var item in tagList)
{
table.AddHiddenCol(item.sourcetag);
table.AddCol("");
table.AddCol(style2, "", item.sourcemonth.ToString("yyyy-MM"));
table.AddCol(style2, "btr", "", item.totalCount);
table.AddCol(style2, "btr", "", string.Format("{0:p}", item.totalCount / allCount));
table.AddCol(style2, "btr", "", item.totalArrivalpay);
table.AddCol(style2, "btr", "", string.Format("{0:p}", item.totalArrivalpay / allArrivalpay));
table.AddCol(style2, "btr2", "", item.firstCount);
table.AddCol(style2, "btr2", "", string.Format("{0:p}", item.firstCount / allCount));
table.AddCol(style2, "btr2", "", item.firstArrivalpay);
table.AddCol(style2, "btr2", "", string.Format("{0:p}", item.firstArrivalpay / allArrivalpay));
table.AddCol(style2, "", item.moreCount);
table.AddCol(style2, "", string.Format("{0:p}", item.moreCount / allCount));
table.AddCol(style2, "", item.moreArrivalpay);
table.AddCol(style2, "", string.Format("{0:p}", item.moreArrivalpay / allArrivalpay));
table.AddRow("style='display:none' typeName='" + tag.sourcetag + "'");
}
}
if (list.Count > 0)
{
table.AddHiddenCol("0");
table.AddCol(" ");
table.AddCol(style3, "", "全部");
table.AddCol("", "btr", "", allCount);
table.AddCol("", "btr", "", string.Format("{0:p}", allCount / allCount));
table.AddCol("", "btr", "", allArrivalpay);
table.AddCol("", "btr", "", string.Format("{0:p}", allArrivalpay / allArrivalpay));
table.AddCol("", "btr2", "", list.Sum(p => p.firstCount));
table.AddCol("", "btr2", "", string.Format("{0:p}", list.Sum(p => p.firstCount) / allCount));
table.AddCol("", "btr2", "", list.Sum(p => p.firstArrivalpay));
table.AddCol("", "btr2", "", string.Format("{0:p}", list.Sum(p => p.firstArrivalpay) / allArrivalpay));
table.AddCol(list.Sum(p => p.moreCount));
table.AddCol(string.Format("{0:p}", list.Sum(p => p.moreCount) / allCount));
table.AddCol(list.Sum(p => p.moreArrivalpay));
table.AddCol(string.Format("{0:p}", list.Sum(p => p.moreArrivalpay) / allArrivalpay));
table.AddFootRow();
//var monthList = list.OrderBy(p => p.sourcemonth);
var monthList = (from q in list group q by q.sourcemonth into g select g).OrderBy(p => p.Key);
foreach (var m in monthList)
{
var item = list.Where(p => p.sourcemonth == m.Key);
table.AddHiddenCol("0");
table.AddCol("");
table.AddCol(style2, "", m.Key.ToString("yyyy-MM"));
table.AddCol(style2, "btr", "", item.Sum(p => p.totalCount));
table.AddCol(style2, "btr", "", string.Format("{0:p}", item.Sum(p => p.totalCount) / allCount));
table.AddCol(style2, "btr", "", item.Sum(p => p.totalArrivalpay));
table.AddCol(style2, "btr", "", string.Format("{0:p}", item.Sum(p => p.totalArrivalpay) / allArrivalpay));
table.AddCol(style2, "btr2", "", item.Sum(p => p.firstCount));
table.AddCol(style2, "btr2", "", string.Format("{0:p}", item.Sum(p => p.firstCount) / allCount));
table.AddCol(style2, "btr2", "", item.Sum(p => p.firstArrivalpay));
table.AddCol(style2, "btr2", "", string.Format("{0:p}", item.Sum(p => p.firstArrivalpay) / allArrivalpay));
table.AddCol(style2, "", item.Sum(p => p.moreCount));
table.AddCol(style2, "", string.Format("{0:p}", item.Sum(p => p.moreCount) / allCount));
table.AddCol(style2, "", item.Sum(p => p.moreArrivalpay));
table.AddCol(style2, "", string.Format("{0:p}", item.Sum(p => p.moreArrivalpay) / allArrivalpay));
table.AddFootRow("style='display:none' typeName='0'");
}
}
var json = new
{
rowsList = table.GetRows(),
foot = table.GetFoot()
};
return Json(json, JsonRequestBehavior.AllowGet);
}
catch (Exception ex)
{
LogHelper.Error("OrderResourceReportController:Index" + ex.Message + ex.StackTrace);
return JsonHandler.ManageMessage(ex.Message, false);
}
}
public JsonResult Chart(string month)
{
if (string.IsNullOrEmpty(month))
return JsonHandler.ManageMessage("参数错误,请确认!", false);
DateTime m1;
if (!DateTime.TryParse(month, out m1))
{
return JsonHandler.ManageMessage("参数错误,请确认!", false);
}
var m2 = m1.AddMonths(1);
var list = _wxWorkaccount.GetOrderResourceReport(m1, m2);
var tags = (from q in list group q by new { q.sourcetag, q.sourcetagName } into g select new { g.Key.sourcetag, g.Key.sourcetagName }).ToList();
tags.Insert(0, new { sourcetag = "0", sourcetagName = "全部" });
var chartList = new List();
#region
var chart = new ChartDto();
var legendArr = new string[] { "新开单数量", "升级订单数量" };
chart.title = new ChartDto.ChartTitle() { text = "订单来源统计销量" };
chart.tooltip = new ChartDto.ChartTooltip() { };
chart.legend = new ChartDto.ChartLegend() { data = legendArr };
chart.xAxis = new ChartDto.ChartXAxis() { data = tags.Select(p => p.sourcetagName).ToArray() };
chart.yAxis = new ChartDto.ChartYAxis() { };
var series = new List();
var dataArr = new List