using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using QMAPP.MESReport.Web.Models.LineQTY; using QMAPP.Common.Web.Controllers; using QMFrameWork.WebUI.Attribute; using QMFrameWork.Data; using QMAPP.MD.Entity; using QMAPP.Entity; using QMAPP.ServicesAgent; using QMFrameWork.WebUI.DataSource; using QMFrameWork.Common.Serialization; using QMAPP.MESReport.Entity.LineQTY; using System.Collections; using System.Reflection; using QMFrameWork.ServiceInterface; using System.Data.SqlClient; using System.Data; using System.Text; using QMAPP.Common.Web.Util; using QMFrameWork.WebUI.QMEChart; using QMFrameWork.WebUI.QMEChart.Data; using QMAPP.MESReport.Web.Models; using QMAPP.Common.Web.Controllers; using QMAPP.MESReport.BLL.LineQTY; using QMFrameWork.Common.ExcelOperation; using QMFrameWork.Log; using ExcelOperationHelper = QMAPP.MESReport.Web.Common.ExcelOperationHelper; namespace QMAPP.MESReport.Web.Controllers { /// /// 班次对比分析 2 /// 于子清 /// 2017-10-10 /// public class LineShiftQTYCountController : QController { public string EmptyItemTitle = " "; #region 获取信息 /// /// 班次对比 /// /// 结果 [HandleException] public ActionResult QTYTrendList(bool? callback) { LineQtyConditionModel seachModel = new LineQtyConditionModel(); 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); } /// /// 班次趋势 /// /// /// [HandleException] public ActionResult QTYQuShi(bool? callback) { LineQtyConditionModel seachModel = new LineQtyConditionModel(); 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); } #endregion #region ///////////////////////////////////////////// //public ActionResult GetTrendData(bool? callBack) //{ // LineDWQTYModel seachModel = null; // DataPage page = null; // ServiceAgent wcfAgent = this.GetServiceAgent(); // LineDayQTYCount condition = null; // DataResult pageResult = new DataResult(); // try // { // //获取查询对象 // seachModel = GetModel(); // #region 获取缓存值 // if (callBack != null) // { // TryGetSelectBuffer(out seachModel); // } // else // { // //保存搜索条件 // SetSelectBuffer(seachModel); // } // #endregion // //获取前台分页设置信息 // page = this.GetDataPage(seachModel); // condition = CopyToModel(seachModel); // #region wcf服务统一接口 // //page = wcfAgent.InvokeServiceFunction("ProductBLL_GetMaterialDetail", condition, page); // pageResult = wcfAgent.InvokeServiceFunction>("LineShiftQTYCountBLL_GetEquQTYByShift", condition, page); // if (pageResult.IsSuccess == false) // { // SetMessage(pageResult.Msg); // return QTYTrendList(true); // } // DateGridResult result = new DateGridResult(); // result.Total = pageResult.Result.RecordCount; // List nli = JsonConvertHelper.GetDeserialize>(pageResult.Result.Result.ToString()); // result.Rows = nli; // #endregion // return Content(result.GetJsonSource()); // } // catch (Exception ex) // { // throw ex; // } //} #endregion /// /// 异步刷新TABLE /// /// /// public ActionResult GetTableTrend(string WORKCENTER_CODE, string EQUIPMENT_CODE, string SHIFT_CODE, string START_DATE, string END_DATE) { ServiceAgent wcfAgent = this.GetServiceAgent(); LineDayQTYCount condition = new LineDayQTYCount(); condition.WORKCENTER_CODE = WORKCENTER_CODE; condition.EQUIPMENT_CODE = EQUIPMENT_CODE; condition.SHIFT_CODE = SHIFT_CODE; condition.START_DATE = START_DATE; condition.END_DATE = END_DATE; List nli = new List(); try { #region wcf服务统一接口 var dataResult = wcfAgent.InvokeServiceFunction>>("LineShiftQTYCountBLL_GetEquQTYByShift", condition); nli = dataResult.Result; #endregion } catch (Exception ex) { throw ex; } string sss = GetHTMLStrTrend(nli, WORKCENTER_CODE); return Json(sss); } /// /// 拼接字符串 /// /// public string GetHTMLStrTrend(List nli, string WORKCENTER_CODE) { StringBuilder html = new StringBuilder(); if (nli == null || nli.Count == 0) { return "没有数据"; } var workcenter = ""; if (!string.IsNullOrEmpty(WORKCENTER_CODE)) { if (WORKCENTER_CODE == "INJECT") { workcenter = "注塑"; } if (WORKCENTER_CODE == "BORADPASSY") { workcenter = "门板"; } if (WORKCENTER_CODE == "BORAIPASSY") { workcenter = "仪表板"; } } Dictionary tdSum = new Dictionary(); List tdList = nli.Select(o => o.EQUIPMENT_NAME).Distinct().OrderBy(x=> x.ToString() ).ToList(); List trList = nli.Select(o => o.SHIFT_NAME).Distinct().OrderByDescending(o => o.ToString()).ToList(); tdList.Add("合计"); //拼接表头 html.Append("" + workcenter + ""); 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(""); html.Append("" + str + ""); //每行的合计 int trCountValue = 0; foreach (var std in tdList) { if (std == "合计") { html.AppendFormat("{0}", trCountValue); tdSum[std] += trCountValue; } else { var no = nli.FirstOrDefault(o => o.SHIFT_NAME == str && o.EQUIPMENT_NAME == std); if (no!=null) { html.AppendFormat("{0}", no.OK_QTY); tdSum[std] += Int32.Parse(no.OK_QTY); trCountValue += Int32.Parse(no.OK_QTY); } } } html.Append(""); } //拼接表尾 html.Append(""); html.Append("合计"); foreach (var std in tdList) { html.AppendFormat("{0}", tdSum[std]); } html.Append(""); return html.ToString(); } /// /// 班次对比分析 /// /// 处理结果 [HandleException] public ActionResult GetEchartDataLine1(string name) { #region 源数据定义 LineQtyConditionModel seachModel = null; DataPage page = null; ServiceAgent wcfAgent = this.GetServiceAgent(); LineDayQTYCount 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>>( "LineShiftQTYCountBLL_GetEquQTYByShift", condition); nli = dataResult.Result; #endregion } //return Content(setOption1(nli)); } catch (Exception ex) { throw ex; } //生成数据 List dataList = new List(); if (nli != null) { foreach (LineDayQTYCount item in nli) { dataList.Add(new TestEChartModel() { Week = item.EQUIPMENT_NAME, TypeValue = item.SHIFT_NAME, Value = item.OK_QTY }); } } #endregion #region 设置图表(必选) //折线图的类型 QMEChartLine chartLine = new QMEChartLine() { LineType = "line1" }; //设置源数据列表 chartLine.DataList = dataList; //标题设置 chartLine.Title.Text = "班次对比分析"; chartLine.Title.Subtext = "数量"; //XAxis横坐标轴设置 chartLine.HaveXAxis = true; chartLine.XAxisField = "Week"; //YAxis纵坐标轴设置 chartLine.HaveYAxis = true; chartLine.YAxis.Type = "value"; //设置Y轴显示格式。 chartLine.YAxis.AxisLabel.Formatter = "{value} 个"; //设置分类 chartLine.LegendField = "TypeValue"; //转换数据格式,用于生成图表的值字段 chartLine.ValueField = "Value"; chartLine.Series = QMEChartHelper.CovertDataToSeries(chartLine); //是否显示图例 chartLine.HaveLegend = true; #endregion #region 设置图表(可选) //是否生成Tooltip提示窗口 chartLine.HaveTooltip = true; //是否存在工具栏 chartLine.HaveToolBox = false; //自定义辅助线 QMECMarkLine ml = new QMECMarkLine() { Data = new List() { new QMECPoint { Type = "average", Name = "平均值" } } }; //设置辅助点 QMECMarkPoint mp = new QMECMarkPoint() { Data = new List() { new QMECPoint() { Type = "max", Name = "最大值" }, new QMECPoint() { Type = "min", Name = "最小值" } } }; foreach (QMECSerie ser in chartLine.Series) { ser.MarkLine = ml; ser.MarkPoint = mp; } #endregion return Content(QMEChartHelper.QMECHelper(chartLine)); } public string setOption1(List nli) { #region 源数据定义 string aqty = "", bqty = "", riqi = "", rdou = "", adou = "", bdou = ""; bool afirst = true; bool bfirst = true; bool rfirst = true; foreach (LineDayQTYCount item in nli) { //jihua=生成数量QTY,wancheng=返修数量COMPLETE_QTY。 借用 if (item.SHIFT_CODE == "A") { aqty += adou + "'" + item.OK_QTY + "'"; if (afirst) { afirst = false; adou = ","; } } if (item.SHIFT_CODE == "B") { bqty += bdou + "'" + item.OK_QTY + "'"; if (bfirst) { bfirst = false; bdou = ","; } } //double fenmu = 1; //if (!string.IsNullOrEmpty(item.COMPLETE_QTY) && item.COMPLETE_QTY != "0") //{ // fenmu = Convert.ToDouble(item.COMPLETE_QTY); //} //double fenzi = 0; //if (!string.IsNullOrEmpty(item.QTY)) //{ // fenzi = Convert.ToDouble(item.QTY); //} //wclv += dou + "'" + (fenzi / fenmu * 100).ToString("0") + "'"; riqi += rdou + "'" + item.STATIS_DATE + "'"; if (rfirst) { rfirst = false; rdou = ","; } } #region 11111 string strOption = @"{ title: { text: '未班次对比分析', subtext: '数量' }, tooltip: { trigger: 'axis' }, legend: { data:['一班','二班'] }, toolbox: { show: true, feature: { dataZoom: { yAxisIndex: 'none' }, dataView: {readOnly: false}, magicType: {type: ['line', 'bar']}, restore: {}, saveAsImage: {} } }, xAxis: { type: 'category', boundaryGap: false, data: [" + riqi + @"] }, yAxis: { type: 'value', axisLabel: { formatter: '{value} 个' } }, series: [ { name:'一班', type:'line', data:[" + aqty + @"], markPoint: { data: [ {type: 'max', name: '最大值'}, {type: 'min', name: '最小值'} ] }, markLine: { data: [ {type: 'average', name: '平均值'} ] } }, { name:'二班', type:'line', data:data:[" + bqty + @"], markPoint: { data: [ {type: 'max', name: '最大值'}, {type: 'min', name: '最小值'} ] }, markLine: { data: [ {type: 'average', name: '平均值'}, [{ symbol: 'none', x: '90%', yAxis: 'max' }, { symbol: 'circle', label: { normal: { position: 'start', formatter: '最大值' } }, type: 'max', name: '最高点' }] ] } } ] }"; #endregion #region 222222222 var strOption1 = @"{ title: { text: '未来一周气温变化', subtext: '纯属虚构' }, tooltip: { trigger: 'axis' }, legend: { data:['最高气温','最低气温'] }, toolbox: { show: true, feature: { dataZoom: { yAxisIndex: 'none' }, dataView: {readOnly: false}, magicType: {type: ['line', 'bar']}, restore: {}, saveAsImage: {} } }, xAxis: { type: 'category', axisLabel : { //坐标轴刻度标签的相关设置。 interval:'auto', rotate:30 }, boundaryGap: false, data: ['周一周一周一周一周一周一周一周一周一周一周一周一周一周一周一周一周一周一周一周一周一周一','周二周二周二周二周二周二周二周二周二周二周二周二周二周二周二周二周二','周三周三周三周三周三周三周三周三周三周三周三周三周三周三周三周三周三周三周三周三','周四','周五','周六','周日'] }, yAxis: { type: 'value', axisLabel: { formatter: '{value} °C' } }, series: [ { name:'最高气温', type:'line', data:[11, 11, 15, 13, 12, 13, 10], markPoint: { data: [ {type: 'max', name: '最大值'}, {type: 'min', name: '最小值'} ] }, markLine: { data: [ {type: 'average', name: '平均值'} ] } }, { name:'最低气温', type:'line', data:[1, -2, 2, 5, 3, 2, 0], markPoint: { data: [ {name: '周最低', value: -2, xAxis: 1, yAxis: -1.5} ] }, markLine: { data: [ {type: 'average', name: '平均值'}, [{ symbol: 'none', x: '90%', yAxis: 'max' }, { symbol: 'circle', label: { normal: { position: 'start', formatter: '最大值' } }, type: 'max', name: '最高点' }] ] } } ] }"; #endregion #endregion return strOption1; } /// /// 异步刷新TABLE /// /// /// public ActionResult GetTable(string WORKCENTER_CODE, string EQUIPMENT_CODE, string SHIFT_CODE, string START_DATE, string END_DATE) { ServiceAgent wcfAgent = this.GetServiceAgent(); LineDayQTYCount condition = new LineDayQTYCount(); condition.WORKCENTER_CODE = WORKCENTER_CODE; condition.EQUIPMENT_CODE = EQUIPMENT_CODE; condition.SHIFT_CODE = SHIFT_CODE; condition.START_DATE = START_DATE; condition.END_DATE = END_DATE; List nli = new List(); try { #region wcf服务统一接口 var dataResult = wcfAgent.InvokeServiceFunction>>("LineShiftQTYCountBLL_GetEquQTYByShift1", condition); nli = dataResult.Result; #endregion } catch (Exception ex) { throw ex; } string sss = GetHTMLStr(nli, WORKCENTER_CODE); return Json(sss); } /// /// 拼接字符串 /// /// public string GetHTMLStr(List nli, string WORKCENTER_CODE) { StringBuilder html = new StringBuilder(); if (nli == null || nli.Count == 0) { return "没有数据"; } var workcenter = ""; if (!string.IsNullOrEmpty(WORKCENTER_CODE)) { if (WORKCENTER_CODE == "INJECT") { workcenter = "注塑"; } if (WORKCENTER_CODE == "BORADPASSY") { workcenter = "门板"; } if (WORKCENTER_CODE == "BORAIPASSY") { workcenter = "仪表板"; } } Dictionary tdSum = new Dictionary(); List tdList = nli.Select(o => o.STATIS_DATE).Distinct().OrderBy(x => x.ToString()).ToList(); List trList = nli.Select(o => o.SHIFT_NAME).Distinct().OrderByDescending(o => o.ToString()).ToList(); tdList.Add("合计"); //拼接表头 html.Append("" + workcenter + ""); 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(""); html.Append("" + str + ""); //每行的合计 int trCountValue = 0; foreach (var std in tdList) { if (std == "合计") { html.AppendFormat("{0}", trCountValue); tdSum[std] += trCountValue; } else { var no = nli.FirstOrDefault(o => o.SHIFT_NAME == str && o.STATIS_DATE == std); var qtyNum = "0"; if (no!=null) { qtyNum = no.OK_QTY; } html.AppendFormat("{0}", qtyNum); tdSum[std] += Int32.Parse(qtyNum); trCountValue += Int32.Parse(qtyNum); } } html.Append(""); } //拼接表尾 html.Append(""); html.Append("合计"); foreach (var std in tdList) { html.AppendFormat("{0}", tdSum[std]); } html.Append(""); return html.ToString(); } /// /// 班次趋势分析 /// /// 处理结果 [HandleException] public ActionResult GetEchartDataLine2(string name) { #region 源数据定义 LineQtyConditionModel seachModel = null; DataPage page = null; ServiceAgent wcfAgent = this.GetServiceAgent(); LineDayQTYCount 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>>("LineShiftQTYCountBLL_GetEquQTYByShift1", condition); nli = dataResult.Result; #endregion } //return Content(setOption1(nli)); } catch (Exception ex) { throw ex; } #endregion //生成数据 List dataList = new List(); foreach (LineDayQTYCount item in nli) { dataList.Add(new TestEChartModel() { Week = item.STATIS_DATE, TypeValue = item.SHIFT_NAME, Value = item.OK_QTY }); } #region 设置图表(必选) //折线图的类型 QMEChartLine chartLine = new QMEChartLine() { LineType = "line1" }; //设置源数据列表 chartLine.DataList = dataList; //标题设置 chartLine.Title.Text = "班次趋势分析"; chartLine.Title.Subtext = "数量"; //XAxis横坐标轴设置 chartLine.HaveXAxis = true; chartLine.XAxisField = "Week"; //YAxis纵坐标轴设置 chartLine.HaveYAxis = true; chartLine.YAxis.Type = "value"; //设置Y轴显示格式。 chartLine.YAxis.AxisLabel.Formatter = "{value} 个"; //设置分类 chartLine.LegendField = "TypeValue"; //转换数据格式,用于生成图表的值字段 chartLine.ValueField = "Value"; chartLine.Series = QMEChartHelper.CovertDataToSeries(chartLine); //是否显示图例 chartLine.HaveLegend = true; #endregion #region 设置图表(可选) //是否生成Tooltip提示窗口 chartLine.HaveTooltip = true; //是否存在工具栏 chartLine.HaveToolBox = false; //自定义辅助线 QMECMarkLine ml = new QMECMarkLine() { Data = new List() { new QMECPoint { Type = "average", Name = "平均值" } } }; //设置辅助点 QMECMarkPoint mp = new QMECMarkPoint() { Data = new List() { new QMECPoint() { Type = "max", Name = "最大值" }, new QMECPoint() { Type = "min", Name = "最小值" } } }; foreach (QMECSerie ser in chartLine.Series) { ser.MarkLine = ml; ser.MarkPoint = mp; } #endregion return Content(QMEChartHelper.QMECHelper(chartLine)); } #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(int Trend) { LineQtyConditionModel seachModel = null; LineDayQTYCount condition = null; DataTable exportDt = new DataTable(); string selectKey = Request["selectKey"]; ServiceAgent wcfAgent = this.GetServiceAgent(); try { //获取查询对象 seachModel = GetModel(); condition = CopyToModel(seachModel); condition.Trend= Trend; //获取数据 var dataResult = wcfAgent.InvokeServiceFunction>("LineShiftQTYCountBLL_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 sheetInfoName = "QTYQuShiExp"; if (Trend == 0) { sheetInfoName = "QTYTrendExp"; } var sheetInfo = ieExcelHelper.GetMainInfo(sheetInfoName); 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(int Trend) { String filePath = Common.ExcelOperationHelper.GetTempPath(); String fileName = Request["FileName"]; var sheetInfoName = "班次趋势分析.xlsx"; if (Trend == 0) { sheetInfoName = "班次对比分析.xlsx"; } ExcelOperationHelper.FileDownload(Response, filePath + fileName, sheetInfoName); return Content(""); } #endregion } }