private void InitData(string orderId)
        {
            PostCardCollectionApi.GetAllEnvelopeByOrderId(orderId, envelopeList =>
            {
                var envelopeInfos = new List <EnvelopeInfo>();

                envelopeList.ForEach(tmpEnvelope =>
                {
                    var envelope = tmpEnvelope.TranslateToEnvelope();
                    envelopeInfos.Add(envelope);
                    if (envelopeInfos.Count == 1)
                    {
                        postCardControl.DataSource = envelope.PostCards;
                    }

                    PostCardItemApi.GetPostCardByEnvelopeId(envelope.Id, postCards =>
                    {
                        if (postCards == null)
                        {
                            return;
                        }
                        //遍历明信片,对明信片进行Node绑定
                        foreach (var postCard1 in postCards)
                        {
                            var tmpPostCard = postCard1.TranlateToPostCard();
                            envelope.PostCards.Add(tmpPostCard);
                            tmpPostCard.ProductSize = envelope.ProductSize;
                        }

                        postCardControl.RefreshDataSource();
                    }, message => { XtraMessageBox.Show(message); });
                });
                envelopeListControl.DataSource = envelopeInfos;
            }, kk => { XtraMessageBox.Show(kk); });
        }
        private static IEnumerable <PostCardResponse> PreparePostCardFromServer(EnvelopeResponse envelopeResponse, Action <double> processHandler = null, Action <string> onError = null)
        {
            var postCardResponses = new List <PostCardResponse>();
            var isWait            = true;

            PostCardItemApi.GetPostCardByEnvelopeId(envelopeResponse.EnvelopeId, postCardList =>
            {
                for (var i1 = 0; i1 < postCardList.Count; i1++)
                {
                    processHandler?.Invoke(i1 / (double)postCardList.Count);
                    var postCard = postCardList[i1];
                    for (var i = 0; i < postCard.Copy; i++)
                    {
                        postCardResponses.Add(postCard);
                    }
                }

                isWait = false;
            }, message =>
            {
                onError?.Invoke(message);
                isWait = false;
            });
            while (isWait)
            {
                Application.DoEvents();
                Thread.Sleep(100);
            }

            processHandler?.Invoke(1);
            return(postCardResponses);
        }
 private void ReCropPostCard(PostCardInfo postCardInfo)
 {
     PostCardItemApi.ReCropPostCard(
         postCardInfo.PostCardId,
         response =>
     {
         postCardInfo.ProductFileId     = response.ProductFileId;
         postCardInfo.BackProductFileId = response.BackProductFileId;
         postCardInfo.ProcessStatusText = response.ProcessStatusText;
         PostCardChanged();
     });
 }
 /// <summary>
 ///     修改明信片正面版式
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void BarButtonItem3_ItemClick(object sender, ItemClickEventArgs e)
 {
     if (PostCardView.GetFocusedRow() is PostCardInfo postCard)
     {
         PostCardItemApi.ChangePostCardFrontStyle(postCard.PostCardId, e.Item.Tag as string, resp =>
         {
             var postCardInfo = resp.TranlateToPostCard();
             // postCard.ProcessStatus = postCardInfo.ProcessStatus;
             postCard.ProductFileId     = postCardInfo.ProductFileId;
             postCard.FrontStyle        = postCardInfo.FrontStyle;
             postCard.ProcessStatusText = postCardInfo.ProcessStatusText;
             PostCardChanged();
         }, message => { XtraMessageBox.Show(message); });
     }
 }
        private void ChangeFrontStyle(string style)
        {
            if (!(PostCardView.GetFocusedRow() is PostCardInfo postCard))
            {
                return;
            }

            PostCardItemApi.ChangePostCardFrontStyle(postCard.PostCardId, style, resp =>
            {
                var postCardInfo = resp.TranlateToPostCard();
                // postCard.ProcessStatus = postCardInfo.ProcessStatus;
                postCard.ProductFileId     = postCardInfo.ProductFileId;
                postCard.FrontStyle        = postCardInfo.FrontStyle;
                postCard.ProcessStatusText = postCardInfo.ProcessStatusText;
                PostCardChanged();
            }, message => { XtraMessageBox.Show(message); });
        }
        private void Timer2_Tick(object sender, EventArgs e)
        {
            if (!(EnvelopeView.GetFocusedRow() is EnvelopeInfo envelopeInfo))
            {
                return;
            }

            if (!(postCardControl.DataSource is List <PostCardInfo> postCardResponses))
            {
                return;
            }

            // 如果所有的都处理完成了
            if (postCardResponses.All(k => k.ProcessStatusText == "处理完成"))
            {
                barButtonItem2.Enabled = true;
                return;
            }

            barButtonItem2.Enabled = false;

            PostCardItemApi.GetPostCardByEnvelopeId(envelopeInfo.Id,
                                                    result =>
            {
                var dictionary = result.ToDictionary(postCardResponse => postCardResponse.Id);

                foreach (var postCardResponse in postCardResponses)
                {
                    if (!dictionary.ContainsKey(postCardResponse.PostCardId))
                    {
                        continue;
                    }

                    var cardResponse = dictionary[postCardResponse.PostCardId];
                    // 处理状态
                    postCardResponse.ProcessStatusText = cardResponse.ProcessStatusText;
                    // 成品文件ID
                    postCardResponse.ProductFileId = cardResponse.ProductFileId;
                    // 反面成品文件ID
                    postCardResponse.BackProductFileId = cardResponse.BackProductFileId;
                }

                postCardControl.RefreshDataSource();
            }
                                                    );
        }
        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 ExportForm_Load(object sender, EventArgs e)
        {
            PostCardCollectionApi.GetEnvelopeInfoById(_postCardCollectionId, envelopeResponse =>
            {
                var waterMarkContext = new WaterMarkContext
                {
                    PaperName  = envelopeResponse.PaperName,     // 纸张名称
                    Width      = envelopeResponse.ProductWidth,  // 纸张宽度
                    Height     = envelopeResponse.ProductHeight, // 纸张高度
                    BackType   = envelopeResponse.BackStyle,     // 反面样式
                    DoubleSide = envelopeResponse.DoubleSide     // 双面还是单面
                };


                textEditPaperName.Text     = envelopeResponse.PaperName;
                textEditProductWidth.Text  = envelopeResponse.ProductWidth.ToString();
                textEditProductHeight.Text = envelopeResponse.ProductHeight.ToString();
                _envelopeResponse          = envelopeResponse;


                PostCardBillApi.GetOrderInfo(_envelopeResponse.OrderId, orderResponse =>
                {
                    // 用户ID
                    waterMarkContext.UserName = orderResponse.TaobaoId;
                    // 订单是否加急
                    waterMarkContext.Urgent   = orderResponse.Urgent;
                    textEditCustomerName.Text = orderResponse.TaobaoId;
                    _orderResponse            = orderResponse;
                    PostCardItemApi.GetPostCardByEnvelopeId(
                        _postCardCollectionId,
                        postCardResponseList =>
                    {
                        _postCardResponses = postCardResponseList;


                        for (var index = 0; index < _postCardResponses.Count; index++)
                        {
                            var postCardResponse = _postCardResponses[index];
                            // 第一张,默认取正面样式
                            if (index == 0)
                            {
                                waterMarkContext.FrontType = postCardResponse.FrontStyle;     // 正面样式
                            }

                            if (waterMarkContext.FrontType != postCardResponse.FrontStyle)
                            {
                                waterMarkContext.FrontType = "";     // 多种正面样式
                            }

                            if (!string.IsNullOrEmpty(postCardResponse.BackStyle))
                            {
                                waterMarkContext.BackType = postCardResponse.BackStyle;     //反面样式
                            }

                            waterMarkContext.Count += postCardResponse.Copy;     // 张数累加
                        }

                        textEditFinalMark.Text     = waterMarkContext.GenerateWaterMarkString();
                        textEditFinalFileName.Text = waterMarkContext.GenerateFileName();
                        simpleButton1.Enabled      = true;
                    },
                        postCardListFailMessage => { XtraMessageBox.Show("获取明信片列表发生错误,请检查网络!"); });
                }, message => { XtraMessageBox.Show("获取明信片订单失败,请检查网络!"); });
            }, failure => { XtraMessageBox.Show("获取明信片集合失败,请检查网络!"); });
        }
        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);
        }