public List <string> RollBack() { //批量回滚已成功的操作 for (var i = 0; i < SuccessfulOperates.Count; i++) { AddMessages(SuccessfulOperates[i].RollBack()); } _rollbacked = true; AddMessage(SuccessfulOperates.Any() ? "批量回滚组成功" : "无需回滚组"); return(_messages); }
public List <string> RollBack() { var rollMsg = new List <string>(); //批量回滚已成功的操作 for (var i = 0; i < SuccessfulOperates.Count; i++) { rollMsg.Add(AddMessage("开始回滚第" + (i + 1) + "个")); rollMsg.Add(AddMessages(new List <string>() { SuccessfulOperates[i].Result.RollBack() })); } _rollbacked = true; rollMsg.Add(AddMessage(SuccessfulOperates.Any()?"批量回滚成功":"无需回滚")); return(rollMsg); }
//注意捕获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); }