Example #1
0
 public void SetParam(Dictionary <string, object> paramDictionary)
 {
     Operates.ForEach(op =>
     {
         op.SetParam(paramDictionary);
     });
 }
 public void Add(DbOperateCollection op)
 {
     if (_excuted)
     {
         throw new Exception("执行过程中不允许添加新组");
     }
     op.IsMain = !Operates.Any();
     Operates.Add(op);
 }
 /// <summary>
 /// 伪指令构造
 /// </summary>
 /// <param name="operate">操作</param>
 /// <param name="argument">参数</param>
 public Command(Operates operate, int argument)
 {
     this.Operate  = operate;
     this.Argument = argument;
 }
        //注意捕获DbOperateCollectionFailException
        public List <string> Excute(bool throwException = true)
        {
            if (_excuted)
            {
                return(_messages);
            }
            _excuted = true;
            //批量执行
            for (var i = 0; i < Operates.Count; i++)
            {
                AddMessage("开始执行第" + (i + 1) + "个组");
                var op = Operates[i];
                AddMessages(op.Excute(false));
                if (op.Successful)
                {
                    SuccessfulOperates.Add(op);
                }
                else
                {
                    //只要有一个没成功则批量回滚并终止
                    AddMessage("第" + (i + 1) + "个组执行失败," + (SuccessfulOperates.Any() ? "即将回滚" + SuccessfulOperates.Count() + "个组" : "操作已终止"));
                    RollBack();
                    if (throwException)
                    {
                        throw new DbOperateCollectionsFailException(("成功" + SuccessfulOperates.Count() + "个(共" + Operates.Count() + "个):<br/>" +
                                                                     op.Message + "<hr/>详细执行过程<br/>" +
                                                                     _messages.PackString("<br/>")).ToHtml(),
                                                                    Operates, _messages, i
                                                                    );
                    }
                    return(_messages);
                }
            }
            _successful = true;
            //处理返回值
            if (Operates.Count == 1) //只有一个操作
            {                        //直接返回
                _result = Operates[0].Result;
            }
            else
            { //合并json
                switch (MainOperate.MainOperate.OperateType)
                {
                case Operate.Add:
                case Operate.Update:
                case Operate.Delete:
                {
                    _result = MainOperate.Result;
                }
                break;

                case Operate.Find:
                {        //单行类
                    var destJson = "";
                    for (var k = 0; k < Operates.Count; k++)
                    {
                        var json = (string)Operates[k].Result;
                        if (k == 0)
                        {        //第一个
                            destJson += json.Substring(0, json.Length - 1);
                        }
                        else
                        {
                            if (k == Operates.Count - 1)
                            {
                                //最后一个 且无数据
                                destJson += !Operates[k].HasData
                                               ? "}"
                                               : "," + json.Substring(1, json.Length - 1);
                            }
                            else
                            {
                                if (Operates[k].HasData)
                                {
                                    destJson += "," + json.Substring(1, json.Length - 1);
                                }
                            }
                        }
                    }
                    _result = destJson;
                }
                break;

                default:
                {    //多行类
                    throw new NotImplementedException("不支持的主操作<br/>当前想要执行的主操作是:" + MainOperate.MainOperate.OperateType);
                }
                }
            }
            return(_messages);
        }