コード例 #1
0
        public override void ProcessActivity(WebRequest request, WebResponse response)
        {
            var Settings = this.AsyncDialog("Settings", g =>
            {
                if (request.SendValues != null && request.SendValues.ContainsKey("media_id"))
                {
                    return(this.DialogValue(new WebMeta().Put("media_id", request.SendValues["media_id"])));
                }
                var from = new Web.UIFormDialog()
                {
                    Title = this.Title ?? "文本数据导入"
                };

                from.AddFile("选择csv文件", "media_id", String.Empty).Put("Accept", "text/csv");
                from.AddRadio("文件编码", "Encoding").Put("中文操作系统标准(GB2312)", "gb2312").Put("英文操作系统标准(UTF8)", "utf-8", true);
                var opts = from.AddTextValue("必须列名");
                foreach (var s in Columns)
                {
                    opts.Add(s, "必须字段");
                }
                from.Submit("确认上传", request, "Pager");
                return(from);
            });
            var media_id = Settings["media_id"];

            if (media_id.EndsWith(".csv") == false)
            {
                this.Prompt("请上传csv文本文件");
            }

            var Key      = this.AsyncDialog("Key", Utility.Guid(Guid.NewGuid()));
            var pathFile = this.AsyncDialog("File", g =>
            {
                string path2 = UMC.Data.Utility.MapPath(String.Format("App_Data\\Static\\TEMP\\{0}\\", Utility.GetRoot(request.Url)));

                var filename = String.Format("{0}{1}.tmp", path2, Key);
                Utility.Copy(new UMC.Net.HttpClient().GetStreamAsync(media_id).Result, filename);

                return(this.DialogValue(filename));
            });

            if (pathFile.EndsWith(".csv"))
            {
                var pad = new Uri(request.Url, String.Format("/TEMP/{0}", pathFile));
                this.Context.Send(new Web.WebMeta().Put("type", "OpenUrl").Put("value", pad.AbsoluteUri), true);
            }
            var columnSettings = this.AsyncDialog("Columns", g =>
            {
                var reader = new System.IO.StreamReader(pathFile, Encoding.GetEncoding(Settings["Encoding"] ?? "utf-8"));
                var cinex  = 0;
                var from   = new Web.UIFormDialog()
                {
                    Title = "核对字段"
                };
                var csvColumns = CSV.FromCsvLine(CSV.ReadLine(reader));
                reader.Close();
                foreach (var c in this.Columns)
                {
                    var opts = from.AddSelect(c, "Col_" + cinex).Put("请选择", "");
                    for (var i = 0; i < csvColumns.Length; i++)
                    {
                        opts.Add(String.Format("{1}(第{0}列)", i + 1, csvColumns[i]), i.ToString(), String.Equals(csvColumns[i], c, StringComparison.CurrentCultureIgnoreCase));
                    }
                    cinex++;
                }
                from.Submit("确认通过");
                return(from);
            });
            var indexs   = new List <int>();
            var maxIndex = -1;

            for (var i = 0; i < this.Columns.Length; i++)
            {
                var cindex = Convert.ToInt32(columnSettings["Col_" + i]);
                if (maxIndex < cindex)
                {
                    maxIndex = cindex;
                }
                indexs.Add(cindex);
            }
            if (indexs.Count == 0)
            {
                this.Prompt("无有效的字段");
            }

            var userName = Utility.GetUsername();

            var log = new CSV.Log(Utility.GetRoot(request.Url), Key, String.Format("开始{0}", this.Title ?? "文本数据导入"));

            Data.Reflection.Start(() =>
            {
                int rowIndex = 1;
                var reader   = new System.IO.StreamReader(pathFile, Encoding.GetEncoding(Settings["Encoding"] ?? "utf-8"));
                System.IO.FileStream file = System.IO.File.Open(String.Format("{0}.csv", pathFile), System.IO.FileMode.Create);


                try
                {
                    var writer = new System.IO.StreamWriter(file, Encoding.UTF8);
                    var total  = 0;
                    var now    = DateTime.Now;


                    var header = CSV.ReadLine(reader);
                    writer.WriteLine(header);

                    int okindex = 0;

                    CSV.EachRow(reader, data =>
                    {
                        rowIndex++;
                        if (data.Length <= maxIndex)
                        {
                            log.Error(String.Format("第{0}行 数据无效", rowIndex));
                            if (okindex + 10 < rowIndex)
                            {
                                throw new ArgumentException(String.Format("连续超过10条无效的数据"));
                            }
                            return;
                        }
                        var hash = new Hashtable();
                        for (var i = 0; i < indexs.Count; i++)
                        {
                            int index = indexs[i];
                            if (data.Length > index)
                            {
                                if (String.IsNullOrEmpty(data[index]))
                                {
                                    log.Error(String.Format("第{0}行 {1}列数据为空", rowIndex, this.Columns[i]));
                                    if (okindex + 10 < rowIndex)
                                    {
                                        throw new ArgumentException(String.Format("连续超过10条无效的数据"));
                                    }
                                    return;
                                }
                            }
                            else
                            {
                                log.Error(String.Format("第{0}行 无{1}列数据", rowIndex, this.Columns[i]));
                                if (okindex + 10 < rowIndex)
                                {
                                    throw new ArgumentException(String.Format("连续超过10条无效的数据"));
                                }
                                return;
                            }
                            hash[this.Columns[i]] = data[index];
                        }
                        if (Do(log, rowIndex, hash))
                        {
                            total++;
                            okindex = rowIndex;
                        }
                        else
                        {
                            foreach (var d in data)
                            {
                                UMC.Data.CSV.CSVFormat(writer, d);
                                writer.Write(",");
                            }
                            writer.WriteLine();
                            writer.Flush();
                        }
                        if (okindex + 10 < rowIndex)
                        {
                            new ArgumentException(String.Format("连续超过10条无效的数据"));
                        }
                    });
                    writer.Flush();
                    writer.Close();
                    log.End("导入数据完成");
                    log.Info(String.Format("导入成功{0}条", total));
                    log.Info(String.Format("用时{0}", DateTime.Now - now));
                }
                catch (Exception ex)
                {
                    log.End(String.Format("在{0}行导入失败", rowIndex));
                    log.Info(ex.Message);
                }
                finally
                {
                    file.Close();
                    reader.Close();
                    log.Close();
                }
            });    //.Start();

            this.Context.Send(new UISectionBuilder("System", "Log", new WebMeta("Key", Key))
                              .Builder(), true);
        }
コード例 #2
0
        public override void ProcessActivity(WebRequest request, WebResponse response)
        {
            var key = this.AsyncDialog("Key", g => this.DialogValue("Editer"));
            var sid = Web.UIDialog.AsyncDialog("Id", d =>
            {
                var grid = new SubjectCategoryDialog()
                {
                    IsPage       = true,
                    CloseEvent   = "UI.Event",
                    RefreshEvent = "Subject.Category"
                };
                if (!request.IsCashier)
                {
                    grid.Visible = Visibility.Visible;
                }
                if (request.IsMaster)
                {
                    grid.Menu("新建", request.Model, request.Command, new WebMeta("Key", "Editer", d, Guid.NewGuid().ToString()));
                }
                return(grid);
            });
            var cmdId = UMC.Data.Utility.Guid(sid) ?? Guid.Empty;

            var category = new Category();

            var objectEntity = Utility.CMS.ObjectEntity <UMC.Data.Entities.Category>();

            category = objectEntity.Where.And().Equal(new Category {
                Id = cmdId
            }).Entities.Single() ?? category;

            if (key == "Editer" && request.IsMaster)
            {
            }
            else
            {
                this.Context.Send(new WebMeta().UIEvent(key, new ListItem(category.Caption, category.Id.ToString())), true);
            }
            var Settings = Web.UIFormDialog.AsyncDialog("Settings", d =>
            {
                var fmdg = new Web.UIFormDialog();
                if (category.Id.HasValue == false)
                {
                    fmdg.Title = "新建栏位";
                    fmdg.AddText("栏位名称", "Caption", category.Caption);
                    fmdg.AddOption("版务人员", "user_id", (category.user_id ?? Guid.Empty).ToString(), "请选择版务人员")
                    .Put("placeholder", "请选择版务人员").Command("Settings", "SelectUser");
                    fmdg.AddNumber("显示顺序", "Sequence", category.Sequence);
                }
                else
                {
                    fmdg.Title = "编辑栏位";
                    fmdg.AddText("栏位名称", "Caption", category.Caption);
                    var value = (category.user_id ?? Guid.Empty);
                    var text  = "请设置";
                    if (value != Guid.Empty)
                    {
                        var uAlias = UMC.Data.Database.Instance().ObjectEntity <User>()
                                     .Where.And().Equal(new User {
                            Id = value
                        }).Entities.Single();
                        if (uAlias != null)
                        {
                            text = uAlias.Alias;
                        }
                    }
                    fmdg.AddOption("版务人员", "user_id", value.ToString(), text).Put("placeholder", "请选择版务人员")
                    .Command("Settings", "SelectUser");
                    fmdg.AddNumber("显示顺序", "Sequence", category.Sequence);
                    fmdg.AddRadio("可见状态", "Visible")
                    .Put("可见", Visibility.Visible.ToString(), category.Visible == Visibility.Visible)
                    .Put("隐藏", Visibility.Hidden.ToString(), category.Visible == Visibility.Hidden);
                }
                fmdg.Submit("确认提交", request, "Subject.Category");
                if (category.Id.HasValue)
                {
                    fmdg.AddUI("专题主题设计", "去设计").Command("Design", "Page", category.Id.ToString());
                }
                return(fmdg);
            });

            UMC.Data.Reflection.SetProperty(category, Settings.GetDictionary());

            if (category.Id.HasValue == false)
            {
                category.Id = Guid.NewGuid();

                category.Count      = 0;
                category.Attentions = 0;
                category.Visible    = Visibility.Visible;

                objectEntity.Insert(category);
            }
            else
            {
                objectEntity.Update(category);
            }
            this.Prompt("修改成功", false);
            this.Context.Send(new UMC.Web.WebMeta().Put("type", "Subject.Category"), true);
        }