lixiaojun
2024-08-15 24f31cf1f11318a0e76d31d382313d30c0ad9e1d
WinFrmUI/Yw.WinFrmUI.Bimface.Core/02-file/BimfaceFileMgrPage.cs
@@ -1,6 +1,4 @@
using DevExpress.CodeParser;
using Mapster;
using Yw.BIMFace;
using Mapster;
namespace Yw.WinFrmUI
{
@@ -13,10 +11,12 @@
            this.gridView1.RegistCustomDrawRowIndicator();
            this.PageTitle.Caption = "模型文件管理";
            this.PageTitle.HeaderSvgImage = this.svgImage32[1];
            this.PageTitle.SvgImageSize = new Size(24, 24);
        }
        private List<BimfaceFileViewModel> _allBindingList = null;
        private List<BimfaceFileMgrViewModel> _allBindingList = null;
        private Lazy<Yw.BLL.BimfaceFile> _bllBimfaceFile = new(() => new Yw.BLL.BimfaceFile());
        private Lazy<Yw.BLL.BimfaceFileRelation> _bllBimfaceFileRelation = new(() => new BLL.BimfaceFileRelation());
@@ -26,40 +26,20 @@
        {
            var allBimfaceFileList = await _bllBimfaceFile.Value.GetAll();
            var allBimfaceFileRelationList = await _bllBimfaceFileRelation.Value.GetAll();
            _allBindingList = new List<BimfaceFileViewModel>();
            _allBindingList = new List<BimfaceFileMgrViewModel>();
            if (allBimfaceFileList != null && allBimfaceFileList.Count > 0)
            {
                foreach (var bimfaceFile in allBimfaceFileList)
                {
                    var hasReason = allBimfaceFileRelationList == null ? false : allBimfaceFileRelationList.Exists(x => x.BimfaceFileID == bimfaceFile.ID);
                    var vm = new BimfaceFileViewModel(bimfaceFile, hasReason);
                    var vm = new BimfaceFileMgrViewModel(bimfaceFile, hasReason);
                    _allBindingList.Add(vm);
                }
            }
            this.bimfaceFileViewModelBindingSource.DataSource = _allBindingList;
            this.bimfaceFileViewModelBindingSource.ResetBindings(false);
            this.bimfaceFileMgrViewModelBindingSource.DataSource = _allBindingList;
            this.bimfaceFileMgrViewModelBindingSource.ResetBindings(false);
        }
        //获取当前对象
        private BimfaceFileViewModel GetCurrentModel()
        {
            if (_allBindingList == null)
            {
                return null;
            }
            if (_allBindingList.Count < 1)
            {
                XtraMessageBox.Show("无数据");
                return null;
            }
            var vm = this.gridView1.GetCurrentViewModel(_allBindingList);
            if (vm == null)
            {
                XtraMessageBox.Show("请选择数据行!");
                return null;
            }
            return vm;
        }
        /// <summary>
        ///初始化数据
@@ -76,6 +56,16 @@
        public override async void RefreshData()
        {
            base.RefreshData();
            if (_allBindingList == null)
            {
                return;
            }
            if (_allBindingList.Exists(x => x.FileStatus == eFileStatus.Uploading || x.FileStatus == eFileStatus.Converting))
            {
                return;
            }
            await LoadData();
        }
@@ -99,72 +89,6 @@
            // PageDataSync<Model.BimFileMapper4Get>.DataOperationEvent -= SyncBimFileMapperData;
        }
        //同步模型文件数据
        //private void SyncBimFileData(DocumentPage page, ePageDataOperation operation, Model.BimFile4Get model)
        //{
        //    if (_allGetModels == null)
        //        return;
        //    if (page == this)
        //        return;
        //    switch (operation)
        //    {
        //        case ePageDataOperation.Insert:
        //            {
        //                _allGetModels.Add(model);
        //                this.bimFile4GetBindingSource.ResetBindings(false);
        //            }
        //            break;
        //        case ePageDataOperation.Update:
        //            {
        //                var rhs = _allGetModels.Find(x => x.ID == model.ID);
        //                var mapper = new MapperConfiguration(cfg => cfg.CreateMap<Model.BimFile4Get, Model.BimFile4Get>()).CreateMapper();
        //                mapper.Map(model, rhs);
        //                this.bimFile4GetBindingSource.ResetBindings(false);
        //            }
        //            break;
        //        case ePageDataOperation.Delete:
        //            {
        //                _allGetModels.RemoveAll(x => x.ID == model.ID);
        //                this.bimFile4GetBindingSource.ResetBindings(false);
        //            }
        //            break;
        //        default: break;
        //    }
        //}
        //同步模型文件映射数据
        //private void SyncBimFileMapperData(DocumentPage page, ePageDataOperation operation, Model.BimFileMapper4Get model)
        //{
        //    if (_allGetMappers == null)
        //        return;
        //    if (page == this)
        //        return;
        //    switch (operation)
        //    {
        //        case ePageDataOperation.Insert:
        //            {
        //                _allGetMappers.Add(model);
        //            }
        //            break;
        //        case ePageDataOperation.Update:
        //            {
        //                var index = _allGetMappers.FindIndex(x => x.ID == model.ID);
        //                if (index >= 0)
        //                    _allGetMappers.RemoveAt(index);
        //                _allGetMappers.Add(model);
        //            }
        //            break;
        //        case ePageDataOperation.Delete:
        //            {
        //                _allGetMappers.RemoveAll(x => x.ID == model.ID);
        //            }
        //            break;
        //        default: break;
        //    }
        //}
        //添加
        private void Add()
        {
@@ -178,9 +102,9 @@
            {
                var bimfaceFile = await _bllBimfaceFile.Value.GetByID(bimfaceFileId);
                var hasReason = await _bllBimfaceFileRelation.Value.IsExistByBimfaceFileID(bimfaceFileId);
                var vm = new BimfaceFileViewModel(bimfaceFile, hasReason);
                var vm = new BimfaceFileMgrViewModel(bimfaceFile, hasReason);
                _allBindingList.Add(vm);
                this.bimfaceFileViewModelBindingSource.ResetBindings(false);
                this.bimfaceFileMgrViewModelBindingSource.ResetBindings(false);
                XtraMessageBox.Show("添加成功!");
            };
            dlg.ShowDialog();
@@ -197,7 +121,7 @@
            dlg.SetBindingData();
            dlg.ReloadDataEvent += async (rhs) =>
            {
                var vm = new BimfaceFileViewModel();
                var vm = new BimfaceFileMgrViewModel();
                vm.BimfaceId = string.Empty;
                vm.Name = rhs.FileName;
                vm.ModelType = eModelType.File;
@@ -208,202 +132,226 @@
                vm.FileSize = string.Format("{0}MB", System.Math.Ceiling(fileInfo.Length / 1024.0 / 1024.0));
                _allBindingList.Add(vm);
                this.bimfaceFileViewModelBindingSource.ResetBindings(false);
                var bimfaceClient = Yw.BIMFaceClient.Instance(Settings.BimfaceParasHelper.Bimface.AppKey, Settings.BimfaceParasHelper.Bimface.AppSecret);
                var uploadTask = Task.Run(() => bimfaceClient.UploadFileExByPolicy(rhs.FilePath, rhs.FileName));
                await uploadTask;
                this.bimfaceFileMgrViewModelBindingSource.ResetBindings(false);
                var bimfaceId = await BimfaceHelper.UploadFile(rhs.FilePath, rhs.FileName);
                if (uploadTask.Result < 1)
                if (bimfaceId < 1)
                {
                    _allBindingList.Remove(vm);
                    this.bimfaceFileViewModelBindingSource.ResetBindings(false);
                    this.FindForm().ShowAlertInfo("模型文件上传提示", $"{rhs.FileName} 上传失败!");
                    this.bimfaceFileMgrViewModelBindingSource.ResetBindings(false);
                    this.ShowAlertInfo("模型文件上传提示", $"{rhs.FileName} 上传失败!");
                    return;
                }
                vm.BimfaceId = uploadTask.Result.ToString();
                vm.BimfaceId = bimfaceId.ToString();
                vm.FileStatus = eFileStatus.UploadSucceed;
                this.bimfaceFileViewModelBindingSource.ResetBindings(false);
                this.bimfaceFileMgrViewModelBindingSource.ResetBindings(false);
                var addModel = vm.Adapt<BimfaceFileViewModel, Yw.Dto.AddBimfaceFileInput>();
                var addModel = vm.Adapt<BimfaceFileMgrViewModel, Yw.Dto.AddBimfaceFileInput>();
                vm.ID = await _bllBimfaceFile.Value.Insert(addModel);
                if (vm.ID < 1)
                {
                    _allBindingList.Remove(vm);
                    this.FindForm().ShowAlertInfo("模型文件上传提示", $"{rhs.FileName} 保存失败!");
                    this.ShowAlertInfo("模型文件上传提示", $"{rhs.FileName} 保存失败!");
                }
                this.bimfaceFileViewModelBindingSource.ResetBindings(false);
                this.bimfaceFileMgrViewModelBindingSource.ResetBindings(false);
            };
            dlg.ShowDialog();
        }
        //转换BIM
        private async void TranslateBim()
        //转换
        private async void Translate()
        {
            if (_allBindingList == null)
            {
                return;
            }
            var vm = GetCurrentModel();
            var vm = GetCurrentViewModel();
            if (vm == null)
            {
                return;
            }
            if (vm.FileStatus != eFileStatus.UploadSucceed)
            {
                XtraMessageBox.Show("此功能仅适用于上传成功的模型文件!");
                MessageBoxHelper.ShowInfo("此功能仅适用于上传成功状态的模型文件!");
                return;
            }
            if (vm.FormatType != eFormatType._3D)
            if (vm.FormatType != eFormatType.d3)
            {
                XtraMessageBox.Show("此功能仅适用于3D模型文件!");
                MessageBoxHelper.ShowInfo("此功能仅适用于3D模型文件!");
                return;
            }
            if (vm.FileSuffix.ToLower() != ".rvt")
            {
                XtraMessageBox.Show("此功能仅适用于rvt模型文件!");
                MessageBoxHelper.ShowInfo("此功能仅适用于rvt模型文件!");
                return;
            }
            var bimfaceClient = Yw.BIMFaceClient.Instance(Settings.BimfaceParasHelper.Bimface.AppKey, Settings.BimfaceParasHelper.Bimface.AppSecret);
            var task = Task.Run(() => bimfaceClient.TranslateRvtFileReturnStatus(vm.BimfaceId, eRvtTranslateStyle.Real));
            await task;
            if (task.Result == eTranslateStatus.ProcessingStatus)
            bool updateFileStatusResult = false;
            var translateStatus = await BimfaceHelper.TranslateRvtFile(vm.BimfaceId);
            if (translateStatus == eTranslateStatus.Processing)
            {
                vm.FileStatus = eFileStatus.Converting;
                this.bimfaceFileViewModelBindingSource.ResetBindings(false);
                if (!await _bllBimfaceFile.Value.UpdateFileStatus(vm.ID, (int)vm.FileStatus))
                this.bimfaceFileMgrViewModelBindingSource.ResetBindings(false);
                updateFileStatusResult = await _bllBimfaceFile.Value.UpdateFileStatus(vm.ID, (int)vm.FileStatus);
                if (!updateFileStatusResult)
                {
                    this.FindForm().ShowAlertInfo("模型文件转换", $"{vm.Name}:更新失败");
                    this.ShowAlertInfo("模型文件转换", $"{vm.Name}:更新文件状态失败!");
                }
                this.FindForm().ShowAlertInfo("模型文件转换提示", "模型文件转换耗时较长,请耐心等待...");
                var statusTask = Task.Run(async () =>
                {
                    while (vm.FileStatus == eFileStatus.Converting)
                    {
                        Thread.Sleep(3000);
                        var result = await bimfaceClient.GetFileTranslateStatusReturnStatus(vm.BimfaceId);
                        if (result == eTranslateStatus.ProcessingStatus)
                        {
                            vm.FileStatus = eFileStatus.ConvertSucceed;
                        }
                    }
                });
                await statusTask;
                this.ShowAlertInfo("模型文件转换提示", "模型文件转换耗时较长,请耐心等待...");
                await BimfaceHelper.WaitFileTranslateStatusUntilSuccess(vm.BimfaceId, 3000);
            }
            vm.FileStatus = eFileStatus.ConvertSucceed;
            this.bimfaceFileViewModelBindingSource.ResetBindings(false);
            if (!await _bllBimfaceFile.Value.UpdateFileStatus(vm.ID, (int)vm.FileStatus))
            this.bimfaceFileMgrViewModelBindingSource.ResetBindings(false);
            updateFileStatusResult = await _bllBimfaceFile.Value.UpdateFileStatus(vm.ID, (int)vm.FileStatus);
            if (!updateFileStatusResult)
            {
                this.FindForm().ShowAlertInfo("模型文件转换", $"{vm.Name}:更新失败");
                this.ShowAlertInfo("模型文件转换", $"{vm.Name}:更新文件状态失败!");
            }
        }
        //编辑
        private void EidtBim()
        private void Edit()
        {
            //if (_allGetModels == null)
            //    return;
            //var getModel = GetCurrentModel();
            //if (getModel.FileStatus == eFileStatus.上传中)
            //{
            //    XtraMessageBox.Show(eFileStatus.上传中.ToString());
            //    return;
            //}
            //var mapper = new MapperConfiguration(cfg => cfg.CreateMap<Model.BimFile4Get, Model.BimFile4Update>()).CreateMapper();
            //var updateModel = mapper.Map<Model.BimFile4Get, Model.BimFile4Update>(getModel);
            //var dlg = new EditBimFileBaseInfoDlg();
            //dlg.SetBindingData(updateModel);
            //dlg.ReloadDataEvent += (rhs) =>
            //{
            //    mapper = new MapperConfiguration(cfg => cfg.CreateMap<Model.BimFile4Update, Model.BimFile4Get>()).CreateMapper();
            //    getModel = mapper.Map<Model.BimFile4Update, Model.BimFile4Get>(rhs, getModel);
            //    this.gridView1.RefreshRow(this.gridView1.FocusedRowHandle);
            //    XtraMessageBox.Show("更新成功!");
            //    PageDataSync<Model.BimFile4Get>.Commit(this, ePageDataOperation.Update, getModel);
            //};
            //dlg.ShowDialog();
            var vm = GetCurrentViewModel();
            if (vm == null)
            {
                return;
            }
            if (vm.FileStatus == eFileStatus.Uploading)
            {
                MessageBoxHelper.ShowWarning("上传中的模型文件不可编辑");
                return;
            }
            if (vm.FileStatus == eFileStatus.Converting)
            {
                MessageBoxHelper.ShowWarning("转换中的模型文件不可编辑");
                return;
            }
            var dlg = new EditBimfaceFileDlg();
            dlg.SetBindingData(vm);
            dlg.ReloadDataEvent += async (bimfaceFileId) =>
            {
                var dto = await _bllBimfaceFile.Value.GetByID(bimfaceFileId);
                vm.Reset(dto);
                this.gridView1.RefreshRow(this.gridView1.FocusedRowHandle);
                XtraMessageBox.Show("更新成功!");
            };
            dlg.ShowDialog();
        }
        //删除
        private void DeleteBim()
        private async void Delete()
        {
            //if (_allGetModels == null)
            //    return;
            //var getModel = GetCurrentModel();
            //if (getModel.FileStatus == eFileStatus.上传中 || getModel.FileStatus == eFileStatus.转换中)
            //{
            //    XtraMessageBox.Show(getModel.FileStatus.ToString());
            //    return;
            //}
            //if (DevExpress.XtraEditors.XtraMessageBox.Show("是否要删除?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            //{
            //    if (!GlobelParas.BimfaceClient.DeleteFile(getModel.BimfaceID))
            //    {
            //        XtraMessageBox.Show("删除失败!");
            //        return;
            //    }
            //    if (!BLLFactory<BLL.BimFile>.Instance.DeleteByID(getModel.ID))
            //    {
            //        XtraMessageBox.Show("删除失败!");
            //        return;
            //    }
            //    _allGetModels.Remove(getModel);
            //    this.bimFile4GetBindingSource.ResetBindings(false);
            //    XtraMessageBox.Show("删除成功!");
            //    PageDataSync<Model.BimFile4Get>.Commit(this, ePageDataOperation.Delete, getModel);
            //}
            var vm = GetCurrentViewModel();
            if (vm == null)
            {
                return;
            }
            if (vm.HasRelation)
            {
                MessageBoxHelper.ShowWarning("存在关联关系,不可删除!");
                return;
            }
            if (vm.FileStatus == eFileStatus.Uploading)
            {
                MessageBoxHelper.ShowWarning("上传中的文件不能删除!");
                return;
            }
            if (vm.FileStatus == eFileStatus.Converting)
            {
                MessageBoxHelper.ShowWarning("转换中的文件不能删除!");
                return;
            }
            if (MessageBoxHelper.ShowQuestion("是否确定删除?") == DialogResult.Yes)
            {
                var deleteResult = await BimfaceHelper.Delete(vm.BimfaceId);
                if (!deleteResult)
                {
                    MessageBoxHelper.ShowError("Bimface删除失败!");
                    return;
                }
                deleteResult = await _bllBimfaceFile.Value.DeleteByID(vm.ID);
                if (!deleteResult)
                {
                    MessageBoxHelper.ShowError("Bimface文件删除失败!");
                    return;
                }
                _allBindingList.Remove(vm);
                this.bimfaceFileMgrViewModelBindingSource.ResetBindings(false);
                MessageBoxHelper.ShowSuccess("删除成功!");
            }
        }
        //显示详细信息
        private void ShowDetail()
        {
            XtraMessageBox.Show("开发中...");
            var vm = GetCurrentViewModel();
            if (vm == null)
            {
                return;
            }
            MessageBoxHelper.ShowInfo("正在开发中...");
        }
        //本地浏览
        private void ShowLocalView()
        private async void ShowLocalView()
        {
            //if (_allGetModels == null)
            //    return;
            //var getModel = GetCurrentModel();
            //if (getModel == null)
            //    return;
            //new BimfaceViewHelper().ShowBimfaceLocalView(getModel, IsExistPage, CreatePage);
            var vm = GetCurrentViewModel();
            if (vm == null)
            {
                return;
            }
            var guid = new PageGuid()
            {
                Modular = Yw.WinFrmUI.PageModular.Bimface,
                MoudingType = Yw.WinFrmUI.Page.eMoudingType.Tab,
                Function = Yw.WinFrmUI.PageFunction.ViewBimface3d,
                TagName = vm.ID.ToString()
            };
            if (!IsExistPage(guid, true))
            {
                var page = new Bimface3dViewPage();
                await page.SetBindingData(vm.ID);
                CreatePage(page, guid);
            }
        }
        //网页浏览
        private void ShowWebView()
        {
            //if (_allGetModels == null)
            //    return;
            //var getModel = GetCurrentModel();
            //if (getModel == null)
            //    return;
            //new BimfaceViewHelper().ShowBimfaceWebView(getModel);
            var vm = GetCurrentViewModel();
            if (vm == null)
            {
                return;
            }
            MessageBoxHelper.ShowInfo("正在开发中...");
        }
        //手机浏览
        private void ShowPhoneView()
        {
            //if (_allGetModels == null)
            //    return;
            //var getModel = GetCurrentModel();
            //if (getModel == null)
            //    return;
            //new BimfaceViewHelper().ShowBimfacePhoneView(getModel);
            var vm = GetCurrentViewModel();
            if (vm == null)
            {
                return;
            }
            MessageBoxHelper.ShowInfo("正在开发中...");
        }
        //显示模型文件设置界面
        private void ShowBimFileSettings()
        {
            //if (_allGetModels == null)
            //    return;
            //var getModel = GetCurrentModel();
            //if (getModel == null)
            //    return;
            //new BimfaceViewHelper().ShowBimFileSettings(getModel, IsExistPage, CreatePage);
            var vm = GetCurrentViewModel();
            if (vm == null)
            {
                return;
            }
            MessageBoxHelper.ShowInfo("正在开发中...");
        }
@@ -454,46 +402,56 @@
        #region Ribbon
        //添加
        private void barBtnAdminAdd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Add();
        }
        //上传
        private void barBtnUpload_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Upload();
        }
        //转换
        private void barBtnTranslate_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            TranslateBim();
            Translate();
        }
        //编辑
        private void barBtnEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            EidtBim();
            Edit();
        }
        //删除
        private void barBtnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DeleteBim();
            Delete();
        }
        //详细信息
        private void barBtnDetail_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ShowDetail();
        }
        //本地视图
        private void barBtnLocal_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ShowLocalView();
        }
        //网页视图
        private void barBtnWeb_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ShowWebView();
        }
        //手机视图
        private void barBtnPhone_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ShowPhoneView();
@@ -505,18 +463,47 @@
            ShowBimFileSettings();
        }
        //刷新数据
        private void barBtnReloadData_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (_allBindingList == null)
            {
                return;
            }
            if (_allBindingList.Exists(x => x.FileStatus == eFileStatus.Uploading || x.FileStatus == eFileStatus.Converting))
            {
                MessageBoxHelper.ShowWarning("正在上传或转换模型文件,请稍后重试!");
                return;
            }
            RefreshData();
        }
        private void gridControl1_Click(object sender, EventArgs e)
        {
        }
        #endregion
        #region 当前
        //获取当前视图
        private BimfaceFileMgrViewModel GetCurrentViewModel()
        {
            if (_allBindingList == null)
            {
                MessageBoxHelper.ShowError("数据初始化失败");
                return null;
            }
            if (_allBindingList.Count < 1)
            {
                MessageBoxHelper.ShowInfo("数据为空");
            }
            var vm = this.gridView1.GetCurrentViewModel(_allBindingList);
            if (vm == null)
            {
                MessageBoxHelper.ShowWarning("请选择数据行");
            }
            return vm;
        }
        #endregion