using Core.Web.App_Start; using Core.Web.WebHelper; using CRM.Core.BLL.Util; using CRM.Core.Common; using CRM.Core.Model.Enum; using Newtonsoft.Json; using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.Data; using System.Linq; using System.Reflection; using System.Text; using System.Web; using System.Web.Mvc; using WX.CRM.Common.Employee; using WX.CRM.WebHelper; using static Core.Web.Controllers.StatisticsController; namespace Core.Web.Controllers { public class StatisticsController : BaseController { [AuthorizeRedirect(RightsConfig.CONST_自动外呼日志, ToolBarConfig.CONST_NotButton, true)] public ActionResult Index() { var now = DateTime.Now; ViewBag.Year = now.Year.ToString(); ViewBag.Month = now.AddMonths(-1).Month.ToString("00"); var yearList = new List(); var monthList = new List(); for (var i = -5; i < 2; i++) { yearList.Add(now.AddYears(i).Year.ToString()); } for (var i = 0; i < 12; i++) { monthList.Add((i + 1).ToString("00")); } ViewBag.YearList = yearList; ViewBag.MonthList = monthList; return View(); } [AuthorizeRedirect(RightsConfig.CONST_自动外呼日志, ToolBarConfig.CONST_NotButton, false)] public JsonResult GetListHtml([Required] string year, [Required] string month) { var data = GetData(year, month); return Json(data, JsonRequestBehavior.AllowGet); } [AuthorizeRedirect(RightsConfig.CONST_自动外呼日志, ToolBarConfig.CONST_NotButton, false)] public FileResult Export([Required] string year, [Required] string month) { var data = GetData(year, month); var tableData = ToDataTable(data.data.Result); var dataSet = new DataSet(); dataSet.Tables.Clear(); dataSet.Tables.Add(tableData); var fileName = $"{year}年{month}月外呼统计数据"; //拼接需要导出的列 List filds = new List(); List fildsName = new List(); foreach (var item in typeof(MonthOutboundDto).GetProperties()) { var dispalyName = item.GetCustomAttributes(typeof(DisplayNameAttribute), true); if (dispalyName.Any()) { filds.Add(item.Name); DisplayNameAttribute attr = dispalyName[0] as DisplayNameAttribute; fildsName.Add(attr.DisplayName); } } string checkedFilds = $"[{string.Join("][", filds)}]"; string checkedTitle = string.Join(",", fildsName); return File(ExcelHelper.ExportDataSetToExcel(dataSet, $"{year}年{month}月外呼统计数据"), "application/ms-excel", $"{fileName}.xls"); } private HGApiResult> GetData(string year, string month) { CACHE_BL cache_BL = new CACHE_BL(); var webapi = cache_BL.GetValue_Parameter(Parameter.Hg_Core_WebApi); //webapi = "http://120.77.165.155:8089"; //webapi = "http://localhost:5090"; var url = $"{webapi}/api/Statistics/MonthOutbound"; var para = $"year={year}&month={month}"; var result = Utility.GetData(url, para); var data = JsonConvert.DeserializeObject>>(result); return data; } private static DataTable ToDataTable(IEnumerable collection) { var props = typeof(T).GetProperties(); var dt = new DataTable(); foreach (var prop in props) { var attribute = (DisplayNameAttribute)System.Attribute.GetCustomAttribute(prop, typeof(DisplayNameAttribute)); var name = attribute == null ? prop.Name : attribute.DisplayName; Type colType = prop.PropertyType; if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>))) { colType = colType.GetGenericArguments()[0]; } dt.Columns.Add(new DataColumn(name, colType)); } if (collection.Count() > 0) { for (int i = 0; i < collection.Count(); i++) { ArrayList tempList = new ArrayList(); foreach (PropertyInfo pi in props) { object obj = pi.GetValue(collection.ElementAt(i), null); tempList.Add(obj); } object[] array = tempList.ToArray(); dt.LoadDataRow(array, true); } } return dt; } public class MonthResultDto { public string Date { get; set; } public List Result { get; set; } } public class MonthOutboundDto { [DisplayName("回访类型")] public string TypeName { get; set; } [DisplayName("回访数量")] public int? Total { get; set; } [DisplayName("占比")] public string Proportion { get; set; } } } }