Example #1
0
        /// <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;
            }
        }