示例#1
0
        private void ExecuteAction(IObjectSpace newFormOs, 单据 fromObject, FlowAction action)
        {
            //action数据是来自于 初始化按钮时的os,它可能不是当前objectspace
            action = ObjectSpace.GetObject(action);
            var os = new NonPersistentObjectSpace(null);


            var toType = ReflectionHelper.FindType(action.To.Form);

            #region 生成目标单据主表属性
            单据 toObject = null;
            //查找当前单据是从那张单据生成过来的
            var record = ObjectSpace.FindObject <单据流程状态记录>(CriteriaOperator.Parse("目标单据=?", CurrentObject.GetMemberValue("Oid").ToString()));

            if (action.目标类型 == 目标类型.生成单据)
            {
                toObject = newFormOs.CreateObject(toType) as 单据;
            }
            else if (action.目标类型 == 目标类型.更新单据)
            {
                var history = ObjectSpace.FindObject <单据流程状态记录>(CriteriaOperator.Parse("来源单据=? and 执行动作.Oid=?", CurrentObject.GetMemberValue("Oid").ToString(), action.Oid));
                if (history != null)
                {
                    //更新动作已经执行过了,不要再执行。
                    return;
                }

                //如果是更新单据,那必然是有已经生成过的单据。
                if (record != null)
                {
                    toObject = newFormOs.GetObject(record.来源单据); // newFormOs.GetObjectByStringKey(action.ToType, record.来源单据) as 单据;
                }
                else
                {
                    return;
                }
                if (toObject == null)
                {
                    return;
                }
            }

            var clsname                    = action.GetFlowLogicClassFullName();
            var actionLogicType            = ReflectionHelper.FindType(clsname);
            IExecuteFlowAction actionLogic = null;
            if (actionLogicType != null)
            {
                actionLogic = Activator.CreateInstance(actionLogicType) as IExecuteFlowAction;
                if (actionLogic != null)
                {
                    actionLogic.ExecuteMasterCore(fromObject, toObject);
                }
            }

            #endregion

            #region 生成目标单据子表记录
            var fromItems  = fromObject.GetMemberValue("明细项目") as XPBaseCollection;
            var toItems    = toObject.GetMemberValue("明细项目") as XPBaseCollection;
            var toItemType = toItems.GetObjectClassInfo().ClassType;
            //来源单据的明细,当前单据
            var detailRecords = new List <Tuple <string, string> >();
            foreach (XPBaseObject item in fromItems)
            {
                //查找记录?
                XPBaseObject toItem = null;
                if (action.目标类型 == 目标类型.更新单据)
                {
                    //如果是更新单据(反写)时,没有找到反写目标明细,则继续。忽略当前条目。
                    //recs可以查到来源明细
                    //
                    var recs = record.明细.FirstOrDefault(x => x.目标 == item.GetMemberValue("Oid").ToString());
                    toItem = (toObject.GetMemberValue("明细项目") as IList).OfType <BaseObject>().FirstOrDefault(x => x.Oid.ToString() == recs.来源);
                    if (toItem == null)
                    {
                        continue;
                    }
                }
                else if (action.目标类型 == 目标类型.生成单据)
                {
                    toItem = newFormOs.CreateObject(toItemType) as XPBaseObject;
                }
                else
                {
                    throw new Exception("未处理的目标类型!");
                }
                toItems.BaseAdd(toItem);

                if (actionLogic != null)
                {
                    actionLogic.ExecuteChildrenCore(item, toItem);
                }
                //foreach (var f in action.ItemsMapping)
                //{
                //    var fromValue = item.Evaluate(f.FromProperty.Name);
                //    toItem.SetMemberValue(f.ToProperty.Name, fromValue);
                //}

                detailRecords.Add(new Tuple <string, string>(item.GetMemberValue("Oid").ToString(), toItem.GetMemberValue("Oid").ToString()));
            }

            #region 生成主表记录
            EventHandler act = null;
            act = (snd, evt) =>
            {
                newFormOs.Committed -= act;
                var process = ObjectSpace.CreateObject <单据流程状态记录>();
                process.来源单据 = this.CurrentObject as 单据;
                process.目标单据 = this.ObjectSpace.GetObject(toObject);

                process.执行动作 = action;
                process.业务项目 = process.来源单据.业务项目;
                foreach (var item in detailRecords)
                {
                    var det = ObjectSpace.CreateObject <单据流程记录明细>();
                    det.来源 = item.Item1;
                    det.目标 = item.Item2;
                    process.明细.Add(det);
                }

                ObjectSpace.CommitChanges();
            };
            newFormOs.Committed += act;
            #endregion

            #endregion

            if (action.自动保存)
            {
                newFormOs.CommitChanges();
            }

            if (action.显示编辑界面)
            {
                var para = new ShowViewParameters();
                para.CreatedView  = Application.CreateDetailView(newFormOs, toObject, true);
                para.TargetWindow = TargetWindow.Default;

                Application.ShowViewStrategy.ShowView(para, new ShowViewSource(this.Frame, this.FlowToNext));
                //e.ShowViewParameters.CreatedView =
            }
        }
示例#2
0
        public FlowInstance(Session s, 单据 form) : base(s)
        {
            this.form = form;
            var records = Session.Query <单据流程状态记录>().Where(t => t.业务项目 == form.业务项目);

            //var status = Session.Query<状态变更记录>().Where(t => t.单据.业务项目 == form.业务项目);
            _actions = new List <FlowInstanceAction>();
            _nodes   = new List <FlowInstanceNode>();

            var nodes = records.Select(x => x.来源单据).Union(records.Select(t => t.目标单据)).OrderBy(x => x.创建时间).Distinct();
            var y     = 100;

            //先绘制单据结点
            foreach (var item in nodes)
            {
                var fin = new FlowInstanceNode(Session);
                fin.Caption = CaptionHelper.GetDisplayText(item);

                fin.Width     = 200;
                fin.Height    = 30;
                fin.X         = 100;
                fin.Y         = y;
                fin.Key       = item;
                fin.ImageName = CaptionHelper.ApplicationModel.BOModel.GetClass(item.GetType()).ImageName;
                _nodes.Add(fin);
                y += 100;
                var xl = 400;

                var status = item.状态记录.OrderBy(x => x.发生日期).Select(x => x.来源状态).Union(item.状态记录.Select(x => x.目标状态)).Where(x => x != null).Distinct();

                foreach (var state in status)
                {
                    var si = new FlowInstanceNode(Session);
                    si.Caption = state.Caption;
                    si.Width   = 100;
                    si.Height  = 30;
                    si.X       = xl;
                    si.Y       = fin.Y;
                    si.Key     = state;
                    si.Key2    = item;
                    _nodes.Add(si);
                    xl += 350;
                }

                foreach (var sa in item.状态记录)
                {
                    if (sa.来源状态 != null)
                    {
                        var line = new FlowInstanceAction(Session);
                        line.From = _nodes.Single(x => x.Key == sa.来源状态 && x.Key2 == item);
                        line.To   = _nodes.Single(x => x.Key == sa.目标状态 && x.Key2 == item);
                        line.BeginItemPointIndex = 1;
                        line.EndItemPointIndex   = 3;
                        line.Caption             = sa.操作人 + " " + sa.发生日期.ToString("yyyy-MM-dd HH:mmss");
                        _actions.Add(line);
                    }
                }

                if (status.Any())
                {
                    var f           = status.FirstOrDefault();
                    var formToState = new FlowInstanceAction(Session);
                    formToState.From = fin;
                    formToState.To   = _nodes.Single(x => x.Key == f && x.Key2 == item);
                    formToState.BeginItemPointIndex = 1;
                    formToState.EndItemPointIndex   = 3;
                    _actions.Add(formToState);
                }
            }

            foreach (var item in records)
            {
                var action = new FlowInstanceAction(Session);
                action.From = _nodes.Single(x => x.Key == item.来源单据);
                action.To   = _nodes.Single(x => x.Key == item.目标单据);
                action.BeginItemPointIndex = 2;
                action.EndItemPointIndex   = 0;
                _actions.Add(action);
            }

            ////取所有来源状态,目标状态,不为空的,取除重复。
            //var stateNodes = status.GroupBy(x => x.单据).OrderBy(x => x.Min(t => t.发生日期));

            ////按单据分组的状态数据
            //var y = 50;
            //foreach (var g in stateNodes)
            //{
            //    var gStatus = g.OrderBy(x=>x.发生日期).Select(x => x.来源状态).Union(g.Select(x => x.目标状态)).Where(x => x != null).Distinct();
            //    xl = 100;
            //    foreach (var item in gStatus)
            //    {
            //        var si = new FlowInstanceNode(Session);
            //        si.Caption = item.Caption;
            //        si.Width = 100;
            //        si.Height = 30;
            //        si.X = xl;
            //        si.Y = y;
            //        si.Key = item;
            //        _nodes.Add(si);
            //        xl += 150;
            //    }

            //    foreach (var item in g)
            //    {
            //        if (item.来源状态 != null)
            //        {
            //            var line = new FlowInstanceAction(Session);
            //            line.From = _nodes.SingleOrDefault(x => x.Key == item.来源状态);
            //            line.To = _nodes.SingleOrDefault(x => x.Key == item.目标状态);
            //            line.BeginItemPointIndex = 1;
            //            line.EndItemPointIndex = 3;
            //            _actions.Add(line);
            //        }
            //    }

            //    y += 100;
            //}



            //foreach (var item in status)
            //{
            //    var si = new FlowInstanceNode(Session);
            //    if (item.来源状态 != null)
            //        si.Caption = item.来源状态.Caption;
            //    si.Width = 100;
            //    si.Height = 30;
            //    _nodes.Add(si);
            //    var sit = new FlowInstanceNode(Session);
            //    sit.Width = 100;
            //    sit.Height = 30;

            //    sit.Caption = item.目标状态.Caption;
            //    _nodes.Add(sit);
            //    _actions.Add(new FlowInstanceAction(Session) { From = si, To = sit });
            //}



            //如何构建出结点和连接线?
        }