Ejemplo n.º 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 =
            }
        }