Ejemplo n.º 1
0
        public async Task <ActionResult <PrintTaskResponse> > PostPrintTask(PrintTaskRequest printTaskRequest)
        {
            //更新TodoItem
            string   taskId     = printTaskRequest.TaskId;
            string   fileName   = printTaskRequest.FileName;
            string   printName  = printTaskRequest.PrintName;
            string   printType  = printTaskRequest.PrintType;
            int      printCount = printTaskRequest.PrintCount;
            TodoItem todoItem   = _context.TodoItems.Find(printTaskRequest.TaskId);

            todoItem.PrintName             = printName;
            todoItem.PrintType             = printType;
            todoItem.PrintCount            = printCount;
            todoItem.FileName              = fileName;
            todoItem.IsComplete            = true;
            _context.Entry(todoItem).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
            _context.SaveChanges();

            bool   result = true;
            string msg    = "Success to print";

            if (printType == "Bartend")
            {
                //生成打印任务
                string printTask = printTaskRequest.GeneratePrintTask(taskId, _hostingEnvironment.WebRootPath, this.TemplateFolder, this.OutputFolder, fileName, this.BartendExePath, printName);
                LogHelper.WriteLog(printTask, new Exception("PostPrintTask"));
                //发送条码打印任务
                await Task.Run(() =>
                {
                    //需要执行Dos命令,
                    //举例:C:\PROGRA~2\Seagull\BARTEN~1\bartend.exe /XMLScript=E:\OA_HOME\Web\OA\AmwayFramework\PrintService.Webapi\wwwroot\PIMOutput\f3d12df1-8d85-4ece-ac24-607e675293eb_ontest.xml /X
                    //方法一,问题是部署到服务器上无法执行,所以采用方法二
                    DosCommandOutputHelper.Execute(printTask, 5000);
                    //方法二,使用System.Diagnostics.Process.Start执行
                    //string[] cmd = printTask.Split(" ");
                    //元素1 C:\PROGRA~2\Seagull\BARTEN~1\bartend.exe
                    //元素2 XMLScript=E:\OA_HOME\Web\OA\AmwayFramework\PrintService.Webapi\wwwroot\PIMOutput\f3d12df1-8d85-4ece-ac24-607e675293eb_ontest.xml
                    //元素3 /X
                    //string cmd2 = new StringBuilder(cmd[1]).Append(" ").Append(cmd.Length > 2 ? cmd[2] : "").ToString();
                    //var psi = new System.Diagnostics.ProcessStartInfo(cmd[0], cmd2);
                    //System.Diagnostics.Process.Start(psi);
                    //Thread.Sleep(5000);
                });

                //生成打印任务图片结果
                result = false;
                msg    = "Fail to print";
                if (printTaskRequest.ExportPrintPreviewToImage(taskId, _hostingEnvironment.WebRootPath, this.TemplateFolder, fileName, this.BartendExePath, printName) == true)
                {
                    result = true;
                    msg    = "Success to print";
                }
            }
            else if (printType == "Excel")
            {
                //根据模板和数据生成Excel文件
                if (!printTaskRequest.GeneratePrintExcel(taskId, _hostingEnvironment.WebRootPath, this.TemplateFolder_Excel, this.OutputFolder_Excel, fileName, out msg))
                {
                    result = false;
                }
                else
                {
                    //根据Excel文件生成Pdf文件
                    string pdfFileName = ExcelHelper.ExcelToPdf(msg);

                    //保存
                    Uri location = new Uri($"{Request.Scheme}://{Request.Host}");
                    todoItem.GenerateExcelFile     = msg;
                    todoItem.GeneratePdfFile       = $"{location.AbsoluteUri}{this.OutputFolder_Excel.Replace(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar)}/{pdfFileName}";
                    _context.Entry(todoItem).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                    _context.SaveChanges();
                }
            }

            //请求打印任务结果
            PrintTaskResponse printTaskResponse = new PrintTaskResponse {
                TaskId = taskId, Result = result, Msg = msg
            };

            return(printTaskResponse);
        }