using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Estsh.BLL;
using System.Collections;
using System.Data;
using Estsh.Web.Util;
using System.IO;
using Estsh.Web.Service;
using NPOI.HSSF.UserModel;
namespace Estsh.Core.Web.Controllers
{
public class XTReWorkQueryController : Controller
{
//
XTReWorkQueryService Service = new XTReWorkQueryService();
public ActionResult Index()
{
return View();
}
string _where = "";
public string SetWhere()
{
int condition = 0;
DateTime startDate = Request.RequestContext.HttpContext.Timestamp.AddDays(-1);
DateTime endDate = Request.RequestContext.HttpContext.Timestamp;
string starttime = startDate.ToString("yyyy-MM-dd HH:mm:ss");
string endtime = endDate.ToString("yyyy-MM-dd HH:mm:ss");
if (!string.IsNullOrEmpty(Request["txtStartTime"]) && !string.IsNullOrEmpty(Request["txtEndTime"]))
{
condition++;
_where += " and b.create_ymd+' '+b.create_hms >= '" + Request["txtStartTime"].Trim() + "'";
_where += " and b.create_ymd+' '+b.create_hms <= '" + Request["txtEndTime"].Trim() + "'";
}
if (!string.IsNullOrEmpty(Request["cbStageName"]))
{
condition++;
_where += " and e.stage_name= '" + Request["cbStageName"].ToString().Trim() + "'";
}
if (!string.IsNullOrEmpty(Request["cmbTypeName"]))
{
condition++;
_where += " and t.type_name = '" + Request["cmbTypeName"].Trim() + "'";
}
if (!string.IsNullOrEmpty(Request["cbModelName"]))
{
condition++;
_where += " and f.model_name = '" + Request["cbModelName"].Trim() + "'";
}
if (condition == 0)
{
_where += " and b.create_ymd+' '+b.create_hms >= '" + starttime + "'";
_where += " and b.create_ymd+' '+b.create_hms <= '" + endtime + "'";
}
return _where;
}
///
/// 汇总
///
///
///
public ActionResult XTReWorkData(Pager pager)
{
_where = SetWhere();
int totalCount = 0;
DataTable dtQuery = Service.XTReWorkData(_where, pager, ref totalCount);
Hashtable result = new Hashtable();
result.Add("rows", DataTypeConvert.NewObject.DataTableToArrayList(dtQuery));
result.Add("pager.totalRows", totalCount);
return Json(result);
}
///
/// 区段集合
///
/// 数据集
public ActionResult GetStageName()
{
Hashtable resault = new Hashtable();
ArrayList list = Service.GetStageName();
resault.Add("list", list);
return Json(resault, JsonRequestBehavior.AllowGet);
}
///
/// 不良类型
///
///
///
public ActionResult GetDefectName(string stageName)
{
Hashtable ht = new Hashtable();
ArrayList alModel = Service.GetDefectName(stageName);
ht.Add("list", alModel);
return Json(ht, JsonRequestBehavior.AllowGet);
}
///
/// 车型
///
///
public ActionResult GetTypeName()
{
Hashtable ht = new Hashtable();
ArrayList alType = Service.GetTypeName();
ht.Add("list", alType);
return Json(ht, JsonRequestBehavior.AllowGet);
}
///
/// 配置
///
///
///
public ActionResult GetModelList(string typeName)
{
Hashtable ht = new Hashtable();
ArrayList alModel = Service.GetModelList(typeName);
ht.Add("list", alModel);
return Json(ht, JsonRequestBehavior.AllowGet);
}
///
/// 导出数据
///
///
///
///
///
///
///
public ActionResult exportData(Pager pager, int a, String sort, String direction, String isPage)
{
_where = SetWhere();
int totalCount = 0;
DataTable dataHt = Service.XTReWorkData(_where, pager, ref totalCount);
HSSFWorkbook workbook = new HSSFWorkbook();
Stream outputStream = Response.OutputStream;
HSSFSheet sheet = (HSSFSheet)workbook.CreateSheet("终检查询报表");
try
{
if (workbook != null)
{
HSSFRow headRow = (HSSFRow)sheet.CreateRow(0);
headRow.CreateCell(0).SetCellValue("车型");
headRow.CreateCell(1).SetCellValue("配置名称");
headRow.CreateCell(2).SetCellValue("区段");
headRow.CreateCell(3).SetCellValue("不良类型");
headRow.CreateCell(4).SetCellValue("不良数");
headRow.CreateCell(5).SetCellValue("下线数");
headRow.CreateCell(6).SetCellValue("生产总数");
headRow.CreateCell(7).SetCellValue("FTT");
}
for (int i = 0; i < dataHt.Rows.Count; i++)
{
int row = i + 1;
HSSFRow dataRow = (HSSFRow)sheet.CreateRow(row);
dataRow.CreateCell(0).SetCellValue(dataHt.Rows[i]["type_name"].ToString());
dataRow.CreateCell(1).SetCellValue(dataHt.Rows[i]["model_name"].ToString());
dataRow.CreateCell(2).SetCellValue(dataHt.Rows[i]["stage_name"].ToString());
dataRow.CreateCell(3).SetCellValue(dataHt.Rows[i]["defect_desc"].ToString());
dataRow.CreateCell(4).SetCellValue(dataHt.Rows[i]["ncqty"].ToString());
dataRow.CreateCell(5).SetCellValue(dataHt.Rows[i]["qty"].ToString());
dataRow.CreateCell(6).SetCellValue(dataHt.Rows[i]["couqty"].ToString());
dataRow.CreateCell(7).SetCellValue(dataHt.Rows[i]["per"].ToString());
}
Response.Clear();
workbook.Write(outputStream);
Response.Buffer = true;
Response.AppendHeader("Content-Disposition", "attachment;filename=终检查询报表_" + DateTime.Now.ToString("yyyy-MM-dd") + ".xls");
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.ContentType = "application/vnd.ms-excel";
Response.Flush();
}
catch (Exception e)
{
}
finally
{
workbook = null;
}
return null;
}
}
}