using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using Estsh.Web.Service; using System.Collections; using System.Data; using Estsh.Web.Util; using Estsh.Web.Models; using NPOI.HSSF.UserModel; using System.IO; namespace Estsh.Core.Web.Controllers { public class EDIKeyDataDefineController : Controller { /*************************************************************************************************** * * 作者:张茂忠 * 创建时间:2013.05.13 * 描述:零件关键数据Controller层 * 修改日志: * * * *************************************************************************************************/ EDIKeyDataDefineService service = new EDIKeyDataDefineService(); public ActionResult Index() { return View(); } public string _where = ""; public string SetWhere() { int condition = 0; _where = ""; 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["txtVIN"])) { condition++; _where += " and b.vin= '" + Request["txtVIN"].ToString().Trim() + "'"; } if (!string.IsNullOrEmpty(Request["txtCSN"])) { condition++; _where += " and b.csn= '" + Request["txtCSN"].ToString().Trim() + "'"; } if (!string.IsNullOrEmpty(Request["txtEnabled"])) { condition++; _where += " and b.enabled= '" + Request["txtEnabled"].ToString().Trim() + "'"; } if (!string.IsNullOrEmpty(Request["txtStatus"])) { condition++; _where += " and b.status= '" + Request["txtStatus"].ToString().Trim() + "'"; } if (string.IsNullOrEmpty(Request["txtVIN"]) && string.IsNullOrEmpty(Request["txtCSN"]) && string.IsNullOrEmpty(Request["txtStatus"]) && string.IsNullOrEmpty(Request["txtEnabled"])) { condition++; _where += " and b.status= '90' "; } if (!string.IsNullOrEmpty(Request["txtStartTime"]) && !string.IsNullOrEmpty(Request["txtEndTime"])) { condition++; _where += " and a.create_ymd+' '+a.create_hms >= '" + Request["txtStartTime"].Trim() + "'"; _where += " and a.create_ymd+' '+a.create_hms <= '" + Request["txtEndTime"].Trim() + "'"; //_where += " and ediTime >= '" + Request["txtStartTime"].Trim() + "'"; //_where += " and ediTime <= '" + Request["txtEndTime"].Trim() + "'"; } if (condition == 0) { _where += " and a.create_ymd+' '+a.create_hms >= '" + starttime + "'"; _where += " and a.create_ymd+' '+a.create_hms <= '" + endtime + "'"; //_where += " and ediTime >= '" + starttime + "'"; //_where += " and ediTime <= '" + endtime + "'"; } return _where; } /// /// 获取列表数据 /// /// 菜单名称 /// 分页 /// 排序方式 /// 排序列 /// public ActionResult GetQuery( Pager pager, String direction, String sort) { _where = SetWhere(); Hashtable result = new Hashtable(); result.Add("pager.pageNo", pager.pageNo); Hashtable dataHt = this.service.GetQuery(_where, pager, direction, sort); result.Add("rows", dataHt["dataList"]); result.Add("pager.totalRows", dataHt["totalCount"]); result.Add("sort", sort); result.Add("direction", direction); return Json(result); } public ActionResult GetModelID() { Hashtable reault = new Hashtable(); ArrayList list = service.GetModelID(); reault.Add("list", list); return Json(reault, JsonRequestBehavior.AllowGet); } /// /// 编辑 /// /// /// public ActionResult edit(string csn) { DataTable DeptInfo = this.service.GetRow(" and b.csn='" + Request["csn"].ToString() + "'"); ViewData.Add("editType", "edit"); if (DeptInfo.Rows.Count > 0) { ViewData.Add("csn", DeptInfo.Rows[0]["csn"]); ViewData.Add("vin", DeptInfo.Rows[0]["vin"]); ViewData.Add("billId", DeptInfo.Rows[0]["billId"]); ViewData.Add("model_id", DeptInfo.Rows[0]["model_id"]); ViewData.Add("is_production", DeptInfo.Rows[0]["is_production"]); ViewData.Add("is_shipping", DeptInfo.Rows[0]["is_shipping"]); } return View("~/Views/YYEDIKeyDataDefine/EditEDIKey.aspx"); } /// /// 删除 /// /// /// public ActionResult delete(string csn) { int delCount = 0; try { delCount = this.service.DeleteDetail(csn); } catch (Exception e) { delCount = -1; } Hashtable result = new Hashtable(); result.Add("status", delCount); return Json(result); } /// /// 保存数据 /// /// public ActionResult saveDept() { String editType = Request["editType"].ToString(); String csn = Request["txtCsn"].ToString(); String vin = Request["txtVin"].ToString(); String billId = Request["txtBillid"].ToString(); String modelname = Request["cmbModelName"].ToString(); String isProduction = Request["is_production"].ToString(); String isShipping = Request["is_shipping"].ToString(); String message = ""; if (editType != null && editType.Trim().Equals("edit")) { try { this.service.UpdateKeyData(csn, vin, billId, modelname, isProduction, isShipping); message = "修改成功"; } catch (Exception e) { message = "修改失败!"; } } else { try { //if (service.GetEDICSN(csn)) //{ // message = "此CSN号数据已经存在!"; //} //else if (service.GetEDIVIN(vin)) //{ // message = "此VIN号数据已经存在!"; //} //else if (service.GetEDIBillId(billId)) //{ // message = "此排序单号已经存在!"; //} //else //{ this.service.insert(csn, vin, billId, modelname, isProduction, isShipping); message = "添加成功"; //} } catch (Exception e) { message = "添加失败!"; } } Hashtable result = new Hashtable(); result.Add("message", message); return Json(result); } /// /// 导出数据到Excel /// BY NOAH /// /// /// /// /// /// /// public ActionResult exportData(String txtVIN,String txtCsn,String txtStatus, String enabled_search, Pager pager, String sort, String direction, String isPage) { int totalCount = 0; _where = SetWhere(); DataTable dataHt = service.GetEDIData(_where, pager, ref totalCount); //DataTable dataHt = this.service.getTableListByPage(_where, enabled_search, pager, direction, sort, paging); HSSFWorkbook workbook = new HSSFWorkbook(); Stream outputStream = Response.OutputStream; HSSFSheet sheet = (HSSFSheet)workbook.CreateSheet("EDI信息数据"); try { if (workbook != null) { HSSFRow headRow = (HSSFRow)sheet.CreateRow(0); headRow.CreateCell(0).SetCellValue("csn"); headRow.CreateCell(1).SetCellValue("vin"); headRow.CreateCell(2).SetCellValue("排序单号"); headRow.CreateCell(3).SetCellValue("配置名称"); headRow.CreateCell(4).SetCellValue("是否生产"); headRow.CreateCell(5).SetCellValue("是否发运"); headRow.CreateCell(6).SetCellValue("备注"); } 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]["csn"].ToString()); dataRow.CreateCell(1).SetCellValue(dataHt.Rows[i]["vin"].ToString()); dataRow.CreateCell(2).SetCellValue(dataHt.Rows[i]["billid"].ToString()); dataRow.CreateCell(3).SetCellValue(dataHt.Rows[i]["model_name"].ToString()); dataRow.CreateCell(4).SetCellValue(dataHt.Rows[i]["is_production"].ToString()); dataRow.CreateCell(5).SetCellValue(dataHt.Rows[i]["is_shipping"].ToString()); dataRow.CreateCell(6).SetCellValue(dataHt.Rows[i]["remark"].ToString()); } Response.Clear(); workbook.Write(outputStream); Response.Buffer = true; Response.AppendHeader("Content-Disposition", "attachment;filename=EDI信息数据_" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ".xls"); Response.ContentEncoding = System.Text.Encoding.UTF8; Response.ContentType = "application/vnd.ms-excel"; Response.Flush(); } catch (Exception e) { } finally { workbook = null; } return null; } /// /// 导入Excel文件 /// /// public ActionResult importEDIKeyData() { Hashtable result = new Hashtable(); HttpPostedFileBase userDataFile = Request.Files[0]; UserInfo user = (UserInfo)Session["loginedUser"]; if (userDataFile == null) { return null; } result = service.ReadExcelFile(userDataFile.InputStream, user.empId); return Json(result); } } }