using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Web.Mvc; using QMAPP.MESReport.Web.Models.LineQTY; using QMAPP.Common.Web.Controllers; using QMFrameWork.WebUI.Attribute; using QMFrameWork.Data; using QMAPP.Entity; using QMAPP.ServicesAgent; using QMAPP.MESReport.Entity.LineQTY; using System.Text; using QMAPP.MESReport.Web.Models; using QMFrameWork.Common.ExcelOperation; using QMFrameWork.Log; using QMFrameWork.WebUI.QMEChart; using QMFrameWork.WebUI.QMEChart.Data; using ExcelOperationHelper = QMAPP.MESReport.Web.Common.ExcelOperationHelper; namespace QMAPP.MESReport.Web.Controllers { /// /// 安东呼叫统计分析 /// 于子清 /// 2017-11-13 /// public class AnDongCallController : QController { public ActionResult Index() { AnDongCallVModel seachModel = new AnDongCallVModel(); seachModel.START_DATE = DateTime.Now.Date.AddDays(-30).ToString("yyyy-MM-dd HH:mm:ss"); seachModel.END_DATE = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); return View(seachModel); } public ActionResult IndexRea() { return View(); } /// /// 异步刷新TABLE /// /// /// public ActionResult GetTable(string CALL_TYPE, string START_DATE, string END_DATE) { ServiceAgent wcfAgent = this.GetServiceAgent(); AnDongCallDModel condition = new AnDongCallDModel(); condition.CALL_TYPE = CALL_TYPE; condition.START_DATE = START_DATE; condition.END_DATE = END_DATE; List nli = new List(); try { #region wcf服务统一接口 var dataResult = wcfAgent.InvokeServiceFunction>>("AnDongCallBLL_GetAllList", condition); nli = dataResult.Result; #endregion } catch (Exception ex) { throw ex; } string sss = GetHTMLStr(nli, CALL_TYPE); return Json(sss); } /// /// 拼接字符串 /// /// public string GetHTMLStr(List nli, string CALL_TYPE) { StringBuilder html = new StringBuilder(); if (nli == null || nli.Count == 0) { return "没有数据"; } //var workcenter = ""; //if (!string.IsNullOrEmpty(CALL_TYPE)) //{ // if (CALL_TYPE == "0") // { // workcenter = "维修"; // } // if (CALL_TYPE == "1") // { // workcenter = "生成"; // } // if (CALL_TYPE == "2") // { // workcenter = "质量"; // } //} Dictionary tdSum = new Dictionary(); List tdList = nli.Select(o => o.MACHINENAME_SHORT).Distinct().OrderBy(x => x.ToString()).ToList(); List trList = nli.Select(o => o.CALL_TYPE).Distinct().OrderBy(o => o.ToString()).ToList(); tdList.Add("合计"); //拼接表头 html.Append(""); foreach (var std in tdList) { if (std == "合计") { html.AppendFormat("{0}", std); } else { html.AppendFormat("{0}", std); } tdSum[std] = 0; } html.Append(""); //拼接表中 foreach (var str in trList) { html.Append(""); var nameStr = ""; if (!string.IsNullOrEmpty(str)) { if (str == "0") { nameStr = "维修"; } else if (str == "1") { nameStr = "质量"; } else if (str == "2") { nameStr = "物料"; } else if (str == "3") { nameStr = "工程"; } } html.Append("" + nameStr + ""); //每行的合计 int trCountValue = 0; foreach (var std in tdList) { if (std == "合计") { html.AppendFormat("{0}", trCountValue); tdSum[std] += trCountValue; } else { var no = nli.FirstOrDefault(o => o.CALL_TYPE == str && o.MACHINENAME_SHORT == std); var num = "0"; if (no!=null) { num = no.NUM; } html.AppendFormat("{0}", num); tdSum[std] += Int32.Parse(num); trCountValue += Int32.Parse(num); } } html.Append(""); } //拼接表尾 html.Append(""); html.Append("合计"); foreach (var std in tdList) { html.AppendFormat("{0}", tdSum[std]); } html.Append(""); return html.ToString(); } /// /// 堆积柱状图 /// /// 处理结果 [HandleException] public ActionResult GetEchartDataBar2() { AnDongCallVModel seachModel = null; DataPage page = null; ServiceAgent wcfAgent = this.GetServiceAgent(); AnDongCallDModel condition = null; List nli = new List(); try { //获取查询对象 seachModel = GetModel(); if (string.IsNullOrEmpty(seachModel.START_DATE) || string.IsNullOrEmpty(seachModel.END_DATE)) { //return Content(null); } else { //获取前台分页设置信息 page = this.GetDataPage(seachModel); condition = CopyToModel(seachModel); #region wcf服务统一接口 var dataResult = wcfAgent.InvokeServiceFunction>>("AnDongCallBLL_GetAllList", condition); nli = dataResult.Result; #endregion } } catch (Exception ex) { throw ex; } #region //源数据定义 //新排序 // Dictionary> DLEq = new Dictionary>(); // if (nli != null && nli.Count > 0) // { // foreach (AnDongCallDModel item in nli) // { // //if (string.IsNullOrEmpty(item.MACHINENAME_SHORT)) // //{ // // item.MACHINENAME_SHORT = item.MACHINENAME; // //} // if (DLEq.Keys.Contains(item.MACHINENAME_SHORT)) // { // DLEq[item.MACHINENAME_SHORT].Add(item); // } // else // { // List li = new List(); // li.Add(item); // DLEq.Add(item.MACHINENAME_SHORT, li); // } // } // } // string category = "", val1 = "", val2 = "", val3 = "", dou = "", dou1 = "", dou2 = "", dou3 = ""; // bool first = true; // //foreach (List item in DLEq.Values) // //{ // // category += dou + "'" + item[0].MACHINENAME_SHORT + "'"; // // foreach (AnDongCallDModel itit in item) // // { // // if (itit.CALL_TYPE == "0") // // { // // val1 += dou1 + "'" + itit.NUM + "','0','0'"; // // } // // else if (itit.CALL_TYPE == "1") // // { // // val2 += dou2 + "'0','" + itit.NUM + "','0'"; // // } // // else if (itit.CALL_TYPE == "2") // // { // // val3 += dou3 + "'0','0','" + itit.NUM + "'"; // // } // // } // // if (first) // // { // // first = false; // // dou = ","; // // } // // if (!string.IsNullOrEmpty(val1)) // // { // // dou1 = ","; // // } // // if (!string.IsNullOrEmpty(val2)) // // { // // dou2 = ","; // // } // // if (!string.IsNullOrEmpty(val3)) // // { // // dou3 = ","; // // } // //} // foreach (AnDongCallDModel item in nli) // { // if (!category.Contains(item.MACHINENAME_SHORT)) // { // category += dou + "'" + item.MACHINENAME_SHORT + "'"; // } // if (item.CALL_TYPE == "0") // { // val1 += dou1 + "'" + item.NUM + "','0','0'"; // } // else if (item.CALL_TYPE == "1") // { // val2 += dou2 + "'0','" + item.NUM + "','0'"; // } // else if (item.CALL_TYPE == "2") // { // val3 += dou3 + "'0','0','" + item.NUM + "'"; // } // if (first) // { // first = false; // dou = ","; // } // if (!string.IsNullOrEmpty(val1)) // { // dou1 = ","; // } // if (!string.IsNullOrEmpty(val2)) // { // dou2 = ","; // } // if (!string.IsNullOrEmpty(val3)) // { // dou3 = ","; // } // } // string strOption = @"{ // tooltip: { // trigger: 'axis' // }, // toolbox: { // show: false, // feature: { // mark: { show: true }, // dataView: { show: true, readOnly: false }, // magicType: { show: true, type: ['line', 'bar'] }, // restore: { show: true }, // saveAsImage: { show: true } // } // }, // calculable: true, // legend: { // data: ['维修呼叫', '生产呼叫', '质量呼叫'] // }, // xAxis: [ // { // type: 'category', // axisLabel : { //坐标轴刻度标签的相关设置。 // interval:'auto', // rotate:10 // }, // data: [" + category + @"] // } // ], // yAxis: [ // { // type: 'value', // name: '次', // axisLabel: { // formatter: '{value}' // } // } // ], // series: [ // // { // name: '维修呼叫', // type: 'bar', // data: [ " + val1 + @" ] // }, // { // name: '生产呼叫', // type: 'bar', // data: [ " + val2 + @" ] // }, // { // name: '质量呼叫', // type: 'bar', // data: [ " + val3 + @" ] // } // ] // }"; // return Content(strOption); #endregion #region 源数据定义 //生成数据 List dataList = new List(); if (nli == null) { nli = new List(); dataList.Add(new TestEChartModel() { Week = "", Value = "0", TypeValue = " ", StackValue = "统计次数" }); } foreach (AnDongCallDModel item in nli) { //var workcenter = ""; //if (!string.IsNullOrEmpty(item.CALL_TYPE)) //{ // if (item.CALL_TYPE == "0") // { // workcenter = "维修"; // } // if (item.CALL_TYPE == "1") // { // workcenter = "质量"; // } // if (item.CALL_TYPE == "2") // { // workcenter = "物料"; // } //} dataList.Add(new TestEChartModel() { Week = item.MACHINENAME_SHORT, Value = item.NUM, TypeValue = item.CALL_TYPE_NAME, StackValue = "统计次数" }); } #endregion #region 默认设置 QMEChartBar chartLBar = new QMEChartBar() { BarType = "bar2" }; //设置源数据列表 chartLBar.DataList = dataList; #region 设置标题 //不为空时显示Title,为空时不显示 //chartLBar.Title = new QMECTitle(); //chartLBar.Title.Text = ""; //chartLBar.Title.Subtext = ""; #endregion #region Tooltip提示窗口设置 //是否显示ToolTip工具条 chartLBar.HaveTooltip = true; #endregion #region Legend图例设置 //是否有Legend chartLBar.HaveLegend = true; //Legend的自动统计字段,对应chartLine.DataList中用于统计的字段 chartLBar.LegendField = "TypeValue"; #endregion #region Toolbox工具栏设置 //是否存在工具栏,为空时使用默认设置 chartLBar.HaveToolBox = false; //chartLBar.Toolbox = new QMECToolbox() { Feature = new QMECFeature() //{ // DataZoom = new QMECDataZoom() // { // Show = true // } //}}; #endregion #region XAxis横坐标轴设置 //为空时使用默认值并从数据源中统计类型,需要设置DataList数据源 chartLBar.HaveXAxis = true; //设置X轴统计类型字段 chartLBar.XAxisField = "Week"; chartLBar.XAxis = new QMECXAxis() { AxisLabel = new QMECAxisLabel() }; //chartLBar.XAxis.AxisLabel.rotate = 20; #endregion #region YAxis纵坐标轴设置 //设置Y轴显示格式。 chartLBar.HaveYAxis = true; //自定义设置 chartLBar.YAxis = new QMECYAxis() { AxisLabel = new QMECAxisLabel() }; chartLBar.YAxis.Type = "value"; #endregion #region 堆积字段设置 //堆积字段,是否是堆积图形 chartLBar.StackField = "StackValue"; #endregion #region 生成图表所需要数据格式 //转换数据格式,值字段 chartLBar.ValueField = "Value"; chartLBar.Series = QMEChartHelper.CovertDataToSeries(chartLBar); #endregion #region 辅助线设置,有则设置,没有则无需设置 //设置辅助线 QMECMarkLine ml = new QMECMarkLine() { ItemStyle = new QMECItemStyle() { Normal = new QMECNormal() { LineStyle = new QMECLineStyle() } }, LineData = new List() }; ml.ItemStyle.Normal.LineStyle = new QMECLineStyle() { Type = "dashed", Width = 1 }; List mp = new List(); QMECPoint mpstart = new QMECPoint(); QMECPoint mpend = new QMECPoint(); mpstart.Type = "min"; mpend.Type = "max"; ml.LineData.Add(mpstart); ml.LineData.Add(mpend); foreach (QMECSerie ser in chartLBar.Series) { if (ser.Name == "搜索引擎") { ser.MarkLine = ml; } if (ser.BarWidth!=null) { if (Int32.Parse(ser.BarWidth) > 100) { ser.BarWidth = "100"; } } } #endregion return Content(QMEChartHelper.QMECHelper(chartLBar)); #endregion } /// /// 原因统计堆积柱状图 /// /// 处理结果 [HandleException] public ActionResult GetEchartDataBar3() { AnDongCallVModel seachModel = null; DataPage page = null; ServiceAgent wcfAgent = this.GetServiceAgent(); AnDongCallDModel condition = null; List nli = new List(); try { //获取查询对象 seachModel = GetModel(); //获取前台分页设置信息 page = this.GetDataPage(seachModel); condition = CopyToModel(seachModel); #region wcf服务统一接口 var dataResult = wcfAgent.InvokeServiceFunction>>("AnDongCallBLL_GetAllList", condition); nli = dataResult.Result; #endregion } catch (Exception ex) { throw ex; } #region 源数据定义 string category = "", val1 = "", val2 = "", dou = ""; bool first = true; foreach (AnDongCallDModel item in nli) { category += dou + "'" + item.NUM + "'"; val1 += dou + "'" + item.NUM + "'"; val2 += dou + "'" + item.NUM + "'"; if (first) { first = false; dou = ","; } } string strOption = @"{ tooltip: { trigger: 'axis' }, toolbox: { show: false, feature: { mark: { show: true }, dataView: { show: true, readOnly: false }, magicType: { show: true, type: ['line', 'bar'] }, restore: { show: true }, saveAsImage: { show: true } } }, calculable: true, legend: { data: ['原因1', '原因2', '原因3'] }, xAxis: [ { type: 'category', data: ['Q5-CAST', 'Q5-PUNCH', 'Q5-MILL', 'Q5-SSS'] } ], yAxis: [ { type: 'value', name: '次', axisLabel: { formatter: '{value}' } } ], series: [ { name: '原因1', type: 'bar', data: [32, 20, 16, 23] }, { name: '原因2', type: 'bar', data: [25, 73, 35, 62] }, { name: '原因3', type: 'bar', data: [20, 24, 17, 23] } ] }"; #endregion return Content(strOption); } #region 导出图片 [HttpPost] [HandleException] public ActionResult ExportBanCi() { string imgSend = Request.Form["hiChartImg"]; imgSend = imgSend.Replace(" ", "+"); string[] contents = imgSend.Split(','); imgSend = contents[1]; byte[] content = Convert.FromBase64String(imgSend); System.Web.Mvc.FileContentResult result = null; result = new System.Web.Mvc.FileContentResult(content, "application/png"); result.FileDownloadName = System.Web.HttpUtility.UrlEncode("生产统计.png", System.Text.Encoding.UTF8); return result; } #endregion #region 导出excel /// /// 导出excel /// /// 结果 [HandleException] public ActionResult ExportExcel() { AnDongCallVModel seachModel = null; AnDongCallDModel condition = null; DataTable exportDt = new DataTable(); string selectKey = Request["selectKey"]; ServiceAgent wcfAgent = this.GetServiceAgent(); try { //获取查询对象 seachModel = GetModel(); condition = CopyToModel(seachModel); //选择Excel condition.selectExcel = 0; //获取数据 var dataResult = wcfAgent.InvokeServiceFunction>("AnDongCallBLL_GetExportData", condition); //通过返回dataResult判断 if (dataResult.Ex != null) { throw dataResult.Ex; } else if (dataResult.IsSuccess) { exportDt = dataResult.Result; } else { SetMessage(dataResult.Msg); return View(true); } //根据所选信息进行导出 if (!String.IsNullOrEmpty(selectKey)) { DataView dv = new DataView(exportDt); string strWhere = ""; string[] list = selectKey.Split(":".ToCharArray()); foreach (string ID in list) { strWhere += " PID='" + ID + "' or"; } if (strWhere != "") { strWhere = strWhere.Remove((strWhere.Length - 2), 2); } dv.RowFilter = strWhere; exportDt = dv.ToTable(); } //导出 IEExcelHelper ieExcelHelper = new IEExcelHelper(); var sheetInfo = ieExcelHelper.GetMainInfo("AnDongCallExp"); var fileName = System.Guid.NewGuid().ToString() + ".xlsx"; ieExcelHelper.ExportExcel(sheetInfo, exportDt, Common.ExcelOperationHelper.GetTempPath() + fileName, true); return Content(fileName); } catch (Exception ex) { LogManager.LogHelper.Error(new LogInfo { ErrorInfo = ex, Info = "导出excel", Tag = "呼叫类型统计" }); SetMessage("导出excel失败"); return View(true); } } /// /// 导出文件 /// /// public ActionResult ExportFile() { String filePath = Common.ExcelOperationHelper.GetTempPath(); String fileName = Request["FileName"]; ExcelOperationHelper.FileDownload(Response, filePath + fileName, "呼叫类型统计.xlsx"); return Content(""); } #endregion } }