/// <summary> /// 执行操作 /// </summary> /// <param name="buildWord">文档操作类</param> public override void Execute(IBuildWord buildWord) { if (this.baseLabel == null) { this.baseLabel = this.ConditionJudgment(); } if (this.baseLabel != null) { this.baseLabel.Execute(buildWord); } else { buildWord.InsertText(this.LabelName, string.Empty); } }
/// <summary> /// 获取标签列表 /// </summary> /// <returns>标签列表</returns> private List <BaseLabel> GetLabelList() { string error = ""; try { List <BaseLabel> labelList = new List <BaseLabel>(); List <string> labels = this.buildWord.GetAllMarks(@"《[^》]+》"); foreach (var name in labels) { string labelName = name.TrimStart('《').TrimEnd('》'); error = "标签'" + labelName + "'出错"; BaseLabel label = this.docMaster.LabelList.Find(it => it.LabelName == labelName); if (label == null) { JObject config = JObject.Parse(@" { ""LabelName"": """ + labelName + @""", ""LabelType"": ""TextLabel"", ""Relate"": [], ""Config"": { ""GetDataMethod"": ""Const"", ""Value"": """" }, ""Control"": { ""ControlType"": ""Text"", ""Required"": ""false"", ""ValidateString"": """" } }"); label = new TextLabel(labelName, config, this.docMaster, this.structureID); this.docMaster.LabelList.Add(label); } labelList.Add(label); } return(labelList); } catch { throw new Exception(error); } }
/// <summary> /// 根据条件选择标签 /// </summary> /// <returns>返回符合条件的标签</returns> public BaseLabel ConditionJudgment() { BaseLabel baseLabel = null; foreach (var v in this.LabelList) { // 根据Condition判断 if (DocHelper.CalcByJs(v.Condition)) { baseLabel = v.BaseLabel; break; } else { continue; } } return(baseLabel); }
/// <summary> /// 初始化数据 /// </summary> /// <param name="jsonStructure">构件结构</param> /// <param name="inputParams">输入参数</param> private void InitData(string jsonStructure, Dictionary<string, string> inputParams) { try { MotherSetDTO motherSet = BuildWordInstance.GetMotherSet((int)this.masterID); if (motherSet != null) { Dictionary<BlockType, List<Structure>> structureCofing; this.FileID = motherSet.FILE_ID.Value; this.DocTemplateType = new DocTemplateType(motherSet.TEMPLATE_TYPE.Value, this.InstanceID, inputParams); var fileStream = FileServerHelper.GetFileStream(motherSet.FILE_ID.Value); if (fileStream != null) this.buildWord = new BuildWord(fileStream); if (jsonStructure == null) { structureCofing = this.GetStructureDictionary(motherSet.SET_CONTENT); } else { structureCofing = this.GetStructureDictionary(jsonStructure); } //获取构建信息 this.StructureInfoList = this.GetStructureInfoList(structureCofing); } //处理返回结果 if (!string.IsNullOrEmpty(this.resultJson)) { JArray ary = JArray.Parse(this.resultJson); decimal id; StructureType type; foreach (var v in ary) { id = v["ID"].Value<decimal>(); type = (StructureType)Enum.Parse(typeof(StructureType), v["StructureType"].Value<string>()); if (!this.InputValue.ContainsKey(v["LabelName"].Value<string>())) { this.InputValue.Add(v["LabelName"].Value<string>(), v["Value"].Value<string>()); } } } // 应用替换值 if (this.InputValue != null && this.InputValue.Count > 0) { this.LabelList.ForEach(label => { if (label is TextLabel) { var textLabel = label as TextLabel; var input = this.InputValue.FirstOrDefault(t => t.Key == label.LabelName); if (!string.IsNullOrEmpty(input.Key)) { textLabel.IsInput = true; textLabel.Value = input.Value; } } }); } //处理条件标签 // 1.这种判断有误,当条件标签的条件没有@标签的时候,内容不会被替换 // 2.条件标签应该都算outside modify by huzy 2016.4.5 var inside = this.LabelList.Where(t => !t.RelateValue.Contains('@')).ToList(); var outside = this.LabelList.Where(t => t.RelateValue.Contains('@')).ToList(); var conditionS = this.LabelList.Where(t => t is ConditionLabel).ToList(); foreach (var c in conditionS) { inside.Remove(c); if (!outside.Contains(c)) outside.Add(c); } var tmpList = new List<BaseLabel>(); while (true) { bool isBreak = true; foreach (var oItem in outside) { foreach (var iItem in inside) { if (oItem.RelateValue.IndexOf(iItem.LabelName) > 0) { if (iItem is TextLabel) { var textLabel = iItem as TextLabel; //var value = string.IsNullOrEmpty(textLabel.RelateValue) ? textLabel.GetValue() : textLabel.RelateValue; var value = textLabel.GetValue(); if (!textLabel.IsAfterCompute) value = textLabel.InnerValue; bool pass = oItem.Replace(iItem.LabelName, value); if (!tmpList.Contains(oItem) && pass) tmpList.Add(oItem); if (isBreak && pass) isBreak = false; } else if (iItem is ConditionLabel) //条件引用条件标签 { var conditionLabel = iItem as ConditionLabel; BaseLabel baseLabel = conditionLabel.ConditionJudgment(); if (baseLabel is TextLabel) { var textLabel = baseLabel as TextLabel; var value = textLabel.GetValue(); bool pass = oItem.Replace(iItem.LabelName, value); if (!tmpList.Contains(oItem) && pass) tmpList.Add(oItem); if (isBreak && pass) isBreak = false; } } } } } foreach (var item in tmpList) { inside.Add(item); outside.Remove(item); } tmpList.Clear(); if (isBreak) break; } //处理构建里无匹配的标签 匹配常量中的书名号《》 this.LabelList.ForEach(label => { if (label is ConditionLabel) { var cl = label as ConditionLabel; cl.LabelList.ForEach(l => { string key = DocHelper.PatternString(l.Condition); var findLable = inside.FirstOrDefault(i => i.LabelName == key); if (findLable != null && findLable is TextLabel) { try { var textLabel = findLable as TextLabel; var value = string.IsNullOrEmpty(textLabel.RelateValue) ? textLabel.GetValue() : textLabel.RelateValue; l.Condition = l.Condition.Replace("@" + key, value); } catch { } } if (DocHelper.CalcByJs(l.Condition) && l.BaseLabel is TextLabel) { var tl = l.BaseLabel as TextLabel; tl.ReplaceWithConst(inside); } }); } if (label is TextLabel) { var tl = label as TextLabel; tl.ReplaceWithConst(inside); } }); } catch (Exception ex) { throw ex; } }
/// <summary> /// 构建标签列表 /// </summary> /// <returns></returns> private List <BaseLabel> GetLabelList() { #region --json // json = @"[ // { // LabelName:'表标签', // LabelType:'TableLabel', // Config:{ // DataSourceName:'dsn', // FilterFieldName:'id', // FilterOperation:'=', // FilterValue:'11', // FillType:'OnlyFillByRow', // Top:4, // ColumnInfo:[ // { // FieldName:'title', // ColumnIndex:0, // FormatInfo:{ // FormatType:'ValueToText', // FormatString:'{0}傻X', // DataSourceName:'sourceName', // ValueField:'Value', // TextField:'Text' // } // }, // { // FieldName:'title2', // ColumnIndex:1, // FormatInfo:{ // FormatType:'Number', // FormatString:'{0}万元', // DecimalCount:2 // } // } // ] // }, // Control:{}, // Relate:[] // }]"; // json = @"[ // { // LabelName:'图片标签', // LabelType:'ImageLabel', // Config:{ // ImageName:'科兴科学园.jpg', // FileID:'10001' // }, // Control:{}, // Relate:[] // }]"; // json = @"[ // { // LabelName:'文档标签', // LabelType:'DocLabel', // Config:{ // GetDataMethod:'Const', // DocName:'文档名', // FileID:'10002' // }, // Control:{}, // Relate:[] // }]"; // json = @"[ // { // LabelName:'文本标签', // LabelType:'TextLabel', // Config:{ // GetDataMethod:'Source', // DataSourceName:'11', // FieldName:'23', // FilterFieldName:'33', // FilterOperation:'44', // FilterValue:'55', // FormatInfo:{ // FormatType:'Number', // FormatString:'{0}万元', // DecimalCount:2, // Dividend:0 // } // }, // Control:{}, // Relate:[] // }]"; #endregion string error = ""; try { if (string.IsNullOrEmpty(this.Json)) { return(null); } List <BaseLabel> labelList = new List <BaseLabel>(); JArray ary = JArray.Parse(this.Json); string labelName = string.Empty; LabelType labelType; foreach (var label in ary) { if (label["LabelName"] != null) { labelName = label["LabelName"].Value <string>(); } error = "标签'" + labelName + "'出错"; BaseLabel baseLabel = this.docMaster.LabelList.Find(it => it.LabelName == labelName); if (baseLabel != null) { labelList.Add(baseLabel); } else { string rs = ""; List <string> s = new List <string>(); s.ForEach(r => rs += r); if (label["LabelType"] != null && !string.IsNullOrEmpty(label["LabelType"].ToString())) { labelType = (LabelType)Enum.Parse(typeof(LabelType), label["LabelType"].Value <string>()); switch (labelType) { case LabelType.TextLabel: baseLabel = new TextLabel(labelName, label, this.docMaster, this.structureID); break; case LabelType.TableLabel: baseLabel = new TableLabel(labelName, label, this.docMaster.DocTemplateType); break; case LabelType.ConditionLabel: baseLabel = new ConditionLabel(labelName, label, this.docMaster, this.structureID); break; case LabelType.DocLabel: baseLabel = new DocLabel(labelName, label, this.docMaster); break; case LabelType.ImageLabel: baseLabel = new ImageLabel(labelName, label, this.docMaster.DocTemplateType); break; } if (baseLabel != null) { baseLabel.LabelTypeName = labelType.ToString(); labelList.Add(baseLabel); this.docMaster.LabelList.Add(baseLabel); } } } } return(labelList); } catch { throw new Exception(error); } }