Esempio n. 1
0
        public static void Process(DoubleSidePostCardCropInfo postCardProcessInfo, Action <DoubleSidePostCardCropInfo> success, Action <DoubleSidePostCardCropInfo> failure = null)
        {
            if (_postCardUploadWorker == null)
            {
                _postCardUploadWorker = new PostCardProcessQueue();
            }

            _postCardUploadWorker.Upload(new PostCardProcessContext
            {
                PostCardProcessInfo = postCardProcessInfo,
                Success             = success,
                Failure             = failure
            });
        }
        private void PostCardChanged()
        {
            if (PostCardView.GetFocusedRow() is PostCardInfo postCardInfo)
            {
                // 还原当前选中双面裁切信息
                _doubleSidePostCardCropInfo   = null;
                progressBarControl1.EditValue = 0;
                //                cropContext.CropInfoSubmitDto = new CropInfoSubmitDto(cropContext.Image.Size, cropContext.PicturePrintAreaSize, fit: cropContext.StyleInfo.Fit);
                //                        pictureCropControl1.CropContext = cropContext;

                if (elementHost1.Child is Photocroper photoCropper)
                {
                    photoCropper.ProductSize = new Size(postCardInfo.ProductSize.Width, postCardInfo.ProductSize.Height);
                    photoCropper.FrontStyle  = postCardInfo.FrontStyle;
                    if (string.IsNullOrEmpty(postCardInfo.ProductFileId))
                    {
                        photoCropper.Preview = false;

                        //FileApi.GetInstance().
                        photoCropper.InitImage(Settings.Default.Host + "/file/" + postCardInfo.FileId, tempCropInfo =>
                        {
                            switch (postCardInfo.FrontStyle)
                            {
                            // D版,直接缩放
                            case "D":
                                tempCropInfo.CropHeight = 1;
                                tempCropInfo.CropWidth  = 1;
                                tempCropInfo.CropTop    = 0;
                                tempCropInfo.CropLeft   = 0;
                                if (postCardInfo.ProcessStatusText == "未提交")
                                {
                                    timer1.Interval = 1000;
                                    timer1.Start();
                                }

                                break;

                            // C版,向左旋转
                            case "C":
                                // 向左旋转
                                photoCropper.LeftRotate();
                                // 最大化
                                photoCropper.FixMax();
                                break;

                            default:
                                // A版、B版:判断是否自动旋转
                                if (barCheckItem1.Checked &&
                                    postCardInfo.ProcessStatusText == "未提交" &&
                                    Math.Abs(tempCropInfo.CropWidth - 1) <= 0.001 &&
                                    Math.Abs(tempCropInfo.CropHeight - 1) <= 0.001)
                                {
                                    tempCropInfo.CropHeight = 1;
                                    tempCropInfo.CropWidth  = 1;
                                    tempCropInfo.CropTop    = 0;
                                    tempCropInfo.CropLeft   = 0;
                                    timer1.Interval         = 10;
                                    //开始定时器,自动提交反面
                                    timer1.Start();
                                    // 提交
                                }

                                break;
                            }
                        });
                    }
                    else
                    {
                        //FileApi.GetInstance().
                        photoCropper.Preview    = true;
                        photoCropper.FrontStyle = "B";
                        photoCropper.InitImage(Settings.Default.Host + "/file/" + postCardInfo.ProductFileId);
                    }
                }
            }
        }
        private void ProcessFront(DoubleSidePostCardCropInfo postCardProcessCropInfo)
        {
            Log(@"开始处理明信片[" + postCardProcessCropInfo.PostCardId + "]");
            // 更新状态为正在处理中
            PostCardItemApi.UpdatePostCardProcessStatus(postCardProcessCropInfo.PostCardId, "PROCESSING");


            // 创建临时目录
            if (!Directory.Exists(Path.GetTempPath() + "/PostCardCrop/"))
            {
                Directory.CreateDirectory(Path.GetTempPath() + "/PostCardCrop/");
            }

            var isWait = true;

            try
            {
                var resultFileInfo = new PostCardItemProductFileSubmitRequest();


                // 正面文件
                Log(@"开始下载正面文件");

                FileApi.DownloadFileByFileIdAsync(postCardProcessCropInfo.FrontCropCropInfo.FileId, new FileInfo(Path.GetTempPath() + "/PostCardCrop/" + Guid.NewGuid() + ".jpg"), frontFileInfo =>
                {
                    Log(@"文件下载成功,开始处理正面文件");
                    //========================================================================================== 处理正面 ==========================================================================================
                    var frontProductFile = frontFileInfo.Process(postCardProcessCropInfo.FrontCropCropInfo, postCardProcessCropInfo.PostCardType, postCardProcessCropInfo.ProductWidth, postCardProcessCropInfo.ProductHeight);
                    if (frontProductFile == null)
                    {
                        Log(@"正面文件处理失败");
                        PostCardItemApi.UpdatePostCardProcessStatus(postCardProcessCropInfo.PostCardId, "PROCESS_FAILURE", null);
                        isWait = false;
                        // 流程结束
                        return;
                    }

                    Log(@"正面文件处理完成");
                    Log(@"开始上传正面成品文件");
                    var frontFileUploadResponse       = frontProductFile.UploadFile("明信片正面成品");
                    resultFileInfo.FrontProductFileId = frontFileUploadResponse.Id;
                    Log(@"正面成品文件上传成功");

                    try
                    {
                        Log(@"开始删除正面文件");
                        // 删除文件
                        frontFileInfo.Delete();
                        // 删除文件
                        frontProductFile.Delete();
                        Log(@"正面文件删除成功");
                    }
                    catch (Exception e)
                    {
                        LogInfo.Error(e.Message, e);
                        Log(@"正面文件删除失败");
                    }

                    //========================================================================================== 没有反面,不用裁切 ==========================================================================================
                    // 有反面裁切
                    if (postCardProcessCropInfo.BackCropCropInfo == null)
                    {
                        Log(@"开始提交成品ID");
                        PostCardItemApi.SubmitPostCardProductFile(
                            postCardProcessCropInfo.PostCardId,
                            resultFileInfo,
                            k =>
                        {
                            Log(@"成品ID提交成功");
                            PostCardItemApi.UpdatePostCardProcessStatus(postCardProcessCropInfo.PostCardId, "AFTER_PROCESS", null);
                            isWait = false;
                        },
                            m =>
                        {
                            Log(@"成品ID提交失败");
                            PostCardItemApi.UpdatePostCardProcessStatus(postCardProcessCropInfo.PostCardId, "PROCESS_FAILURE", null);
                            isWait = false;
                        });
                    }
                    //========================================================================================== 有反面,但是不需要裁切 ==========================================================================================
                    else
                    {
                        var backCropInfo = postCardProcessCropInfo.BackCropCropInfo;
                        if (
                            string.IsNullOrEmpty(backCropInfo.FileId) ||
                            (
                                Math.Abs(backCropInfo.CropLeft) < 0.001 &&
                                Math.Abs(backCropInfo.CropTop) < 0.001 &&
                                Math.Abs(backCropInfo.CropWidth - 1) < 0.001 &&
                                Math.Abs(backCropInfo.CropHeight - 1) < 0.001)
                            )
                        {
                            Log(@"反面为标准尺寸,不需要裁切!");
                            resultFileInfo.BackProductFileId = backCropInfo.FileId;
                            Log(@"开始提交成品ID");
                            //========================================================================================== 提交 ==========================================================================================
                            PostCardItemApi.SubmitPostCardProductFile(
                                postCardProcessCropInfo.PostCardId,
                                resultFileInfo,
                                k =>
                            {
                                Log(@"成品ID提交成功");
                                PostCardItemApi.UpdatePostCardProcessStatus(postCardProcessCropInfo.PostCardId, "AFTER_PROCESS", null);
                                isWait = false;
                            },
                                m =>
                            {
                                Log(@"成品ID提交失败");
                                PostCardItemApi.UpdatePostCardProcessStatus(postCardProcessCropInfo.PostCardId, "PROCESS_FAILURE", null);
                                isWait = false;
                            });
                        }
                        //========================================================================================== 反面需要裁切 ==========================================================================================
                        else
                        {
                            Log(@"开始下载反面文件");

                            FileApi.DownloadFileByFileIdAsync(postCardProcessCropInfo.BackCropCropInfo.FileId, new FileInfo(Path.GetTempPath() + "/PostCardCrop/" + Guid.NewGuid() + ".jpg"), backFileInfo =>
                            {
                                Log(@"反面文件下载完成");
                                Log(@"开始处理反面文件");
                                var backProductFile = backFileInfo.Process(postCardProcessCropInfo.BackCropCropInfo, "B", postCardProcessCropInfo.ProductWidth, postCardProcessCropInfo.ProductHeight);
                                Log(@"反面文件处理完成");
                                Log(@"开始上传反面成品文件");
                                var backFileUploadResponse       = backProductFile.UploadFile("明信片正面成品");
                                resultFileInfo.BackProductFileId = backFileUploadResponse.Id;
                                Log(@"反面成品文件上传成功");
                                Log(@"开始删除反面文件");
                                try
                                {
                                    // 删除文件
                                    backFileInfo.Delete();
                                    // 删除文件
                                    backProductFile.Delete();
                                    Log(@"反面文件删除成功");
                                }
                                catch (Exception e)
                                {
                                    LogInfo.Error(e.Message, e);
                                    Log(@"反面文件删除失败");
                                }

                                Log(@"开始提交成品ID");
                                //========================================================================================== 提交 ==========================================================================================
                                PostCardItemApi.SubmitPostCardProductFile(
                                    postCardProcessCropInfo.PostCardId,
                                    resultFileInfo,
                                    k =>
                                {
                                    Log(@"成品ID提交成功");
                                    PostCardItemApi.UpdatePostCardProcessStatus(postCardProcessCropInfo.PostCardId, "AFTER_PROCESS", null);
                                    isWait = false;
                                },
                                    m =>
                                {
                                    Log(@"成品ID提交失败");
                                    PostCardItemApi.UpdatePostCardProcessStatus(postCardProcessCropInfo.PostCardId, "PROCESS_FAILURE", null);
                                    isWait = false;
                                });
                                //========================================================================================== 反面下载失败 ==========================================================================================
                            }, message =>
                            {
                                PostCardItemApi.UpdatePostCardProcessStatus(postCardProcessCropInfo.PostCardId, "PROCESS_FAILURE", null);
                                isWait = false;
                            });
                        }
                    }
                }, failure =>
                {
                    //========================================================================================== 正面下载失败 ==========================================================================================
                    Log("正面文件下载失败");
                    PostCardItemApi.UpdatePostCardProcessStatus(postCardProcessCropInfo.PostCardId, "PROCESS_FAILURE", null);
                    isWait = false;
                });
            }
            catch (Exception e)
            {
                Log(e.Message);
                PostCardItemApi.UpdatePostCardProcessStatus(postCardProcessCropInfo.PostCardId, "PROCESS_FAILURE", null);
                isWait = false;
            }

            while (isWait)
            {
                Application.DoEvents();
            }
        }
        private void SubmitPostCard(int rowHandler, CropInfo cropInfo)
        {
            if (_currentEnvelopeInfo == null)
            {
                return;
            }

            if (PostCardView.GetRow(rowHandler) is PostCardInfo postCardInfo)
            {
                // 如果当前提交doubleSide提交为null,创建一个(第一次提交为 null
                if (_doubleSidePostCardCropInfo == null)
                {
                    _doubleSidePostCardCropInfo = new DoubleSidePostCardCropInfo
                    {
                        PostCardId        = postCardInfo.PostCardId,         // ID,用于提交数据
                        ProductHeight     = postCardInfo.ProductSize.Height, // 成品高度,用于确定成品尺寸
                        ProductWidth      = postCardInfo.ProductSize.Width,  // 成品宽度,用于确定成品尺寸
                        PostCardType      = postCardInfo.FrontStyle,         // 正面样式,A B C D,用于确定板式
                        FrontCropCropInfo = new PostCardProcessCropInfo      // 正面裁切信息
                        {
                            FileId      = postCardInfo.FileId,
                            CropLeft    = cropInfo.CropLeft,
                            CropTop     = cropInfo.CropTop,
                            CropHeight  = cropInfo.CropHeight,
                            CropWidth   = cropInfo.CropWidth,
                            Rotation    = cropInfo.Rotation,
                            ImageWidth  = cropInfo.ImageWidth,
                            ImageHeight = cropInfo.ImageHeight
                        }
                    };
                    // 如果是双面,裁切双面
                    if (!string.IsNullOrEmpty(postCardInfo.BackFileId))
                    {
                        // 肯定是的,设置为反面
                        if (elementHost1.Child is Photocroper photoCropper)
                        {
                            photoCropper.ProductSize = new Size(postCardInfo.ProductSize.Width, postCardInfo.ProductSize.Height);
                            photoCropper.FrontStyle  = "B";
                            photoCropper.Preview     = false;
                            photoCropper.InitImage(Settings.Default.Host + "/file/" + postCardInfo.BackFileId, action: tempCropInfo =>
                            {
                                if (Math.Abs(tempCropInfo.CropWidth - 1) <= 0.001 && Math.Abs(tempCropInfo.CropHeight - 1) <= 0.001)
                                {
                                    tempCropInfo.CropHeight = 1;
                                    tempCropInfo.CropWidth  = 1;
                                    tempCropInfo.CropTop    = 0;
                                    tempCropInfo.CropLeft   = 0;
                                    timer1.Interval         = 100;
                                    //开始定时器,自动提交反面
                                    timer1.Start();
                                    // 提交
                                }
                            });
                            return;
                        }
                    }
                }
                else
                {
                    // 如果有反面文件ID,提交反面数据,否则不提交
                    if (!string.IsNullOrEmpty(postCardInfo.BackFileId))
                    {
                        _doubleSidePostCardCropInfo.BackCropCropInfo = new PostCardProcessCropInfo
                        {
                            FileId      = postCardInfo.BackFileId,
                            CropLeft    = cropInfo.CropLeft,   // 反面左边
                            CropTop     = cropInfo.CropTop,    // 反面右边
                            CropHeight  = cropInfo.CropHeight, // 反面高度
                            CropWidth   = cropInfo.CropWidth,  // 反面宽度
                            Rotation    = cropInfo.Rotation,   // 反面旋转角度
                            ImageWidth  = cropInfo.ImageWidth, // 原始图像宽度
                            ImageHeight = cropInfo.ImageHeight // 原始图像高度
                        }
                    }
                    ;
                }

                postCardInfo.ProcessStatusText = "已提交";

                PostCardProcessQueue.Process(_doubleSidePostCardCropInfo,
                                             info => { PostCardItemApi.UpdatePostCardProcessStatus(postCardInfo.PostCardId, "AFTER_SUBMIT"); },
                                             info => { PostCardItemApi.UpdatePostCardProcessStatus(postCardInfo.PostCardId, "SUBMIT_FAILURE"); });
            }

            MoveToNextPosition(rowHandler);
        }