报表-条码未扫查询Controller

using Newtonsoft.Json;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;

namespace Midea.Web.Areas.MES.Controllers
{
    [Authorize]
    public class MoUnScannedBarcodeController : BaseController
    {
        [Permission("A0A9C60518F94422BB86BA471223DCB6", "未扫条码查询", "未扫条码查询")]
        public ActionResult Index()
        {
            List<SelectListItem> MO_Status = new List<SelectListItem>();
            MO_Status.Add(new SelectListItem() { Text = "请选择作业状态", Value = "" });
            MO_Status.Add(new SelectListItem() { Text = "已终检未下线", Value = "Online" });
            MO_Status.Add(new SelectListItem() { Text = "已下线未入库", Value = "Offline" });

            ViewData.Add("MO_Status", MO_Status);



        


            return View();
        }
        #region 原有方式

        public ActionResult LoadData_old(string moCodeStr, string beginL, string beginR,
            string mitemDesc, string workShopStr, string lineCodeStr, string mo_status, string mostatus)
        {
            int start = Convert.ToInt32(Request.Params["start"]);//起始数
            int rowsize = Convert.ToInt32(Request.Params["length"]);  //长度
            int draw = Convert.ToInt32(Request.Params["draw"]);//画的页数
            int pageindex = (start / rowsize) + 1;

            beginL = string.IsNullOrWhiteSpace(beginL) ? string.Empty : beginL.Trim();
            beginR = string.IsNullOrWhiteSpace(beginR) ? string.Empty : beginR.Trim();

            var data = MESDomain.MES.MoUnScannedBarcode.QueryUnScannedBarcode(
                moCodeStr,
                beginL == string.Empty ? null : new Nullable<DateTime>(DateTime.Parse(beginL)),
                beginR == string.Empty ? null : new Nullable<DateTime>(DateTime.Parse(beginR)),
                mitemDesc, workShopStr, lineCodeStr
                , CurUserDefaultInvOrgID, mo_status, mostatus,pageindex, rowsize);

            return Json(new DataTablesResultInfo(data.Items, draw, data.ItemCount, data.ItemCount), JsonRequestBehavior.AllowGet);
        }

        #region 导出
        public EmptyResult ExportData_old(string postjson)
        {
            //如果查询条件为空
            if (postjson == "")
            {
                postjson = Request.QueryString["postjson"];
            }
            //获取查询条件并反序列化json字符
            paramModel model = Newtonsoft.Json.JsonConvert.DeserializeObject<paramModel>(postjson);

            int totalrow = 0;
            int pageIndex = 1;
            int pageSize = 65535;
            var result = MESDomain.MES.MoUnScannedBarcode.QueryUnScannedBarcode(
                model.MOCODE,
                model.BEGINDATE == string.Empty ? null : new Nullable<DateTime>(DateTime.Parse(model.BEGINDATE)),
                model.ENDDATE == string.Empty ? null : new Nullable<DateTime>(DateTime.Parse(model.ENDDATE)),
                model.MITEMDESC, model.WORKSHOP, model.LINECODE
                , CurUserDefaultInvOrgID, model.MOSTATUS,model.MOstaus2, pageIndex, pageSize);


            var workbook = new XSSFWorkbook();
            var ms = new MemoryStream();
            int iCurrentRowIndex = 0;

            //时间格式
            var dateTimeCellStyle = workbook.CreateCellStyle();
            dateTimeCellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("yyyy-MM-dd HH:mm:ss");

            // 新增页。 
            ISheet sheet = workbook.CreateSheet("未扫条码查询");
            //报表列头
            IRow rowTableHeader = sheet.CreateRow(iCurrentRowIndex);

            rowTableHeader.CreateCell(0).SetCellValue("未扫描条码");
            rowTableHeader.CreateCell(1).SetCellValue("作业名称");
            rowTableHeader.CreateCell(2).SetCellValue("车间代码");
            rowTableHeader.CreateCell(3).SetCellValue("生产线代码");
            rowTableHeader.CreateCell(4).SetCellValue("当前工序");
            rowTableHeader.CreateCell(5).SetCellValue("当前工序描述");
            rowTableHeader.CreateCell(6).SetCellValue("装配件描述");
            rowTableHeader.CreateCell(7).SetCellValue("维护人员");
            rowTableHeader.CreateCell(8).SetCellValue("维护日期");
            rowTableHeader.CreateCell(9).SetCellValue("维护时间");
            iCurrentRowIndex++;

            foreach (var item in result.Items)
            {
                IRow currentRow = sheet.CreateRow(iCurrentRowIndex);

                currentRow.CreateCell(0).SetCellValue(item.BarcodeCode);
                currentRow.CreateCell(1).SetCellValue(item.MoName);
                currentRow.CreateCell(2).SetCellValue(item.WorkshopCode);
                currentRow.CreateCell(3).SetCellValue(item.LineCode);
                currentRow.CreateCell(4).SetCellValue(item.ProcessName);
                currentRow.CreateCell(5).SetCellValue(item.ProcessDesc);
                currentRow.CreateCell(6).SetCellValue(item.MaterialDesc);
                currentRow.CreateCell(7).SetCellValue(item.Maintainer);
                currentRow.CreateCell(8).SetCellValue(item.MaintenanceDate);
                currentRow.CreateCell(9).SetCellValue(item.MaintenanceTime);
                iCurrentRowIndex++;

            }

            workbook.Write(ms);
            Response.AddHeader("Content-Disposition",
             string.Format("attachment; filename={0}_{1}.xls",
                 Server.UrlEncode("作业未扫条码查询"),
                 DateTime.Now.ToString("yyyyMMddHHmmss")
             ));
            Response.BinaryWrite(ms.ToArray());
            return new EmptyResult();
        }
        #endregion
        #endregion

        class paramModel
        {
            public string MOCODE { get; set; }

            public string BEGINDATE { get; set; }
            public string ENDDATE { get; set; }
            public string MITEMDESC { get; set; }
            public string WORKSHOP { get; set; }
            public string LINECODE { get; set; }
            public string MOSTATUS { get; set; }

            public string MOstaus2 { get; set; }

        }

        /// <summary>
        /// 时间间隔大于三个月校验
        /// </summary>
        /// <param name="StartDate"></param>
        /// <param name="EndDate"></param>
        /// <returns></returns>
        public ActionResult UnScannedDateTimeValidate(Nullable<DateTime> StartDate = null, Nullable<DateTime> EndDate = null)
        {
            DateTime StartDateTime = StartDate == null ? DateTime.Now : Convert.ToDateTime(StartDate);
            DateTime EndDateTime = EndDate == null ? DateTime.Now : Convert.ToDateTime(EndDate);
            if (StartDateTime.AddMonths(3) < EndDateTime)
            {
                return Json(new JsonResultInfo(JsonResultInfo.FLAG_ERROR, null));
            }
            return Json(new JsonResultInfo(JsonResultInfo.FLAG_SUCCESS, null, ""));
        }
        #region 包调用方式
        public ActionResult LoadData(string moCodeStr, string beginL, string beginR,
            string mitemDesc, string workShopStr, string lineCodeStr, string mo_status, string mostatus)
        {

            int start = Convert.ToInt32(Request.Params["start"]);//起始数
            int rowsize = Convert.ToInt32(Request.Params["length"]);  //长度
            int draw = Convert.ToInt32(Request.Params["draw"]);//画的页数
            int pageindex = (start / rowsize) + 1;
            int page_total = 0;
            beginL = string.IsNullOrWhiteSpace(beginL) ? string.Empty : beginL.Trim();
            beginR = string.IsNullOrWhiteSpace(beginR) ? string.Empty : beginR.Trim();
            DateTime? begindate = null;
            DateTime? enddate = null;
            if (beginL != string.Empty)
            {
                begindate = DateTime.Parse(beginL);
            }
            if (beginR != string.Empty)
            {
                enddate = DateTime.Parse(beginR);
            }

            ICollection<VW_SFC_MO_UNSCANNED_BARCODE> data = null;
            data = MESDomain.MES.MoUnScannedBarcode.QueryUnScannedBarcodeData(moCodeStr, begindate, enddate, mitemDesc,
                workShopStr, lineCodeStr, CurUserDefaultInvOrgID, mo_status,mostatus, pageindex, rowsize, out page_total);

            var json = from q in data
                       select new
                       {
                            BarcodeCode=q.BARCODE,
                            MoName = q.MO_NAME,
                            WorkshopCode = q.WORKSHOP_NAME,
                            LineCode = q.LINE_NAME,
                            ProcessName = q.PROCESS_CODE,
                            ProcessDesc = q.PROCESS_DESC,
                            MaterialDesc = q.MITEM_DESC,
                            Maintainer = q.MAINTAINER,
                            MaintenanceDate = q.MAINTENANCE_DATETIME == null ? string.Empty : q.MAINTENANCE_DATETIME.Value.ToString("yyyy-MM-dd"),
                            MaintenanceTime =q.MAINTENANCE_DATETIME==null?string.Empty:q.MAINTENANCE_DATETIME.Value.ToString("HH:mm:ss")
                       
                       };
            return Json(new DataTablesResultInfo(json, draw, page_total, page_total), JsonRequestBehavior.AllowGet); 
        }

        public EmptyResult ExportData(string postjson)
        {
            //如果查询条件为空
            if (postjson == "")
            {
                postjson = Request.QueryString["postjson"];
            }
            //获取查询条件并反序列化json字符
            paramModel model = Newtonsoft.Json.JsonConvert.DeserializeObject<paramModel>(postjson);

            int totalrow = 0;
            int pageIndex = 1;
            int pageSize = 165535;

 
            DateTime? begindate = null;
            DateTime? enddate = null;
            if (!string.IsNullOrWhiteSpace(model.BEGINDATE))
            {
                begindate = DateTime.Parse(model.BEGINDATE);
            }
            if (!string.IsNullOrWhiteSpace( model.ENDDATE))
            {
                enddate = DateTime.Parse(model.ENDDATE);
            }
            ICollection<VW_SFC_MO_UNSCANNED_BARCODE> data = null;
            data = MESDomain.MES.MoUnScannedBarcode.QueryUnScannedBarcodeData(
                model.MOCODE,
                begindate,
                enddate,
                model.MITEMDESC, model.WORKSHOP, model.LINECODE
                , CurUserDefaultInvOrgID, model.MOSTATUS,model.MOstaus2, pageIndex, pageSize, out totalrow);


            var json = from q in data
                       select new
                       {
                           BarcodeCode = q.BARCODE,
                           MoName = q.MO_NAME,
                           WorkshopCode = q.WORKSHOP_NAME,
                           LineCode = q.LINE_NAME,
                           ProcessName = q.PROCESS_CODE,

                           ProcessDesc = q.PROCESS_DESC,
                           MaterialDesc = q.MITEM_DESC,
                           Maintainer = q.MAINTAINER,
                           MaintenanceDate = q.MAINTENANCE_DATETIME == null ? string.Empty : q.MAINTENANCE_DATETIME.Value.ToString("yyyy-MM-dd"),
                           MaintenanceTime = q.MAINTENANCE_DATETIME == null ? string.Empty : q.MAINTENANCE_DATETIME.Value.ToString("HH:mm:ss")

                       };

            var workbook = new XSSFWorkbook();
            var ms = new MemoryStream();
            int iCurrentRowIndex = 0;

            //时间格式
            var dateTimeCellStyle = workbook.CreateCellStyle();
            dateTimeCellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("yyyy-MM-dd HH:mm:ss");

            // 新增页。 
            ISheet sheet = workbook.CreateSheet("作业未扫条码查询");
            //报表列头
            IRow rowTableHeader = sheet.CreateRow(iCurrentRowIndex);

            rowTableHeader.CreateCell(0).SetCellValue("未扫描条码");
            rowTableHeader.CreateCell(1).SetCellValue("作业名称");
            rowTableHeader.CreateCell(2).SetCellValue("车间代码");
            rowTableHeader.CreateCell(3).SetCellValue("生产线代码");
            rowTableHeader.CreateCell(4).SetCellValue("当前工序");
            rowTableHeader.CreateCell(5).SetCellValue("当前工序描述");
            rowTableHeader.CreateCell(6).SetCellValue("装配件描述");
            rowTableHeader.CreateCell(7).SetCellValue("维护人员");
            rowTableHeader.CreateCell(8).SetCellValue("维护日期");
            rowTableHeader.CreateCell(9).SetCellValue("维护时间");
            iCurrentRowIndex++;

            foreach (var item in json)
            {
                IRow currentRow = sheet.CreateRow(iCurrentRowIndex);

                currentRow.CreateCell(0).SetCellValue(item.BarcodeCode);
                currentRow.CreateCell(1).SetCellValue(item.MoName);
                currentRow.CreateCell(2).SetCellValue(item.WorkshopCode);
                currentRow.CreateCell(3).SetCellValue(item.LineCode);
                currentRow.CreateCell(4).SetCellValue(item.ProcessName);
                currentRow.CreateCell(5).SetCellValue(item.ProcessDesc);
                currentRow.CreateCell(6).SetCellValue(item.MaterialDesc);
                currentRow.CreateCell(7).SetCellValue(item.Maintainer);
                currentRow.CreateCell(8).SetCellValue(item.MaintenanceDate);
                currentRow.CreateCell(9).SetCellValue(item.MaintenanceTime);
                iCurrentRowIndex++;

            }

            workbook.Write(ms);
            Response.AddHeader("Content-Disposition",
             string.Format("attachment; filename={0}_{1}.xls",
                 Server.UrlEncode("作业未扫条码查询"),
                 DateTime.Now.ToString("yyyyMMddHHmmss")
             ));
            Response.BinaryWrite(ms.ToArray());
            return new EmptyResult();
        }

        #endregion

    }
}


你可能感兴趣的:(public,条码)